]> git.proxmox.com Git - libgit2.git/blob - src/path.h
Merge branch 'new-error-handling' into development
[libgit2.git] / src / path.h
1 /*
2 * Copyright (C) 2009-2012 the libgit2 contributors
3 *
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
6 */
7 #ifndef INCLUDE_path_h__
8 #define INCLUDE_path_h__
9
10 #include "common.h"
11 #include "buffer.h"
12 #include "vector.h"
13
14 /**
15 * Path manipulation utils
16 *
17 * These are path utilities that munge paths without actually
18 * looking at the real filesystem.
19 */
20
21 /*
22 * The dirname() function shall take a pointer to a character string
23 * that contains a pathname, and return a pointer to a string that is a
24 * pathname of the parent directory of that file. Trailing '/' characters
25 * in the path are not counted as part of the path.
26 *
27 * If path does not contain a '/', then dirname() shall return a pointer to
28 * the string ".". If path is a null pointer or points to an empty string,
29 * dirname() shall return a pointer to the string "." .
30 *
31 * The `git_path_dirname` implementation is thread safe. The returned
32 * string must be manually free'd.
33 *
34 * The `git_path_dirname_r` implementation writes the dirname to a `git_buf`
35 * if the buffer pointer is not NULL.
36 * It returns an error code < 0 if there is an allocation error, otherwise
37 * the length of the dirname (which will be > 0).
38 */
39 extern char *git_path_dirname(const char *path);
40 extern int git_path_dirname_r(git_buf *buffer, const char *path);
41
42 /*
43 * This function returns the basename of the file, which is the last
44 * part of its full name given by fname, with the drive letter and
45 * leading directories stripped off. For example, the basename of
46 * c:/foo/bar/file.ext is file.ext, and the basename of a:foo is foo.
47 *
48 * Trailing slashes and backslashes are significant: the basename of
49 * c:/foo/bar/ is an empty string after the rightmost slash.
50 *
51 * The `git_path_basename` implementation is thread safe. The returned
52 * string must be manually free'd.
53 *
54 * The `git_path_basename_r` implementation writes the basename to a `git_buf`.
55 * It returns an error code < 0 if there is an allocation error, otherwise
56 * the length of the basename (which will be >= 0).
57 */
58 extern char *git_path_basename(const char *path);
59 extern int git_path_basename_r(git_buf *buffer, const char *path);
60
61 extern const char *git_path_topdir(const char *path);
62
63 /**
64 * Find offset to root of path if path has one.
65 *
66 * This will return a number >= 0 which is the offset to the start of the
67 * path, if the path is rooted (i.e. "/rooted/path" returns 0 and
68 * "c:/windows/rooted/path" returns 2). If the path is not rooted, this
69 * returns < 0.
70 */
71 extern int git_path_root(const char *path);
72
73 /**
74 * Ensure path has a trailing '/'.
75 */
76 extern int git_path_to_dir(git_buf *path);
77
78 /**
79 * Ensure string has a trailing '/' if there is space for it.
80 */
81 extern void git_path_string_to_dir(char* path, size_t size);
82
83 #ifdef GIT_WIN32
84 /**
85 * Convert backslashes in path to forward slashes.
86 */
87 GIT_INLINE(void) git_path_mkposix(char *path)
88 {
89 while (*path) {
90 if (*path == '\\')
91 *path = '/';
92
93 path++;
94 }
95 }
96 #else
97 # define git_path_mkposix(p) /* blank */
98 #endif
99
100 extern int git__percent_decode(git_buf *decoded_out, const char *input);
101
102 /**
103 * Extract path from file:// URL.
104 */
105 extern int git_path_fromurl(git_buf *local_path_out, const char *file_url);
106
107
108 /**
109 * Path filesystem utils
110 *
111 * These are path utilities that actually access the filesystem.
112 */
113
114 /**
115 * Check if a file exists and can be accessed.
116 * @return true or false
117 */
118 extern bool git_path_exists(const char *path);
119
120 /**
121 * Check if the given path points to a directory.
122 * @return true or false
123 */
124 extern bool git_path_isdir(const char *path);
125
126 /**
127 * Check if the given path points to a regular file.
128 * @return true or false
129 */
130 extern bool git_path_isfile(const char *path);
131
132 /**
133 * Stat a file and/or link and set error if needed.
134 */
135 extern int git_path_lstat(const char *path, struct stat *st);
136
137 /**
138 * Check if the parent directory contains the item.
139 *
140 * @param dir Directory to check.
141 * @param item Item that might be in the directory.
142 * @return 0 if item exists in directory, <0 otherwise.
143 */
144 extern bool git_path_contains(git_buf *dir, const char *item);
145
146 /**
147 * Check if the given path contains the given subdirectory.
148 *
149 * @param parent Directory path that might contain subdir
150 * @param subdir Subdirectory name to look for in parent
151 * @param append_if_exists If true, then subdir will be appended to the parent path if it does exist
152 * @return true if subdirectory exists, false otherwise.
153 */
154 extern bool git_path_contains_dir(git_buf *parent, const char *subdir);
155
156 /**
157 * Check if the given path contains the given file.
158 *
159 * @param dir Directory path that might contain file
160 * @param file File name to look for in parent
161 * @param append_if_exists If true, then file will be appended to the path if it does exist
162 * @return true if file exists, false otherwise.
163 */
164 extern bool git_path_contains_file(git_buf *dir, const char *file);
165
166 /**
167 * Clean up path, prepending base if it is not already rooted.
168 */
169 extern int git_path_prettify(git_buf *path_out, const char *path, const char *base);
170
171 /**
172 * Clean up path, prepending base if it is not already rooted and
173 * appending a slash.
174 */
175 extern int git_path_prettify_dir(git_buf *path_out, const char *path, const char *base);
176
177 /**
178 * Get a directory from a path.
179 *
180 * If path is a directory, this acts like `git_path_prettify_dir`
181 * (cleaning up path and appending a '/'). If path is a normal file,
182 * this prettifies it, then removed the filename a la dirname and
183 * appends the trailing '/'. If the path does not exist, it is
184 * treated like a regular filename.
185 */
186 extern int git_path_find_dir(git_buf *dir, const char *path, const char *base);
187
188 /**
189 * Walk each directory entry, except '.' and '..', calling fn(state).
190 *
191 * @param pathbuf buffer the function reads the initial directory
192 * path from, and updates with each successive entry's name.
193 * @param fn function to invoke with each entry. The first arg is
194 * the input state and the second arg is pathbuf. The function
195 * may modify the pathbuf, but only by appending new text.
196 * @param state to pass to fn as the first arg.
197 */
198 extern int git_path_direach(
199 git_buf *pathbuf,
200 int (*fn)(void *, git_buf *),
201 void *state);
202
203 /**
204 * Sort function to order two paths.
205 */
206 extern int git_path_cmp(
207 const char *name1, size_t len1, int isdir1,
208 const char *name2, size_t len2, int isdir2);
209
210 /**
211 * Invoke callback up path directory by directory until the ceiling is
212 * reached (inclusive of a final call at the root_path).
213 *
214 * Returning anything other than 0 from the callback function
215 * will stop the iteration and propogate the error to the caller.
216 *
217 * @param pathbuf Buffer the function reads the directory from and
218 * and updates with each successive name.
219 * @param ceiling Prefix of path at which to stop walking up. If NULL,
220 * this will walk all the way up to the root. If not a prefix of
221 * pathbuf, the callback will be invoked a single time on the
222 * original input path.
223 * @param fn Function to invoke on each path. The first arg is the
224 * input satte and the second arg is the pathbuf. The function
225 * should not modify the pathbuf.
226 * @param state Passed to fn as the first ath.
227 */
228 extern int git_path_walk_up(
229 git_buf *pathbuf,
230 const char *ceiling,
231 int (*fn)(void *state, git_buf *),
232 void *state);
233
234 /**
235 * Load all directory entries (except '.' and '..') into a vector.
236 *
237 * For cases where `git_path_direach()` is not appropriate, this
238 * allows you to load the filenames in a directory into a vector
239 * of strings. That vector can then be sorted, iterated, or whatever.
240 * Remember to free alloc of the allocated strings when you are done.
241 *
242 * @param path The directory to read from.
243 * @param prefix_len When inserting entries, the trailing part of path
244 * will be prefixed after this length. I.e. given path "/a/b" and
245 * prefix_len 3, the entries will look like "b/e1", "b/e2", etc.
246 * @param alloc_extra Extra bytes to add to each string allocation in
247 * case you want to append anything funny.
248 * @param contents Vector to fill with directory entry names.
249 */
250 extern int git_path_dirload(
251 const char *path,
252 size_t prefix_len,
253 size_t alloc_extra,
254 git_vector *contents);
255
256
257 typedef struct {
258 struct stat st;
259 size_t path_len;
260 char path[GIT_FLEX_ARRAY];
261 } git_path_with_stat;
262
263 extern int git_path_with_stat_cmp(const void *a, const void *b);
264
265 /**
266 * Load all directory entries along with stat info into a vector.
267 *
268 * This is just like git_path_dirload except that each entry in the
269 * vector is a git_path_with_stat structure that contains both the
270 * path and the stat info, plus directories will have a / suffixed
271 * to their path name.
272 */
273 extern int git_path_dirload_with_stat(
274 const char *path,
275 size_t prefix_len,
276 git_vector *contents);
277
278 #endif