Blame


1 324d37e7 2019-05-11 stsp /*
2 324d37e7 2019-05-11 stsp * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
3 324d37e7 2019-05-11 stsp *
4 324d37e7 2019-05-11 stsp * Permission to use, copy, modify, and distribute this software for any
5 324d37e7 2019-05-11 stsp * purpose with or without fee is hereby granted, provided that the above
6 324d37e7 2019-05-11 stsp * copyright notice and this permission notice appear in all copies.
7 324d37e7 2019-05-11 stsp *
8 324d37e7 2019-05-11 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 324d37e7 2019-05-11 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 324d37e7 2019-05-11 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 324d37e7 2019-05-11 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 324d37e7 2019-05-11 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 324d37e7 2019-05-11 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 324d37e7 2019-05-11 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 324d37e7 2019-05-11 stsp */
16 324d37e7 2019-05-11 stsp
17 324d37e7 2019-05-11 stsp /* Utilities for dealing with filesystem paths. */
18 324d37e7 2019-05-11 stsp
19 839bbaae 2023-02-01 op #define GOT_DEFAULT_PACK_MODE (S_IFREG | \
20 839bbaae 2023-02-01 op S_IRUSR | S_IRGRP | S_IROTH)
21 bb51a5b4 2020-01-13 stsp #define GOT_DEFAULT_FILE_MODE (S_IFREG | \
22 bb51a5b4 2020-01-13 stsp S_IRUSR|S_IWUSR | S_IRGRP | S_IROTH)
23 bb51a5b4 2020-01-13 stsp #define GOT_DEFAULT_DIR_MODE (S_IFDIR | \
24 bb51a5b4 2020-01-13 stsp S_IRWXU | S_IRGRP|S_IXGRP | S_IROTH|S_IXOTH)
25 324d37e7 2019-05-11 stsp
26 20ccae39 2020-07-21 stsp struct dirent;
27 20ccae39 2020-07-21 stsp
28 324d37e7 2019-05-11 stsp /* Determine whether a path is an absolute path. */
29 324d37e7 2019-05-11 stsp int got_path_is_absolute(const char *);
30 324d37e7 2019-05-11 stsp
31 324d37e7 2019-05-11 stsp /*
32 324d37e7 2019-05-11 stsp * Canonicalize absolute paths by removing redundant path separators
33 324d37e7 2019-05-11 stsp * and resolving references to parent directories ("/../").
34 324d37e7 2019-05-11 stsp * Relative paths are copied from input to buf as-is.
35 324d37e7 2019-05-11 stsp */
36 324d37e7 2019-05-11 stsp const struct got_error *got_canonpath(const char *, char *, size_t);
37 324d37e7 2019-05-11 stsp
38 324d37e7 2019-05-11 stsp /*
39 324d37e7 2019-05-11 stsp * Get child part of two absolute paths. The second path must equal the first
40 324d37e7 2019-05-11 stsp * path up to some path component, and must be longer than the first path.
41 324d37e7 2019-05-11 stsp * The result is allocated with malloc(3).
42 324d37e7 2019-05-11 stsp */
43 324d37e7 2019-05-11 stsp const struct got_error *got_path_skip_common_ancestor(char **, const char *,
44 324d37e7 2019-05-11 stsp const char *);
45 324d37e7 2019-05-11 stsp
46 9d6cabd5 2022-04-07 op /*
47 9d6cabd5 2022-04-07 op * Remove leading components from path. It's an error to strip more
48 9d6cabd5 2022-04-07 op * component than present. The result is allocated dynamically.
49 9d6cabd5 2022-04-07 op */
50 9d6cabd5 2022-04-07 op const struct got_error *got_path_strip(char **, const char *, int);
51 9d6cabd5 2022-04-07 op
52 324d37e7 2019-05-11 stsp /* Determine whether a path points to the root directory "/" . */
53 324d37e7 2019-05-11 stsp int got_path_is_root_dir(const char *);
54 324d37e7 2019-05-11 stsp
55 324d37e7 2019-05-11 stsp /* Determine whether a path is a path-wise child of another path. */
56 324d37e7 2019-05-11 stsp int got_path_is_child(const char *, const char *, size_t);
57 324d37e7 2019-05-11 stsp
58 324d37e7 2019-05-11 stsp /*
59 324d37e7 2019-05-11 stsp * Like strcmp() but orders children in subdirectories directly after
60 d572f586 2019-08-02 stsp * their parents. String lengths must also be passed in.
61 324d37e7 2019-05-11 stsp */
62 d572f586 2019-08-02 stsp int got_path_cmp(const char *, const char *, size_t, size_t);
63 324d37e7 2019-05-11 stsp
64 324d37e7 2019-05-11 stsp /*
65 324d37e7 2019-05-11 stsp * Path lists allow for predictable concurrent iteration over multiple lists
66 324d37e7 2019-05-11 stsp * of paths obtained from disparate sources which don't all provide the same
67 324d37e7 2019-05-11 stsp * ordering guarantees (e.g. git trees, file index, and on-disk directories).
68 324d37e7 2019-05-11 stsp */
69 324d37e7 2019-05-11 stsp struct got_pathlist_entry {
70 324d37e7 2019-05-11 stsp TAILQ_ENTRY(got_pathlist_entry) entry;
71 324d37e7 2019-05-11 stsp const char *path;
72 f2b16ada 2019-08-02 stsp size_t path_len;
73 324d37e7 2019-05-11 stsp void *data; /* data pointer provided to got_pathlist_insert() */
74 324d37e7 2019-05-11 stsp };
75 324d37e7 2019-05-11 stsp TAILQ_HEAD(got_pathlist_head, got_pathlist_entry);
76 324d37e7 2019-05-11 stsp
77 324d37e7 2019-05-11 stsp /*
78 324d37e7 2019-05-11 stsp * Insert a path into the list of paths in a predictable order.
79 324d37e7 2019-05-11 stsp * The caller should already have initialized the list head. This list stores
80 324d37e7 2019-05-11 stsp * the pointer to the path as-is, i.e. the path is not copied internally and
81 324d37e7 2019-05-11 stsp * must remain available until the list is freed with got_pathlist_free().
82 324d37e7 2019-05-11 stsp * If the first argument is not NULL, set it to a pointer to the newly inserted
83 324d37e7 2019-05-11 stsp * element, or to a NULL pointer in case the path was already on the list.
84 324d37e7 2019-05-11 stsp */
85 324d37e7 2019-05-11 stsp const struct got_error *got_pathlist_insert(struct got_pathlist_entry **,
86 324d37e7 2019-05-11 stsp struct got_pathlist_head *, const char *, void *);
87 324d37e7 2019-05-11 stsp
88 72ea6654 2019-07-27 stsp /*
89 72ea6654 2019-07-27 stsp * Append a path to the list of paths.
90 72ea6654 2019-07-27 stsp * The caller should already have initialized the list head. This list stores
91 72ea6654 2019-07-27 stsp * the pointer to the path as-is, i.e. the path is not copied internally and
92 72ea6654 2019-07-27 stsp * must remain available until the list is freed with got_pathlist_free().
93 72ea6654 2019-07-27 stsp */
94 adc19d55 2019-07-28 stsp const struct got_error *got_pathlist_append(struct got_pathlist_head *,
95 adc19d55 2019-07-28 stsp const char *, void *);
96 72ea6654 2019-07-27 stsp
97 d8bacb93 2023-01-10 mark /* Flags passed to got_pathlist_free() to control which pointers are freed. */
98 d8bacb93 2023-01-10 mark #define GOT_PATHLIST_FREE_NONE 0 /* pathlist entry only */
99 d8bacb93 2023-01-10 mark #define GOT_PATHLIST_FREE_PATH (1 << 0) /* entry and path pointer */
100 d8bacb93 2023-01-10 mark #define GOT_PATHLIST_FREE_DATA (1 << 1) /* entry and data pointer */
101 d8bacb93 2023-01-10 mark #define GOT_PATHLIST_FREE_ALL (GOT_PATHLIST_FREE_PATH|GOT_PATHLIST_FREE_DATA)
102 d8bacb93 2023-01-10 mark
103 324d37e7 2019-05-11 stsp /* Free resources allocated for a path list. */
104 d8bacb93 2023-01-10 mark void got_pathlist_free(struct got_pathlist_head *, int);
105 324d37e7 2019-05-11 stsp
106 324d37e7 2019-05-11 stsp /* Attempt to create a directory at a given path. */
107 324d37e7 2019-05-11 stsp const struct got_error *got_path_mkdir(const char *);
108 324d37e7 2019-05-11 stsp
109 3c45a30a 2019-05-12 jcs /* Determine whether a directory has no files or directories in it. */
110 280f921b 2019-05-12 stsp int got_path_dir_is_empty(const char *);
111 3c45a30a 2019-05-12 jcs
112 0c4004e3 2020-10-20 stsp /*
113 0c4004e3 2020-10-20 stsp * dirname(3) with error handling, dynamically allocated result, and
114 0c4004e3 2020-10-20 stsp * unmodified input.
115 0c4004e3 2020-10-20 stsp */
116 324d37e7 2019-05-11 stsp const struct got_error *got_path_dirname(char **, const char *);
117 72151b04 2019-05-11 stsp
118 20ccae39 2020-07-21 stsp /*
119 20ccae39 2020-07-21 stsp * Obtain the file type of a given directory entry.
120 20ccae39 2020-07-21 stsp *
121 20ccae39 2020-07-21 stsp * If the entry has some type other than DT_UNKNOWN, resolve to this type.
122 20ccae39 2020-07-21 stsp *
123 20ccae39 2020-07-21 stsp * Otherwise, attempt to resolve the type of a DT_UNKNOWN directory
124 20ccae39 2020-07-21 stsp * entry with lstat(2), though the result may still be DT_UNKNOWN.
125 20ccae39 2020-07-21 stsp * This is a fallback to accommodate filesystems which do not provide
126 20ccae39 2020-07-21 stsp * directory entry type information.
127 20ccae39 2020-07-21 stsp * DT_UNKNOWN directory entries occur on NFS mounts without "readdir plus" RPC.
128 20ccae39 2020-07-21 stsp */
129 20ccae39 2020-07-21 stsp const struct got_error *got_path_dirent_type(int *, const char *,
130 20ccae39 2020-07-21 stsp struct dirent *);
131 20ccae39 2020-07-21 stsp
132 562386c5 2020-10-19 stsp /* basename(3) with dynamically allocated result and unmodified input. */
133 f2ea84fa 2019-07-27 stsp const struct got_error *got_path_basename(char **, const char *);
134 f2ea84fa 2019-07-27 stsp
135 72151b04 2019-05-11 stsp /* Strip trailing slashes from a path; path will be modified in-place. */
136 72151b04 2019-05-11 stsp void got_path_strip_trailing_slashes(char *);
137 0ee7065d 2019-05-13 stsp
138 0ee7065d 2019-05-13 stsp /* Look up the absolute path of a program in $PATH */
139 0ee7065d 2019-05-13 stsp const struct got_error *got_path_find_prog(char **, const char *);
140 2c7829a4 2019-06-17 stsp
141 2c7829a4 2019-06-17 stsp /* Create a new file at a specified path, with optional content. */
142 2c7829a4 2019-06-17 stsp const struct got_error *got_path_create_file(const char *, const char *);
143 5bb4ff2b 2022-10-15 stsp
144 5bb4ff2b 2022-10-15 stsp /*
145 5bb4ff2b 2022-10-15 stsp * Attempt to move an existing file to a new path, creating missing parent
146 5bb4ff2b 2022-10-15 stsp * directories at the destination path if necessary.
147 5bb4ff2b 2022-10-15 stsp * (Cross-mount-point moves are not yet implemented.)
148 5bb4ff2b 2022-10-15 stsp */
149 5bb4ff2b 2022-10-15 stsp const struct got_error *got_path_move_file(const char *, const char *);