1
0
mirror of git://git.gnupg.org/gnupg.git synced 2024-12-22 10:19:57 +01:00

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

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