diff options
Diffstat (limited to 'src/fileops.c')
-rw-r--r-- | src/fileops.c | 220 |
1 files changed, 0 insertions, 220 deletions
diff --git a/src/fileops.c b/src/fileops.c index a60689f3f..5709499b0 100644 --- a/src/fileops.c +++ b/src/fileops.c @@ -558,226 +558,6 @@ int git_futils_rmdir_r( return error; } - -static int git_futils_guess_system_dirs(git_buf *out) -{ -#ifdef GIT_WIN32 - return git_win32__find_system_dirs(out, L"etc\\"); -#else - return git_buf_sets(out, "/etc"); -#endif -} - -static int git_futils_guess_global_dirs(git_buf *out) -{ -#ifdef GIT_WIN32 - return git_win32__find_global_dirs(out); -#else - return git_buf_sets(out, getenv("HOME")); -#endif -} - -static int git_futils_guess_xdg_dirs(git_buf *out) -{ -#ifdef GIT_WIN32 - return git_win32__find_xdg_dirs(out); -#else - const char *env = NULL; - - if ((env = getenv("XDG_CONFIG_HOME")) != NULL) - return git_buf_joinpath(out, env, "git"); - else if ((env = getenv("HOME")) != NULL) - return git_buf_joinpath(out, env, ".config/git"); - - git_buf_clear(out); - return 0; -#endif -} - -static int git_futils_guess_template_dirs(git_buf *out) -{ -#ifdef GIT_WIN32 - return git_win32__find_system_dirs(out, L"share\\git-core\\templates"); -#else - return git_buf_sets(out, "/usr/share/git-core/templates"); -#endif -} - -typedef int (*git_futils_dirs_guess_cb)(git_buf *out); - -static git_buf git_futils__dirs[GIT_FUTILS_DIR__MAX] = - { GIT_BUF_INIT, GIT_BUF_INIT, GIT_BUF_INIT, GIT_BUF_INIT }; - -static git_futils_dirs_guess_cb git_futils__dir_guess[GIT_FUTILS_DIR__MAX] = { - git_futils_guess_system_dirs, - git_futils_guess_global_dirs, - git_futils_guess_xdg_dirs, - git_futils_guess_template_dirs, -}; - -static int git_futils__dirs_shutdown_set = 0; - -void git_futils_dirs_global_shutdown(void) -{ - int i; - for (i = 0; i < GIT_FUTILS_DIR__MAX; ++i) - git_buf_free(&git_futils__dirs[i]); -} - -int git_futils_dirs_global_init(void) -{ - git_futils_dir_t i; - const git_buf *path; - int error = 0; - - for (i = 0; !error && i < GIT_FUTILS_DIR__MAX; i++) - error = git_futils_dirs_get(&path, i); - - return error; -} - -static int git_futils_check_selector(git_futils_dir_t which) -{ - if (which < GIT_FUTILS_DIR__MAX) - return 0; - giterr_set(GITERR_INVALID, "config directory selector out of range"); - return -1; -} - -int git_futils_dirs_get(const git_buf **out, git_futils_dir_t which) -{ - assert(out); - - *out = NULL; - - GITERR_CHECK_ERROR(git_futils_check_selector(which)); - - if (!git_buf_len(&git_futils__dirs[which])) { - /* prepare shutdown if we're going to need it */ - if (!git_futils__dirs_shutdown_set) { - git__on_shutdown(git_futils_dirs_global_shutdown); - git_futils__dirs_shutdown_set = 1; - } - - GITERR_CHECK_ERROR( - git_futils__dir_guess[which](&git_futils__dirs[which])); - } - - *out = &git_futils__dirs[which]; - return 0; -} - -int git_futils_dirs_get_str(char *out, size_t outlen, git_futils_dir_t which) -{ - const git_buf *path = NULL; - - GITERR_CHECK_ERROR(git_futils_check_selector(which)); - GITERR_CHECK_ERROR(git_futils_dirs_get(&path, which)); - - if (!out || path->size >= outlen) { - giterr_set(GITERR_NOMEMORY, "Buffer is too short for the path"); - return GIT_EBUFS; - } - - git_buf_copy_cstr(out, outlen, path); - return 0; -} - -#define PATH_MAGIC "$PATH" - -int git_futils_dirs_set(git_futils_dir_t which, const char *search_path) -{ - const char *expand_path = NULL; - git_buf merge = GIT_BUF_INIT; - - GITERR_CHECK_ERROR(git_futils_check_selector(which)); - - if (search_path != NULL) - expand_path = strstr(search_path, PATH_MAGIC); - - /* init with default if not yet done and needed (ignoring error) */ - if ((!search_path || expand_path) && - !git_buf_len(&git_futils__dirs[which])) - git_futils__dir_guess[which](&git_futils__dirs[which]); - - /* if $PATH is not referenced, then just set the path */ - if (!expand_path) - return git_buf_sets(&git_futils__dirs[which], search_path); - - /* otherwise set to join(before $PATH, old value, after $PATH) */ - if (expand_path > search_path) - git_buf_set(&merge, search_path, expand_path - search_path); - - if (git_buf_len(&git_futils__dirs[which])) - git_buf_join(&merge, GIT_PATH_LIST_SEPARATOR, - merge.ptr, git_futils__dirs[which].ptr); - - expand_path += strlen(PATH_MAGIC); - if (*expand_path) - git_buf_join(&merge, GIT_PATH_LIST_SEPARATOR, merge.ptr, expand_path); - - git_buf_swap(&git_futils__dirs[which], &merge); - git_buf_free(&merge); - - return git_buf_oom(&git_futils__dirs[which]) ? -1 : 0; -} - -static int git_futils_find_in_dirlist( - git_buf *path, const char *name, git_futils_dir_t which, const char *label) -{ - size_t len; - const char *scan, *next = NULL; - const git_buf *syspath; - - GITERR_CHECK_ERROR(git_futils_dirs_get(&syspath, which)); - - for (scan = git_buf_cstr(syspath); scan; scan = next) { - for (next = strchr(scan, GIT_PATH_LIST_SEPARATOR); - next && next > scan && next[-1] == '\\'; - next = strchr(next + 1, GIT_PATH_LIST_SEPARATOR)) - /* find unescaped separator or end of string */; - - len = next ? (size_t)(next++ - scan) : strlen(scan); - if (!len) - continue; - - GITERR_CHECK_ERROR(git_buf_set(path, scan, len)); - if (name) - GITERR_CHECK_ERROR(git_buf_joinpath(path, path->ptr, name)); - - if (git_path_exists(path->ptr)) - return 0; - } - - git_buf_clear(path); - giterr_set(GITERR_OS, "The %s file '%s' doesn't exist", label, name); - return GIT_ENOTFOUND; -} - -int git_futils_find_system_file(git_buf *path, const char *filename) -{ - return git_futils_find_in_dirlist( - path, filename, GIT_FUTILS_DIR_SYSTEM, "system"); -} - -int git_futils_find_global_file(git_buf *path, const char *filename) -{ - return git_futils_find_in_dirlist( - path, filename, GIT_FUTILS_DIR_GLOBAL, "global"); -} - -int git_futils_find_xdg_file(git_buf *path, const char *filename) -{ - return git_futils_find_in_dirlist( - path, filename, GIT_FUTILS_DIR_XDG, "global/xdg"); -} - -int git_futils_find_template_dir(git_buf *path) -{ - return git_futils_find_in_dirlist( - path, NULL, GIT_FUTILS_DIR_TEMPLATE, "template"); -} - int git_futils_fake_symlink(const char *old, const char *new) { int retcode = GIT_ERROR; |