2 5d56da81 2019-01-13 stsp * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
3 e6eac3b8 2018-06-17 stsp * Copyright (c) 2015 Theo de Raadt <deraadt@openbsd.org>
4 0ee7065d 2019-05-13 stsp * Copyright (c) 1997 Todd C. Miller <millert@openbsd.org>
6 7b19e0f1 2017-11-05 stsp * Permission to use, copy, modify, and distribute this software for any
7 7b19e0f1 2017-11-05 stsp * purpose with or without fee is hereby granted, provided that the above
8 7b19e0f1 2017-11-05 stsp * copyright notice and this permission notice appear in all copies.
10 7b19e0f1 2017-11-05 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 7b19e0f1 2017-11-05 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 7b19e0f1 2017-11-05 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 7b19e0f1 2017-11-05 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 7b19e0f1 2017-11-05 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 7b19e0f1 2017-11-05 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 7b19e0f1 2017-11-05 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 0cd1c46a 2019-03-11 stsp #include <sys/stat.h>
21 0cd1c46a 2019-03-11 stsp #include <errno.h>
22 2c7829a4 2019-06-17 stsp #include <fcntl.h>
23 4027f31a 2017-11-04 stsp #include <limits.h>
24 0cd1c46a 2019-03-11 stsp #include <libgen.h>
25 4027f31a 2017-11-04 stsp #include <stdlib.h>
26 4027f31a 2017-11-04 stsp #include <unistd.h>
27 4027f31a 2017-11-04 stsp #include <stdio.h>
28 4027f31a 2017-11-04 stsp #include <string.h>
29 3c45a30a 2019-05-12 jcs #include <dirent.h>
30 0ee7065d 2019-05-13 stsp #include <paths.h>
32 dd038bc6 2021-09-21 thomas.ad #include "got_compat.h"
34 9d31a1d8 2018-03-11 stsp #include "got_error.h"
35 324d37e7 2019-05-11 stsp #include "got_path.h"
38 e0159033 2019-01-08 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
42 4027f31a 2017-11-04 stsp got_path_is_absolute(const char *path)
44 4027f31a 2017-11-04 stsp return path[0] == '/';
47 f7d20e89 2018-06-17 stsp /* based on canonpath() from kern_pledge.c */
48 f7d20e89 2018-06-17 stsp const struct got_error *
49 e6eac3b8 2018-06-17 stsp got_canonpath(const char *input, char *buf, size_t bufsize)
51 e6eac3b8 2018-06-17 stsp const char *p;
54 e6eac3b8 2018-06-17 stsp /* can't canon relative paths, don't bother */
55 e6eac3b8 2018-06-17 stsp if (!got_path_is_absolute(input)) {
56 e6eac3b8 2018-06-17 stsp if (strlcpy(buf, input, bufsize) >= bufsize)
57 f7d20e89 2018-06-17 stsp return got_error(GOT_ERR_NO_SPACE);
58 f7d20e89 2018-06-17 stsp return NULL;
63 e6eac3b8 2018-06-17 stsp while (*p && (q - buf < bufsize)) {
64 e6eac3b8 2018-06-17 stsp if (p[0] == '/' && (p[1] == '/' || p[1] == '\0')) {
67 e6eac3b8 2018-06-17 stsp } else if (p[0] == '/' && p[1] == '.' &&
68 e6eac3b8 2018-06-17 stsp (p[2] == '/' || p[2] == '\0')) {
71 e6eac3b8 2018-06-17 stsp } else if (p[0] == '/' && p[1] == '.' && p[2] == '.' &&
72 e6eac3b8 2018-06-17 stsp (p[3] == '/' || p[3] == '\0')) {
74 e6eac3b8 2018-06-17 stsp if (q != buf) /* "/../" at start of buf */
75 e6eac3b8 2018-06-17 stsp while (*--q != '/')
79 e6eac3b8 2018-06-17 stsp *q++ = *p++;
82 e6eac3b8 2018-06-17 stsp if ((*p == '\0') && (q - buf < bufsize)) {
84 f7d20e89 2018-06-17 stsp return NULL;
86 f7d20e89 2018-06-17 stsp return got_error(GOT_ERR_NO_SPACE);
89 04ca23f4 2018-07-16 stsp const struct got_error *
90 04ca23f4 2018-07-16 stsp got_path_skip_common_ancestor(char **child, const char *parent_abspath,
91 04ca23f4 2018-07-16 stsp const char *abspath)
93 04ca23f4 2018-07-16 stsp const struct got_error *err = NULL;
94 04ca23f4 2018-07-16 stsp size_t len_parent, len, bufsize;
96 db5ba8ed 2019-03-26 stsp *child = NULL;
98 04ca23f4 2018-07-16 stsp len_parent = strlen(parent_abspath);
99 04ca23f4 2018-07-16 stsp len = strlen(abspath);
100 04ca23f4 2018-07-16 stsp if (len_parent >= len)
101 63f810e6 2020-02-29 stsp return got_error_path(abspath, GOT_ERR_BAD_PATH);
102 04ca23f4 2018-07-16 stsp if (strncmp(parent_abspath, abspath, len_parent) != 0)
103 63f810e6 2020-02-29 stsp return got_error_path(abspath, GOT_ERR_BAD_PATH);
104 eb4304b9 2019-05-09 stsp if (!got_path_is_root_dir(parent_abspath) && abspath[len_parent] != '/')
105 63f810e6 2020-02-29 stsp return got_error_path(abspath, GOT_ERR_BAD_PATH);
106 5e3ce57a 2019-03-26 stsp while (abspath[len_parent] == '/')
108 04ca23f4 2018-07-16 stsp bufsize = len - len_parent + 1;
109 04ca23f4 2018-07-16 stsp *child = malloc(bufsize);
110 04ca23f4 2018-07-16 stsp if (*child == NULL)
111 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
112 04ca23f4 2018-07-16 stsp if (strlcpy(*child, abspath + len_parent, bufsize) >= bufsize) {
113 638f9024 2019-05-13 stsp err = got_error_from_errno("strlcpy");
114 04ca23f4 2018-07-16 stsp free(*child);
115 04ca23f4 2018-07-16 stsp *child = NULL;
116 04ca23f4 2018-07-16 stsp return err;
118 04ca23f4 2018-07-16 stsp return NULL;
122 31cedeaf 2018-09-15 stsp got_path_is_root_dir(const char *path)
124 b8b3f209 2020-02-29 stsp while (*path == '/')
126 b8b3f209 2020-02-29 stsp return (*path == '\0');
130 a129376b 2019-03-28 stsp got_path_is_current_dir(const char *path)
132 a129376b 2019-03-28 stsp return (path[0] == '.' && path[1] == '\0');
136 8da9e5f4 2019-01-12 stsp got_path_is_child(const char *child, const char *parent, size_t parent_len)
138 eb4304b9 2019-05-09 stsp if (parent_len == 0 || got_path_is_root_dir(parent))
141 8da9e5f4 2019-01-12 stsp if (strncmp(parent, child, parent_len) != 0)
143 8da9e5f4 2019-01-12 stsp if (child[parent_len] != '/')
150 d572f586 2019-08-02 stsp got_path_cmp(const char *path1, const char *path2, size_t len1, size_t len2)
152 466d3b32 2019-08-02 stsp size_t min_len;
153 e0159033 2019-01-08 stsp size_t i = 0;
155 e08cc72d 2019-02-05 stsp /* Leading directory separators are insignificant. */
156 466d3b32 2019-08-02 stsp while (path1[0] == '/') {
160 466d3b32 2019-08-02 stsp while (path2[0] == '/') {
165 e08cc72d 2019-02-05 stsp min_len = MIN(len1, len2);
167 e0159033 2019-01-08 stsp /* Skip over common prefix. */
168 e0159033 2019-01-08 stsp while (i < min_len && path1[i] == path2[i])
171 e08cc72d 2019-02-05 stsp /* Are the paths exactly equal (besides path separators)? */
172 e0159033 2019-01-08 stsp if (len1 == len2 && i >= min_len)
175 e08cc72d 2019-02-05 stsp /* Skip over redundant trailing path seperators. */
176 e08cc72d 2019-02-05 stsp while (path1[i] == '/' && path1[i + 1] == '/')
178 e08cc72d 2019-02-05 stsp while (path2[i] == '/' && path2[i + 1] == '/')
181 e08cc72d 2019-02-05 stsp /* Trailing path separators are insignificant. */
182 e08cc72d 2019-02-05 stsp if (path1[i] == '/' && path1[i + 1] == '\0' && path2[i] == '\0')
184 e08cc72d 2019-02-05 stsp if (path2[i] == '/' && path2[i + 1] == '\0' && path1[i] == '\0')
187 e0159033 2019-01-08 stsp /* Order children in subdirectories directly after their parents. */
188 e0159033 2019-01-08 stsp if (path1[i] == '/' && path2[i] == '\0')
190 e0159033 2019-01-08 stsp if (path2[i] == '/' && path1[i] == '\0')
192 e08cc72d 2019-02-05 stsp if (path1[i] == '/' && path2[i] != '\0')
194 e08cc72d 2019-02-05 stsp if (path2[i] == '/' && path1[i] != '\0')
197 e0159033 2019-01-08 stsp /* Next character following the common prefix determines order. */
198 e0159033 2019-01-08 stsp return (unsigned char)path1[i] < (unsigned char)path2[i] ? -1 : 1;
201 e08cc72d 2019-02-05 stsp const struct got_error *
202 7e5c804b 2019-02-05 stsp got_pathlist_insert(struct got_pathlist_entry **inserted,
203 3d8df59c 2019-02-05 stsp struct got_pathlist_head *pathlist, const char *path, void *data)
205 e08cc72d 2019-02-05 stsp struct got_pathlist_entry *new, *pe;
207 7e5c804b 2019-02-05 stsp if (inserted)
208 7e5c804b 2019-02-05 stsp *inserted = NULL;
210 e08cc72d 2019-02-05 stsp new = malloc(sizeof(*new));
211 e08cc72d 2019-02-05 stsp if (new == NULL)
212 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
213 e08cc72d 2019-02-05 stsp new->path = path;
214 f2b16ada 2019-08-02 stsp new->path_len = strlen(path);
215 3d8df59c 2019-02-05 stsp new->data = data;
218 e08cc72d 2019-02-05 stsp * Many callers will provide paths in a somewhat sorted order while
219 e08cc72d 2019-02-05 stsp * constructing a path list from inputs such as tree objects or
220 e08cc72d 2019-02-05 stsp * dirents. Iterating backwards from the tail of the list should
221 e08cc72d 2019-02-05 stsp * be more efficient than traversing through the entire list each
222 e08cc72d 2019-02-05 stsp * time an element is inserted.
224 e08cc72d 2019-02-05 stsp pe = TAILQ_LAST(pathlist, got_pathlist_head);
225 e08cc72d 2019-02-05 stsp while (pe) {
226 f2b16ada 2019-08-02 stsp int cmp = got_path_cmp(pe->path, new->path,
227 f2b16ada 2019-08-02 stsp pe->path_len, new->path_len);
228 e08cc72d 2019-02-05 stsp if (cmp == 0) {
229 e08cc72d 2019-02-05 stsp free(new); /* duplicate */
230 e08cc72d 2019-02-05 stsp return NULL;
231 e08cc72d 2019-02-05 stsp } else if (cmp < 0) {
232 e08cc72d 2019-02-05 stsp TAILQ_INSERT_AFTER(pathlist, pe, new, entry);
233 7e5c804b 2019-02-05 stsp if (inserted)
234 7e5c804b 2019-02-05 stsp *inserted = new;
235 e08cc72d 2019-02-05 stsp return NULL;
237 e08cc72d 2019-02-05 stsp pe = TAILQ_PREV(pe, got_pathlist_head, entry);
240 e08cc72d 2019-02-05 stsp TAILQ_INSERT_HEAD(pathlist, new, entry);
241 7e5c804b 2019-02-05 stsp if (inserted)
242 7e5c804b 2019-02-05 stsp *inserted = new;
243 e08cc72d 2019-02-05 stsp return NULL;
246 72ea6654 2019-07-27 stsp const struct got_error *
247 adc19d55 2019-07-28 stsp got_pathlist_append(struct got_pathlist_head *pathlist,
248 adc19d55 2019-07-28 stsp const char *path, void *data)
250 72ea6654 2019-07-27 stsp struct got_pathlist_entry *new;
252 72ea6654 2019-07-27 stsp new = malloc(sizeof(*new));
253 72ea6654 2019-07-27 stsp if (new == NULL)
254 72ea6654 2019-07-27 stsp return got_error_from_errno("malloc");
255 72ea6654 2019-07-27 stsp new->path = path;
256 f2b16ada 2019-08-02 stsp new->path_len = strlen(path);
257 72ea6654 2019-07-27 stsp new->data = data;
258 72ea6654 2019-07-27 stsp TAILQ_INSERT_TAIL(pathlist, new, entry);
259 72ea6654 2019-07-27 stsp return NULL;
263 e08cc72d 2019-02-05 stsp got_pathlist_free(struct got_pathlist_head *pathlist)
265 e08cc72d 2019-02-05 stsp struct got_pathlist_entry *pe;
267 e08cc72d 2019-02-05 stsp while ((pe = TAILQ_FIRST(pathlist)) != NULL) {
268 e08cc72d 2019-02-05 stsp TAILQ_REMOVE(pathlist, pe, entry);
273 0cd1c46a 2019-03-11 stsp static const struct got_error *
274 0cd1c46a 2019-03-11 stsp make_parent_dirs(const char *abspath)
276 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
277 d1667f0d 2019-03-11 stsp char *parent;
279 d1667f0d 2019-03-11 stsp err = got_path_dirname(&parent, abspath);
281 d1667f0d 2019-03-11 stsp return err;
283 0cd1c46a 2019-03-11 stsp if (mkdir(parent, GOT_DEFAULT_DIR_MODE) == -1) {
284 0cd1c46a 2019-03-11 stsp if (errno == ENOENT) {
285 0cd1c46a 2019-03-11 stsp err = make_parent_dirs(parent);
288 5e1c9f23 2019-03-11 stsp if (mkdir(parent, GOT_DEFAULT_DIR_MODE) == -1) {
289 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", parent);
293 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", parent);
296 5e1c9f23 2019-03-11 stsp free(parent);
297 0cd1c46a 2019-03-11 stsp return err;
300 0cd1c46a 2019-03-11 stsp const struct got_error *
301 0cd1c46a 2019-03-11 stsp got_path_mkdir(const char *abspath)
303 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
305 0cd1c46a 2019-03-11 stsp if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1) {
306 ddcd8544 2019-03-11 stsp if (errno == ENOENT) {
307 0cd1c46a 2019-03-11 stsp err = make_parent_dirs(abspath);
310 0cd1c46a 2019-03-11 stsp if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1)
311 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", abspath);
313 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", abspath);
317 0cd1c46a 2019-03-11 stsp return err;
321 280f921b 2019-05-12 stsp got_path_dir_is_empty(const char *dir)
324 3c45a30a 2019-05-12 jcs struct dirent *dent;
325 3c45a30a 2019-05-12 jcs int empty = 1;
327 3c45a30a 2019-05-12 jcs d = opendir(dir);
328 3c45a30a 2019-05-12 jcs if (d == NULL)
331 3c45a30a 2019-05-12 jcs while ((dent = readdir(d)) != NULL) {
332 3c45a30a 2019-05-12 jcs if (strcmp(dent->d_name, ".") == 0 ||
333 3c45a30a 2019-05-12 jcs strcmp(dent->d_name, "..") == 0)
340 7f2a8dc2 2019-05-12 stsp closedir(d);
341 3c45a30a 2019-05-12 jcs return empty;
344 d1667f0d 2019-03-11 stsp const struct got_error *
345 d1667f0d 2019-03-11 stsp got_path_dirname(char **parent, const char *path)
347 0c4004e3 2020-10-20 stsp char buf[PATH_MAX];
350 0c4004e3 2020-10-20 stsp if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
351 0c4004e3 2020-10-20 stsp return got_error(GOT_ERR_NO_SPACE);
353 0c4004e3 2020-10-20 stsp p = dirname(buf);
354 d1667f0d 2019-03-11 stsp if (p == NULL)
355 638f9024 2019-05-13 stsp return got_error_from_errno2("dirname", path);
357 d1667f0d 2019-03-11 stsp if (p[0] == '.' && p[1] == '\0')
358 63f810e6 2020-02-29 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
360 d1667f0d 2019-03-11 stsp *parent = strdup(p);
361 d1667f0d 2019-03-11 stsp if (*parent == NULL)
362 f2ea84fa 2019-07-27 stsp return got_error_from_errno("strdup");
364 f2ea84fa 2019-07-27 stsp return NULL;
367 f2ea84fa 2019-07-27 stsp const struct got_error *
368 20ccae39 2020-07-21 stsp got_path_dirent_type(int *type, const char *path_parent, struct dirent *dent)
370 20ccae39 2020-07-21 stsp const struct got_error *err = NULL;
371 20ccae39 2020-07-21 stsp char *path_child;
372 20ccae39 2020-07-21 stsp struct stat sb;
374 20ccae39 2020-07-21 stsp if (dent->d_type != DT_UNKNOWN) {
375 20ccae39 2020-07-21 stsp *type = dent->d_type;
376 20ccae39 2020-07-21 stsp return NULL;
379 20ccae39 2020-07-21 stsp *type = DT_UNKNOWN;
382 20ccae39 2020-07-21 stsp * This is a fallback to accommodate filesystems which do not
383 20ccae39 2020-07-21 stsp * provide directory entry type information. DT_UNKNOWN directory
384 20ccae39 2020-07-21 stsp * entries occur on NFS mounts without "readdir plus" RPC.
387 20ccae39 2020-07-21 stsp if (asprintf(&path_child, "%s/%s", path_parent, dent->d_name) == -1)
388 20ccae39 2020-07-21 stsp return got_error_from_errno("asprintf");
390 20ccae39 2020-07-21 stsp if (lstat(path_child, &sb) == -1) {
391 20ccae39 2020-07-21 stsp err = got_error_from_errno2("lstat", path_child);
395 20ccae39 2020-07-21 stsp if (S_ISFIFO(sb.st_mode))
396 20ccae39 2020-07-21 stsp *type = DT_FIFO;
397 20ccae39 2020-07-21 stsp else if (S_ISCHR(sb.st_mode))
398 20ccae39 2020-07-21 stsp *type = DT_CHR;
399 20ccae39 2020-07-21 stsp else if (S_ISDIR(sb.st_mode))
400 20ccae39 2020-07-21 stsp *type = DT_DIR;
401 20ccae39 2020-07-21 stsp else if (S_ISBLK(sb.st_mode))
402 20ccae39 2020-07-21 stsp *type = DT_BLK;
403 20ccae39 2020-07-21 stsp else if (S_ISLNK(sb.st_mode))
404 20ccae39 2020-07-21 stsp *type = DT_LNK;
405 20ccae39 2020-07-21 stsp else if (S_ISREG(sb.st_mode))
406 20ccae39 2020-07-21 stsp *type = DT_REG;
407 20ccae39 2020-07-21 stsp else if (S_ISSOCK(sb.st_mode))
408 20ccae39 2020-07-21 stsp *type = DT_SOCK;
410 20ccae39 2020-07-21 stsp free(path_child);
411 20ccae39 2020-07-21 stsp return err;
414 20ccae39 2020-07-21 stsp const struct got_error *
415 f2ea84fa 2019-07-27 stsp got_path_basename(char **s, const char *path)
417 0a9483d0 2020-10-19 stsp char buf[PATH_MAX];
418 f2ea84fa 2019-07-27 stsp char *base;
420 0a9483d0 2020-10-19 stsp if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
421 0a9483d0 2020-10-19 stsp return got_error(GOT_ERR_NO_SPACE);
423 0a9483d0 2020-10-19 stsp base = basename(buf);
424 f2ea84fa 2019-07-27 stsp if (base == NULL)
425 f2ea84fa 2019-07-27 stsp return got_error_from_errno2("basename", path);
427 f2ea84fa 2019-07-27 stsp *s = strdup(base);
428 f2ea84fa 2019-07-27 stsp if (*s == NULL)
429 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
431 d1667f0d 2019-03-11 stsp return NULL;
435 72151b04 2019-05-11 stsp got_path_strip_trailing_slashes(char *path)
439 455de7fa 2020-01-12 tb x = strlen(path);
440 455de7fa 2020-01-12 tb while (x-- > 0 && path[x] == '/')
441 72151b04 2019-05-11 stsp path[x] = '\0';
444 08680430 2019-05-13 stsp /* based on findprog() from usr.sbin/which/which.c */
445 0ee7065d 2019-05-13 stsp const struct got_error *
446 0ee7065d 2019-05-13 stsp got_path_find_prog(char **filename, const char *prog)
448 202329ae 2019-08-11 stsp const struct got_error *err = NULL;
451 0ee7065d 2019-05-13 stsp struct stat sbuf;
452 0ee7065d 2019-05-13 stsp char *path, *pathcpy;
454 0ee7065d 2019-05-13 stsp *filename = NULL;
456 0ee7065d 2019-05-13 stsp path = getenv("PATH");
457 0ee7065d 2019-05-13 stsp if (path == NULL)
458 0ee7065d 2019-05-13 stsp path = _PATH_DEFPATH;
460 0ee7065d 2019-05-13 stsp /* Special case if prog contains '/' */
461 0ee7065d 2019-05-13 stsp if (strchr(prog, '/')) {
462 0ee7065d 2019-05-13 stsp if ((stat(prog, &sbuf) == 0) && S_ISREG(sbuf.st_mode) &&
463 0ee7065d 2019-05-13 stsp access(prog, X_OK) == 0) {
464 0ee7065d 2019-05-13 stsp *filename = strdup(prog);
465 0ee7065d 2019-05-13 stsp if (*filename == NULL)
466 0ee7065d 2019-05-13 stsp return got_error_from_errno("strdup");
468 0ee7065d 2019-05-13 stsp return NULL;
471 0ee7065d 2019-05-13 stsp if ((path = strdup(path)) == NULL)
472 0ee7065d 2019-05-13 stsp return got_error_from_errno("strdup");
473 0ee7065d 2019-05-13 stsp pathcpy = path;
475 0ee7065d 2019-05-13 stsp while ((p = strsep(&pathcpy, ":")) != NULL) {
476 0ee7065d 2019-05-13 stsp if (*p == '\0')
479 0ee7065d 2019-05-13 stsp len = strlen(p);
480 0ee7065d 2019-05-13 stsp while (len > 0 && p[len-1] == '/')
481 0ee7065d 2019-05-13 stsp p[--len] = '\0'; /* strip trailing '/' */
483 0ee7065d 2019-05-13 stsp if (asprintf(filename, "%s/%s", p, prog) == -1) {
484 202329ae 2019-08-11 stsp err = got_error_from_errno("asprintf");
487 0ee7065d 2019-05-13 stsp if ((stat(*filename, &sbuf) == 0) && S_ISREG(sbuf.st_mode) &&
488 718ef3e9 2019-08-11 stsp access(*filename, X_OK) == 0)
490 0ee7065d 2019-05-13 stsp free(*filename);
491 0ee7065d 2019-05-13 stsp *filename = NULL;
494 0ee7065d 2019-05-13 stsp free(path);
495 5cade901 2019-09-22 stsp return err;
498 2c7829a4 2019-06-17 stsp const struct got_error *
499 2c7829a4 2019-06-17 stsp got_path_create_file(const char *path, const char *content)
501 2c7829a4 2019-06-17 stsp const struct got_error *err = NULL;
502 2c7829a4 2019-06-17 stsp int fd = -1;
504 2c7829a4 2019-06-17 stsp fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
505 2c7829a4 2019-06-17 stsp GOT_DEFAULT_FILE_MODE);
506 2c7829a4 2019-06-17 stsp if (fd == -1) {
507 2c7829a4 2019-06-17 stsp err = got_error_from_errno2("open", path);
511 2c7829a4 2019-06-17 stsp if (content) {
512 2c7829a4 2019-06-17 stsp int len = dprintf(fd, "%s\n", content);
513 2c7829a4 2019-06-17 stsp if (len != strlen(content) + 1) {
514 2c7829a4 2019-06-17 stsp err = got_error_from_errno("dprintf");
520 2c7829a4 2019-06-17 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
521 2c7829a4 2019-06-17 stsp err = got_error_from_errno("close");
522 2c7829a4 2019-06-17 stsp return err;