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