From: Christoph Hohmann Date: Wed, 7 Aug 2002 20:41:01 +0000 (+0000) Subject: * src/utils.[ch] X-Git-Tag: before_folder_color~63 X-Git-Url: http://git.claws-mail.org/?p=claws.git;a=commitdiff_plain;h=a77a9d0d0dbb0a155f8bd7a1e764474d9e73bafd * src/utils.[ch] o canonicalize_file_replace should create the temporary file in the same directory as the original file. o added get_tmpfile_in_dir that creates a temorary file in a specified directory. --- diff --git a/ChangeLog.claws b/ChangeLog.claws index 2ae9e51a8..bc5d067cc 100644 --- a/ChangeLog.claws +++ b/ChangeLog.claws @@ -1,3 +1,11 @@ +2002-08-07 [christoph] 0.8.1claws24 + + * src/utils.[ch] + o canonicalize_file_replace should create the temporary + file in the same directory as the original file. + o added get_tmpfile_in_dir that creates a temorary file + in a specified directory. + 2002-08-07 [melvin] 0.8.1claws33 * src/toolbar.c diff --git a/configure.in b/configure.in index 74325dbb2..b7ff53e83 100644 --- a/configure.in +++ b/configure.in @@ -8,7 +8,7 @@ MINOR_VERSION=8 MICRO_VERSION=1 INTERFACE_AGE=0 BINARY_AGE=0 -EXTRA_VERSION=claws33 +EXTRA_VERSION=claws34 VERSION=$MAJOR_VERSION.$MINOR_VERSION.$MICRO_VERSION$EXTRA_VERSION dnl set $target diff --git a/src/utils.c b/src/utils.c index 0ba94c971..e5bc91810 100644 --- a/src/utils.c +++ b/src/utils.c @@ -2078,30 +2078,15 @@ gint move_file(const gchar *src, const gchar *dest) /* convert line endings into CRLF. If the last line doesn't end with * linebreak, add it. */ -gint canonicalize_file(const gchar *src, const gchar *dest) +gint canonicalize_file(FILE *src_fp, FILE *dest_fp) { - FILE *src_fp, *dest_fp; gchar buf[BUFFSIZE]; gint len; gboolean err = FALSE; gboolean last_linebreak = FALSE; - if ((src_fp = fopen(src, "rb")) == NULL) { - FILE_OP_ERROR(src, "fopen"); - return -1; - } - - if ((dest_fp = fopen(dest, "wb")) == NULL) { - FILE_OP_ERROR(dest, "fopen"); - fclose(src_fp); - return -1; - } - - if (change_file_mode_rw(dest_fp, dest) < 0) { - FILE_OP_ERROR(dest, "chmod"); - g_warning("can't change file mode\n"); - } - + rewind(src_fp); + rewind(dest_fp); while (fgets(buf, sizeof(buf), src_fp) != NULL) { gint r = 0; @@ -2125,10 +2110,8 @@ gint canonicalize_file(const gchar *src, const gchar *dest) } if (r == EOF) { - g_warning("writing to %s failed.\n", dest); - fclose(dest_fp); - fclose(src_fp); - unlink(dest); + g_warning("writing to destination file failed.\n"); + return -1; } } @@ -2138,18 +2121,7 @@ gint canonicalize_file(const gchar *src, const gchar *dest) err = TRUE; } - if (ferror(src_fp)) { - FILE_OP_ERROR(src, "fread"); - err = TRUE; - } - fclose(src_fp); - if (fclose(dest_fp) == EOF) { - FILE_OP_ERROR(dest, "fclose"); - err = TRUE; - } - if (err) { - unlink(dest); return -1; } @@ -2158,20 +2130,36 @@ gint canonicalize_file(const gchar *src, const gchar *dest) gint canonicalize_file_replace(const gchar *file) { - gchar *tmp_file; + gchar *tmp_file, *dirname; + FILE *src, *dest; - tmp_file = get_tmp_file(); + src = fopen(file, "r"); - if (canonicalize_file(file, tmp_file) < 0) + dirname = g_dirname(file); + dest = get_tmpfile_in_dir(dirname, &tmp_file); + + debug_print("Writing canonicalized file to %s\n", tmp_file); + + if (canonicalize_file(src, dest) < 0) { + fclose(dest); + fclose(src); + + g_free(tmp_file); return -1; + } + fclose(dest); + fclose(src); unlink(file); if (rename(tmp_file, file) < 0) { FILE_OP_ERROR(file, "rename"); unlink(tmp_file); + + g_free(tmp_file); return -1; } + g_free(tmp_file); return 0; } @@ -2785,3 +2773,13 @@ gboolean subject_is_reply(const gchar *subject) if (subject == NULL) return FALSE; else return 0 == g_strncasecmp(subject, "Re: ", 4); } + +FILE *get_tmpfile_in_dir(const gchar *dir, gchar **filename) +{ + int fd; + + *filename = g_strdup_printf("%s%csylpheed.XXXXXX", dir, G_DIR_SEPARATOR); + fd = mkstemp(*filename); + + return fdopen(fd, "w+"); +} diff --git a/src/utils.h b/src/utils.h index 791e9adcc..f5a1a561e 100644 --- a/src/utils.h +++ b/src/utils.h @@ -333,13 +333,13 @@ gint copy_file (const gchar *src, const gchar *dest); gint move_file (const gchar *src, const gchar *dest); -gint canonicalize_file (const gchar *src, - const gchar *dest); gint canonicalize_file_replace (const gchar *file); gint change_file_mode_rw (FILE *fp, const gchar *file); FILE *my_tmpfile (void); FILE *str_open_as_stream (const gchar *str); +FILE *get_tmpfile_in_dir (const gchar *dir, + gchar **filename); /* process execution */ gint execute_async (gchar *const argv[]);