vendor/libgit2/src/fileops.c in rugged-0.23.3 vs vendor/libgit2/src/fileops.c in rugged-0.24.0b0
- old
+ new
@@ -16,11 +16,11 @@
GIT__USE_STRMAP
int git_futils_mkpath2file(const char *file_path, const mode_t mode)
{
return git_futils_mkdir(
- file_path, NULL, mode,
+ file_path, mode,
GIT_MKDIR_PATH | GIT_MKDIR_SKIP_LAST | GIT_MKDIR_VERIFY_DIR);
}
int git_futils_mktmp(git_buf *path_out, const char *filename, mode_t mode)
{
@@ -287,101 +287,234 @@
void git_futils_mmap_free(git_map *out)
{
p_munmap(out);
}
-GIT_INLINE(int) validate_existing(
- const char *make_path,
+GIT_INLINE(int) mkdir_validate_dir(
+ const char *path,
struct stat *st,
mode_t mode,
uint32_t flags,
- struct git_futils_mkdir_perfdata *perfdata)
+ struct git_futils_mkdir_options *opts)
{
+ /* with exclusive create, existing dir is an error */
+ if ((flags & GIT_MKDIR_EXCL) != 0) {
+ giterr_set(GITERR_FILESYSTEM,
+ "Failed to make directory '%s': directory exists", path);
+ return GIT_EEXISTS;
+ }
+
if ((S_ISREG(st->st_mode) && (flags & GIT_MKDIR_REMOVE_FILES)) ||
(S_ISLNK(st->st_mode) && (flags & GIT_MKDIR_REMOVE_SYMLINKS))) {
- if (p_unlink(make_path) < 0) {
+ if (p_unlink(path) < 0) {
giterr_set(GITERR_OS, "Failed to remove %s '%s'",
- S_ISLNK(st->st_mode) ? "symlink" : "file", make_path);
+ S_ISLNK(st->st_mode) ? "symlink" : "file", path);
return GIT_EEXISTS;
}
- perfdata->mkdir_calls++;
+ opts->perfdata.mkdir_calls++;
- if (p_mkdir(make_path, mode) < 0) {
- giterr_set(GITERR_OS, "Failed to make directory '%s'", make_path);
+ if (p_mkdir(path, mode) < 0) {
+ giterr_set(GITERR_OS, "Failed to make directory '%s'", path);
return GIT_EEXISTS;
}
}
else if (S_ISLNK(st->st_mode)) {
/* Re-stat the target, make sure it's a directory */
- perfdata->stat_calls++;
+ opts->perfdata.stat_calls++;
- if (p_stat(make_path, st) < 0) {
- giterr_set(GITERR_OS, "Failed to make directory '%s'", make_path);
+ if (p_stat(path, st) < 0) {
+ giterr_set(GITERR_OS, "Failed to make directory '%s'", path);
return GIT_EEXISTS;
}
}
else if (!S_ISDIR(st->st_mode)) {
giterr_set(GITERR_FILESYSTEM,
- "Failed to make directory '%s': directory exists", make_path);
+ "Failed to make directory '%s': directory exists", path);
return GIT_EEXISTS;
}
return 0;
}
-int git_futils_mkdir_ext(
+GIT_INLINE(int) mkdir_validate_mode(
const char *path,
- const char *base,
+ struct stat *st,
+ bool terminal_path,
mode_t mode,
uint32_t flags,
struct git_futils_mkdir_options *opts)
{
- int error = -1;
- git_buf make_path = GIT_BUF_INIT;
- ssize_t root = 0, min_root_len, root_len;
- char lastch = '/', *tail;
- struct stat st;
+ if (((terminal_path && (flags & GIT_MKDIR_CHMOD) != 0) ||
+ (flags & GIT_MKDIR_CHMOD_PATH) != 0) && st->st_mode != mode) {
- /* build path and find "root" where we should start calling mkdir */
- if (git_path_join_unrooted(&make_path, path, base, &root) < 0)
- return -1;
+ opts->perfdata.chmod_calls++;
- if (make_path.size == 0) {
- giterr_set(GITERR_OS, "Attempt to create empty path");
- goto done;
+ if (p_chmod(path, mode) < 0) {
+ giterr_set(GITERR_OS, "failed to set permissions on '%s'", path);
+ return -1;
+ }
}
+ return 0;
+}
+
+GIT_INLINE(int) mkdir_canonicalize(
+ git_buf *path,
+ uint32_t flags)
+{
+ ssize_t root_len;
+
+ if (path->size == 0) {
+ giterr_set(GITERR_OS, "attempt to create empty path");
+ return -1;
+ }
+
/* Trim trailing slashes (except the root) */
- if ((root_len = git_path_root(make_path.ptr)) < 0)
+ if ((root_len = git_path_root(path->ptr)) < 0)
root_len = 0;
else
root_len++;
- while (make_path.size > (size_t)root_len &&
- make_path.ptr[make_path.size - 1] == '/')
- make_path.ptr[--make_path.size] = '\0';
+ while (path->size > (size_t)root_len && path->ptr[path->size - 1] == '/')
+ path->ptr[--path->size] = '\0';
/* if we are not supposed to made the last element, truncate it */
if ((flags & GIT_MKDIR_SKIP_LAST2) != 0) {
- git_path_dirname_r(&make_path, make_path.ptr);
+ git_path_dirname_r(path, path->ptr);
flags |= GIT_MKDIR_SKIP_LAST;
}
if ((flags & GIT_MKDIR_SKIP_LAST) != 0) {
- git_path_dirname_r(&make_path, make_path.ptr);
+ git_path_dirname_r(path, path->ptr);
}
/* We were either given the root path (or trimmed it to
- * the root), we don't have anything to do.
+ * the root), we don't have anything to do.
+ */
+ if (path->size <= (size_t)root_len)
+ git_buf_clear(path);
+
+ return 0;
+}
+
+int git_futils_mkdir(
+ const char *path,
+ mode_t mode,
+ uint32_t flags)
+{
+ git_buf make_path = GIT_BUF_INIT, parent_path = GIT_BUF_INIT;
+ const char *relative;
+ struct git_futils_mkdir_options opts = { 0 };
+ struct stat st;
+ size_t depth = 0;
+ int len = 0, root_len, error;
+
+ if ((error = git_buf_puts(&make_path, path)) < 0 ||
+ (error = mkdir_canonicalize(&make_path, flags)) < 0 ||
+ (error = git_buf_puts(&parent_path, make_path.ptr)) < 0 ||
+ make_path.size == 0)
+ goto done;
+
+ root_len = git_path_root(make_path.ptr);
+
+ /* find the first parent directory that exists. this will be used
+ * as the base to dirname_relative.
*/
- if (make_path.size <= (size_t)root_len) {
- error = 0;
+ for (relative = make_path.ptr; parent_path.size; ) {
+ error = p_lstat(parent_path.ptr, &st);
+
+ if (error == 0) {
+ break;
+ } else if (errno != ENOENT) {
+ giterr_set(GITERR_OS, "failed to stat '%s'", parent_path.ptr);
+ goto done;
+ }
+
+ depth++;
+
+ /* examine the parent of the current path */
+ if ((len = git_path_dirname_r(&parent_path, parent_path.ptr)) < 0) {
+ error = len;
+ goto done;
+ }
+
+ assert(len);
+
+ /* we've walked all the given path's parents and it's either relative
+ * or rooted. either way, give up and make the entire path.
+ */
+ if ((len == 1 && parent_path.ptr[0] == '.') || len == root_len+1) {
+ relative = make_path.ptr;
+ break;
+ }
+
+ relative = make_path.ptr + len + 1;
+
+ /* not recursive? just make this directory relative to its parent. */
+ if ((flags & GIT_MKDIR_PATH) == 0)
+ break;
+ }
+
+ /* we found an item at the location we're trying to create,
+ * validate it.
+ */
+ if (depth == 0) {
+ error = mkdir_validate_dir(make_path.ptr, &st, mode, flags, &opts);
+
+ if (!error)
+ error = mkdir_validate_mode(
+ make_path.ptr, &st, true, mode, flags, &opts);
+
goto done;
}
+ /* we already took `SKIP_LAST` and `SKIP_LAST2` into account when
+ * canonicalizing `make_path`.
+ */
+ flags &= ~(GIT_MKDIR_SKIP_LAST2 | GIT_MKDIR_SKIP_LAST);
+
+ error = git_futils_mkdir_relative(relative,
+ parent_path.size ? parent_path.ptr : NULL, mode, flags, &opts);
+
+done:
+ git_buf_free(&make_path);
+ git_buf_free(&parent_path);
+ return error;
+}
+
+int git_futils_mkdir_r(const char *path, const mode_t mode)
+{
+ return git_futils_mkdir(path, mode, GIT_MKDIR_PATH);
+}
+
+int git_futils_mkdir_relative(
+ const char *relative_path,
+ const char *base,
+ mode_t mode,
+ uint32_t flags,
+ struct git_futils_mkdir_options *opts)
+{
+ git_buf make_path = GIT_BUF_INIT;
+ ssize_t root = 0, min_root_len;
+ char lastch = '/', *tail;
+ struct stat st;
+ struct git_futils_mkdir_options empty_opts = {0};
+ int error;
+
+ if (!opts)
+ opts = &empty_opts;
+
+ /* build path and find "root" where we should start calling mkdir */
+ if (git_path_join_unrooted(&make_path, relative_path, base, &root) < 0)
+ return -1;
+
+ if ((error = mkdir_canonicalize(&make_path, flags)) < 0 ||
+ make_path.size == 0)
+ goto done;
+
/* if we are not supposed to make the whole path, reset root */
if ((flags & GIT_MKDIR_PATH) == 0)
root = git_buf_rfind(&make_path, '/');
/* advance root past drive name or network mount prefix */
@@ -435,37 +568,20 @@
giterr_set(GITERR_OS, "Failed to make directory '%s'", make_path.ptr);
error = -1;
goto done;
}
} else {
- /* with exclusive create, existing dir is an error */
- if ((flags & GIT_MKDIR_EXCL) != 0) {
- giterr_set(GITERR_FILESYSTEM, "Failed to make directory '%s': directory exists", make_path.ptr);
- error = GIT_EEXISTS;
+ if ((error = mkdir_validate_dir(
+ make_path.ptr, &st, mode, flags, opts)) < 0)
goto done;
- }
-
- if ((error = validate_existing(
- make_path.ptr, &st, mode, flags, &opts->perfdata)) < 0)
- goto done;
}
/* chmod if requested and necessary */
- if (((flags & GIT_MKDIR_CHMOD_PATH) != 0 ||
- (lastch == '\0' && (flags & GIT_MKDIR_CHMOD) != 0)) &&
- st.st_mode != mode) {
+ if ((error = mkdir_validate_mode(
+ make_path.ptr, &st, (lastch == '\0'), mode, flags, opts)) < 0)
+ goto done;
- opts->perfdata.chmod_calls++;
-
- if ((error = p_chmod(make_path.ptr, mode)) < 0 &&
- lastch == '\0') {
- giterr_set(GITERR_OS, "Failed to set permissions on '%s'",
- make_path.ptr);
- goto done;
- }
- }
-
if (opts->dir_map && opts->pool) {
char *cache_path;
size_t alloc_size;
GITERR_CHECK_ALLOC_ADD(&alloc_size, make_path.size, 1);
@@ -499,25 +615,10 @@
done:
git_buf_free(&make_path);
return error;
}
-int git_futils_mkdir(
- const char *path,
- const char *base,
- mode_t mode,
- uint32_t flags)
-{
- struct git_futils_mkdir_options options = {0};
- return git_futils_mkdir_ext(path, base, mode, flags, &options);
-}
-
-int git_futils_mkdir_r(const char *path, const char *base, const mode_t mode)
-{
- return git_futils_mkdir(path, base, mode, GIT_MKDIR_PATH);
-}
-
typedef struct {
const char *base;
size_t baselen;
uint32_t flags;
int depth;
@@ -775,20 +876,20 @@
int error = 0;
/* create root directory the first time we need to create a directory */
if ((info->flags & GIT_CPDIR__MKDIR_DONE_FOR_TO_ROOT) == 0) {
error = git_futils_mkdir(
- info->to_root, NULL, info->dirmode,
+ info->to_root, info->dirmode,
(info->flags & GIT_CPDIR_CHMOD_DIRS) ? GIT_MKDIR_CHMOD : 0);
info->flags |= GIT_CPDIR__MKDIR_DONE_FOR_TO_ROOT;
}
/* create directory with root as base to prevent excess chmods */
if (!error)
- error = git_futils_mkdir(
+ error = git_futils_mkdir_relative(
from->ptr + info->from_prefix, info->to_root,
- info->dirmode, info->mkdir_flags);
+ info->dirmode, info->mkdir_flags, NULL);
return error;
}
static int _cp_r_callback(void *ref, git_buf *from)