bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 Sami Kerola <kerolasa@iki.fi> |
| 3 | */ |
| 4 | |
| 5 | #include <stdio.h> |
| 6 | #include <stdlib.h> |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 7 | #include <paths.h> |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 8 | #include <sys/stat.h> |
| 9 | #include <unistd.h> |
| 10 | #include <sys/time.h> |
| 11 | #include <sys/resource.h> |
| 12 | |
| 13 | #include "c.h" |
| 14 | #include "fileutils.h" |
| 15 | #include "pathnames.h" |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 16 | |
Ethan Yonker | c798c9c | 2015-10-09 11:15:26 -0500 | [diff] [blame] | 17 | #ifndef _PATH_TMP |
| 18 | #define _PATH_TMP "/tmp/" |
| 19 | #endif |
| 20 | |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 21 | /* Create open temporary file in safe way. Please notice that the |
| 22 | * file permissions are -rw------- by default. */ |
| 23 | int xmkstemp(char **tmpname, char *dir) |
| 24 | { |
| 25 | char *localtmp; |
| 26 | char *tmpenv; |
| 27 | mode_t old_mode; |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 28 | int fd, rc; |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 29 | |
| 30 | /* Some use cases must be capable of being moved atomically |
| 31 | * with rename(2), which is the reason why dir is here. */ |
| 32 | if (dir != NULL) |
| 33 | tmpenv = dir; |
| 34 | else |
| 35 | tmpenv = getenv("TMPDIR"); |
| 36 | |
| 37 | if (tmpenv) |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 38 | rc = asprintf(&localtmp, "%s/%s.XXXXXX", tmpenv, |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 39 | program_invocation_short_name); |
| 40 | else |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 41 | rc = asprintf(&localtmp, "%s/%s.XXXXXX", _PATH_TMP, |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 42 | program_invocation_short_name); |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 43 | |
| 44 | if (rc < 0) |
| 45 | return -1; |
| 46 | |
bigbiff bigbiff | e60683a | 2013-02-22 20:55:50 -0500 | [diff] [blame] | 47 | old_mode = umask(077); |
| 48 | fd = mkstemp(localtmp); |
| 49 | umask(old_mode); |
| 50 | if (fd == -1) { |
| 51 | free(localtmp); |
| 52 | localtmp = NULL; |
| 53 | } |
| 54 | *tmpname = localtmp; |
| 55 | return fd; |
| 56 | } |
| 57 | |
| 58 | /* |
| 59 | * portable getdtablesize() |
| 60 | */ |
| 61 | int get_fd_tabsize(void) |
| 62 | { |
| 63 | int m; |
| 64 | |
| 65 | #if defined(HAVE_GETDTABLESIZE) |
| 66 | m = getdtablesize(); |
| 67 | #elif defined(HAVE_GETRLIMIT) && defined(RLIMIT_NOFILE) |
| 68 | struct rlimit rl; |
| 69 | |
| 70 | getrlimit(RLIMIT_NOFILE, &rl); |
| 71 | m = rl.rlim_cur; |
| 72 | #elif defined(HAVE_SYSCONF) && defined(_SC_OPEN_MAX) |
| 73 | m = sysconf(_SC_OPEN_MAX); |
| 74 | #else |
| 75 | m = OPEN_MAX; |
| 76 | #endif |
| 77 | return m; |
| 78 | } |
| 79 | |
| 80 | #ifdef TEST_PROGRAM |
| 81 | int main(void) |
| 82 | { |
| 83 | FILE *f; |
| 84 | char *tmpname; |
| 85 | f = xfmkstemp(&tmpname, NULL); |
| 86 | unlink(tmpname); |
| 87 | free(tmpname); |
| 88 | fclose(f); |
| 89 | return EXIT_FAILURE; |
| 90 | } |
| 91 | #endif |
bigbiff | 7b4c7a6 | 2015-01-01 19:44:14 -0500 | [diff] [blame] | 92 | |
| 93 | |
| 94 | int mkdir_p(const char *path, mode_t mode) |
| 95 | { |
| 96 | char *p, *dir; |
| 97 | int rc = 0; |
| 98 | |
| 99 | if (!path || !*path) |
| 100 | return -EINVAL; |
| 101 | |
| 102 | dir = p = strdup(path); |
| 103 | if (!dir) |
| 104 | return -ENOMEM; |
| 105 | |
| 106 | if (*p == '/') |
| 107 | p++; |
| 108 | |
| 109 | while (p && *p) { |
| 110 | char *e = strchr(p, '/'); |
| 111 | if (e) |
| 112 | *e = '\0'; |
| 113 | if (*p) { |
| 114 | rc = mkdir(dir, mode); |
| 115 | if (rc && errno != EEXIST) |
| 116 | break; |
| 117 | rc = 0; |
| 118 | } |
| 119 | if (!e) |
| 120 | break; |
| 121 | *e = '/'; |
| 122 | p = e + 1; |
| 123 | } |
| 124 | |
| 125 | free(dir); |
| 126 | return rc; |
| 127 | } |
| 128 | |
| 129 | /* returns basename and keeps dirname in the @path, if @path is "/" (root) |
| 130 | * then returns empty string */ |
| 131 | char *stripoff_last_component(char *path) |
| 132 | { |
| 133 | char *p = path ? strrchr(path, '/') : NULL; |
| 134 | |
| 135 | if (!p) |
| 136 | return NULL; |
| 137 | *p = '\0'; |
| 138 | return p + 1; |
| 139 | } |