PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/src/path.h

https://github.com/rowanj/libgit2
C Header | 431 lines | 132 code | 58 blank | 241 comment | 23 complexity | 3df18bd3555a25e6b1487a673f69e1c9 MD5 | raw file
  1. /*
  2. * Copyright (C) the libgit2 contributors. All rights reserved.
  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. #include "common.h"
  10. #include "buffer.h"
  11. #include "vector.h"
  12. /**
  13. * Path manipulation utils
  14. *
  15. * These are path utilities that munge paths without actually
  16. * looking at the real filesystem.
  17. */
  18. /*
  19. * The dirname() function shall take a pointer to a character string
  20. * that contains a pathname, and return a pointer to a string that is a
  21. * pathname of the parent directory of that file. Trailing '/' characters
  22. * in the path are not counted as part of the path.
  23. *
  24. * If path does not contain a '/', then dirname() shall return a pointer to
  25. * the string ".". If path is a null pointer or points to an empty string,
  26. * dirname() shall return a pointer to the string "." .
  27. *
  28. * The `git_path_dirname` implementation is thread safe. The returned
  29. * string must be manually free'd.
  30. *
  31. * The `git_path_dirname_r` implementation writes the dirname to a `git_buf`
  32. * if the buffer pointer is not NULL.
  33. * It returns an error code < 0 if there is an allocation error, otherwise
  34. * the length of the dirname (which will be > 0).
  35. */
  36. extern char *git_path_dirname(const char *path);
  37. extern int git_path_dirname_r(git_buf *buffer, const char *path);
  38. /*
  39. * This function returns the basename of the file, which is the last
  40. * part of its full name given by fname, with the drive letter and
  41. * leading directories stripped off. For example, the basename of
  42. * c:/foo/bar/file.ext is file.ext, and the basename of a:foo is foo.
  43. *
  44. * Trailing slashes and backslashes are significant: the basename of
  45. * c:/foo/bar/ is an empty string after the rightmost slash.
  46. *
  47. * The `git_path_basename` implementation is thread safe. The returned
  48. * string must be manually free'd.
  49. *
  50. * The `git_path_basename_r` implementation writes the basename to a `git_buf`.
  51. * It returns an error code < 0 if there is an allocation error, otherwise
  52. * the length of the basename (which will be >= 0).
  53. */
  54. extern char *git_path_basename(const char *path);
  55. extern int git_path_basename_r(git_buf *buffer, const char *path);
  56. /* Return the offset of the start of the basename. Unlike the other
  57. * basename functions, this returns 0 if the path is empty.
  58. */
  59. extern size_t git_path_basename_offset(git_buf *buffer);
  60. extern const char *git_path_topdir(const char *path);
  61. /**
  62. * Find offset to root of path if path has one.
  63. *
  64. * This will return a number >= 0 which is the offset to the start of the
  65. * path, if the path is rooted (i.e. "/rooted/path" returns 0 and
  66. * "c:/windows/rooted/path" returns 2). If the path is not rooted, this
  67. * returns < 0.
  68. */
  69. extern int git_path_root(const char *path);
  70. /**
  71. * Ensure path has a trailing '/'.
  72. */
  73. extern int git_path_to_dir(git_buf *path);
  74. /**
  75. * Ensure string has a trailing '/' if there is space for it.
  76. */
  77. extern void git_path_string_to_dir(char* path, size_t size);
  78. /**
  79. * Taken from git.git; returns nonzero if the given path is "." or "..".
  80. */
  81. GIT_INLINE(int) git_path_is_dot_or_dotdot(const char *name)
  82. {
  83. return (name[0] == '.' &&
  84. (name[1] == '\0' ||
  85. (name[1] == '.' && name[2] == '\0')));
  86. }
  87. #ifdef GIT_WIN32
  88. GIT_INLINE(int) git_path_is_dot_or_dotdotW(const wchar_t *name)
  89. {
  90. return (name[0] == L'.' &&
  91. (name[1] == L'\0' ||
  92. (name[1] == L'.' && name[2] == L'\0')));
  93. }
  94. /**
  95. * Convert backslashes in path to forward slashes.
  96. */
  97. GIT_INLINE(void) git_path_mkposix(char *path)
  98. {
  99. while (*path) {
  100. if (*path == '\\')
  101. *path = '/';
  102. path++;
  103. }
  104. }
  105. #else
  106. # define git_path_mkposix(p) /* blank */
  107. #endif
  108. extern int git__percent_decode(git_buf *decoded_out, const char *input);
  109. /**
  110. * Extract path from file:// URL.
  111. */
  112. extern int git_path_fromurl(git_buf *local_path_out, const char *file_url);
  113. /**
  114. * Path filesystem utils
  115. *
  116. * These are path utilities that actually access the filesystem.
  117. */
  118. /**
  119. * Check if a file exists and can be accessed.
  120. * @return true or false
  121. */
  122. extern bool git_path_exists(const char *path);
  123. /**
  124. * Check if the given path points to a directory.
  125. * @return true or false
  126. */
  127. extern bool git_path_isdir(const char *path);
  128. /**
  129. * Check if the given path points to a regular file.
  130. * @return true or false
  131. */
  132. extern bool git_path_isfile(const char *path);
  133. /**
  134. * Check if the given path is a directory, and is empty.
  135. */
  136. extern bool git_path_is_empty_dir(const char *path);
  137. /**
  138. * Stat a file and/or link and set error if needed.
  139. */
  140. extern int git_path_lstat(const char *path, struct stat *st);
  141. /**
  142. * Check if the parent directory contains the item.
  143. *
  144. * @param dir Directory to check.
  145. * @param item Item that might be in the directory.
  146. * @return 0 if item exists in directory, <0 otherwise.
  147. */
  148. extern bool git_path_contains(git_buf *dir, const char *item);
  149. /**
  150. * Check if the given path contains the given subdirectory.
  151. *
  152. * @param parent Directory path that might contain subdir
  153. * @param subdir Subdirectory name to look for in parent
  154. * @return true if subdirectory exists, false otherwise.
  155. */
  156. extern bool git_path_contains_dir(git_buf *parent, const char *subdir);
  157. /**
  158. * Check if the given path contains the given file.
  159. *
  160. * @param dir Directory path that might contain file
  161. * @param file File name to look for in parent
  162. * @return true if file exists, false otherwise.
  163. */
  164. extern bool git_path_contains_file(git_buf *dir, const char *file);
  165. /**
  166. * Prepend base to unrooted path or just copy path over.
  167. *
  168. * This will optionally return the index into the path where the "root"
  169. * is, either the end of the base directory prefix or the path root.
  170. */
  171. extern int git_path_join_unrooted(
  172. git_buf *path_out, const char *path, const char *base, ssize_t *root_at);
  173. /**
  174. * Clean up path, prepending base if it is not already rooted.
  175. */
  176. extern int git_path_prettify(git_buf *path_out, const char *path, const char *base);
  177. /**
  178. * Clean up path, prepending base if it is not already rooted and
  179. * appending a slash.
  180. */
  181. extern int git_path_prettify_dir(git_buf *path_out, const char *path, const char *base);
  182. /**
  183. * Get a directory from a path.
  184. *
  185. * If path is a directory, this acts like `git_path_prettify_dir`
  186. * (cleaning up path and appending a '/'). If path is a normal file,
  187. * this prettifies it, then removed the filename a la dirname and
  188. * appends the trailing '/'. If the path does not exist, it is
  189. * treated like a regular filename.
  190. */
  191. extern int git_path_find_dir(git_buf *dir, const char *path, const char *base);
  192. /**
  193. * Resolve relative references within a path.
  194. *
  195. * This eliminates "./" and "../" relative references inside a path,
  196. * as well as condensing multiple slashes into single ones. It will
  197. * not touch the path before the "ceiling" length.
  198. *
  199. * Additionally, this will recognize an "c:/" drive prefix or a "xyz://" URL
  200. * prefix and not touch that part of the path.
  201. */
  202. extern int git_path_resolve_relative(git_buf *path, size_t ceiling);
  203. /**
  204. * Apply a relative path to base path.
  205. *
  206. * Note that the base path could be a filename or a URL and this
  207. * should still work. The relative path is walked segment by segment
  208. * with three rules: series of slashes will be condensed to a single
  209. * slash, "." will be eaten with no change, and ".." will remove a
  210. * segment from the base path.
  211. */
  212. extern int git_path_apply_relative(git_buf *target, const char *relpath);
  213. enum {
  214. GIT_PATH_DIR_IGNORE_CASE = (1u << 0),
  215. GIT_PATH_DIR_PRECOMPOSE_UNICODE = (1u << 1),
  216. };
  217. /**
  218. * Walk each directory entry, except '.' and '..', calling fn(state).
  219. *
  220. * @param pathbuf Buffer the function reads the initial directory
  221. * path from, and updates with each successive entry's name.
  222. * @param flags Combination of GIT_PATH_DIR flags.
  223. * @param callback Callback for each entry. Passed the `payload` and each
  224. * successive path inside the directory as a full path. This may
  225. * safely append text to the pathbuf if needed. Return non-zero to
  226. * cancel iteration (and return value will be propagated back).
  227. * @param payload Passed to callback as first argument.
  228. * @return 0 on success or error code from OS error or from callback
  229. */
  230. extern int git_path_direach(
  231. git_buf *pathbuf,
  232. uint32_t flags,
  233. int (*callback)(void *payload, git_buf *path),
  234. void *payload);
  235. /**
  236. * Sort function to order two paths
  237. */
  238. extern int git_path_cmp(
  239. const char *name1, size_t len1, int isdir1,
  240. const char *name2, size_t len2, int isdir2,
  241. int (*compare)(const char *, const char *, size_t));
  242. /**
  243. * Invoke callback up path directory by directory until the ceiling is
  244. * reached (inclusive of a final call at the root_path).
  245. *
  246. * Returning anything other than 0 from the callback function
  247. * will stop the iteration and propogate the error to the caller.
  248. *
  249. * @param pathbuf Buffer the function reads the directory from and
  250. * and updates with each successive name.
  251. * @param ceiling Prefix of path at which to stop walking up. If NULL,
  252. * this will walk all the way up to the root. If not a prefix of
  253. * pathbuf, the callback will be invoked a single time on the
  254. * original input path.
  255. * @param callback Function to invoke on each path. Passed the `payload`
  256. * and the buffer containing the current path. The path should not
  257. * be modified in any way. Return non-zero to stop iteration.
  258. * @param state Passed to fn as the first ath.
  259. */
  260. extern int git_path_walk_up(
  261. git_buf *pathbuf,
  262. const char *ceiling,
  263. int (*callback)(void *payload, git_buf *path),
  264. void *payload);
  265. /**
  266. * Load all directory entries (except '.' and '..') into a vector.
  267. *
  268. * For cases where `git_path_direach()` is not appropriate, this
  269. * allows you to load the filenames in a directory into a vector
  270. * of strings. That vector can then be sorted, iterated, or whatever.
  271. * Remember to free alloc of the allocated strings when you are done.
  272. *
  273. * @param path The directory to read from.
  274. * @param prefix_len When inserting entries, the trailing part of path
  275. * will be prefixed after this length. I.e. given path "/a/b" and
  276. * prefix_len 3, the entries will look like "b/e1", "b/e2", etc.
  277. * @param alloc_extra Extra bytes to add to each string allocation in
  278. * case you want to append anything funny.
  279. * @param flags Combination of GIT_PATH_DIR flags.
  280. * @param contents Vector to fill with directory entry names.
  281. */
  282. extern int git_path_dirload(
  283. const char *path,
  284. size_t prefix_len,
  285. size_t alloc_extra,
  286. uint32_t flags,
  287. git_vector *contents);
  288. typedef struct {
  289. struct stat st;
  290. size_t path_len;
  291. char path[GIT_FLEX_ARRAY];
  292. } git_path_with_stat;
  293. extern int git_path_with_stat_cmp(const void *a, const void *b);
  294. extern int git_path_with_stat_cmp_icase(const void *a, const void *b);
  295. /**
  296. * Load all directory entries along with stat info into a vector.
  297. *
  298. * This adds four things on top of plain `git_path_dirload`:
  299. *
  300. * 1. Each entry in the vector is a `git_path_with_stat` struct that
  301. * contains both the path and the stat info
  302. * 2. The entries will be sorted alphabetically
  303. * 3. Entries that are directories will be suffixed with a '/'
  304. * 4. Optionally, you can be a start and end prefix and only elements
  305. * after the start and before the end (inclusively) will be stat'ed.
  306. *
  307. * @param path The directory to read from
  308. * @param prefix_len The trailing part of path to prefix to entry paths
  309. * @param flags GIT_PATH_DIR flags from above
  310. * @param start_stat As optimization, only stat values after this prefix
  311. * @param end_stat As optimization, only stat values before this prefix
  312. * @param contents Vector to fill with git_path_with_stat structures
  313. */
  314. extern int git_path_dirload_with_stat(
  315. const char *path,
  316. size_t prefix_len,
  317. uint32_t flags,
  318. const char *start_stat,
  319. const char *end_stat,
  320. git_vector *contents);
  321. enum { GIT_PATH_NOTEQUAL = 0, GIT_PATH_EQUAL = 1, GIT_PATH_PREFIX = 2 };
  322. /*
  323. * Determines if a path is equal to or potentially a child of another.
  324. * @param parent The possible parent
  325. * @param child The possible child
  326. */
  327. GIT_INLINE(int) git_path_equal_or_prefixed(
  328. const char *parent,
  329. const char *child)
  330. {
  331. const char *p = parent, *c = child;
  332. while (*p && *c) {
  333. if (*p++ != *c++)
  334. return GIT_PATH_NOTEQUAL;
  335. }
  336. if (*p != '\0')
  337. return GIT_PATH_NOTEQUAL;
  338. if (*c == '\0')
  339. return GIT_PATH_EQUAL;
  340. if (*c == '/')
  341. return GIT_PATH_PREFIX;
  342. return GIT_PATH_NOTEQUAL;
  343. }
  344. /* translate errno to libgit2 error code and set error message */
  345. extern int git_path_set_error(
  346. int errno_value, const char *path, const char *action);
  347. /* check if non-ascii characters are present in filename */
  348. extern bool git_path_has_non_ascii(const char *path, size_t pathlen);
  349. #define GIT_PATH_REPO_ENCODING "UTF-8"
  350. #ifdef __APPLE__
  351. #define GIT_PATH_NATIVE_ENCODING "UTF-8-MAC"
  352. #else
  353. #define GIT_PATH_NATIVE_ENCODING "UTF-8"
  354. #endif
  355. #ifdef GIT_USE_ICONV
  356. #include <iconv.h>
  357. typedef struct {
  358. iconv_t map;
  359. git_buf buf;
  360. } git_path_iconv_t;
  361. #define GIT_PATH_ICONV_INIT { (iconv_t)-1, GIT_BUF_INIT }
  362. /* Init iconv data for converting decomposed UTF-8 to precomposed */
  363. extern int git_path_iconv_init_precompose(git_path_iconv_t *ic);
  364. /* Clear allocated iconv data */
  365. extern void git_path_iconv_clear(git_path_iconv_t *ic);
  366. /*
  367. * Rewrite `in` buffer using iconv map if necessary, replacing `in`
  368. * pointer internal iconv buffer if rewrite happened. The `in` pointer
  369. * will be left unchanged if no rewrite was needed.
  370. */
  371. extern int git_path_iconv(git_path_iconv_t *ic, char **in, size_t *inlen);
  372. #endif /* GIT_USE_ICONV */
  373. #endif