New function mu_tempfile().
Showing
2 changed files
with
57 additions
and
0 deletions
... | @@ -76,6 +76,7 @@ extern struct passwd * getpwnam_virtual __P((const char *u)); | ... | @@ -76,6 +76,7 @@ extern struct passwd * getpwnam_virtual __P((const char *u)); |
76 | extern char * mu_get_user_email __P((char *name)); | 76 | extern char * mu_get_user_email __P((char *name)); |
77 | extern char * mu_normalize_path __P((char *path, const char *delim)); | 77 | extern char * mu_normalize_path __P((char *path, const char *delim)); |
78 | extern char * mu_normalize_maildir __P((const char *dir)); | 78 | extern char * mu_normalize_maildir __P((const char *dir)); |
79 | extern int mu_tempfile __P((const char *tmpdir, char **namep)); | ||
79 | 80 | ||
80 | #ifdef __cplusplus | 81 | #ifdef __cplusplus |
81 | } | 82 | } | ... | ... |
... | @@ -32,6 +32,7 @@ | ... | @@ -32,6 +32,7 @@ |
32 | #include <netdb.h> | 32 | #include <netdb.h> |
33 | #include <errno.h> | 33 | #include <errno.h> |
34 | 34 | ||
35 | #include <mailutils/error.h> | ||
35 | #include <mailutils/mutil.h> | 36 | #include <mailutils/mutil.h> |
36 | #include <mailutils/iterator.h> | 37 | #include <mailutils/iterator.h> |
37 | 38 | ||
... | @@ -529,3 +530,58 @@ mu_normalize_maildir (const char *dir) | ... | @@ -529,3 +530,58 @@ mu_normalize_maildir (const char *dir) |
529 | } | 530 | } |
530 | } | 531 | } |
531 | 532 | ||
533 | /* Create and open a temporary file. Be vary careful about it, since we | ||
534 | may be running with extra privilege i.e setgid(). | ||
535 | Returns file descriptor of the open file. | ||
536 | If namep is not NULL, the pointer to the malloced file name will | ||
537 | be stored there. Otherwise, the file is unlinked right after open, | ||
538 | i.e. it will disappear after close(fd). */ | ||
539 | |||
540 | #ifndef P_tmpdir | ||
541 | # define P_tmpdir "/tmp" | ||
542 | #endif | ||
543 | |||
544 | int | ||
545 | mu_tempfile (const char *tmpdir, char **namep) | ||
546 | { | ||
547 | char *filename; | ||
548 | int fd; | ||
549 | |||
550 | if (!tmpdir) | ||
551 | tmpdir = (getenv ("TMPDIR")) ? getenv ("TMPDIR") : P_tmpdir; | ||
552 | |||
553 | filename = malloc (strlen (tmpdir) + /*'/'*/1 + /* "muXXXXXX" */8 + 1); | ||
554 | if (!filename) | ||
555 | return -1; | ||
556 | sprintf (filename, "%s/muXXXXXX", tmpdir); | ||
557 | |||
558 | #ifdef HAVE_MKSTEMP | ||
559 | { | ||
560 | int save_mask = umask (077); | ||
561 | fd = mkstemp (filename); | ||
562 | umask (save_mask); | ||
563 | } | ||
564 | #else | ||
565 | if (mktemp (filename)) | ||
566 | fd = open (filename, O_CREAT|O_EXCL|O_RDWR, 0600); | ||
567 | else | ||
568 | fd = -1; | ||
569 | #endif | ||
570 | |||
571 | if (fd == -1) | ||
572 | { | ||
573 | mu_error ("Can not open temporary file: %s", strerror(errno)); | ||
574 | free (filename); | ||
575 | return -1; | ||
576 | } | ||
577 | |||
578 | if (namep) | ||
579 | *namep = filename; | ||
580 | else | ||
581 | { | ||
582 | unlink (filename); | ||
583 | free (filename); | ||
584 | } | ||
585 | |||
586 | return fd; | ||
587 | } | ... | ... |
-
Please register or sign in to post a comment