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