Add a memory-backed my_tmpfile() implementation.
[claws.git] / src / common / file-utils.c
1 /*
2  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2018 Colin Leroy and the Claws Mail team
4  *
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.
9  *
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.
14  *
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/>.
17  */
18
19 #ifdef HAVE_CONFIG_H
20 #  include "config.h"
21 #include "claws-features.h"
22 #endif
23
24 #include <errno.h>
25 #include <stdio.h>
26 #include <unistd.h>
27 #include <fcntl.h>
28
29 #include "defs.h"
30 #include "codeconv.h"
31 #include "timing.h"
32 #include "file-utils.h"
33
34 gboolean prefs_common_get_flush_metadata(void);
35 gboolean prefs_common_get_use_shred(void);
36
37 /* FIXME make static once every file I/O is done using claws_* wrappers */
38 int safe_fclose(FILE *fp)
39 {
40         int r;
41         START_TIMING("");
42
43         if (fflush(fp) != 0) {
44                 return EOF;
45         }
46         if (prefs_common_get_flush_metadata() && fsync(fileno(fp)) != 0) {
47                 return EOF;
48         }
49
50         r = fclose(fp);
51         END_TIMING();
52
53         return r;
54 }
55
56 #if HAVE_FGETS_UNLOCKED
57
58 /* Open a file and locks it once
59  * so subsequent I/O is faster
60  */
61 FILE *claws_fopen(const char *file, const char *mode)
62 {
63         FILE *fp = fopen(file, mode);
64         if (!fp)
65                 return NULL;
66         flockfile(fp);
67         return fp;
68 }
69
70 FILE *claws_fdopen(int fd, const char *mode)
71 {
72         FILE *fp = fdopen(fd, mode);
73         if (!fp)
74                 return NULL;
75         flockfile(fp);
76         return fp;
77 }
78
79 /* Unlocks and close a file pointer
80  */
81
82 int claws_fclose(FILE *fp)
83 {
84         funlockfile(fp);
85         return fclose(fp);
86 }
87
88 /* Unlock, then safe-close a file pointer
89  * Safe close is done using fflush + fsync
90  * if the according preference says so.
91  */
92 int claws_safe_fclose(FILE *fp)
93 {
94         funlockfile(fp);
95         return safe_fclose(fp);
96 }
97
98 #ifdef G_OS_WIN32
99 #define WEXITSTATUS(x) (x)
100 #endif
101
102 int claws_unlink(const char *filename) 
103 {
104         GStatBuf s;
105         static int found_shred = -1;
106         static const gchar *args[4];
107
108         if (filename == NULL)
109                 return 0;
110
111         if (prefs_common_get_use_shred()) {
112                 if (found_shred == -1) {
113                         /* init */
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;
117                         args[1] = "-f";
118                         args[3] = NULL;
119                 }
120                 if (found_shred == 1) {
121                         if (g_stat(filename, &s) == 0 && S_ISREG(s.st_mode)) {
122                                 if (s.st_nlink == 1) {
123                                         gint status=0;
124                                         args[2] = filename;
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);
131                                 }
132                         }
133                 }
134         }
135         return g_unlink(filename);
136 }
137
138 gint file_strip_crs(const gchar *file)
139 {
140         FILE *fp = NULL, *outfp = NULL;
141         gchar buf[4096];
142         gchar *out = get_tmp_file();
143         if (file == NULL)
144                 goto freeout;
145
146         fp = claws_fopen(file, "rb");
147         if (!fp)
148                 goto freeout;
149
150         outfp = claws_fopen(out, "wb");
151         if (!outfp) {
152                 claws_fclose(fp);
153                 goto freeout;
154         }
155
156         while (claws_fgets(buf, sizeof (buf), fp) != NULL) {
157                 strcrchomp(buf);
158                 if (claws_fputs(buf, outfp) == EOF) {
159                         claws_fclose(fp);
160                         claws_fclose(outfp);
161                         goto unlinkout;
162                 }
163         }
164
165         claws_fclose(fp);
166         if (claws_safe_fclose(outfp) == EOF) {
167                 goto unlinkout;
168         }
169         
170         if (move_file(out, file, TRUE) < 0)
171                 goto unlinkout;
172         
173         g_free(out);
174         return 0;
175 unlinkout:
176         claws_unlink(out);
177 freeout:
178         g_free(out);
179         return -1;
180 }
181
182 /*
183  * Append src file body to the tail of dest file.
184  * Now keep_backup has no effects.
185  */
186 gint append_file(const gchar *src, const gchar *dest, gboolean keep_backup)
187 {
188         FILE *src_fp, *dest_fp;
189         gint n_read;
190         gchar buf[BUFSIZ];
191
192         gboolean err = FALSE;
193
194         if ((src_fp = claws_fopen(src, "rb")) == NULL) {
195                 FILE_OP_ERROR(src, "claws_fopen");
196                 return -1;
197         }
198
199         if ((dest_fp = claws_fopen(dest, "ab")) == NULL) {
200                 FILE_OP_ERROR(dest, "claws_fopen");
201                 claws_fclose(src_fp);
202                 return -1;
203         }
204
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);
208         }
209
210         while ((n_read = claws_fread(buf, sizeof(gchar), sizeof(buf), src_fp)) > 0) {
211                 if (n_read < sizeof(buf) && claws_ferror(src_fp))
212                         break;
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);
217                         claws_unlink(dest);
218                         return -1;
219                 }
220         }
221
222         if (claws_ferror(src_fp)) {
223                 FILE_OP_ERROR(src, "claws_fread");
224                 err = TRUE;
225         }
226         claws_fclose(src_fp);
227         if (claws_fclose(dest_fp) == EOF) {
228                 FILE_OP_ERROR(dest, "claws_fclose");
229                 err = TRUE;
230         }
231
232         if (err) {
233                 claws_unlink(dest);
234                 return -1;
235         }
236
237         return 0;
238 }
239
240 gint copy_file(const gchar *src, const gchar *dest, gboolean keep_backup)
241 {
242         FILE *src_fp, *dest_fp;
243         gint n_read;
244         gchar buf[BUFSIZ];
245         gchar *dest_bak = NULL;
246         gboolean err = FALSE;
247
248         if ((src_fp = claws_fopen(src, "rb")) == NULL) {
249                 FILE_OP_ERROR(src, "claws_fopen");
250                 return -1;
251         }
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);
257                         g_free(dest_bak);
258                         return -1;
259                 }
260         }
261
262         if ((dest_fp = claws_fopen(dest, "wb")) == NULL) {
263                 FILE_OP_ERROR(dest, "claws_fopen");
264                 claws_fclose(src_fp);
265                 if (dest_bak) {
266                         if (rename_force(dest_bak, dest) < 0)
267                                 FILE_OP_ERROR(dest_bak, "rename");
268                         g_free(dest_bak);
269                 }
270                 return -1;
271         }
272
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);
276         }
277
278         while ((n_read = claws_fread(buf, sizeof(gchar), sizeof(buf), src_fp)) > 0) {
279                 if (n_read < sizeof(buf) && claws_ferror(src_fp))
280                         break;
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);
285                         claws_unlink(dest);
286                         if (dest_bak) {
287                                 if (rename_force(dest_bak, dest) < 0)
288                                         FILE_OP_ERROR(dest_bak, "rename");
289                                 g_free(dest_bak);
290                         }
291                         return -1;
292                 }
293         }
294
295         if (claws_ferror(src_fp)) {
296                 FILE_OP_ERROR(src, "claws_fread");
297                 err = TRUE;
298         }
299         claws_fclose(src_fp);
300         if (claws_safe_fclose(dest_fp) == EOF) {
301                 FILE_OP_ERROR(dest, "claws_fclose");
302                 err = TRUE;
303         }
304
305         if (err) {
306                 claws_unlink(dest);
307                 if (dest_bak) {
308                         if (rename_force(dest_bak, dest) < 0)
309                                 FILE_OP_ERROR(dest_bak, "rename");
310                         g_free(dest_bak);
311                 }
312                 return -1;
313         }
314
315         if (keep_backup == FALSE && dest_bak)
316                 claws_unlink(dest_bak);
317
318         g_free(dest_bak);
319
320         return 0;
321 }
322
323 gint move_file(const gchar *src, const gchar *dest, gboolean overwrite)
324 {
325         if (overwrite == FALSE && is_file_exist(dest)) {
326                 g_warning("move_file(): file %s already exists.", dest);
327                 return -1;
328         }
329
330         if (rename_force(src, dest) == 0) return 0;
331
332         if (EXDEV != errno) {
333                 FILE_OP_ERROR(src, "rename");
334                 return -1;
335         }
336
337         if (copy_file(src, dest, FALSE) < 0) return -1;
338
339         claws_unlink(src);
340
341         return 0;
342 }
343
344 gint copy_file_part_to_fp(FILE *fp, off_t offset, size_t length, FILE *dest_fp)
345 {
346         gint n_read;
347         gint bytes_left, to_read;
348         gchar buf[BUFSIZ];
349
350         if (fseek(fp, offset, SEEK_SET) < 0) {
351                 perror("fseek");
352                 return -1;
353         }
354
355         bytes_left = length;
356         to_read = MIN(bytes_left, sizeof(buf));
357
358         while ((n_read = claws_fread(buf, sizeof(gchar), to_read, fp)) > 0) {
359                 if (n_read < to_read && claws_ferror(fp))
360                         break;
361                 if (claws_fwrite(buf, 1, n_read, dest_fp) < n_read) {
362                         return -1;
363                 }
364                 bytes_left -= n_read;
365                 if (bytes_left == 0)
366                         break;
367                 to_read = MIN(bytes_left, sizeof(buf));
368         }
369
370         if (claws_ferror(fp)) {
371                 perror("claws_fread");
372                 return -1;
373         }
374
375         return 0;
376 }
377
378 gint copy_file_part(FILE *fp, off_t offset, size_t length, const gchar *dest)
379 {
380         FILE *dest_fp;
381         gboolean err = FALSE;
382
383         if ((dest_fp = claws_fopen(dest, "wb")) == NULL) {
384                 FILE_OP_ERROR(dest, "claws_fopen");
385                 return -1;
386         }
387
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);
391         }
392
393         if (copy_file_part_to_fp(fp, offset, length, dest_fp) < 0)
394                 err = TRUE;
395
396         if (claws_safe_fclose(dest_fp) == EOF) {
397                 FILE_OP_ERROR(dest, "claws_fclose");
398                 err = TRUE;
399         }
400
401         if (err) {
402                 g_warning("writing to %s failed.", dest);
403                 claws_unlink(dest);
404                 return -1;
405         }
406
407         return 0;
408 }
409
410 gint canonicalize_file(const gchar *src, const gchar *dest)
411 {
412         FILE *src_fp, *dest_fp;
413         gchar buf[BUFFSIZE];
414         gint len;
415         gboolean err = FALSE;
416         gboolean last_linebreak = FALSE;
417
418         if (src == NULL || dest == NULL)
419                 return -1;
420
421         if ((src_fp = claws_fopen(src, "rb")) == NULL) {
422                 FILE_OP_ERROR(src, "claws_fopen");
423                 return -1;
424         }
425
426         if ((dest_fp = claws_fopen(dest, "wb")) == NULL) {
427                 FILE_OP_ERROR(dest, "claws_fopen");
428                 claws_fclose(src_fp);
429                 return -1;
430         }
431
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);
435         }
436
437         while (claws_fgets(buf, sizeof(buf), src_fp) != NULL) {
438                 gint r = 0;
439
440                 len = strlen(buf);
441                 if (len == 0) break;
442                 last_linebreak = FALSE;
443
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);
449                 } else {
450                         if (len > 1) {
451                                 r = claws_fwrite(buf, 1, len - 1, dest_fp);
452                                 if (r != (len -1))
453                                         r = EOF;
454                         }
455                         if (r != EOF)
456                                 r = claws_fputs("\r\n", dest_fp);
457                 }
458
459                 if (r == EOF) {
460                         g_warning("writing to %s failed.", dest);
461                         claws_fclose(dest_fp);
462                         claws_fclose(src_fp);
463                         claws_unlink(dest);
464                         return -1;
465                 }
466         }
467
468         if (last_linebreak == TRUE) {
469                 if (claws_fputs("\r\n", dest_fp) == EOF)
470                         err = TRUE;
471         }
472
473         if (claws_ferror(src_fp)) {
474                 FILE_OP_ERROR(src, "claws_fgets");
475                 err = TRUE;
476         }
477         claws_fclose(src_fp);
478         if (claws_safe_fclose(dest_fp) == EOF) {
479                 FILE_OP_ERROR(dest, "claws_fclose");
480                 err = TRUE;
481         }
482
483         if (err) {
484                 claws_unlink(dest);
485                 return -1;
486         }
487
488         return 0;
489 }
490
491 gint canonicalize_file_replace(const gchar *file)
492 {
493         gchar *tmp_file;
494
495         tmp_file = get_tmp_file();
496
497         if (canonicalize_file(file, tmp_file) < 0) {
498                 g_free(tmp_file);
499                 return -1;
500         }
501
502         if (move_file(tmp_file, file, TRUE) < 0) {
503                 g_warning("can't replace file: %s", file);
504                 claws_unlink(tmp_file);
505                 g_free(tmp_file);
506                 return -1;
507         }
508
509         g_free(tmp_file);
510         return 0;
511 }
512
513
514 gint str_write_to_file(const gchar *str, const gchar *file)
515 {
516         FILE *fp;
517         size_t len;
518
519         cm_return_val_if_fail(str != NULL, -1);
520         cm_return_val_if_fail(file != NULL, -1);
521
522         if ((fp = claws_fopen(file, "wb")) == NULL) {
523                 FILE_OP_ERROR(file, "claws_fopen");
524                 return -1;
525         }
526
527         len = strlen(str);
528         if (len == 0) {
529                 claws_fclose(fp);
530                 return 0;
531         }
532
533         if (claws_fwrite(str, 1, len, fp) != len) {
534                 FILE_OP_ERROR(file, "claws_fwrite");
535                 claws_fclose(fp);
536                 claws_unlink(file);
537                 return -1;
538         }
539
540         if (claws_safe_fclose(fp) == EOF) {
541                 FILE_OP_ERROR(file, "claws_fclose");
542                 claws_unlink(file);
543                 return -1;
544         }
545
546         return 0;
547 }
548
549 static gchar *file_read_stream_to_str_full(FILE *fp, gboolean recode)
550 {
551         GByteArray *array;
552         guchar buf[BUFSIZ];
553         gint n_read;
554         gchar *str;
555
556         cm_return_val_if_fail(fp != NULL, NULL);
557
558         array = g_byte_array_new();
559
560         while ((n_read = claws_fread(buf, sizeof(gchar), sizeof(buf), fp)) > 0) {
561                 if (n_read < sizeof(buf) && claws_ferror(fp))
562                         break;
563                 g_byte_array_append(array, buf, n_read);
564         }
565
566         if (claws_ferror(fp)) {
567                 FILE_OP_ERROR("file stream", "claws_fread");
568                 g_byte_array_free(array, TRUE);
569                 return NULL;
570         }
571
572         buf[0] = '\0';
573         g_byte_array_append(array, buf, 1);
574         str = (gchar *)array->data;
575         g_byte_array_free(array, FALSE);
576
577         if (recode && !g_utf8_validate(str, -1, NULL)) {
578                 const gchar *src_codeset, *dest_codeset;
579                 gchar *tmp = NULL;
580                 src_codeset = conv_get_locale_charset_str();
581                 dest_codeset = CS_UTF_8;
582                 tmp = conv_codeset_strdup(str, src_codeset, dest_codeset);
583                 g_free(str);
584                 str = tmp;
585         }
586
587         return str;
588 }
589
590 static gchar *file_read_to_str_full(const gchar *file, gboolean recode)
591 {
592         FILE *fp;
593         gchar *str;
594         GStatBuf s;
595 #ifndef G_OS_WIN32
596         gint fd, err;
597         struct timeval timeout = {1, 0};
598         fd_set fds;
599         int fflags = 0;
600 #endif
601
602         cm_return_val_if_fail(file != NULL, NULL);
603
604         if (g_stat(file, &s) != 0) {
605                 FILE_OP_ERROR(file, "stat");
606                 return NULL;
607         }
608         if (S_ISDIR(s.st_mode)) {
609                 g_warning("%s: is a directory", file);
610                 return NULL;
611         }
612
613 #ifdef G_OS_WIN32
614         fp = claws_fopen (file, "rb");
615         if (fp == NULL) {
616                 FILE_OP_ERROR(file, "open");
617                 return NULL;
618         }
619 #else     
620         /* test whether the file is readable without blocking */
621         fd = g_open(file, O_RDONLY | O_NONBLOCK, 0);
622         if (fd == -1) {
623                 FILE_OP_ERROR(file, "open");
624                 return NULL;
625         }
626
627         FD_ZERO(&fds);
628         FD_SET(fd, &fds);
629
630         /* allow for one second */
631         err = select(fd+1, &fds, NULL, NULL, &timeout);
632         if (err <= 0 || !FD_ISSET(fd, &fds)) {
633                 if (err < 0) {
634                         FILE_OP_ERROR(file, "select");
635                 } else {
636                         g_warning("%s: doesn't seem readable", file);
637                 }
638                 close(fd);
639                 return NULL;
640         }
641         
642         /* Now clear O_NONBLOCK */
643         if ((fflags = fcntl(fd, F_GETFL)) < 0) {
644                 FILE_OP_ERROR(file, "fcntl (F_GETFL)");
645                 close(fd);
646                 return NULL;
647         }
648         if (fcntl(fd, F_SETFL, (fflags & ~O_NONBLOCK)) < 0) {
649                 FILE_OP_ERROR(file, "fcntl (F_SETFL)");
650                 close(fd);
651                 return NULL;
652         }
653         
654         /* get the FILE pointer */
655         fp = claws_fdopen(fd, "rb");
656
657         if (fp == NULL) {
658                 FILE_OP_ERROR(file, "claws_fdopen");
659                 close(fd); /* if fp isn't NULL, we'll use claws_fclose instead! */
660                 return NULL;
661         }
662 #endif
663
664         str = file_read_stream_to_str_full(fp, recode);
665
666         claws_fclose(fp);
667
668         return str;
669 }
670
671 gchar *file_read_to_str(const gchar *file)
672 {
673         return file_read_to_str_full(file, TRUE);
674 }
675 gchar *file_read_stream_to_str(FILE *fp)
676 {
677         return file_read_stream_to_str_full(fp, TRUE);
678 }
679
680 gchar *file_read_to_str_no_recode(const gchar *file)
681 {
682         return file_read_to_str_full(file, FALSE);
683 }
684 gchar *file_read_stream_to_str_no_recode(FILE *fp)
685 {
686         return file_read_stream_to_str_full(fp, FALSE);
687 }
688
689 gint rename_force(const gchar *oldpath, const gchar *newpath)
690 {
691 #ifndef G_OS_UNIX
692         if (!is_file_entry_exist(oldpath)) {
693                 errno = ENOENT;
694                 return -1;
695         }
696         if (is_file_exist(newpath)) {
697                 if (claws_unlink(newpath) < 0)
698                         FILE_OP_ERROR(newpath, "unlink");
699         }
700 #endif
701         return g_rename(oldpath, newpath);
702 }
703
704 gint copy_dir(const gchar *src, const gchar *dst)
705 {
706         GDir *dir;
707         const gchar *name;
708
709         if ((dir = g_dir_open(src, 0, NULL)) == NULL) {
710                 g_warning("failed to open directory: %s", src);
711                 return -1;
712         }
713
714         if (make_dir(dst) < 0)
715                 return -1;
716
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);
724                         if (r < 0) {
725                                 g_dir_close(dir);
726                                 return r;
727                         }
728                 }
729 #ifndef G_OS_WIN32
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;
735                         gint r = 0;
736                         gchar *target = g_file_read_link(old_file, &error);
737                         if (target)
738                                 r = symlink(target, new_file);
739                         g_free(target);
740                         if (r < 0) {
741                                 g_dir_close(dir);
742                                 return r;
743                         }
744                  }
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);
748                         if (r < 0) {
749                                 g_dir_close(dir);
750                                 return r;
751                         }
752                 }
753         }
754         g_dir_close(dir);
755         return 0;
756 }
757
758 gint change_file_mode_rw(FILE *fp, const gchar *file)
759 {
760 #if HAVE_FCHMOD
761         return fchmod(fileno(fp), S_IRUSR|S_IWUSR);
762 #else
763         return g_chmod(file, S_IRUSR|S_IWUSR);
764 #endif
765 }
766
767 FILE *my_tmpfile(void)
768 {
769         const gchar suffix[] = ".XXXXXX";
770         const gchar *tmpdir;
771         guint tmplen;
772         const gchar *progname;
773         guint proglen;
774         gchar *fname;
775         gint fd;
776         FILE *fp;
777 #ifndef G_OS_WIN32
778         gchar buf[2]="\0";
779 #endif
780
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),
788                 return tmpfile());
789
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));
794
795         fd = g_mkstemp(fname);
796         if (fd < 0)
797                 return tmpfile();
798
799 #ifndef G_OS_WIN32
800         claws_unlink(fname);
801         
802         /* verify that we can write in the file after unlinking */
803         if (write(fd, buf, 1) < 0) {
804                 close(fd);
805                 return tmpfile();
806         }
807         
808 #endif
809
810         fp = claws_fdopen(fd, "w+b");
811         if (!fp)
812                 close(fd);
813         else {
814                 rewind(fp);
815                 return fp;
816         }
817
818         return tmpfile();
819 }
820
821 FILE *my_tmpfile_with_len(size_t len)
822 {
823 #if HAVE_FMEMOPEN
824         FILE *tmpfp = fmemopen(NULL, len, "w+b");
825
826         if (tmpfp == NULL) {
827                 return my_tmpfile();
828         }
829
830         return tmpfp;
831 #else
832         return my_tmpfile();
833 #endif
834 }
835
836 FILE *get_tmpfile_in_dir(const gchar *dir, gchar **filename)
837 {
838         int fd;
839         *filename = g_strdup_printf("%s%cclaws.XXXXXX", dir, G_DIR_SEPARATOR);
840         fd = g_mkstemp(*filename);
841         if (fd < 0)
842                 return NULL;
843         return claws_fdopen(fd, "w+");
844 }
845
846 FILE *str_open_as_stream(const gchar *str)
847 {
848         FILE *fp;
849         size_t len;
850
851         cm_return_val_if_fail(str != NULL, NULL);
852
853         fp = my_tmpfile();
854         if (!fp) {
855                 FILE_OP_ERROR("str_open_as_stream", "my_tmpfile");
856                 return NULL;
857         }
858
859         len = strlen(str);
860         if (len == 0) return fp;
861
862         if (claws_fwrite(str, 1, len, fp) != len) {
863                 FILE_OP_ERROR("str_open_as_stream", "claws_fwrite");
864                 claws_fclose(fp);
865                 return NULL;
866         }
867
868         rewind(fp);
869         return fp;
870 }
871
872 #endif