Blame


1 718b3ab0 2018-03-17 stsp /*
2 5d56da81 2019-01-13 stsp * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
3 718b3ab0 2018-03-17 stsp *
4 718b3ab0 2018-03-17 stsp * Permission to use, copy, modify, and distribute this software for any
5 718b3ab0 2018-03-17 stsp * purpose with or without fee is hereby granted, provided that the above
6 718b3ab0 2018-03-17 stsp * copyright notice and this permission notice appear in all copies.
7 718b3ab0 2018-03-17 stsp *
8 718b3ab0 2018-03-17 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 718b3ab0 2018-03-17 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 718b3ab0 2018-03-17 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 718b3ab0 2018-03-17 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 718b3ab0 2018-03-17 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 718b3ab0 2018-03-17 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 718b3ab0 2018-03-17 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 718b3ab0 2018-03-17 stsp */
16 718b3ab0 2018-03-17 stsp
17 718b3ab0 2018-03-17 stsp /* Utilities for dealing with filesystem paths. */
18 718b3ab0 2018-03-17 stsp
19 718b3ab0 2018-03-17 stsp #define GOT_DEFAULT_FILE_MODE (S_IRUSR|S_IWUSR | S_IRGRP | S_IROTH)
20 718b3ab0 2018-03-17 stsp #define GOT_DEFAULT_DIR_MODE (S_IRWXU | S_IRGRP|S_IXGRP | S_IROTH|S_IXOTH)
21 718b3ab0 2018-03-17 stsp
22 718b3ab0 2018-03-17 stsp /* Determine whether a path is an absolute path. */
23 718b3ab0 2018-03-17 stsp int got_path_is_absolute(const char *);
24 718b3ab0 2018-03-17 stsp
25 718b3ab0 2018-03-17 stsp /*
26 718b3ab0 2018-03-17 stsp * Return an absolute version of a relative path.
27 718b3ab0 2018-03-17 stsp * The result is allocated with malloc(3).
28 718b3ab0 2018-03-17 stsp */
29 718b3ab0 2018-03-17 stsp char *got_path_get_absolute(const char *);
30 718b3ab0 2018-03-17 stsp
31 718b3ab0 2018-03-17 stsp /*
32 718b3ab0 2018-03-17 stsp * Normalize a path for internal processing.
33 718b3ab0 2018-03-17 stsp * The result is allocated with malloc(3).
34 718b3ab0 2018-03-17 stsp */
35 718b3ab0 2018-03-17 stsp char *got_path_normalize(const char *);
36 e6eac3b8 2018-06-17 stsp
37 e6eac3b8 2018-06-17 stsp /*
38 e6eac3b8 2018-06-17 stsp * Canonicalize absolute paths by removing redundant path separators
39 e6eac3b8 2018-06-17 stsp * and resolving references to parent directories ("/../").
40 e6eac3b8 2018-06-17 stsp * Relative paths are copied from input to buf as-is.
41 e6eac3b8 2018-06-17 stsp */
42 f7d20e89 2018-06-17 stsp const struct got_error *got_canonpath(const char *, char *, size_t);
43 04ca23f4 2018-07-16 stsp
44 04ca23f4 2018-07-16 stsp /*
45 04ca23f4 2018-07-16 stsp * Get child part of two absolute paths. The second path must equal the first
46 04ca23f4 2018-07-16 stsp * path up to some path component, and must be longer than the first path.
47 04ca23f4 2018-07-16 stsp * The result is allocated with malloc(3).
48 04ca23f4 2018-07-16 stsp */
49 04ca23f4 2018-07-16 stsp const struct got_error *got_path_skip_common_ancestor(char **, const char *,
50 04ca23f4 2018-07-16 stsp const char *);
51 31cedeaf 2018-09-15 stsp
52 31cedeaf 2018-09-15 stsp /* Determine whether a path points to the root directory "/" . */
53 31cedeaf 2018-09-15 stsp int got_path_is_root_dir(const char *);
54 e0159033 2019-01-08 stsp
55 8da9e5f4 2019-01-12 stsp /* Determine whether a path is a path-wise child of another path. */
56 8da9e5f4 2019-01-12 stsp int got_path_is_child(const char *, const char *, size_t);
57 8da9e5f4 2019-01-12 stsp
58 e0159033 2019-01-08 stsp /*
59 e0159033 2019-01-08 stsp * Like strcmp() but orders children in subdirectories directly after
60 e0159033 2019-01-08 stsp * their parents.
61 e0159033 2019-01-08 stsp */
62 1beed999 2019-01-12 stsp int got_path_cmp(const char *, const char *);
63 e08cc72d 2019-02-05 stsp
64 e08cc72d 2019-02-05 stsp /*
65 e08cc72d 2019-02-05 stsp * Path lists allow for predictable concurrent iteration over multiple lists
66 e08cc72d 2019-02-05 stsp * of paths obtained from disparate sources which don't all provide the same
67 e08cc72d 2019-02-05 stsp * ordering guarantees (e.g. git trees, file index, and on-disk directories).
68 e08cc72d 2019-02-05 stsp */
69 e08cc72d 2019-02-05 stsp struct got_pathlist_entry {
70 e08cc72d 2019-02-05 stsp TAILQ_ENTRY(got_pathlist_entry) entry;
71 e08cc72d 2019-02-05 stsp const char *path;
72 3d8df59c 2019-02-05 stsp void *data; /* data pointer provided to got_pathlist_insert() */
73 e08cc72d 2019-02-05 stsp };
74 e08cc72d 2019-02-05 stsp TAILQ_HEAD(got_pathlist_head, got_pathlist_entry);
75 e08cc72d 2019-02-05 stsp
76 e08cc72d 2019-02-05 stsp /*
77 e08cc72d 2019-02-05 stsp * Insert a path into the list of paths in a predictable order.
78 e08cc72d 2019-02-05 stsp * The caller should already have initialized the list head. This list stores
79 e08cc72d 2019-02-05 stsp * the pointer to the path as-is, i.e. the path is not copied internally and
80 e08cc72d 2019-02-05 stsp * must remain available until the list is freed with got_pathlist_free().
81 7e5c804b 2019-02-05 stsp * If the first argument is not NULL, set it to a pointer to the newly inserted
82 7e5c804b 2019-02-05 stsp * element, or to a NULL pointer in case the path was already on the list.
83 e08cc72d 2019-02-05 stsp */
84 7e5c804b 2019-02-05 stsp const struct got_error *got_pathlist_insert(struct got_pathlist_entry **,
85 3d8df59c 2019-02-05 stsp struct got_pathlist_head *, const char *, void *);
86 e08cc72d 2019-02-05 stsp
87 e08cc72d 2019-02-05 stsp /* Free resources allocated for a path list. */
88 e08cc72d 2019-02-05 stsp void got_pathlist_free(struct got_pathlist_head *);
89 0cd1c46a 2019-03-11 stsp
90 0cd1c46a 2019-03-11 stsp /* Attempt to create a directory at a given path. */
91 0cd1c46a 2019-03-11 stsp const struct got_error *got_path_mkdir(const char *);
92 d1667f0d 2019-03-11 stsp
93 d1667f0d 2019-03-11 stsp /* dirname(3) with error handling and dynamically allocated result. */
94 d1667f0d 2019-03-11 stsp const struct got_error *got_path_dirname(char **, const char *);