]>
git.proxmox.com Git - libgit2.git/blob - src/path.h
2 * Copyright (C) 2009-2012 the libgit2 contributors
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.
7 #ifndef INCLUDE_path_h__
8 #define INCLUDE_path_h__
15 * Path manipulation utils
17 * These are path utilities that munge paths without actually
18 * looking at the real filesystem.
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.
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 "." .
31 * The `git_path_dirname` implementation is thread safe. The returned
32 * string must be manually free'd.
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).
39 extern char *git_path_dirname(const char *path
);
40 extern int git_path_dirname_r(git_buf
*buffer
, const char *path
);
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.
48 * Trailing slashes and backslashes are significant: the basename of
49 * c:/foo/bar/ is an empty string after the rightmost slash.
51 * The `git_path_basename` implementation is thread safe. The returned
52 * string must be manually free'd.
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).
58 extern char *git_path_basename(const char *path
);
59 extern int git_path_basename_r(git_buf
*buffer
, const char *path
);
61 extern const char *git_path_topdir(const char *path
);
64 * Find offset to root of path if path has one.
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
71 extern int git_path_root(const char *path
);
74 * Ensure path has a trailing '/'.
76 extern int git_path_to_dir(git_buf
*path
);
79 * Ensure string has a trailing '/' if there is space for it.
81 extern void git_path_string_to_dir(char* path
, size_t size
);
84 * Taken from git.git; returns nonzero if the given path is "." or "..".
86 GIT_INLINE(int) git_path_is_dot_or_dotdot(const char *name
)
88 return (name
[0] == '.' &&
90 (name
[1] == '.' && name
[2] == '\0')));
94 GIT_INLINE(int) git_path_is_dot_or_dotdotW(const wchar_t *name
)
96 return (name
[0] == L
'.' &&
98 (name
[1] == L
'.' && name
[2] == L
'\0')));
102 * Convert backslashes in path to forward slashes.
104 GIT_INLINE(void) git_path_mkposix(char *path
)
114 # define git_path_mkposix(p) /* blank */
117 extern int git__percent_decode(git_buf
*decoded_out
, const char *input
);
120 * Extract path from file:// URL.
122 extern int git_path_fromurl(git_buf
*local_path_out
, const char *file_url
);
126 * Path filesystem utils
128 * These are path utilities that actually access the filesystem.
132 * Check if a file exists and can be accessed.
133 * @return true or false
135 extern bool git_path_exists(const char *path
);
138 * Check if the given path points to a directory.
139 * @return true or false
141 extern bool git_path_isdir(const char *path
);
144 * Check if the given path points to a regular file.
145 * @return true or false
147 extern bool git_path_isfile(const char *path
);
150 * Check if the given path is a directory, and is empty.
152 extern bool git_path_is_empty_dir(const char *path
);
155 * Stat a file and/or link and set error if needed.
157 extern int git_path_lstat(const char *path
, struct stat
*st
);
160 * Check if the parent directory contains the item.
162 * @param dir Directory to check.
163 * @param item Item that might be in the directory.
164 * @return 0 if item exists in directory, <0 otherwise.
166 extern bool git_path_contains(git_buf
*dir
, const char *item
);
169 * Check if the given path contains the given subdirectory.
171 * @param parent Directory path that might contain subdir
172 * @param subdir Subdirectory name to look for in parent
173 * @param append_if_exists If true, then subdir will be appended to the parent path if it does exist
174 * @return true if subdirectory exists, false otherwise.
176 extern bool git_path_contains_dir(git_buf
*parent
, const char *subdir
);
179 * Check if the given path contains the given file.
181 * @param dir Directory path that might contain file
182 * @param file File name to look for in parent
183 * @param append_if_exists If true, then file will be appended to the path if it does exist
184 * @return true if file exists, false otherwise.
186 extern bool git_path_contains_file(git_buf
*dir
, const char *file
);
189 * Clean up path, prepending base if it is not already rooted.
191 extern int git_path_prettify(git_buf
*path_out
, const char *path
, const char *base
);
194 * Clean up path, prepending base if it is not already rooted and
197 extern int git_path_prettify_dir(git_buf
*path_out
, const char *path
, const char *base
);
200 * Get a directory from a path.
202 * If path is a directory, this acts like `git_path_prettify_dir`
203 * (cleaning up path and appending a '/'). If path is a normal file,
204 * this prettifies it, then removed the filename a la dirname and
205 * appends the trailing '/'. If the path does not exist, it is
206 * treated like a regular filename.
208 extern int git_path_find_dir(git_buf
*dir
, const char *path
, const char *base
);
211 * Resolve relative references within a path.
213 * This eliminates "./" and "../" relative references inside a path,
214 * as well as condensing multiple slashes into single ones. It will
215 * not touch the path before the "ceiling" length.
217 * Additionally, this will recognize an "c:/" drive prefix or a "xyz://" URL
218 * prefix and not touch that part of the path.
220 extern int git_path_resolve_relative(git_buf
*path
, size_t ceiling
);
223 * Apply a relative path to base path.
225 * Note that the base path could be a filename or a URL and this
226 * should still work. The relative path is walked segment by segment
227 * with three rules: series of slashes will be condensed to a single
228 * slash, "." will be eaten with no change, and ".." will remove a
229 * segment from the base path.
231 extern int git_path_apply_relative(git_buf
*target
, const char *relpath
);
234 * Walk each directory entry, except '.' and '..', calling fn(state).
236 * @param pathbuf buffer the function reads the initial directory
237 * path from, and updates with each successive entry's name.
238 * @param fn function to invoke with each entry. The first arg is
239 * the input state and the second arg is pathbuf. The function
240 * may modify the pathbuf, but only by appending new text.
241 * @param state to pass to fn as the first arg.
243 extern int git_path_direach(
245 int (*fn
)(void *, git_buf
*),
249 * Sort function to order two paths.
251 extern int git_path_cmp(
252 const char *name1
, size_t len1
, int isdir1
,
253 const char *name2
, size_t len2
, int isdir2
);
256 * Invoke callback up path directory by directory until the ceiling is
257 * reached (inclusive of a final call at the root_path).
259 * Returning anything other than 0 from the callback function
260 * will stop the iteration and propogate the error to the caller.
262 * @param pathbuf Buffer the function reads the directory from and
263 * and updates with each successive name.
264 * @param ceiling Prefix of path at which to stop walking up. If NULL,
265 * this will walk all the way up to the root. If not a prefix of
266 * pathbuf, the callback will be invoked a single time on the
267 * original input path.
268 * @param fn Function to invoke on each path. The first arg is the
269 * input satte and the second arg is the pathbuf. The function
270 * should not modify the pathbuf.
271 * @param state Passed to fn as the first ath.
273 extern int git_path_walk_up(
276 int (*fn
)(void *state
, git_buf
*),
280 * Load all directory entries (except '.' and '..') into a vector.
282 * For cases where `git_path_direach()` is not appropriate, this
283 * allows you to load the filenames in a directory into a vector
284 * of strings. That vector can then be sorted, iterated, or whatever.
285 * Remember to free alloc of the allocated strings when you are done.
287 * @param path The directory to read from.
288 * @param prefix_len When inserting entries, the trailing part of path
289 * will be prefixed after this length. I.e. given path "/a/b" and
290 * prefix_len 3, the entries will look like "b/e1", "b/e2", etc.
291 * @param alloc_extra Extra bytes to add to each string allocation in
292 * case you want to append anything funny.
293 * @param contents Vector to fill with directory entry names.
295 extern int git_path_dirload(
299 git_vector
*contents
);
305 char path
[GIT_FLEX_ARRAY
];
306 } git_path_with_stat
;
308 extern int git_path_with_stat_cmp(const void *a
, const void *b
);
311 * Load all directory entries along with stat info into a vector.
313 * This is just like git_path_dirload except that each entry in the
314 * vector is a git_path_with_stat structure that contains both the
315 * path and the stat info, plus directories will have a / suffixed
316 * to their path name.
318 extern int git_path_dirload_with_stat(
321 git_vector
*contents
);