tools: Use platform abstraction for I/O.

* tools/gpgconf-comp.c (retrieve_options_from_file): Use a
gpgrt_stream_t for 'list_file'.  Adapt accordingly.
(copy_file): Likewise for 'src' and 'dst'.
(change_options_file): Likewise for 'src_file' and 'dest_file'.
(change_options_program): Likewise for 'src_file' and 'dest_file'.
(gc_process_gpgconf_conf): Likewise for 'config'.
--
Werner asked me to make gpgconf use the platform abstractions that
were introduced after gpgconf's inception.

Signed-off-by: Justus Winter <justus@g10code.com>
This commit is contained in:
Justus Winter 2017-01-24 15:06:43 +01:00
parent bfd75e9492
commit 77b8aff4e1
1 changed files with 95 additions and 96 deletions

View File

@ -2266,7 +2266,7 @@ retrieve_options_from_file (gc_component_t component, gc_backend_t backend)
gc_option_t *list_option;
gc_option_t *config_option;
char *list_filename;
FILE *list_file;
gpgrt_stream_t list_file;
char *line = NULL;
size_t line_len = 0;
ssize_t length;
@ -2278,13 +2278,13 @@ retrieve_options_from_file (gc_component_t component, gc_backend_t backend)
assert (!list_option->active);
list_filename = get_config_filename (component, backend);
list_file = fopen (list_filename, "r");
list_file = gpgrt_fopen (list_filename, "r");
if (!list_file)
gc_error (0, errno, "warning: can not open list file %s", list_filename);
else
{
while ((length = read_line (list_file, &line, &line_len, NULL)) > 0)
while ((length = gpgrt_read_line (list_file, &line, &line_len, NULL)) > 0)
{
char *start;
char *end;
@ -2317,7 +2317,7 @@ retrieve_options_from_file (gc_component_t component, gc_backend_t backend)
else
list = xasprintf ("\"%s", gc_percent_escape (start));
}
if (length < 0 || ferror (list_file))
if (length < 0 || gpgrt_ferror (list_file))
gc_error (1, errno, "can not read list file %s", list_filename);
}
@ -2330,7 +2330,7 @@ retrieve_options_from_file (gc_component_t component, gc_backend_t backend)
if (config_option->flags & GC_OPT_FLAG_NO_CHANGE)
list_option->flags |= GC_OPT_FLAG_NO_CHANGE;
if (list_file && fclose (list_file))
if (list_file && gpgrt_fclose (list_file))
gc_error (1, errno, "error closing %s", list_filename);
xfree (line);
}
@ -2516,7 +2516,6 @@ option_check_validity (gc_option_t *option, unsigned long flags,
while (arg && *arg);
}
#ifdef HAVE_W32_SYSTEM
int
copy_file (const char *src_name, const char *dst_name)
@ -2524,18 +2523,18 @@ copy_file (const char *src_name, const char *dst_name)
#define BUF_LEN 4096
char buffer[BUF_LEN];
int len;
FILE *src;
FILE *dst;
gpgrt_stream_t src;
gpgrt_stream_t dst;
src = fopen (src_name, "r");
src = gpgrt_fopen (src_name, "r");
if (src == NULL)
return -1;
dst = fopen (dst_name, "w");
dst = gpgrt_fopen (dst_name, "w");
if (dst == NULL)
{
int saved_err = errno;
fclose (src);
gpgrt_fclose (src);
gpg_err_set_errno (saved_err);
return -1;
}
@ -2544,28 +2543,28 @@ copy_file (const char *src_name, const char *dst_name)
{
int written;
len = fread (buffer, 1, BUF_LEN, src);
len = gpgrt_fread (buffer, 1, BUF_LEN, src);
if (len == 0)
break;
written = fwrite (buffer, 1, len, dst);
written = gpgrt_fwrite (buffer, 1, len, dst);
if (written != len)
break;
}
while (!feof (src) && !ferror (src) && !ferror (dst));
while (! gpgrt_feof (src) && ! gpgrt_ferror (src) && ! gpgrt_ferror (dst));
if (ferror (src) || ferror (dst) || !feof (src))
if (gpgrt_ferror (src) || gpgrt_ferror (dst) || ! gpgrt_feof (src))
{
int saved_errno = errno;
fclose (src);
fclose (dst);
gpgrt_fclose (src);
gpgrt_fclose (dst);
unlink (dst_name);
gpg_err_set_errno (saved_errno);
return -1;
}
if (fclose (dst))
if (gpgrt_fclose (dst))
gc_error (1, errno, "error closing %s", dst_name);
if (fclose (src))
if (gpgrt_fclose (src))
gc_error (1, errno, "error closing %s", src_name);
return 0;
@ -2602,8 +2601,8 @@ change_options_file (gc_component_t component, gc_backend_t backend,
ssize_t length;
int res;
int fd;
FILE *src_file = NULL;
FILE *dest_file = NULL;
gpgrt_stream_t src_file = NULL;
gpgrt_stream_t dest_file = NULL;
char *src_filename;
char *dest_filename;
char *orig_filename;
@ -2675,7 +2674,7 @@ change_options_file (gc_component_t component, gc_backend_t backend,
fd = open (src_filename, O_CREAT | O_EXCL | O_WRONLY, 0644);
if (fd < 0)
return -1;
src_file = fdopen (fd, "w");
src_file = gpgrt_fdopen (fd, "w");
res = errno;
if (!src_file)
{
@ -2689,11 +2688,11 @@ change_options_file (gc_component_t component, gc_backend_t backend,
process. */
if (orig_filename)
{
dest_file = fopen (dest_filename, "r");
dest_file = gpgrt_fopen (dest_filename, "r");
if (!dest_file)
goto change_file_one_err;
while ((length = read_line (dest_file, &line, &line_len, NULL)) > 0)
while ((length = gpgrt_read_line (dest_file, &line, &line_len, NULL)) > 0)
{
int disable = 0;
char *start;
@ -2764,24 +2763,24 @@ change_options_file (gc_component_t component, gc_backend_t backend,
{
if (!in_marker)
{
fprintf (src_file,
gpgrt_fprintf (src_file,
"# %s disabled this option here at %s\n",
GPGCONF_DISP_NAME, asctimestamp (gnupg_get_time ()));
if (ferror (src_file))
if (gpgrt_ferror (src_file))
goto change_file_one_err;
fprintf (src_file, "# %s", line);
if (ferror (src_file))
gpgrt_fprintf (src_file, "# %s", line);
if (gpgrt_ferror (src_file))
goto change_file_one_err;
}
}
else
{
fprintf (src_file, "%s", line);
if (ferror (src_file))
gpgrt_fprintf (src_file, "%s", line);
if (gpgrt_ferror (src_file))
goto change_file_one_err;
}
}
if (length < 0 || ferror (dest_file))
if (length < 0 || gpgrt_ferror (dest_file))
goto change_file_one_err;
}
@ -2792,8 +2791,8 @@ change_options_file (gc_component_t component, gc_backend_t backend,
proceed. Note that we first write a newline, this guards us
against files which lack the newline at the end of the last
line, while it doesn't hurt us in all other cases. */
fprintf (src_file, "\n%s\n", marker);
if (ferror (src_file))
gpgrt_fprintf (src_file, "\n%s\n", marker);
if (gpgrt_ferror (src_file))
goto change_file_one_err;
}
@ -2803,7 +2802,7 @@ change_options_file (gc_component_t component, gc_backend_t backend,
followed by the rest of the original file. */
while (cur_arg)
{
fprintf (src_file, "%s\n", cur_arg);
gpgrt_fprintf (src_file, "%s\n", cur_arg);
/* Find next argument. */
if (arg)
@ -2828,52 +2827,52 @@ change_options_file (gc_component_t component, gc_backend_t backend,
cur_arg = NULL;
}
fprintf (src_file, "%s %s\n", marker, asctimestamp (gnupg_get_time ()));
if (ferror (src_file))
gpgrt_fprintf (src_file, "%s %s\n", marker, asctimestamp (gnupg_get_time ()));
if (gpgrt_ferror (src_file))
goto change_file_one_err;
if (!in_marker)
{
fprintf (src_file, "# %s edited this configuration file.\n",
gpgrt_fprintf (src_file, "# %s edited this configuration file.\n",
GPGCONF_DISP_NAME);
if (ferror (src_file))
if (gpgrt_ferror (src_file))
goto change_file_one_err;
fprintf (src_file, "# It will disable options before this marked "
gpgrt_fprintf (src_file, "# It will disable options before this marked "
"block, but it will\n");
if (ferror (src_file))
if (gpgrt_ferror (src_file))
goto change_file_one_err;
fprintf (src_file, "# never change anything below these lines.\n");
if (ferror (src_file))
gpgrt_fprintf (src_file, "# never change anything below these lines.\n");
if (gpgrt_ferror (src_file))
goto change_file_one_err;
}
if (dest_file)
{
while ((length = read_line (dest_file, &line, &line_len, NULL)) > 0)
while ((length = gpgrt_read_line (dest_file, &line, &line_len, NULL)) > 0)
{
fprintf (src_file, "%s", line);
if (ferror (src_file))
gpgrt_fprintf (src_file, "%s", line);
if (gpgrt_ferror (src_file))
goto change_file_one_err;
}
if (length < 0 || ferror (dest_file))
if (length < 0 || gpgrt_ferror (dest_file))
goto change_file_one_err;
}
xfree (line);
line = NULL;
res = fclose (src_file);
res = gpgrt_fclose (src_file);
if (res)
{
res = errno;
close (fd);
if (dest_file)
fclose (dest_file);
gpgrt_fclose (dest_file);
gpg_err_set_errno (res);
return -1;
}
close (fd);
if (dest_file)
{
res = fclose (dest_file);
res = gpgrt_fclose (dest_file);
if (res)
return -1;
}
@ -2884,11 +2883,11 @@ change_options_file (gc_component_t component, gc_backend_t backend,
res = errno;
if (src_file)
{
fclose (src_file);
gpgrt_fclose (src_file);
close (fd);
}
if (dest_file)
fclose (dest_file);
gpgrt_fclose (dest_file);
gpg_err_set_errno (res);
return -1;
}
@ -2924,8 +2923,8 @@ change_options_program (gc_component_t component, gc_backend_t backend,
ssize_t length;
int res;
int fd;
FILE *src_file = NULL;
FILE *dest_file = NULL;
gpgrt_stream_t src_file = NULL;
gpgrt_stream_t dest_file = NULL;
char *src_filename;
char *dest_filename;
char *orig_filename;
@ -2967,7 +2966,7 @@ change_options_program (gc_component_t component, gc_backend_t backend,
fd = open (src_filename, O_CREAT | O_EXCL | O_WRONLY, 0644);
if (fd < 0)
return -1;
src_file = fdopen (fd, "w");
src_file = gpgrt_fdopen (fd, "w");
res = errno;
if (!src_file)
{
@ -2981,11 +2980,11 @@ change_options_program (gc_component_t component, gc_backend_t backend,
process. */
if (orig_filename)
{
dest_file = fopen (dest_filename, "r");
dest_file = gpgrt_fopen (dest_filename, "r");
if (!dest_file)
goto change_one_err;
while ((length = read_line (dest_file, &line, &line_len, NULL)) > 0)
while ((length = gpgrt_read_line (dest_file, &line, &line_len, NULL)) > 0)
{
int disable = 0;
char *start;
@ -3032,24 +3031,24 @@ change_options_program (gc_component_t component, gc_backend_t backend,
{
if (!in_marker)
{
fprintf (src_file,
gpgrt_fprintf (src_file,
"# %s disabled this option here at %s\n",
GPGCONF_DISP_NAME, asctimestamp (gnupg_get_time ()));
if (ferror (src_file))
if (gpgrt_ferror (src_file))
goto change_one_err;
fprintf (src_file, "# %s", line);
if (ferror (src_file))
gpgrt_fprintf (src_file, "# %s", line);
if (gpgrt_ferror (src_file))
goto change_one_err;
}
}
else
{
fprintf (src_file, "%s", line);
if (ferror (src_file))
gpgrt_fprintf (src_file, "%s", line);
if (gpgrt_ferror (src_file))
goto change_one_err;
}
}
if (length < 0 || ferror (dest_file))
if (length < 0 || gpgrt_ferror (dest_file))
goto change_one_err;
}
@ -3060,8 +3059,8 @@ change_options_program (gc_component_t component, gc_backend_t backend,
proceed. Note that we first write a newline, this guards us
against files which lack the newline at the end of the last
line, while it doesn't hurt us in all other cases. */
fprintf (src_file, "\n%s\n", marker);
if (ferror (src_file))
gpgrt_fprintf (src_file, "\n%s\n", marker);
if (gpgrt_ferror (src_file))
goto change_one_err;
}
/* At this point, we have copied everything up to the end marker
@ -3072,7 +3071,7 @@ change_options_program (gc_component_t component, gc_backend_t backend,
/* We have to turn on UTF8 strings for GnuPG. */
if (backend == GC_BACKEND_GPG && ! utf8strings_seen)
fprintf (src_file, "utf8-strings\n");
gpgrt_fprintf (src_file, "utf8-strings\n");
option = gc_component[component].options;
while (option->name)
@ -3087,16 +3086,16 @@ change_options_program (gc_component_t component, gc_backend_t backend,
{
if (*arg == '\0' || *arg == ',')
{
fprintf (src_file, "%s\n", option->name);
if (ferror (src_file))
gpgrt_fprintf (src_file, "%s\n", option->name);
if (gpgrt_ferror (src_file))
goto change_one_err;
}
else if (gc_arg_type[option->arg_type].fallback
== GC_ARG_TYPE_NONE)
{
assert (*arg == '1');
fprintf (src_file, "%s\n", option->name);
if (ferror (src_file))
gpgrt_fprintf (src_file, "%s\n", option->name);
if (gpgrt_ferror (src_file))
goto change_one_err;
arg++;
@ -3118,9 +3117,9 @@ change_options_program (gc_component_t component, gc_backend_t backend,
else
end = NULL;
fprintf (src_file, "%s %s\n", option->name,
gpgrt_fprintf (src_file, "%s %s\n", option->name,
verbatim? arg : percent_deescape (arg));
if (ferror (src_file))
if (gpgrt_ferror (src_file))
goto change_one_err;
if (end)
@ -3135,8 +3134,8 @@ change_options_program (gc_component_t component, gc_backend_t backend,
if (end)
*end = '\0';
fprintf (src_file, "%s %s\n", option->name, arg);
if (ferror (src_file))
gpgrt_fprintf (src_file, "%s %s\n", option->name, arg);
if (gpgrt_ferror (src_file))
goto change_one_err;
if (end)
@ -3153,52 +3152,52 @@ change_options_program (gc_component_t component, gc_backend_t backend,
option++;
}
fprintf (src_file, "%s %s\n", marker, asctimestamp (gnupg_get_time ()));
if (ferror (src_file))
gpgrt_fprintf (src_file, "%s %s\n", marker, asctimestamp (gnupg_get_time ()));
if (gpgrt_ferror (src_file))
goto change_one_err;
if (!in_marker)
{
fprintf (src_file, "# %s edited this configuration file.\n",
gpgrt_fprintf (src_file, "# %s edited this configuration file.\n",
GPGCONF_DISP_NAME);
if (ferror (src_file))
if (gpgrt_ferror (src_file))
goto change_one_err;
fprintf (src_file, "# It will disable options before this marked "
gpgrt_fprintf (src_file, "# It will disable options before this marked "
"block, but it will\n");
if (ferror (src_file))
if (gpgrt_ferror (src_file))
goto change_one_err;
fprintf (src_file, "# never change anything below these lines.\n");
if (ferror (src_file))
gpgrt_fprintf (src_file, "# never change anything below these lines.\n");
if (gpgrt_ferror (src_file))
goto change_one_err;
}
if (dest_file)
{
while ((length = read_line (dest_file, &line, &line_len, NULL)) > 0)
while ((length = gpgrt_read_line (dest_file, &line, &line_len, NULL)) > 0)
{
fprintf (src_file, "%s", line);
if (ferror (src_file))
gpgrt_fprintf (src_file, "%s", line);
if (gpgrt_ferror (src_file))
goto change_one_err;
}
if (length < 0 || ferror (dest_file))
if (length < 0 || gpgrt_ferror (dest_file))
goto change_one_err;
}
xfree (line);
line = NULL;
res = fclose (src_file);
res = gpgrt_fclose (src_file);
if (res)
{
res = errno;
close (fd);
if (dest_file)
fclose (dest_file);
gpgrt_fclose (dest_file);
gpg_err_set_errno (res);
return -1;
}
close (fd);
if (dest_file)
{
res = fclose (dest_file);
res = gpgrt_fclose (dest_file);
if (res)
return -1;
}
@ -3209,11 +3208,11 @@ change_options_program (gc_component_t component, gc_backend_t backend,
res = errno;
if (src_file)
{
fclose (src_file);
gpgrt_fclose (src_file);
close (fd);
}
if (dest_file)
fclose (dest_file);
gpgrt_fclose (dest_file);
gpg_err_set_errno (res);
return -1;
}
@ -3646,7 +3645,7 @@ gc_process_gpgconf_conf (const char *fname_arg, int update, int defaults,
char *line = NULL;
size_t line_len = 0;
ssize_t length;
FILE *config;
gpgrt_stream_t config;
int lineno = 0;
int in_rule = 0;
int got_match = 0;
@ -3663,7 +3662,7 @@ gc_process_gpgconf_conf (const char *fname_arg, int update, int defaults,
for (backend_id = 0; backend_id < GC_BACKEND_NR; backend_id++)
runtime[backend_id] = 0;
config = fopen (fname, "r");
config = gpgrt_fopen (fname, "r");
if (!config)
{
/* Do not print an error if the file is not available, except
@ -3677,7 +3676,7 @@ gc_process_gpgconf_conf (const char *fname_arg, int update, int defaults,
return result;
}
while ((length = read_line (config, &line, &line_len, NULL)) > 0)
while ((length = gpgrt_read_line (config, &line, &line_len, NULL)) > 0)
{
char *key, *component, *option, *flags, *value;
char *empty;
@ -3903,12 +3902,12 @@ gc_process_gpgconf_conf (const char *fname_arg, int update, int defaults,
}
}
if (length < 0 || ferror (config))
if (length < 0 || gpgrt_ferror (config))
{
gc_error (0, errno, "error reading from '%s'", fname);
result = -1;
}
if (fclose (config))
if (gpgrt_fclose (config))
gc_error (0, errno, "error closing '%s'", fname);
xfree (line);