NeoMutt  2023-11-03-107-g582dc1
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
file.c
Go to the documentation of this file.
1
29#include "config.h"
30#include <ctype.h>
31#include <errno.h>
32#include <fcntl.h>
33#include <libgen.h>
34#include <limits.h>
35#include <stdbool.h>
36#include <stdio.h>
37#include <stdlib.h>
38#include <string.h>
39#include <sys/stat.h>
40#include <unistd.h>
41#include <utime.h>
42#include <wchar.h>
43#include "file.h"
44#include "buffer.h"
45#include "charset.h"
46#include "date.h"
47#include "logging2.h"
48#include "memory.h"
49#include "message.h"
50#include "path.h"
51#include "pool.h"
52#include "string2.h"
53#ifdef USE_FLOCK
54#include <sys/file.h>
55#endif
56
58static const char RxSpecialChars[] = "^.[$()|*+?{\\";
59
61const char FilenameSafeChars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+@{}._-:%/";
62
63#define MAX_LOCK_ATTEMPTS 5
64
65/* This is defined in POSIX:2008 which isn't a build requirement */
66#ifndef O_NOFOLLOW
67#define O_NOFOLLOW 0
68#endif
69
79static bool stat_equal(struct stat *st_old, struct stat *st_new)
80{
81 return (st_old->st_dev == st_new->st_dev) && (st_old->st_ino == st_new->st_ino) &&
82 (st_old->st_rdev == st_new->st_rdev);
83}
84
93static int mkwrapdir(const char *path, struct Buffer *newfile, struct Buffer *newdir)
94{
95 const char *basename = NULL;
96 int rc = 0;
97
98 struct Buffer parent = buf_make(PATH_MAX);
99 buf_strcpy(&parent, NONULL(path));
100
101 char *p = strrchr(parent.data, '/');
102 if (p)
103 {
104 *p = '\0';
105 basename = p + 1;
106 }
107 else
108 {
109 buf_strcpy(&parent, ".");
110 basename = path;
111 }
112
113 buf_printf(newdir, "%s/%s", buf_string(&parent), ".muttXXXXXX");
114 if (!mkdtemp(newdir->data))
115 {
116 mutt_debug(LL_DEBUG1, "mkdtemp() failed\n");
117 rc = -1;
118 goto cleanup;
119 }
120
121 buf_printf(newfile, "%s/%s", newdir->data, NONULL(basename));
122
123cleanup:
124 buf_dealloc(&parent);
125 return rc;
126}
127
136static int put_file_in_place(const char *path, const char *safe_file, const char *safe_dir)
137{
138 int rc;
139
140 rc = mutt_file_safe_rename(safe_file, path);
141 unlink(safe_file);
142 rmdir(safe_dir);
143 return rc;
144}
145
152int mutt_file_fclose(FILE **fp)
153{
154 if (!fp || !*fp)
155 return 0;
156
157 int rc = fclose(*fp);
158 *fp = NULL;
159 return rc;
160}
161
169{
170 if (!fp || !*fp)
171 return 0;
172
173 int rc = 0;
174
175 if (fflush(*fp) || fsync(fileno(*fp)))
176 {
177 int save_errno = errno;
178 rc = -1;
180 errno = save_errno;
181 }
182 else
183 {
184 rc = mutt_file_fclose(fp);
185 }
186
187 return rc;
188}
189
196void mutt_file_unlink(const char *s)
197{
198 if (!s)
199 return;
200
201 struct stat st = { 0 };
202 /* Defend against symlink attacks */
203
204 const bool is_regular_file = (lstat(s, &st) == 0) && S_ISREG(st.st_mode);
205 if (!is_regular_file)
206 return;
207
208 const int fd = open(s, O_RDWR | O_NOFOLLOW);
209 if (fd < 0)
210 return;
211
212 struct stat st2 = { 0 };
213 if ((fstat(fd, &st2) != 0) || !S_ISREG(st2.st_mode) ||
214 (st.st_dev != st2.st_dev) || (st.st_ino != st2.st_ino))
215 {
216 close(fd);
217 return;
218 }
219
220 unlink(s);
221 close(fd);
222}
223
232int mutt_file_copy_bytes(FILE *fp_in, FILE *fp_out, size_t size)
233{
234 if (!fp_in || !fp_out)
235 return -1;
236
237 while (size > 0)
238 {
239 char buf[2048] = { 0 };
240 size_t chunk = (size > sizeof(buf)) ? sizeof(buf) : size;
241 chunk = fread(buf, 1, chunk, fp_in);
242 if (chunk < 1)
243 break;
244 if (fwrite(buf, 1, chunk, fp_out) != chunk)
245 return -1;
246
247 size -= chunk;
248 }
249
250 if (fflush(fp_out) != 0)
251 return -1;
252 return 0;
253}
254
262int mutt_file_copy_stream(FILE *fp_in, FILE *fp_out)
263{
264 if (!fp_in || !fp_out)
265 return -1;
266
267 size_t total = 0;
268 size_t l;
269 char buf[1024] = { 0 };
270
271 while ((l = fread(buf, 1, sizeof(buf), fp_in)) > 0)
272 {
273 if (fwrite(buf, 1, l, fp_out) != l)
274 return -1;
275 total += l;
276 }
277
278 if (fflush(fp_out) != 0)
279 return -1;
280 return total;
281}
282
290int mutt_file_symlink(const char *oldpath, const char *newpath)
291{
292 struct stat st_old = { 0 };
293 struct stat st_new = { 0 };
294
295 if (!oldpath || !newpath)
296 return -1;
297
298 if ((unlink(newpath) == -1) && (errno != ENOENT))
299 return -1;
300
301 if (oldpath[0] == '/')
302 {
303 if (symlink(oldpath, newpath) == -1)
304 return -1;
305 }
306 else
307 {
308 struct Buffer abs_oldpath = buf_make(PATH_MAX);
309
310 if (!mutt_path_getcwd(&abs_oldpath))
311 {
312 buf_dealloc(&abs_oldpath);
313 return -1;
314 }
315
316 buf_addch(&abs_oldpath, '/');
317 buf_addstr(&abs_oldpath, oldpath);
318 if (symlink(buf_string(&abs_oldpath), newpath) == -1)
319 {
320 buf_dealloc(&abs_oldpath);
321 return -1;
322 }
323
324 buf_dealloc(&abs_oldpath);
325 }
326
327 if ((stat(oldpath, &st_old) == -1) || (stat(newpath, &st_new) == -1) ||
328 !stat_equal(&st_old, &st_new))
329 {
330 unlink(newpath);
331 return -1;
332 }
333
334 return 0;
335}
336
346int mutt_file_safe_rename(const char *src, const char *target)
347{
348 struct stat st_src = { 0 };
349 struct stat st_target = { 0 };
350 int link_errno;
351
352 if (!src || !target)
353 return -1;
354
355 if (link(src, target) != 0)
356 {
357 link_errno = errno;
358
359 /* It is historically documented that link can return -1 if NFS
360 * dies after creating the link. In that case, we are supposed
361 * to use stat to check if the link was created.
362 *
363 * Derek Martin notes that some implementations of link() follow a
364 * source symlink. It might be more correct to use stat() on src.
365 * I am not doing so to minimize changes in behavior: the function
366 * used lstat() further below for 20 years without issue, and I
367 * believe was never intended to be used on a src symlink. */
368 if ((lstat(src, &st_src) == 0) && (lstat(target, &st_target) == 0) &&
369 (stat_equal(&st_src, &st_target) == 0))
370 {
371 mutt_debug(LL_DEBUG1, "link (%s, %s) reported failure: %s (%d) but actually succeeded\n",
372 src, target, strerror(errno), errno);
373 goto success;
374 }
375
376 errno = link_errno;
377
378 /* Coda does not allow cross-directory links, but tells
379 * us it's a cross-filesystem linking attempt.
380 *
381 * However, the Coda rename call is allegedly safe to use.
382 *
383 * With other file systems, rename should just fail when
384 * the files reside on different file systems, so it's safe
385 * to try it here. */
386 mutt_debug(LL_DEBUG1, "link (%s, %s) failed: %s (%d)\n", src, target,
387 strerror(errno), errno);
388
389 /* FUSE may return ENOSYS. VFAT may return EPERM. FreeBSD's
390 * msdosfs may return EOPNOTSUPP. ENOTSUP can also appear. */
391 if ((errno == EXDEV) || (errno == ENOSYS) || errno == EPERM
392#ifdef ENOTSUP
393 || errno == ENOTSUP
394#endif
395#ifdef EOPNOTSUPP
396 || errno == EOPNOTSUPP
397#endif
398 )
399 {
400 mutt_debug(LL_DEBUG1, "trying rename\n");
401 if (rename(src, target) == -1)
402 {
403 mutt_debug(LL_DEBUG1, "rename (%s, %s) failed: %s (%d)\n", src, target,
404 strerror(errno), errno);
405 return -1;
406 }
407 mutt_debug(LL_DEBUG1, "rename succeeded\n");
408
409 return 0;
410 }
411
412 return -1;
413 }
414
415 /* Remove the stat_equal() check, because it causes problems with maildir
416 * on filesystems that don't properly support hard links, such as sshfs. The
417 * filesystem creates the link, but the resulting file is given a different
418 * inode number by the sshfs layer. This results in an infinite loop
419 * creating links. */
420#if 0
421 /* Stat both links and check if they are equal. */
422 if (lstat(src, &st_src) == -1)
423 {
424 mutt_debug(LL_DEBUG1, "#1 can't stat %s: %s (%d)\n", src, strerror(errno), errno);
425 return -1;
426 }
427
428 if (lstat(target, &st_target) == -1)
429 {
430 mutt_debug(LL_DEBUG1, "#2 can't stat %s: %s (%d)\n", src, strerror(errno), errno);
431 return -1;
432 }
433
434 /* pretend that the link failed because the target file did already exist. */
435
436 if (!stat_equal(&st_src, &st_target))
437 {
438 mutt_debug(LL_DEBUG1, "stat blocks for %s and %s diverge; pretending EEXIST\n", src, target);
439 errno = EEXIST;
440 return -1;
441 }
442#endif
443
444success:
445 /* Unlink the original link.
446 * Should we really ignore the return value here? XXX */
447 if (unlink(src) == -1)
448 {
449 mutt_debug(LL_DEBUG1, "unlink (%s) failed: %s (%d)\n", src, strerror(errno), errno);
450 }
451
452 return 0;
453}
454
461int mutt_file_rmtree(const char *path)
462{
463 if (!path)
464 return -1;
465
466 struct dirent *de = NULL;
467 struct stat st = { 0 };
468 int rc = 0;
469
470 DIR *dir = mutt_file_opendir(path, MUTT_OPENDIR_NONE);
471 if (!dir)
472 {
473 mutt_debug(LL_DEBUG1, "error opening directory %s\n", path);
474 return -1;
475 }
476
477 /* We avoid using the buffer pool for this function, because it
478 * invokes recursively to an unknown depth. */
479 struct Buffer cur = buf_make(PATH_MAX);
480
481 while ((de = readdir(dir)))
482 {
483 if ((mutt_str_equal(".", de->d_name)) || (mutt_str_equal("..", de->d_name)))
484 continue;
485
486 buf_printf(&cur, "%s/%s", path, de->d_name);
487 /* XXX make nonrecursive version */
488
489 if (stat(buf_string(&cur), &st) == -1)
490 {
491 rc = 1;
492 continue;
493 }
494
495 if (S_ISDIR(st.st_mode))
496 rc |= mutt_file_rmtree(buf_string(&cur));
497 else
498 rc |= unlink(buf_string(&cur));
499 }
500 closedir(dir);
501
502 rc |= rmdir(path);
503
504 buf_dealloc(&cur);
505 return rc;
506}
507
521const char *mutt_file_rotate(const char *path, int count)
522{
523 if (!path)
524 return NULL;
525
526 struct Buffer *old_file = buf_pool_get();
527 struct Buffer *new_file = buf_pool_get();
528
529 /* rotate the old debug logs */
530 for (count -= 2; count >= 0; count--)
531 {
532 buf_printf(old_file, "%s%d", path, count);
533 buf_printf(new_file, "%s%d", path, count + 1);
534 (void) rename(buf_string(old_file), buf_string(new_file));
535 }
536
537 path = buf_strdup(old_file);
538 buf_pool_release(&old_file);
539 buf_pool_release(&new_file);
540
541 return path;
542}
543
551int mutt_file_open(const char *path, uint32_t flags)
552{
553 if (!path)
554 return -1;
555
556 int fd;
557 struct Buffer safe_file = buf_make(0);
558 struct Buffer safe_dir = buf_make(0);
559
560 if (flags & O_EXCL)
561 {
562 buf_alloc(&safe_file, PATH_MAX);
563 buf_alloc(&safe_dir, PATH_MAX);
564
565 if (mkwrapdir(path, &safe_file, &safe_dir) == -1)
566 {
567 fd = -1;
568 goto cleanup;
569 }
570
571 fd = open(buf_string(&safe_file), flags, 0600);
572 if (fd < 0)
573 {
574 rmdir(buf_string(&safe_dir));
575 goto cleanup;
576 }
577
578 /* NFS and I believe cygwin do not handle movement of open files well */
579 close(fd);
580 if (put_file_in_place(path, buf_string(&safe_file), buf_string(&safe_dir)) == -1)
581 {
582 fd = -1;
583 goto cleanup;
584 }
585 }
586
587 fd = open(path, flags & ~O_EXCL, 0600);
588 if (fd < 0)
589 goto cleanup;
590
591 /* make sure the file is not symlink */
592 struct stat st_old = { 0 };
593 struct stat st_new = { 0 };
594 if (((lstat(path, &st_old) < 0) || (fstat(fd, &st_new) < 0)) ||
595 !stat_equal(&st_old, &st_new))
596 {
597 close(fd);
598 fd = -1;
599 goto cleanup;
600 }
601
602cleanup:
603 buf_dealloc(&safe_file);
604 buf_dealloc(&safe_dir);
605
606 return fd;
607}
608
616DIR *mutt_file_opendir(const char *path, enum MuttOpenDirMode mode)
617{
618 if ((mode == MUTT_OPENDIR_CREATE) && (mutt_file_mkdir(path, S_IRWXU) == -1))
619 {
620 return NULL;
621 }
622 errno = 0;
623 return opendir(path);
624}
625
636FILE *mutt_file_fopen(const char *path, const char *mode)
637{
638 if (!path || !mode)
639 return NULL;
640
641 if (mode[0] == 'w')
642 {
643 uint32_t flags = O_CREAT | O_EXCL | O_NOFOLLOW;
644
645 if (mode[1] == '+')
646 flags |= O_RDWR;
647 else
648 flags |= O_WRONLY;
649
650 int fd = mutt_file_open(path, flags);
651 if (fd < 0)
652 return NULL;
653
654 return fdopen(fd, mode);
655 }
656 else
657 {
658 return fopen(path, mode);
659 }
660}
661
667void mutt_file_sanitize_filename(char *path, bool slash)
668{
669 if (!path)
670 return;
671
672 size_t size = strlen(path);
673
674 wchar_t c;
675 mbstate_t mbstate = { 0 };
676 for (size_t consumed; size && (consumed = mbrtowc(&c, path, size, &mbstate));
677 size -= consumed, path += consumed)
678 {
679 switch (consumed)
680 {
682 mbstate = (mbstate_t){ 0 };
683 consumed = 1;
684 memset(path, '_', consumed);
685 break;
686
688 consumed = size;
689 memset(path, '_', consumed);
690 break;
691
692 default:
693 if ((slash && (c == L'/')) || ((c <= 0x7F) && !strchr(FilenameSafeChars, c)))
694 {
695 memset(path, '_', consumed);
696 }
697 break;
698 }
699 }
700}
701
709int mutt_file_sanitize_regex(struct Buffer *dest, const char *src)
710{
711 if (!dest || !src)
712 return -1;
713
714 buf_reset(dest);
715 while (*src != '\0')
716 {
717 if (strchr(RxSpecialChars, *src))
718 buf_addch(dest, '\\');
719 buf_addch(dest, *src++);
720 }
721
722 return 0;
723}
724
733bool mutt_file_seek(FILE *fp, LOFF_T offset, int whence)
734{
735 if (!fp)
736 {
737 return false;
738 }
739
740 if (fseeko(fp, offset, whence) != 0)
741 {
742 mutt_perror(_("Failed to seek file: %s"), strerror(errno));
743 return false;
744 }
745
746 return true;
747}
748
763char *mutt_file_read_line(char *line, size_t *size, FILE *fp, int *line_num, ReadLineFlags flags)
764{
765 if (!size || !fp)
766 return NULL;
767
768 size_t offset = 0;
769 char *ch = NULL;
770
771 if (!line)
772 {
773 *size = 256;
774 line = mutt_mem_malloc(*size);
775 }
776
777 while (true)
778 {
779 if (!fgets(line + offset, *size - offset, fp))
780 {
781 FREE(&line);
782 return NULL;
783 }
784 ch = strchr(line + offset, '\n');
785 if (ch)
786 {
787 if (line_num)
788 (*line_num)++;
789 if (flags & MUTT_RL_EOL)
790 return line;
791 *ch = '\0';
792 if ((ch > line) && (*(ch - 1) == '\r'))
793 *--ch = '\0';
794 if (!(flags & MUTT_RL_CONT) || (ch == line) || (*(ch - 1) != '\\'))
795 return line;
796 offset = ch - line - 1;
797 }
798 else
799 {
800 int c;
801 c = getc(fp); /* This is kind of a hack. We want to know if the
802 char at the current point in the input stream is EOF.
803 feof() will only tell us if we've already hit EOF, not
804 if the next character is EOF. So, we need to read in
805 the next character and manually check if it is EOF. */
806 if (c == EOF)
807 {
808 /* The last line of fp isn't \n terminated */
809 if (line_num)
810 (*line_num)++;
811 return line;
812 }
813 else
814 {
815 ungetc(c, fp); /* undo our damage */
816 /* There wasn't room for the line -- increase "line" */
817 offset = *size - 1; /* overwrite the terminating 0 */
818 *size += 256;
819 mutt_mem_realloc(&line, *size);
820 }
821 }
822 }
823}
824
844bool mutt_file_iter_line(struct MuttFileIter *iter, FILE *fp, ReadLineFlags flags)
845{
846 if (!iter)
847 return false;
848
849 char *p = mutt_file_read_line(iter->line, &iter->size, fp, &iter->line_num, flags);
850 if (!p)
851 return false;
852 iter->line = p;
853 return true;
854}
855
865bool mutt_file_map_lines(mutt_file_map_t func, void *user_data, FILE *fp, ReadLineFlags flags)
866{
867 if (!func || !fp)
868 return false;
869
870 struct MuttFileIter iter = { 0 };
871 while (mutt_file_iter_line(&iter, fp, flags))
872 {
873 if (!(*func)(iter.line, iter.line_num, user_data))
874 {
875 FREE(&iter.line);
876 return false;
877 }
878 }
879 return true;
880}
881
891size_t mutt_file_quote_filename(const char *filename, char *buf, size_t buflen)
892{
893 if (!buf)
894 return 0;
895
896 if (!filename)
897 {
898 *buf = '\0';
899 return 0;
900 }
901
902 size_t j = 0;
903
904 /* leave some space for the trailing characters. */
905 buflen -= 6;
906
907 buf[j++] = '\'';
908
909 for (size_t i = 0; (j < buflen) && filename[i]; i++)
910 {
911 if ((filename[i] == '\'') || (filename[i] == '`'))
912 {
913 buf[j++] = '\'';
914 buf[j++] = '\\';
915 buf[j++] = filename[i];
916 buf[j++] = '\'';
917 }
918 else
919 {
920 buf[j++] = filename[i];
921 }
922 }
923
924 buf[j++] = '\'';
925 buf[j] = '\0';
926
927 return j;
928}
929
936void buf_quote_filename(struct Buffer *buf, const char *filename, bool add_outer)
937{
938 if (!buf || !filename)
939 return;
940
941 buf_reset(buf);
942 if (add_outer)
943 buf_addch(buf, '\'');
944
945 for (; *filename != '\0'; filename++)
946 {
947 if ((*filename == '\'') || (*filename == '`'))
948 {
949 buf_addch(buf, '\'');
950 buf_addch(buf, '\\');
951 buf_addch(buf, *filename);
952 buf_addch(buf, '\'');
953 }
954 else
955 {
956 buf_addch(buf, *filename);
957 }
958 }
959
960 if (add_outer)
961 buf_addch(buf, '\'');
962}
963
977int mutt_file_mkdir(const char *path, mode_t mode)
978{
979 if (!path || (*path == '\0'))
980 {
981 errno = EINVAL;
982 return -1;
983 }
984
985 errno = 0;
986 char tmp_path[PATH_MAX] = { 0 };
987 const size_t len = strlen(path);
988
989 if (len >= sizeof(tmp_path))
990 {
991 errno = ENAMETOOLONG;
992 return -1;
993 }
994
995 struct stat st = { 0 };
996 if ((stat(path, &st) == 0) && S_ISDIR(st.st_mode))
997 return 0;
998
999 /* Create a mutable copy */
1000 mutt_str_copy(tmp_path, path, sizeof(tmp_path));
1001
1002 for (char *p = tmp_path + 1; *p; p++)
1003 {
1004 if (*p != '/')
1005 continue;
1006
1007 /* Temporarily truncate the path */
1008 *p = '\0';
1009
1010 if ((mkdir(tmp_path, S_IRWXU | S_IRWXG | S_IRWXO) != 0) && (errno != EEXIST))
1011 return -1;
1012
1013 *p = '/';
1014 }
1015
1016 if ((mkdir(tmp_path, mode) != 0) && (errno != EEXIST))
1017 return -1;
1018
1019 return 0;
1020}
1021
1031time_t mutt_file_decrease_mtime(const char *fp, struct stat *st)
1032{
1033 if (!fp)
1034 return -1;
1035
1036 struct utimbuf utim = { 0 };
1037 struct stat st2 = { 0 };
1038 time_t mtime;
1039
1040 if (!st)
1041 {
1042 if (stat(fp, &st2) == -1)
1043 return -1;
1044 st = &st2;
1045 }
1046
1047 mtime = st->st_mtime;
1048 if (mtime == mutt_date_now())
1049 {
1050 mtime -= 1;
1051 utim.actime = mtime;
1052 utim.modtime = mtime;
1053 int rc;
1054 do
1055 {
1056 rc = utime(fp, &utim);
1057 } while ((rc == -1) && (errno == EINTR));
1058
1059 if (rc == -1)
1060 return -1;
1061 }
1062
1063 return mtime;
1064}
1065
1071void mutt_file_set_mtime(const char *from, const char *to)
1072{
1073 if (!from || !to)
1074 return;
1075
1076 struct utimbuf utim = { 0 };
1077 struct stat st = { 0 };
1078
1079 if (stat(from, &st) != -1)
1080 {
1081 utim.actime = st.st_mtime;
1082 utim.modtime = st.st_mtime;
1083 utime(to, &utim);
1084 }
1085}
1086
1095{
1096#ifdef HAVE_FUTIMENS
1097 struct timespec times[2] = { { 0, UTIME_NOW }, { 0, UTIME_OMIT } };
1098 futimens(fd, times);
1099#endif
1100}
1101
1110int mutt_file_chmod(const char *path, mode_t mode)
1111{
1112 if (!path)
1113 return -1;
1114
1115 return chmod(path, mode);
1116}
1117
1135int mutt_file_chmod_add(const char *path, mode_t mode)
1136{
1137 return mutt_file_chmod_add_stat(path, mode, NULL);
1138}
1139
1158int mutt_file_chmod_add_stat(const char *path, mode_t mode, struct stat *st)
1159{
1160 if (!path)
1161 return -1;
1162
1163 struct stat st2 = { 0 };
1164
1165 if (!st)
1166 {
1167 if (stat(path, &st2) == -1)
1168 return -1;
1169 st = &st2;
1170 }
1171 return chmod(path, st->st_mode | mode);
1172}
1173
1191int mutt_file_chmod_rm(const char *path, mode_t mode)
1192{
1193 return mutt_file_chmod_rm_stat(path, mode, NULL);
1194}
1195
1214int mutt_file_chmod_rm_stat(const char *path, mode_t mode, struct stat *st)
1215{
1216 if (!path)
1217 return -1;
1218
1219 struct stat st2 = { 0 };
1220
1221 if (!st)
1222 {
1223 if (stat(path, &st2) == -1)
1224 return -1;
1225 st = &st2;
1226 }
1227 return chmod(path, st->st_mode & ~mode);
1228}
1229
1230#if defined(USE_FCNTL)
1243int mutt_file_lock(int fd, bool excl, bool timeout)
1244{
1245 struct stat st = { 0 }, prev_sb = { 0 };
1246 int count = 0;
1247 int attempt = 0;
1248
1249 struct flock lck = { 0 };
1250 lck.l_type = excl ? F_WRLCK : F_RDLCK;
1251 lck.l_whence = SEEK_SET;
1252
1253 while (fcntl(fd, F_SETLK, &lck) == -1)
1254 {
1255 mutt_debug(LL_DEBUG1, "fcntl errno %d\n", errno);
1256 if ((errno != EAGAIN) && (errno != EACCES))
1257 {
1258 mutt_perror("fcntl");
1259 return -1;
1260 }
1261
1262 if (fstat(fd, &st) != 0)
1263 st.st_size = 0;
1264
1265 if (count == 0)
1266 prev_sb = st;
1267
1268 /* only unlock file if it is unchanged */
1269 if ((prev_sb.st_size == st.st_size) && (++count >= (timeout ? MAX_LOCK_ATTEMPTS : 0)))
1270 {
1271 if (timeout)
1272 mutt_error(_("Timeout exceeded while attempting fcntl lock"));
1273 return -1;
1274 }
1275
1276 prev_sb = st;
1277
1278 mutt_message(_("Waiting for fcntl lock... %d"), ++attempt);
1279 sleep(1);
1280 }
1281
1282 return 0;
1283}
1284
1291{
1292 struct flock unlockit = { 0 };
1293 unlockit.l_type = F_UNLCK;
1294 unlockit.l_whence = SEEK_SET;
1295 (void) fcntl(fd, F_SETLK, &unlockit);
1296
1297 return 0;
1298}
1299#elif defined(USE_FLOCK)
1312int mutt_file_lock(int fd, bool excl, bool timeout)
1313{
1314 struct stat st = { 0 }, prev_sb = { 0 };
1315 int rc = 0;
1316 int count = 0;
1317 int attempt = 0;
1318
1319 while (flock(fd, (excl ? LOCK_EX : LOCK_SH) | LOCK_NB) == -1)
1320 {
1321 if (errno != EWOULDBLOCK)
1322 {
1323 mutt_perror("flock");
1324 rc = -1;
1325 break;
1326 }
1327
1328 if (fstat(fd, &st) != 0)
1329 st.st_size = 0;
1330
1331 if (count == 0)
1332 prev_sb = st;
1333
1334 /* only unlock file if it is unchanged */
1335 if ((prev_sb.st_size == st.st_size) && (++count >= (timeout ? MAX_LOCK_ATTEMPTS : 0)))
1336 {
1337 if (timeout)
1338 mutt_error(_("Timeout exceeded while attempting flock lock"));
1339 rc = -1;
1340 break;
1341 }
1342
1343 prev_sb = st;
1344
1345 mutt_message(_("Waiting for flock attempt... %d"), ++attempt);
1346 sleep(1);
1347 }
1348
1349 /* release any other locks obtained in this routine */
1350 if (rc != 0)
1351 {
1352 flock(fd, LOCK_UN);
1353 }
1354
1355 return rc;
1356}
1357
1363int mutt_file_unlock(int fd)
1364{
1365 flock(fd, LOCK_UN);
1366 return 0;
1367}
1368#else
1369#error "You must select a locking mechanism via USE_FCNTL or USE_FLOCK"
1370#endif
1371
1376void mutt_file_unlink_empty(const char *path)
1377{
1378 if (!path)
1379 return;
1380
1381 struct stat st = { 0 };
1382
1383 int fd = open(path, O_RDWR);
1384 if (fd == -1)
1385 return;
1386
1387 if (mutt_file_lock(fd, true, true) == -1)
1388 {
1389 close(fd);
1390 return;
1391 }
1392
1393 if ((fstat(fd, &st) == 0) && (st.st_size == 0))
1394 unlink(path);
1395
1396 mutt_file_unlock(fd);
1397 close(fd);
1398}
1399
1412int mutt_file_rename(const char *oldfile, const char *newfile)
1413{
1414 if (!oldfile || !newfile)
1415 return -1;
1416 if (access(oldfile, F_OK) != 0)
1417 return 1;
1418 if (access(newfile, F_OK) == 0)
1419 return 2;
1420
1421 FILE *fp_old = fopen(oldfile, "r");
1422 if (!fp_old)
1423 return 3;
1424 FILE *fp_new = mutt_file_fopen(newfile, "w");
1425 if (!fp_new)
1426 {
1427 mutt_file_fclose(&fp_old);
1428 return 3;
1429 }
1430 mutt_file_copy_stream(fp_old, fp_new);
1431 mutt_file_fclose(&fp_new);
1432 mutt_file_fclose(&fp_old);
1433 mutt_file_unlink(oldfile);
1434 return 0;
1435}
1436
1447char *mutt_file_read_keyword(const char *file, char *buf, size_t buflen)
1448{
1449 FILE *fp = mutt_file_fopen(file, "r");
1450 if (!fp)
1451 return NULL;
1452
1453 buf = fgets(buf, buflen, fp);
1454 mutt_file_fclose(&fp);
1455
1456 if (!buf)
1457 return NULL;
1458
1459 SKIPWS(buf);
1460 char *start = buf;
1461
1462 while ((*buf != '\0') && !isspace(*buf))
1463 buf++;
1464
1465 *buf = '\0';
1466
1467 return start;
1468}
1469
1477int mutt_file_check_empty(const char *path)
1478{
1479 if (!path)
1480 return -1;
1481
1482 struct stat st = { 0 };
1483 if (stat(path, &st) == -1)
1484 return -1;
1485
1486 return st.st_size == 0;
1487}
1488
1497void buf_file_expand_fmt_quote(struct Buffer *dest, const char *fmt, const char *src)
1498{
1499 struct Buffer tmp = buf_make(PATH_MAX);
1500
1501 buf_quote_filename(&tmp, src, true);
1502 mutt_file_expand_fmt(dest, fmt, buf_string(&tmp));
1503 buf_dealloc(&tmp);
1504}
1505
1512void mutt_file_expand_fmt(struct Buffer *dest, const char *fmt, const char *src)
1513{
1514 if (!dest || !fmt || !src)
1515 return;
1516
1517 const char *p = NULL;
1518 bool found = false;
1519
1520 buf_reset(dest);
1521
1522 for (p = fmt; *p; p++)
1523 {
1524 if (*p == '%')
1525 {
1526 switch (p[1])
1527 {
1528 case '%':
1529 buf_addch(dest, *p++);
1530 break;
1531 case 's':
1532 found = true;
1533 buf_addstr(dest, src);
1534 p++;
1535 break;
1536 default:
1537 buf_addch(dest, *p);
1538 break;
1539 }
1540 }
1541 else
1542 {
1543 buf_addch(dest, *p);
1544 }
1545 }
1546
1547 if (!found)
1548 {
1549 buf_addch(dest, ' ');
1550 buf_addstr(dest, src);
1551 }
1552}
1553
1560long mutt_file_get_size(const char *path)
1561{
1562 if (!path)
1563 return 0;
1564
1565 struct stat st = { 0 };
1566 if (stat(path, &st) != 0)
1567 return 0;
1568
1569 return st.st_size;
1570}
1571
1579{
1580 if (!fp)
1581 return 0;
1582
1583 struct stat st = { 0 };
1584 if (fstat(fileno(fp), &st) != 0)
1585 return 0;
1586
1587 return st.st_size;
1588}
1589
1599{
1600 if (!a || !b)
1601 return 0;
1602 if (a->tv_sec < b->tv_sec)
1603 return -1;
1604 if (a->tv_sec > b->tv_sec)
1605 return 1;
1606
1607 if (a->tv_nsec < b->tv_nsec)
1608 return -1;
1609 if (a->tv_nsec > b->tv_nsec)
1610 return 1;
1611 return 0;
1612}
1613
1620void mutt_file_get_stat_timespec(struct timespec *dest, struct stat *st, enum MuttStatType type)
1621{
1622 if (!dest || !st)
1623 return;
1624
1625 dest->tv_sec = 0;
1626 dest->tv_nsec = 0;
1627
1628 switch (type)
1629 {
1630 case MUTT_STAT_ATIME:
1631 dest->tv_sec = st->st_atime;
1632#ifdef HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC
1633 dest->tv_nsec = st->st_atim.tv_nsec;
1634#endif
1635 break;
1636 case MUTT_STAT_MTIME:
1637 dest->tv_sec = st->st_mtime;
1638#ifdef HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC
1639 dest->tv_nsec = st->st_mtim.tv_nsec;
1640#endif
1641 break;
1642 case MUTT_STAT_CTIME:
1643 dest->tv_sec = st->st_ctime;
1644#ifdef HAVE_STRUCT_STAT_ST_CTIM_TV_NSEC
1645 dest->tv_nsec = st->st_ctim.tv_nsec;
1646#endif
1647 break;
1648 }
1649}
1650
1660int mutt_file_stat_timespec_compare(struct stat *st, enum MuttStatType type,
1661 struct timespec *b)
1662{
1663 if (!st || !b)
1664 return 0;
1665
1666 struct timespec a = { 0 };
1667
1668 mutt_file_get_stat_timespec(&a, st, type);
1669 return mutt_file_timespec_compare(&a, b);
1670}
1671
1682int mutt_file_stat_compare(struct stat *st1, enum MuttStatType st1_type,
1683 struct stat *st2, enum MuttStatType st2_type)
1684{
1685 if (!st1 || !st2)
1686 return 0;
1687
1688 struct timespec a = { 0 };
1689 struct timespec b = { 0 };
1690
1691 mutt_file_get_stat_timespec(&a, st1, st1_type);
1692 mutt_file_get_stat_timespec(&b, st2, st2_type);
1693 return mutt_file_timespec_compare(&a, &b);
1694}
1695
1701{
1702 struct stat st = { 0 };
1703 int rc = lstat(buf_string(buf), &st);
1704 if ((rc != -1) && S_ISLNK(st.st_mode))
1705 {
1706 char path[PATH_MAX] = { 0 };
1707 if (realpath(buf_string(buf), path))
1708 {
1709 buf_strcpy(buf, path);
1710 }
1711 }
1712}
1713
1720size_t mutt_file_save_str(FILE *fp, const char *str)
1721{
1722 if (!fp)
1723 return 0;
1724
1725 size_t len = mutt_str_len(str);
1726 if (len == 0)
1727 return 0;
1728
1729 return fwrite(str, 1, len, fp);
1730}
int buf_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:173
void buf_dealloc(struct Buffer *buf)
Release the memory allocated by a buffer.
Definition: buffer.c:389
void buf_reset(struct Buffer *buf)
Reset an existing Buffer.
Definition: buffer.c:88
struct Buffer buf_make(size_t size)
Make a new buffer on the stack.
Definition: buffer.c:70
size_t buf_addch(struct Buffer *buf, char c)
Add a single character to a Buffer.
Definition: buffer.c:253
size_t buf_addstr(struct Buffer *buf, const char *s)
Add a string to a Buffer.
Definition: buffer.c:238
size_t buf_strcpy(struct Buffer *buf, const char *s)
Copy a string into a Buffer.
Definition: buffer.c:407
char * buf_strdup(const struct Buffer *buf)
Copy a Buffer's string.
Definition: buffer.c:542
void buf_alloc(struct Buffer *buf, size_t new_size)
Make sure a buffer can store at least new_size bytes.
Definition: buffer.c:349
General purpose object for storing and parsing strings.
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:93
Time and date handling routines.
void mutt_file_get_stat_timespec(struct timespec *dest, struct stat *st, enum MuttStatType type)
Read the stat() time into a time value.
Definition: file.c:1620
int mutt_file_copy_stream(FILE *fp_in, FILE *fp_out)
Copy the contents of one file into another.
Definition: file.c:262
void buf_quote_filename(struct Buffer *buf, const char *filename, bool add_outer)
Quote a filename to survive the shell's quoting rules.
Definition: file.c:936
int mutt_file_open(const char *path, uint32_t flags)
Open a file.
Definition: file.c:551
char * mutt_file_read_line(char *line, size_t *size, FILE *fp, int *line_num, ReadLineFlags flags)
Read a line from a file.
Definition: file.c:763
FILE * mutt_file_fopen(const char *path, const char *mode)
Call fopen() safely.
Definition: file.c:636
int mutt_file_safe_rename(const char *src, const char *target)
NFS-safe renaming of files.
Definition: file.c:346
void mutt_file_unlink_empty(const char *path)
Delete a file if it's empty.
Definition: file.c:1376
const char FilenameSafeChars[]
Set of characters <=0x7F that are safe to use in filenames.
Definition: file.c:61
int mutt_file_stat_compare(struct stat *st1, enum MuttStatType st1_type, struct stat *st2, enum MuttStatType st2_type)
Compare two stat infos.
Definition: file.c:1682
int mutt_file_copy_bytes(FILE *fp_in, FILE *fp_out, size_t size)
Copy some content from one file to another.
Definition: file.c:232
int mutt_file_fclose(FILE **fp)
Close a FILE handle (and NULL the pointer)
Definition: file.c:152
char * mutt_file_read_keyword(const char *file, char *buf, size_t buflen)
Read a keyword from a file.
Definition: file.c:1447
#define MAX_LOCK_ATTEMPTS
Definition: file.c:63
void buf_file_expand_fmt_quote(struct Buffer *dest, const char *fmt, const char *src)
Replace s in a string with a filename.
Definition: file.c:1497
void mutt_file_touch_atime(int fd)
Set the access time to current time.
Definition: file.c:1094
int mutt_file_sanitize_regex(struct Buffer *dest, const char *src)
Escape any regex-magic characters in a string.
Definition: file.c:709
int mutt_file_check_empty(const char *path)
Is the mailbox empty.
Definition: file.c:1477
int mutt_file_mkdir(const char *path, mode_t mode)
Recursively create directories.
Definition: file.c:977
int mutt_file_lock(int fd, bool excl, bool timeout)
(Try to) Lock a file using fcntl()
Definition: file.c:1243
long mutt_file_get_size_fp(FILE *fp)
Get the size of a file.
Definition: file.c:1578
void mutt_file_sanitize_filename(char *path, bool slash)
Replace unsafe characters in a filename.
Definition: file.c:667
int mutt_file_timespec_compare(struct timespec *a, struct timespec *b)
Compare to time values.
Definition: file.c:1598
int mutt_file_unlock(int fd)
Unlock a file previously locked by mutt_file_lock()
Definition: file.c:1290
int mutt_file_chmod_rm(const char *path, mode_t mode)
Remove permissions from a file.
Definition: file.c:1191
time_t mutt_file_decrease_mtime(const char *fp, struct stat *st)
Decrease a file's modification time by 1 second.
Definition: file.c:1031
bool mutt_file_map_lines(mutt_file_map_t func, void *user_data, FILE *fp, ReadLineFlags flags)
Process lines of text read from a file pointer.
Definition: file.c:865
size_t mutt_file_quote_filename(const char *filename, char *buf, size_t buflen)
Quote a filename to survive the shell's quoting rules.
Definition: file.c:891
DIR * mutt_file_opendir(const char *path, enum MuttOpenDirMode mode)
Open a directory.
Definition: file.c:616
bool mutt_file_seek(FILE *fp, LOFF_T offset, int whence)
Wrapper for fseeko with error handling.
Definition: file.c:733
static bool stat_equal(struct stat *st_old, struct stat *st_new)
Compare the struct stat's of two files/dirs.
Definition: file.c:79
long mutt_file_get_size(const char *path)
Get the size of a file.
Definition: file.c:1560
int mutt_file_chmod(const char *path, mode_t mode)
Set permissions of a file.
Definition: file.c:1110
int mutt_file_rename(const char *oldfile, const char *newfile)
Rename a file.
Definition: file.c:1412
#define O_NOFOLLOW
Definition: file.c:67
bool mutt_file_iter_line(struct MuttFileIter *iter, FILE *fp, ReadLineFlags flags)
Iterate over the lines from an open file pointer.
Definition: file.c:844
int mutt_file_symlink(const char *oldpath, const char *newpath)
Create a symlink.
Definition: file.c:290
int mutt_file_chmod_add_stat(const char *path, mode_t mode, struct stat *st)
Add permissions to a file.
Definition: file.c:1158
static int put_file_in_place(const char *path, const char *safe_file, const char *safe_dir)
Move a file into place.
Definition: file.c:136
void mutt_file_expand_fmt(struct Buffer *dest, const char *fmt, const char *src)
Replace s in a string with a filename.
Definition: file.c:1512
void mutt_file_resolve_symlink(struct Buffer *buf)
Resolve a symlink in place.
Definition: file.c:1700
static const char RxSpecialChars[]
These characters must be escaped in regular expressions.
Definition: file.c:58
void mutt_file_set_mtime(const char *from, const char *to)
Set the modification time of one file from another.
Definition: file.c:1071
static int mkwrapdir(const char *path, struct Buffer *newfile, struct Buffer *newdir)
Create a temporary directory next to a file name.
Definition: file.c:93
int mutt_file_stat_timespec_compare(struct stat *st, enum MuttStatType type, struct timespec *b)
Compare stat info with a time value.
Definition: file.c:1660
const char * mutt_file_rotate(const char *path, int count)
Rotate a set of numbered files.
Definition: file.c:521
int mutt_file_chmod_add(const char *path, mode_t mode)
Add permissions to a file.
Definition: file.c:1135
void mutt_file_unlink(const char *s)
Delete a file, carefully.
Definition: file.c:196
int mutt_file_fsync_close(FILE **fp)
Flush the data, before closing a file (and NULL the pointer)
Definition: file.c:168
int mutt_file_rmtree(const char *path)
Recursively remove a directory.
Definition: file.c:461
size_t mutt_file_save_str(FILE *fp, const char *str)
Save a string to a file.
Definition: file.c:1720
int mutt_file_chmod_rm_stat(const char *path, mode_t mode, struct stat *st)
Remove permissions from a file.
Definition: file.c:1214
File management functions.
MuttOpenDirMode
Mode flag for mutt_file_opendir()
Definition: file.h:72
@ MUTT_OPENDIR_CREATE
Create the directory if it doesn't exist.
Definition: file.h:74
@ MUTT_OPENDIR_NONE
Plain opendir()
Definition: file.h:73
#define MUTT_RL_CONT
-continuation
Definition: file.h:40
bool(* mutt_file_map_t)(char *line, int line_num, void *user_data)
Definition: file.h:98
#define MUTT_RL_EOL
don't strip \n / \r\n
Definition: file.h:41
MuttStatType
Flags for mutt_file_get_stat_timespec.
Definition: file.h:62
@ MUTT_STAT_CTIME
File/dir's ctime - creation time.
Definition: file.h:65
@ MUTT_STAT_ATIME
File/dir's atime - last accessed time.
Definition: file.h:63
@ MUTT_STAT_MTIME
File/dir's mtime - last modified time.
Definition: file.h:64
uint8_t ReadLineFlags
Flags for mutt_file_read_line(), e.g. MUTT_RL_CONT.
Definition: file.h:38
#define mutt_error(...)
Definition: logging2.h:92
#define mutt_message(...)
Definition: logging2.h:91
#define mutt_debug(LEVEL,...)
Definition: logging2.h:89
#define mutt_perror(...)
Definition: logging2.h:93
Logging Dispatcher.
@ LL_DEBUG1
Log at debug level 1.
Definition: logging2.h:43
void * mutt_mem_malloc(size_t size)
Allocate memory on the heap.
Definition: memory.c:90
void mutt_mem_realloc(void *ptr, size_t size)
Resize a block of memory on the heap.
Definition: memory.c:114
Memory management wrappers.
#define FREE(x)
Definition: memory.h:45
Conversion between different character encodings.
#define ICONV_BUF_TOO_SMALL
Error value for iconv() - Buffer too small.
Definition: charset.h:105
#define ICONV_ILLEGAL_SEQ
Error value for iconv() - Illegal sequence.
Definition: charset.h:103
time_t mutt_date_now(void)
Return the number of seconds since the Unix epoch.
Definition: date.c:446
Message logging.
#define _(a)
Definition: message.h:28
const char * mutt_path_getcwd(struct Buffer *cwd)
Get the current working directory.
Definition: path.c:469
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:763
size_t mutt_str_len(const char *a)
Calculate the length of a string, safely.
Definition: string.c:568
size_t mutt_str_copy(char *dest, const char *src, size_t dsize)
Copy a string into a buffer (guaranteeing NUL-termination)
Definition: string.c:653
#define PATH_MAX
Definition: mutt.h:41
Path manipulation functions.
struct Buffer * buf_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:81
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
Definition: pool.c:94
A global pool of Buffers.
String manipulation functions.
#define NONULL(x)
Definition: string2.h:37
#define SKIPWS(ch)
Definition: string2.h:45
String manipulation buffer.
Definition: buffer.h:34
char * data
Pointer to data.
Definition: buffer.h:35
State record for mutt_file_iter_line()
Definition: file.h:81
char * line
the line data
Definition: file.h:82
int line_num
line number
Definition: file.h:84
size_t size
allocated size of line data
Definition: file.h:83
Time value with nanosecond precision.
Definition: file.h:50
long tv_nsec
Number of nanosecond, on top.
Definition: file.h:52
time_t tv_sec
Number of seconds since the epoch.
Definition: file.h:51