]>
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
);
85 * Convert backslashes in path to forward slashes.
87 GIT_INLINE(void) git_path_mkposix(char *path
)
97 # define git_path_mkposix(p) /* blank */
100 extern int git__percent_decode(git_buf
*decoded_out
, const char *input
);
103 * Extract path from file:// URL.
105 extern int git_path_fromurl(git_buf
*local_path_out
, const char *file_url
);
109 * Path filesystem utils
111 * These are path utilities that actually access the filesystem.
115 * Check if a file exists and can be accessed.
116 * @return true or false
118 extern bool git_path_exists(const char *path
);
121 * Check if the given path points to a directory.
122 * @return true or false
124 extern bool git_path_isdir(const char *path
);
127 * Check if the given path points to a regular file.
128 * @return true or false
130 extern bool git_path_isfile(const char *path
);
133 * Stat a file and/or link and set error if needed.
135 extern int git_path_lstat(const char *path
, struct stat
*st
);
138 * Check if the parent directory contains the item.
140 * @param dir Directory to check.
141 * @param item Item that might be in the directory.
142 * @return GIT_SUCCESS if item exists in directory, <0 otherwise.
144 extern bool git_path_contains(git_buf
*dir
, const char *item
);
147 * Check if the given path contains the given subdirectory.
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.
154 extern bool git_path_contains_dir(git_buf
*parent
, const char *subdir
);
157 * Check if the given path contains the given file.
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.
164 extern bool git_path_contains_file(git_buf
*dir
, const char *file
);
167 * Clean up path, prepending base if it is not already rooted.
169 extern int git_path_prettify(git_buf
*path_out
, const char *path
, const char *base
);
172 * Clean up path, prepending base if it is not already rooted and
175 extern int git_path_prettify_dir(git_buf
*path_out
, const char *path
, const char *base
);
178 * Get a directory from a path.
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.
186 extern int git_path_find_dir(git_buf
*dir
, const char *path
, const char *base
);
189 * Walk each directory entry, except '.' and '..', calling fn(state).
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.
198 extern int git_path_direach(
200 int (*fn
)(void *, git_buf
*),
204 * Sort function to order two paths.
206 extern int git_path_cmp(
207 const char *name1
, int len1
, int isdir1
,
208 const char *name2
, int len2
, int isdir2
);
211 * Invoke callback up path directory by directory until the ceiling is
212 * reached (inclusive of a final call at the root_path).
214 * Returning anything other than GIT_SUCCESS from the callback function
215 * will stop the iteration and propogate the error to the caller.
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.
228 extern int git_path_walk_up(
231 int (*fn
)(void *state
, git_buf
*),
235 * Load all directory entries (except '.' and '..') into a vector.
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.
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.
250 extern int git_path_dirload(
254 git_vector
*contents
);
260 char path
[GIT_FLEX_ARRAY
];
261 } git_path_with_stat
;
263 extern int git_path_with_stat_cmp(const void *a
, const void *b
);
266 * Load all directory entries along with stat info into a vector.
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.
273 extern int git_path_dirload_with_stat(
276 git_vector
*contents
);