2 * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2018 Colin Leroy and the Claws Mail team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "claws-features.h"
32 #include "file-utils.h"
34 gboolean prefs_common_get_flush_metadata(void);
35 gboolean prefs_common_get_use_shred(void);
37 /* FIXME make static once every file I/O is done using claws_* wrappers */
38 int safe_fclose(FILE *fp)
43 if (fflush(fp) != 0) {
46 if (prefs_common_get_flush_metadata() && fsync(fileno(fp)) != 0) {
56 #if HAVE_FGETS_UNLOCKED
58 /* Open a file and locks it once
59 * so subsequent I/O is faster
61 FILE *claws_fopen(const char *file, const char *mode)
63 FILE *fp = fopen(file, mode);
70 FILE *claws_fdopen(int fd, const char *mode)
72 FILE *fp = fdopen(fd, mode);
79 /* Unlocks and close a file pointer
82 int claws_fclose(FILE *fp)
88 /* Unlock, then safe-close a file pointer
89 * Safe close is done using fflush + fsync
90 * if the according preference says so.
92 int claws_safe_fclose(FILE *fp)
95 return safe_fclose(fp);
99 #define WEXITSTATUS(x) (x)
102 int claws_unlink(const char *filename)
105 static int found_shred = -1;
106 static const gchar *args[4];
108 if (filename == NULL)
111 if (prefs_common_get_use_shred()) {
112 if (found_shred == -1) {
114 args[0] = g_find_program_in_path("shred");
115 debug_print("found shred: %s\n", args[0]);
116 found_shred = (args[0] != NULL) ? 1:0;
120 if (found_shred == 1) {
121 if (g_stat(filename, &s) == 0 && S_ISREG(s.st_mode)) {
122 if (s.st_nlink == 1) {
125 g_spawn_sync(NULL, (gchar **)args, NULL, 0,
126 NULL, NULL, NULL, NULL, &status, NULL);
127 debug_print("%s %s exited with status %d\n",
128 args[0], filename, WEXITSTATUS(status));
129 if (truncate(filename, 0) < 0)
130 g_warning("couln't truncate: %s", filename);
135 return g_unlink(filename);
138 gint file_strip_crs(const gchar *file)
140 FILE *fp = NULL, *outfp = NULL;
142 gchar *out = get_tmp_file();
146 fp = claws_fopen(file, "rb");
150 outfp = claws_fopen(out, "wb");
156 while (claws_fgets(buf, sizeof (buf), fp) != NULL) {
158 if (claws_fputs(buf, outfp) == EOF) {
166 if (claws_safe_fclose(outfp) == EOF) {
170 if (move_file(out, file, TRUE) < 0)
183 * Append src file body to the tail of dest file.
184 * Now keep_backup has no effects.
186 gint append_file(const gchar *src, const gchar *dest, gboolean keep_backup)
188 FILE *src_fp, *dest_fp;
192 gboolean err = FALSE;
194 if ((src_fp = claws_fopen(src, "rb")) == NULL) {
195 FILE_OP_ERROR(src, "claws_fopen");
199 if ((dest_fp = claws_fopen(dest, "ab")) == NULL) {
200 FILE_OP_ERROR(dest, "claws_fopen");
201 claws_fclose(src_fp);
205 if (change_file_mode_rw(dest_fp, dest) < 0) {
206 FILE_OP_ERROR(dest, "chmod");
207 g_warning("can't change file mode: %s", dest);
210 while ((n_read = claws_fread(buf, sizeof(gchar), sizeof(buf), src_fp)) > 0) {
211 if (n_read < sizeof(buf) && claws_ferror(src_fp))
213 if (claws_fwrite(buf, 1, n_read, dest_fp) < n_read) {
214 g_warning("writing to %s failed.", dest);
215 claws_fclose(dest_fp);
216 claws_fclose(src_fp);
222 if (claws_ferror(src_fp)) {
223 FILE_OP_ERROR(src, "claws_fread");
226 claws_fclose(src_fp);
227 if (claws_fclose(dest_fp) == EOF) {
228 FILE_OP_ERROR(dest, "claws_fclose");
240 gint copy_file(const gchar *src, const gchar *dest, gboolean keep_backup)
242 FILE *src_fp, *dest_fp;
245 gchar *dest_bak = NULL;
246 gboolean err = FALSE;
248 if ((src_fp = claws_fopen(src, "rb")) == NULL) {
249 FILE_OP_ERROR(src, "claws_fopen");
252 if (is_file_exist(dest)) {
253 dest_bak = g_strconcat(dest, ".bak", NULL);
254 if (rename_force(dest, dest_bak) < 0) {
255 FILE_OP_ERROR(dest, "rename");
256 claws_fclose(src_fp);
262 if ((dest_fp = claws_fopen(dest, "wb")) == NULL) {
263 FILE_OP_ERROR(dest, "claws_fopen");
264 claws_fclose(src_fp);
266 if (rename_force(dest_bak, dest) < 0)
267 FILE_OP_ERROR(dest_bak, "rename");
273 if (change_file_mode_rw(dest_fp, dest) < 0) {
274 FILE_OP_ERROR(dest, "chmod");
275 g_warning("can't change file mode: %s", dest);
278 while ((n_read = claws_fread(buf, sizeof(gchar), sizeof(buf), src_fp)) > 0) {
279 if (n_read < sizeof(buf) && claws_ferror(src_fp))
281 if (claws_fwrite(buf, 1, n_read, dest_fp) < n_read) {
282 g_warning("writing to %s failed.", dest);
283 claws_fclose(dest_fp);
284 claws_fclose(src_fp);
287 if (rename_force(dest_bak, dest) < 0)
288 FILE_OP_ERROR(dest_bak, "rename");
295 if (claws_ferror(src_fp)) {
296 FILE_OP_ERROR(src, "claws_fread");
299 claws_fclose(src_fp);
300 if (claws_safe_fclose(dest_fp) == EOF) {
301 FILE_OP_ERROR(dest, "claws_fclose");
308 if (rename_force(dest_bak, dest) < 0)
309 FILE_OP_ERROR(dest_bak, "rename");
315 if (keep_backup == FALSE && dest_bak)
316 claws_unlink(dest_bak);
323 gint move_file(const gchar *src, const gchar *dest, gboolean overwrite)
325 if (overwrite == FALSE && is_file_exist(dest)) {
326 g_warning("move_file(): file %s already exists.", dest);
330 if (rename_force(src, dest) == 0) return 0;
332 if (EXDEV != errno) {
333 FILE_OP_ERROR(src, "rename");
337 if (copy_file(src, dest, FALSE) < 0) return -1;
344 gint copy_file_part_to_fp(FILE *fp, off_t offset, size_t length, FILE *dest_fp)
347 gint bytes_left, to_read;
350 if (fseek(fp, offset, SEEK_SET) < 0) {
356 to_read = MIN(bytes_left, sizeof(buf));
358 while ((n_read = claws_fread(buf, sizeof(gchar), to_read, fp)) > 0) {
359 if (n_read < to_read && claws_ferror(fp))
361 if (claws_fwrite(buf, 1, n_read, dest_fp) < n_read) {
364 bytes_left -= n_read;
367 to_read = MIN(bytes_left, sizeof(buf));
370 if (claws_ferror(fp)) {
371 perror("claws_fread");
378 gint copy_file_part(FILE *fp, off_t offset, size_t length, const gchar *dest)
381 gboolean err = FALSE;
383 if ((dest_fp = claws_fopen(dest, "wb")) == NULL) {
384 FILE_OP_ERROR(dest, "claws_fopen");
388 if (change_file_mode_rw(dest_fp, dest) < 0) {
389 FILE_OP_ERROR(dest, "chmod");
390 g_warning("can't change file mode: %s", dest);
393 if (copy_file_part_to_fp(fp, offset, length, dest_fp) < 0)
396 if (claws_safe_fclose(dest_fp) == EOF) {
397 FILE_OP_ERROR(dest, "claws_fclose");
402 g_warning("writing to %s failed.", dest);
410 gint canonicalize_file(const gchar *src, const gchar *dest)
412 FILE *src_fp, *dest_fp;
415 gboolean err = FALSE;
416 gboolean last_linebreak = FALSE;
418 if (src == NULL || dest == NULL)
421 if ((src_fp = claws_fopen(src, "rb")) == NULL) {
422 FILE_OP_ERROR(src, "claws_fopen");
426 if ((dest_fp = claws_fopen(dest, "wb")) == NULL) {
427 FILE_OP_ERROR(dest, "claws_fopen");
428 claws_fclose(src_fp);
432 if (change_file_mode_rw(dest_fp, dest) < 0) {
433 FILE_OP_ERROR(dest, "chmod");
434 g_warning("can't change file mode: %s", dest);
437 while (claws_fgets(buf, sizeof(buf), src_fp) != NULL) {
442 last_linebreak = FALSE;
444 if (buf[len - 1] != '\n') {
445 last_linebreak = TRUE;
446 r = claws_fputs(buf, dest_fp);
447 } else if (len > 1 && buf[len - 1] == '\n' && buf[len - 2] == '\r') {
448 r = claws_fputs(buf, dest_fp);
451 r = claws_fwrite(buf, 1, len - 1, dest_fp);
456 r = claws_fputs("\r\n", dest_fp);
460 g_warning("writing to %s failed.", dest);
461 claws_fclose(dest_fp);
462 claws_fclose(src_fp);
468 if (last_linebreak == TRUE) {
469 if (claws_fputs("\r\n", dest_fp) == EOF)
473 if (claws_ferror(src_fp)) {
474 FILE_OP_ERROR(src, "claws_fgets");
477 claws_fclose(src_fp);
478 if (claws_safe_fclose(dest_fp) == EOF) {
479 FILE_OP_ERROR(dest, "claws_fclose");
491 gint canonicalize_file_replace(const gchar *file)
495 tmp_file = get_tmp_file();
497 if (canonicalize_file(file, tmp_file) < 0) {
502 if (move_file(tmp_file, file, TRUE) < 0) {
503 g_warning("can't replace file: %s", file);
504 claws_unlink(tmp_file);
514 gint str_write_to_file(const gchar *str, const gchar *file)
519 cm_return_val_if_fail(str != NULL, -1);
520 cm_return_val_if_fail(file != NULL, -1);
522 if ((fp = claws_fopen(file, "wb")) == NULL) {
523 FILE_OP_ERROR(file, "claws_fopen");
533 if (claws_fwrite(str, 1, len, fp) != len) {
534 FILE_OP_ERROR(file, "claws_fwrite");
540 if (claws_safe_fclose(fp) == EOF) {
541 FILE_OP_ERROR(file, "claws_fclose");
549 static gchar *file_read_stream_to_str_full(FILE *fp, gboolean recode)
556 cm_return_val_if_fail(fp != NULL, NULL);
558 array = g_byte_array_new();
560 while ((n_read = claws_fread(buf, sizeof(gchar), sizeof(buf), fp)) > 0) {
561 if (n_read < sizeof(buf) && claws_ferror(fp))
563 g_byte_array_append(array, buf, n_read);
566 if (claws_ferror(fp)) {
567 FILE_OP_ERROR("file stream", "claws_fread");
568 g_byte_array_free(array, TRUE);
573 g_byte_array_append(array, buf, 1);
574 str = (gchar *)array->data;
575 g_byte_array_free(array, FALSE);
577 if (recode && !g_utf8_validate(str, -1, NULL)) {
578 const gchar *src_codeset, *dest_codeset;
580 src_codeset = conv_get_locale_charset_str();
581 dest_codeset = CS_UTF_8;
582 tmp = conv_codeset_strdup(str, src_codeset, dest_codeset);
590 static gchar *file_read_to_str_full(const gchar *file, gboolean recode)
597 struct timeval timeout = {1, 0};
602 cm_return_val_if_fail(file != NULL, NULL);
604 if (g_stat(file, &s) != 0) {
605 FILE_OP_ERROR(file, "stat");
608 if (S_ISDIR(s.st_mode)) {
609 g_warning("%s: is a directory", file);
614 fp = claws_fopen (file, "rb");
616 FILE_OP_ERROR(file, "open");
620 /* test whether the file is readable without blocking */
621 fd = g_open(file, O_RDONLY | O_NONBLOCK, 0);
623 FILE_OP_ERROR(file, "open");
630 /* allow for one second */
631 err = select(fd+1, &fds, NULL, NULL, &timeout);
632 if (err <= 0 || !FD_ISSET(fd, &fds)) {
634 FILE_OP_ERROR(file, "select");
636 g_warning("%s: doesn't seem readable", file);
642 /* Now clear O_NONBLOCK */
643 if ((fflags = fcntl(fd, F_GETFL)) < 0) {
644 FILE_OP_ERROR(file, "fcntl (F_GETFL)");
648 if (fcntl(fd, F_SETFL, (fflags & ~O_NONBLOCK)) < 0) {
649 FILE_OP_ERROR(file, "fcntl (F_SETFL)");
654 /* get the FILE pointer */
655 fp = claws_fdopen(fd, "rb");
658 FILE_OP_ERROR(file, "claws_fdopen");
659 close(fd); /* if fp isn't NULL, we'll use claws_fclose instead! */
664 str = file_read_stream_to_str_full(fp, recode);
671 gchar *file_read_to_str(const gchar *file)
673 return file_read_to_str_full(file, TRUE);
675 gchar *file_read_stream_to_str(FILE *fp)
677 return file_read_stream_to_str_full(fp, TRUE);
680 gchar *file_read_to_str_no_recode(const gchar *file)
682 return file_read_to_str_full(file, FALSE);
684 gchar *file_read_stream_to_str_no_recode(FILE *fp)
686 return file_read_stream_to_str_full(fp, FALSE);
689 gint rename_force(const gchar *oldpath, const gchar *newpath)
692 if (!is_file_entry_exist(oldpath)) {
696 if (is_file_exist(newpath)) {
697 if (claws_unlink(newpath) < 0)
698 FILE_OP_ERROR(newpath, "unlink");
701 return g_rename(oldpath, newpath);
704 gint copy_dir(const gchar *src, const gchar *dst)
709 if ((dir = g_dir_open(src, 0, NULL)) == NULL) {
710 g_warning("failed to open directory: %s", src);
714 if (make_dir(dst) < 0)
717 while ((name = g_dir_read_name(dir)) != NULL) {
718 gchar *old_file, *new_file;
719 old_file = g_strconcat(src, G_DIR_SEPARATOR_S, name, NULL);
720 new_file = g_strconcat(dst, G_DIR_SEPARATOR_S, name, NULL);
721 debug_print("copying: %s -> %s\n", old_file, new_file);
722 if (g_file_test(old_file, G_FILE_TEST_IS_REGULAR)) {
723 gint r = copy_file(old_file, new_file, TRUE);
730 /* Windows has no symlinks. Or well, Vista seems to
731 have something like this but the semantics might be
732 different. Thus we don't use it under Windows. */
733 else if (g_file_test(old_file, G_FILE_TEST_IS_SYMLINK)) {
734 GError *error = NULL;
736 gchar *target = g_file_read_link(old_file, &error);
738 r = symlink(target, new_file);
745 #endif /*G_OS_WIN32*/
746 else if (g_file_test(old_file, G_FILE_TEST_IS_DIR)) {
747 gint r = copy_dir(old_file, new_file);
758 gint change_file_mode_rw(FILE *fp, const gchar *file)
761 return fchmod(fileno(fp), S_IRUSR|S_IWUSR);
763 return g_chmod(file, S_IRUSR|S_IWUSR);
767 FILE *my_tmpfile(void)
769 const gchar suffix[] = ".XXXXXX";
772 const gchar *progname;
781 tmpdir = get_tmp_dir();
782 tmplen = strlen(tmpdir);
783 progname = g_get_prgname();
784 if (progname == NULL)
785 progname = "claws-mail";
786 proglen = strlen(progname);
787 Xalloca(fname, tmplen + 1 + proglen + sizeof(suffix),
790 memcpy(fname, tmpdir, tmplen);
791 fname[tmplen] = G_DIR_SEPARATOR;
792 memcpy(fname + tmplen + 1, progname, proglen);
793 memcpy(fname + tmplen + 1 + proglen, suffix, sizeof(suffix));
795 fd = g_mkstemp(fname);
802 /* verify that we can write in the file after unlinking */
803 if (write(fd, buf, 1) < 0) {
810 fp = claws_fdopen(fd, "w+b");
821 FILE *my_tmpfile_with_len(size_t len)
824 FILE *tmpfp = fmemopen(NULL, len, "w+b");
836 FILE *get_tmpfile_in_dir(const gchar *dir, gchar **filename)
839 *filename = g_strdup_printf("%s%cclaws.XXXXXX", dir, G_DIR_SEPARATOR);
840 fd = g_mkstemp(*filename);
843 return claws_fdopen(fd, "w+");
846 FILE *str_open_as_stream(const gchar *str)
851 cm_return_val_if_fail(str != NULL, NULL);
855 FILE_OP_ERROR("str_open_as_stream", "my_tmpfile");
860 if (len == 0) return fp;
862 if (claws_fwrite(str, 1, len, fp) != len) {
863 FILE_OP_ERROR("str_open_as_stream", "claws_fwrite");