Blame src/fileops.c

Packit Service 20376f
/*
Packit Service 20376f
 * Copyright (C) the libgit2 contributors. All rights reserved.
Packit Service 20376f
 *
Packit Service 20376f
 * This file is part of libgit2, distributed under the GNU GPL v2 with
Packit Service 20376f
 * a Linking Exception. For full terms see the included COPYING file.
Packit Service 20376f
 */
Packit Service 20376f
#include "common.h"
Packit Service 20376f
#include "fileops.h"
Packit Service 20376f
#include "global.h"
Packit Service 20376f
#include "strmap.h"
Packit Service 20376f
#include <ctype.h>
Packit Service 20376f
#if GIT_WIN32
Packit Service 20376f
#include "win32/findfile.h"
Packit Service 20376f
#endif
Packit Service 20376f
Packit Service 20376f
int git_futils_mkpath2file(const char *file_path, const mode_t mode)
Packit Service 20376f
{
Packit Service 20376f
	return git_futils_mkdir(
Packit Service 20376f
		file_path, mode,
Packit Service 20376f
		GIT_MKDIR_PATH | GIT_MKDIR_SKIP_LAST | GIT_MKDIR_VERIFY_DIR);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_futils_mktmp(git_buf *path_out, const char *filename, mode_t mode)
Packit Service 20376f
{
Packit Service 20376f
	int fd;
Packit Service 20376f
	mode_t mask;
Packit Service 20376f
Packit Service 20376f
	p_umask(mask = p_umask(0));
Packit Service 20376f
Packit Service 20376f
	git_buf_sets(path_out, filename);
Packit Service 20376f
	git_buf_puts(path_out, "_git2_XXXXXX");
Packit Service 20376f
Packit Service 20376f
	if (git_buf_oom(path_out))
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	if ((fd = p_mkstemp(path_out->ptr)) < 0) {
Packit Service 20376f
		giterr_set(GITERR_OS,
Packit Service 20376f
			"failed to create temporary file '%s'", path_out->ptr);
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (p_chmod(path_out->ptr, (mode & ~mask))) {
Packit Service 20376f
		giterr_set(GITERR_OS,
Packit Service 20376f
			"failed to set permissions on file '%s'", path_out->ptr);
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return fd;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_futils_creat_withpath(const char *path, const mode_t dirmode, const mode_t mode)
Packit Service 20376f
{
Packit Service 20376f
	int fd;
Packit Service 20376f
Packit Service 20376f
	if (git_futils_mkpath2file(path, dirmode) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	fd = p_creat(path, mode);
Packit Service 20376f
	if (fd < 0) {
Packit Service 20376f
		giterr_set(GITERR_OS, "failed to create file '%s'", path);
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return fd;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_futils_creat_locked(const char *path, const mode_t mode)
Packit Service 20376f
{
Packit Service 20376f
	int fd = p_open(path, O_WRONLY | O_CREAT | O_EXCL | O_BINARY | O_CLOEXEC,
Packit Service 20376f
		mode);
Packit Service 20376f
Packit Service 20376f
	if (fd < 0) {
Packit Service 20376f
		int error = errno;
Packit Service 20376f
		giterr_set(GITERR_OS, "failed to create locked file '%s'", path);
Packit Service 20376f
		switch (error) {
Packit Service 20376f
		case EEXIST:
Packit Service 20376f
			return GIT_ELOCKED;
Packit Service 20376f
		case ENOENT:
Packit Service 20376f
			return GIT_ENOTFOUND;
Packit Service 20376f
		default:
Packit Service 20376f
			return -1;
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return fd;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_futils_creat_locked_withpath(const char *path, const mode_t dirmode, const mode_t mode)
Packit Service 20376f
{
Packit Service 20376f
	if (git_futils_mkpath2file(path, dirmode) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	return git_futils_creat_locked(path, mode);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_futils_open_ro(const char *path)
Packit Service 20376f
{
Packit Service 20376f
	int fd = p_open(path, O_RDONLY);
Packit Service 20376f
	if (fd < 0)
Packit Service 20376f
		return git_path_set_error(errno, path, "open");
Packit Service 20376f
	return fd;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
git_off_t git_futils_filesize(git_file fd)
Packit Service 20376f
{
Packit Service 20376f
	struct stat sb;
Packit Service 20376f
Packit Service 20376f
	if (p_fstat(fd, &sb)) {
Packit Service 20376f
		giterr_set(GITERR_OS, "failed to stat file descriptor");
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return sb.st_size;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
mode_t git_futils_canonical_mode(mode_t raw_mode)
Packit Service 20376f
{
Packit Service 20376f
	if (S_ISREG(raw_mode))
Packit Service 20376f
		return S_IFREG | GIT_PERMS_CANONICAL(raw_mode);
Packit Service 20376f
	else if (S_ISLNK(raw_mode))
Packit Service 20376f
		return S_IFLNK;
Packit Service 20376f
	else if (S_ISGITLINK(raw_mode))
Packit Service 20376f
		return S_IFGITLINK;
Packit Service 20376f
	else if (S_ISDIR(raw_mode))
Packit Service 20376f
		return S_IFDIR;
Packit Service 20376f
	else
Packit Service 20376f
		return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_futils_readbuffer_fd(git_buf *buf, git_file fd, size_t len)
Packit Service 20376f
{
Packit Service 20376f
	ssize_t read_size = 0;
Packit Service 20376f
	size_t alloc_len;
Packit Service 20376f
Packit Service 20376f
	git_buf_clear(buf);
Packit Service 20376f
Packit Service 20376f
	if (!git__is_ssizet(len)) {
Packit Service 20376f
		giterr_set(GITERR_INVALID, "read too large");
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	GITERR_CHECK_ALLOC_ADD(&alloc_len, len, 1);
Packit Service 20376f
	if (git_buf_grow(buf, alloc_len) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	/* p_read loops internally to read len bytes */
Packit Service 20376f
	read_size = p_read(fd, buf->ptr, len);
Packit Service 20376f
Packit Service 20376f
	if (read_size != (ssize_t)len) {
Packit Service 20376f
		giterr_set(GITERR_OS, "failed to read descriptor");
Packit Service 20376f
		git_buf_free(buf);
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	buf->ptr[read_size] = '\0';
Packit Service 20376f
	buf->size = read_size;
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_futils_readbuffer_updated(
Packit Service 20376f
	git_buf *out, const char *path, git_oid *checksum, int *updated)
Packit Service 20376f
{
Packit Service 20376f
	int error;
Packit Service 20376f
	git_file fd;
Packit Service 20376f
	struct stat st;
Packit Service 20376f
	git_buf buf = GIT_BUF_INIT;
Packit Service 20376f
	git_oid checksum_new;
Packit Service 20376f
Packit Service 20376f
	assert(out && path && *path);
Packit Service 20376f
Packit Service 20376f
	if (updated != NULL)
Packit Service 20376f
		*updated = 0;
Packit Service 20376f
Packit Service 20376f
	if (p_stat(path, &st) < 0)
Packit Service 20376f
		return git_path_set_error(errno, path, "stat");
Packit Service 20376f
Packit Service 20376f
Packit Service 20376f
	if (S_ISDIR(st.st_mode)) {
Packit Service 20376f
		giterr_set(GITERR_INVALID, "requested file is a directory");
Packit Service 20376f
		return GIT_ENOTFOUND;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (!git__is_sizet(st.st_size+1)) {
Packit Service 20376f
		giterr_set(GITERR_OS, "invalid regular file stat for '%s'", path);
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if ((fd = git_futils_open_ro(path)) < 0)
Packit Service 20376f
		return fd;
Packit Service 20376f
Packit Service 20376f
	if (git_futils_readbuffer_fd(&buf, fd, (size_t)st.st_size) < 0) {
Packit Service 20376f
		p_close(fd);
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	p_close(fd);
Packit Service 20376f
Packit Service 20376f
	if (checksum) {
Packit Service 20376f
		if ((error = git_hash_buf(&checksum_new, buf.ptr, buf.size)) < 0) {
Packit Service 20376f
			git_buf_free(&buf;;
Packit Service 20376f
			return error;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		/*
Packit Service 20376f
		 * If we were given a checksum, we only want to use it if it's different
Packit Service 20376f
		 */
Packit Service 20376f
		if (!git_oid__cmp(checksum, &checksum_new)) {
Packit Service 20376f
			git_buf_free(&buf;;
Packit Service 20376f
			if (updated)
Packit Service 20376f
				*updated = 0;
Packit Service 20376f
Packit Service 20376f
			return 0;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		git_oid_cpy(checksum, &checksum_new);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/*
Packit Service 20376f
	 * If we're here, the file did change, or the user didn't have an old version
Packit Service 20376f
	 */
Packit Service 20376f
	if (updated != NULL)
Packit Service 20376f
		*updated = 1;
Packit Service 20376f
Packit Service 20376f
	git_buf_swap(out, &buf;;
Packit Service 20376f
	git_buf_free(&buf;;
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_futils_readbuffer(git_buf *buf, const char *path)
Packit Service 20376f
{
Packit Service 20376f
	return git_futils_readbuffer_updated(buf, path, NULL, NULL);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_futils_writebuffer(
Packit Service 20376f
	const git_buf *buf,	const char *path, int flags, mode_t mode)
Packit Service 20376f
{
Packit Service 20376f
	int fd, do_fsync = 0, error = 0;
Packit Service 20376f
Packit Service 20376f
	if (!flags)
Packit Service 20376f
		flags = O_CREAT | O_TRUNC | O_WRONLY;
Packit Service 20376f
Packit Service 20376f
	if ((flags & O_FSYNC) != 0)
Packit Service 20376f
		do_fsync = 1;
Packit Service 20376f
Packit Service 20376f
	flags &= ~O_FSYNC;
Packit Service 20376f
Packit Service 20376f
	if (!mode)
Packit Service 20376f
		mode = GIT_FILEMODE_BLOB;
Packit Service 20376f
Packit Service 20376f
	if ((fd = p_open(path, flags, mode)) < 0) {
Packit Service 20376f
		giterr_set(GITERR_OS, "could not open '%s' for writing", path);
Packit Service 20376f
		return fd;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if ((error = p_write(fd, git_buf_cstr(buf), git_buf_len(buf))) < 0) {
Packit Service 20376f
		giterr_set(GITERR_OS, "could not write to '%s'", path);
Packit Service 20376f
		(void)p_close(fd);
Packit Service 20376f
		return error;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (do_fsync && (error = p_fsync(fd)) < 0) {
Packit Service 20376f
		giterr_set(GITERR_OS, "could not fsync '%s'", path);
Packit Service 20376f
		p_close(fd);
Packit Service 20376f
		return error;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if ((error = p_close(fd)) < 0) {
Packit Service 20376f
		giterr_set(GITERR_OS, "error while closing '%s'", path);
Packit Service 20376f
		return error;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (do_fsync && (flags & O_CREAT))
Packit Service 20376f
		error = git_futils_fsync_parent(path);
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_futils_mv_withpath(const char *from, const char *to, const mode_t dirmode)
Packit Service 20376f
{
Packit Service 20376f
	if (git_futils_mkpath2file(to, dirmode) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	if (p_rename(from, to) < 0) {
Packit Service 20376f
		giterr_set(GITERR_OS, "failed to rename '%s' to '%s'", from, to);
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_futils_mmap_ro(git_map *out, git_file fd, git_off_t begin, size_t len)
Packit Service 20376f
{
Packit Service 20376f
	return p_mmap(out, len, GIT_PROT_READ, GIT_MAP_SHARED, fd, begin);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_futils_mmap_ro_file(git_map *out, const char *path)
Packit Service 20376f
{
Packit Service 20376f
	git_file fd = git_futils_open_ro(path);
Packit Service 20376f
	git_off_t len;
Packit Service 20376f
	int result;
Packit Service 20376f
Packit Service 20376f
	if (fd < 0)
Packit Service 20376f
		return fd;
Packit Service 20376f
Packit Service 20376f
	if ((len = git_futils_filesize(fd)) < 0) {
Packit Service 20376f
		result = -1;
Packit Service 20376f
		goto out;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (!git__is_sizet(len)) {
Packit Service 20376f
		giterr_set(GITERR_OS, "file `%s` too large to mmap", path);
Packit Service 20376f
		result = -1;
Packit Service 20376f
		goto out;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	result = git_futils_mmap_ro(out, fd, 0, (size_t)len);
Packit Service 20376f
out:
Packit Service 20376f
	p_close(fd);
Packit Service 20376f
	return result;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void git_futils_mmap_free(git_map *out)
Packit Service 20376f
{
Packit Service 20376f
	p_munmap(out);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
GIT_INLINE(int) mkdir_validate_dir(
Packit Service 20376f
	const char *path,
Packit Service 20376f
	struct stat *st,
Packit Service 20376f
	mode_t mode,
Packit Service 20376f
	uint32_t flags,
Packit Service 20376f
	struct git_futils_mkdir_options *opts)
Packit Service 20376f
{
Packit Service 20376f
	/* with exclusive create, existing dir is an error */
Packit Service 20376f
	if ((flags & GIT_MKDIR_EXCL) != 0) {
Packit Service 20376f
		giterr_set(GITERR_FILESYSTEM,
Packit Service 20376f
			"failed to make directory '%s': directory exists", path);
Packit Service 20376f
		return GIT_EEXISTS;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if ((S_ISREG(st->st_mode) && (flags & GIT_MKDIR_REMOVE_FILES)) ||
Packit Service 20376f
		(S_ISLNK(st->st_mode) && (flags & GIT_MKDIR_REMOVE_SYMLINKS))) {
Packit Service 20376f
		if (p_unlink(path) < 0) {
Packit Service 20376f
			giterr_set(GITERR_OS, "failed to remove %s '%s'",
Packit Service 20376f
				S_ISLNK(st->st_mode) ? "symlink" : "file", path);
Packit Service 20376f
			return GIT_EEXISTS;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		opts->perfdata.mkdir_calls++;
Packit Service 20376f
Packit Service 20376f
		if (p_mkdir(path, mode) < 0) {
Packit Service 20376f
			giterr_set(GITERR_OS, "failed to make directory '%s'", path);
Packit Service 20376f
			return GIT_EEXISTS;
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	else if (S_ISLNK(st->st_mode)) {
Packit Service 20376f
		/* Re-stat the target, make sure it's a directory */
Packit Service 20376f
		opts->perfdata.stat_calls++;
Packit Service 20376f
Packit Service 20376f
		if (p_stat(path, st) < 0) {
Packit Service 20376f
			giterr_set(GITERR_OS, "failed to make directory '%s'", path);
Packit Service 20376f
			return GIT_EEXISTS;
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	else if (!S_ISDIR(st->st_mode)) {
Packit Service 20376f
		giterr_set(GITERR_FILESYSTEM,
Packit Service 20376f
			"failed to make directory '%s': directory exists", path);
Packit Service 20376f
		return GIT_EEXISTS;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
GIT_INLINE(int) mkdir_validate_mode(
Packit Service 20376f
	const char *path,
Packit Service 20376f
	struct stat *st,
Packit Service 20376f
	bool terminal_path,
Packit Service 20376f
	mode_t mode,
Packit Service 20376f
	uint32_t flags,
Packit Service 20376f
	struct git_futils_mkdir_options *opts)
Packit Service 20376f
{
Packit Service 20376f
	if (((terminal_path && (flags & GIT_MKDIR_CHMOD) != 0) ||
Packit Service 20376f
		(flags & GIT_MKDIR_CHMOD_PATH) != 0) && st->st_mode != mode) {
Packit Service 20376f
Packit Service 20376f
		opts->perfdata.chmod_calls++;
Packit Service 20376f
Packit Service 20376f
		if (p_chmod(path, mode) < 0) {
Packit Service 20376f
			giterr_set(GITERR_OS, "failed to set permissions on '%s'", path);
Packit Service 20376f
			return -1;
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
GIT_INLINE(int) mkdir_canonicalize(
Packit Service 20376f
	git_buf *path,
Packit Service 20376f
	uint32_t flags)
Packit Service 20376f
{
Packit Service 20376f
	ssize_t root_len;
Packit Service 20376f
Packit Service 20376f
	if (path->size == 0) {
Packit Service 20376f
		giterr_set(GITERR_OS, "attempt to create empty path");
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/* Trim trailing slashes (except the root) */
Packit Service 20376f
	if ((root_len = git_path_root(path->ptr)) < 0)
Packit Service 20376f
		root_len = 0;
Packit Service 20376f
	else
Packit Service 20376f
		root_len++;
Packit Service 20376f
Packit Service 20376f
	while (path->size > (size_t)root_len && path->ptr[path->size - 1] == '/')
Packit Service 20376f
		path->ptr[--path->size] = '\0';
Packit Service 20376f
Packit Service 20376f
	/* if we are not supposed to made the last element, truncate it */
Packit Service 20376f
	if ((flags & GIT_MKDIR_SKIP_LAST2) != 0) {
Packit Service 20376f
		git_path_dirname_r(path, path->ptr);
Packit Service 20376f
		flags |= GIT_MKDIR_SKIP_LAST;
Packit Service 20376f
	}
Packit Service 20376f
	if ((flags & GIT_MKDIR_SKIP_LAST) != 0) {
Packit Service 20376f
		git_path_dirname_r(path, path->ptr);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/* We were either given the root path (or trimmed it to
Packit Service 20376f
	* the root), we don't have anything to do.
Packit Service 20376f
	*/
Packit Service 20376f
	if (path->size <= (size_t)root_len)
Packit Service 20376f
		git_buf_clear(path);
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_futils_mkdir(
Packit Service 20376f
	const char *path,
Packit Service 20376f
	mode_t mode,
Packit Service 20376f
	uint32_t flags)
Packit Service 20376f
{
Packit Service 20376f
	git_buf make_path = GIT_BUF_INIT, parent_path = GIT_BUF_INIT;
Packit Service 20376f
	const char *relative;
Packit Service 20376f
	struct git_futils_mkdir_options opts = { 0 };
Packit Service 20376f
	struct stat st;
Packit Service 20376f
	size_t depth = 0;
Packit Service 20376f
	int len = 0, root_len, error;
Packit Service 20376f
Packit Service 20376f
	if ((error = git_buf_puts(&make_path, path)) < 0 ||
Packit Service 20376f
		(error = mkdir_canonicalize(&make_path, flags)) < 0 ||
Packit Service 20376f
		(error = git_buf_puts(&parent_path, make_path.ptr)) < 0 ||
Packit Service 20376f
		make_path.size == 0)
Packit Service 20376f
		goto done;
Packit Service 20376f
Packit Service 20376f
	root_len = git_path_root(make_path.ptr);
Packit Service 20376f
Packit Service 20376f
	/* find the first parent directory that exists.  this will be used
Packit Service 20376f
	 * as the base to dirname_relative.
Packit Service 20376f
	 */
Packit Service 20376f
	for (relative = make_path.ptr; parent_path.size; ) {
Packit Service 20376f
		error = p_lstat(parent_path.ptr, &st);
Packit Service 20376f
Packit Service 20376f
		if (error == 0) {
Packit Service 20376f
			break;
Packit Service 20376f
		} else if (errno != ENOENT) {
Packit Service 20376f
			giterr_set(GITERR_OS, "failed to stat '%s'", parent_path.ptr);
Packit Service 20376f
			goto done;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		depth++;
Packit Service 20376f
Packit Service 20376f
		/* examine the parent of the current path */
Packit Service 20376f
		if ((len = git_path_dirname_r(&parent_path, parent_path.ptr)) < 0) {
Packit Service 20376f
			error = len;
Packit Service 20376f
			goto done;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		assert(len);
Packit Service 20376f
Packit Service 20376f
		/* we've walked all the given path's parents and it's either relative
Packit Service 20376f
		 * or rooted.  either way, give up and make the entire path.
Packit Service 20376f
		 */
Packit Service 20376f
		if ((len == 1 && parent_path.ptr[0] == '.') || len == root_len+1) {
Packit Service 20376f
			relative = make_path.ptr;
Packit Service 20376f
			break;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		relative = make_path.ptr + len + 1;
Packit Service 20376f
Packit Service 20376f
		/* not recursive? just make this directory relative to its parent. */
Packit Service 20376f
		if ((flags & GIT_MKDIR_PATH) == 0)
Packit Service 20376f
			break;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/* we found an item at the location we're trying to create,
Packit Service 20376f
	 * validate it.
Packit Service 20376f
	 */
Packit Service 20376f
	if (depth == 0) {
Packit Service 20376f
		error = mkdir_validate_dir(make_path.ptr, &st, mode, flags, &opts);
Packit Service 20376f
Packit Service 20376f
		if (!error)
Packit Service 20376f
			error = mkdir_validate_mode(
Packit Service 20376f
				make_path.ptr, &st, true, mode, flags, &opts);
Packit Service 20376f
Packit Service 20376f
		goto done;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/* we already took `SKIP_LAST` and `SKIP_LAST2` into account when
Packit Service 20376f
	 * canonicalizing `make_path`.
Packit Service 20376f
	 */
Packit Service 20376f
	flags &= ~(GIT_MKDIR_SKIP_LAST2 | GIT_MKDIR_SKIP_LAST);
Packit Service 20376f
Packit Service 20376f
	error = git_futils_mkdir_relative(relative,
Packit Service 20376f
		parent_path.size ? parent_path.ptr : NULL, mode, flags, &opts);
Packit Service 20376f
Packit Service 20376f
done:
Packit Service 20376f
	git_buf_free(&make_path);
Packit Service 20376f
	git_buf_free(&parent_path);
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_futils_mkdir_r(const char *path, const mode_t mode)
Packit Service 20376f
{
Packit Service 20376f
	return git_futils_mkdir(path, mode, GIT_MKDIR_PATH);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_futils_mkdir_relative(
Packit Service 20376f
	const char *relative_path,
Packit Service 20376f
	const char *base,
Packit Service 20376f
	mode_t mode,
Packit Service 20376f
	uint32_t flags,
Packit Service 20376f
	struct git_futils_mkdir_options *opts)
Packit Service 20376f
{
Packit Service 20376f
	git_buf make_path = GIT_BUF_INIT;
Packit Service 20376f
	ssize_t root = 0, min_root_len;
Packit Service 20376f
	char lastch = '/', *tail;
Packit Service 20376f
	struct stat st;
Packit Service 20376f
	struct git_futils_mkdir_options empty_opts = {0};
Packit Service 20376f
	int error;
Packit Service 20376f
Packit Service 20376f
	if (!opts)
Packit Service 20376f
		opts = &empty_opts;
Packit Service 20376f
Packit Service 20376f
	/* build path and find "root" where we should start calling mkdir */
Packit Service 20376f
	if (git_path_join_unrooted(&make_path, relative_path, base, &root) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	if ((error = mkdir_canonicalize(&make_path, flags)) < 0 ||
Packit Service 20376f
		make_path.size == 0)
Packit Service 20376f
		goto done;
Packit Service 20376f
Packit Service 20376f
	/* if we are not supposed to make the whole path, reset root */
Packit Service 20376f
	if ((flags & GIT_MKDIR_PATH) == 0)
Packit Service 20376f
		root = git_buf_rfind(&make_path, '/');
Packit Service 20376f
Packit Service 20376f
	/* advance root past drive name or network mount prefix */
Packit Service 20376f
	min_root_len = git_path_root(make_path.ptr);
Packit Service 20376f
	if (root < min_root_len)
Packit Service 20376f
		root = min_root_len;
Packit Service 20376f
	while (root >= 0 && make_path.ptr[root] == '/')
Packit Service 20376f
		++root;
Packit Service 20376f
Packit Service 20376f
	/* clip root to make_path length */
Packit Service 20376f
	if (root > (ssize_t)make_path.size)
Packit Service 20376f
		root = (ssize_t)make_path.size; /* i.e. NUL byte of string */
Packit Service 20376f
	if (root < 0)
Packit Service 20376f
		root = 0;
Packit Service 20376f
Packit Service 20376f
	/* walk down tail of path making each directory */
Packit Service 20376f
	for (tail = &make_path.ptr[root]; *tail; *tail = lastch) {
Packit Service 20376f
		bool mkdir_attempted = false;
Packit Service 20376f
Packit Service 20376f
		/* advance tail to include next path component */
Packit Service 20376f
		while (*tail == '/')
Packit Service 20376f
			tail++;
Packit Service 20376f
		while (*tail && *tail != '/')
Packit Service 20376f
			tail++;
Packit Service 20376f
Packit Service 20376f
		/* truncate path at next component */
Packit Service 20376f
		lastch = *tail;
Packit Service 20376f
		*tail = '\0';
Packit Service 20376f
		st.st_mode = 0;
Packit Service 20376f
Packit Service 20376f
		if (opts->dir_map && git_strmap_exists(opts->dir_map, make_path.ptr))
Packit Service 20376f
			continue;
Packit Service 20376f
Packit Service 20376f
		/* See what's going on with this path component */
Packit Service 20376f
		opts->perfdata.stat_calls++;
Packit Service 20376f
Packit Service 20376f
retry_lstat:
Packit Service 20376f
		if (p_lstat(make_path.ptr, &st) < 0) {
Packit Service 20376f
			if (mkdir_attempted || errno != ENOENT) {
Packit Service 20376f
				giterr_set(GITERR_OS, "cannot access component in path '%s'", make_path.ptr);
Packit Service 20376f
				error = -1;
Packit Service 20376f
				goto done;
Packit Service 20376f
			}
Packit Service 20376f
Packit Service 20376f
			giterr_clear();
Packit Service 20376f
			opts->perfdata.mkdir_calls++;
Packit Service 20376f
			mkdir_attempted = true;
Packit Service 20376f
			if (p_mkdir(make_path.ptr, mode) < 0) {
Packit Service 20376f
				if (errno == EEXIST)
Packit Service 20376f
					goto retry_lstat;
Packit Service 20376f
				giterr_set(GITERR_OS, "failed to make directory '%s'", make_path.ptr);
Packit Service 20376f
				error = -1;
Packit Service 20376f
				goto done;
Packit Service 20376f
			}
Packit Service 20376f
		} else {
Packit Service 20376f
			if ((error = mkdir_validate_dir(
Packit Service 20376f
				make_path.ptr, &st, mode, flags, opts)) < 0)
Packit Service 20376f
				goto done;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		/* chmod if requested and necessary */
Packit Service 20376f
		if ((error = mkdir_validate_mode(
Packit Service 20376f
			make_path.ptr, &st, (lastch == '\0'), mode, flags, opts)) < 0)
Packit Service 20376f
			goto done;
Packit Service 20376f
Packit Service 20376f
		if (opts->dir_map && opts->pool) {
Packit Service 20376f
			char *cache_path;
Packit Service 20376f
			size_t alloc_size;
Packit Service 20376f
Packit Service 20376f
			GITERR_CHECK_ALLOC_ADD(&alloc_size, make_path.size, 1);
Packit Service 20376f
			if (!git__is_uint32(alloc_size))
Packit Service 20376f
				return -1;
Packit Service 20376f
			cache_path = git_pool_malloc(opts->pool, (uint32_t)alloc_size);
Packit Service 20376f
			GITERR_CHECK_ALLOC(cache_path);
Packit Service 20376f
Packit Service 20376f
			memcpy(cache_path, make_path.ptr, make_path.size + 1);
Packit Service 20376f
Packit Service 20376f
			git_strmap_insert(opts->dir_map, cache_path, cache_path, &error);
Packit Service 20376f
			if (error < 0)
Packit Service 20376f
				goto done;
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	error = 0;
Packit Service 20376f
Packit Service 20376f
	/* check that full path really is a directory if requested & needed */
Packit Service 20376f
	if ((flags & GIT_MKDIR_VERIFY_DIR) != 0 &&
Packit Service 20376f
		lastch != '\0') {
Packit Service 20376f
		opts->perfdata.stat_calls++;
Packit Service 20376f
Packit Service 20376f
		if (p_stat(make_path.ptr, &st) < 0 || !S_ISDIR(st.st_mode)) {
Packit Service 20376f
			giterr_set(GITERR_OS, "path is not a directory '%s'",
Packit Service 20376f
				make_path.ptr);
Packit Service 20376f
			error = GIT_ENOTFOUND;
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
done:
Packit Service 20376f
	git_buf_free(&make_path);
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
typedef struct {
Packit Service 20376f
	const char *base;
Packit Service 20376f
	size_t baselen;
Packit Service 20376f
	uint32_t flags;
Packit Service 20376f
	int depth;
Packit Service 20376f
} futils__rmdir_data;
Packit Service 20376f
Packit Service 20376f
#define FUTILS_MAX_DEPTH 100
Packit Service 20376f
Packit Service 20376f
static int futils__error_cannot_rmdir(const char *path, const char *filemsg)
Packit Service 20376f
{
Packit Service 20376f
	if (filemsg)
Packit Service 20376f
		giterr_set(GITERR_OS, "could not remove directory '%s': %s",
Packit Service 20376f
				   path, filemsg);
Packit Service 20376f
	else
Packit Service 20376f
		giterr_set(GITERR_OS, "could not remove directory '%s'", path);
Packit Service 20376f
Packit Service 20376f
	return -1;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int futils__rm_first_parent(git_buf *path, const char *ceiling)
Packit Service 20376f
{
Packit Service 20376f
	int error = GIT_ENOTFOUND;
Packit Service 20376f
	struct stat st;
Packit Service 20376f
Packit Service 20376f
	while (error == GIT_ENOTFOUND) {
Packit Service 20376f
		git_buf_rtruncate_at_char(path, '/');
Packit Service 20376f
Packit Service 20376f
		if (!path->size || git__prefixcmp(path->ptr, ceiling) != 0)
Packit Service 20376f
			error = 0;
Packit Service 20376f
		else if (p_lstat_posixly(path->ptr, &st) == 0) {
Packit Service 20376f
			if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode))
Packit Service 20376f
				error = p_unlink(path->ptr);
Packit Service 20376f
			else if (!S_ISDIR(st.st_mode))
Packit Service 20376f
				error = -1; /* fail to remove non-regular file */
Packit Service 20376f
		} else if (errno != ENOTDIR)
Packit Service 20376f
			error = -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (error)
Packit Service 20376f
		futils__error_cannot_rmdir(path->ptr, "cannot remove parent");
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int futils__rmdir_recurs_foreach(void *opaque, git_buf *path)
Packit Service 20376f
{
Packit Service 20376f
	int error = 0;
Packit Service 20376f
	futils__rmdir_data *data = opaque;
Packit Service 20376f
	struct stat st;
Packit Service 20376f
Packit Service 20376f
	if (data->depth > FUTILS_MAX_DEPTH)
Packit Service 20376f
		error = futils__error_cannot_rmdir(
Packit Service 20376f
			path->ptr, "directory nesting too deep");
Packit Service 20376f
Packit Service 20376f
	else if ((error = p_lstat_posixly(path->ptr, &st)) < 0) {
Packit Service 20376f
		if (errno == ENOENT)
Packit Service 20376f
			error = 0;
Packit Service 20376f
		else if (errno == ENOTDIR) {
Packit Service 20376f
			/* asked to remove a/b/c/d/e and a/b is a normal file */
Packit Service 20376f
			if ((data->flags & GIT_RMDIR_REMOVE_BLOCKERS) != 0)
Packit Service 20376f
				error = futils__rm_first_parent(path, data->base);
Packit Service 20376f
			else
Packit Service 20376f
				futils__error_cannot_rmdir(
Packit Service 20376f
					path->ptr, "parent is not directory");
Packit Service 20376f
		}
Packit Service 20376f
		else
Packit Service 20376f
			error = git_path_set_error(errno, path->ptr, "rmdir");
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	else if (S_ISDIR(st.st_mode)) {
Packit Service 20376f
		data->depth++;
Packit Service 20376f
Packit Service 20376f
		error = git_path_direach(path, 0, futils__rmdir_recurs_foreach, data);
Packit Service 20376f
Packit Service 20376f
		data->depth--;
Packit Service 20376f
Packit Service 20376f
		if (error < 0)
Packit Service 20376f
			return error;
Packit Service 20376f
Packit Service 20376f
		if (data->depth == 0 && (data->flags & GIT_RMDIR_SKIP_ROOT) != 0)
Packit Service 20376f
			return error;
Packit Service 20376f
Packit Service 20376f
		if ((error = p_rmdir(path->ptr)) < 0) {
Packit Service 20376f
			if ((data->flags & GIT_RMDIR_SKIP_NONEMPTY) != 0 &&
Packit Service 20376f
				(errno == ENOTEMPTY || errno == EEXIST || errno == EBUSY))
Packit Service 20376f
				error = 0;
Packit Service 20376f
			else
Packit Service 20376f
				error = git_path_set_error(errno, path->ptr, "rmdir");
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	else if ((data->flags & GIT_RMDIR_REMOVE_FILES) != 0) {
Packit Service 20376f
		if (p_unlink(path->ptr) < 0)
Packit Service 20376f
			error = git_path_set_error(errno, path->ptr, "remove");
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	else if ((data->flags & GIT_RMDIR_SKIP_NONEMPTY) == 0)
Packit Service 20376f
		error = futils__error_cannot_rmdir(path->ptr, "still present");
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int futils__rmdir_empty_parent(void *opaque, const char *path)
Packit Service 20376f
{
Packit Service 20376f
	futils__rmdir_data *data = opaque;
Packit Service 20376f
	int error = 0;
Packit Service 20376f
Packit Service 20376f
	if (strlen(path) <= data->baselen)
Packit Service 20376f
		error = GIT_ITEROVER;
Packit Service 20376f
Packit Service 20376f
	else if (p_rmdir(path) < 0) {
Packit Service 20376f
		int en = errno;
Packit Service 20376f
Packit Service 20376f
		if (en == ENOENT || en == ENOTDIR) {
Packit Service 20376f
			/* do nothing */
Packit Service 20376f
		} else if ((data->flags & GIT_RMDIR_SKIP_NONEMPTY) == 0 &&
Packit Service 20376f
			en == EBUSY) {
Packit Service 20376f
			error = git_path_set_error(errno, path, "rmdir");
Packit Service 20376f
		} else if (en == ENOTEMPTY || en == EEXIST || en == EBUSY) {
Packit Service 20376f
			error = GIT_ITEROVER;
Packit Service 20376f
		} else {
Packit Service 20376f
			error = git_path_set_error(errno, path, "rmdir");
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_futils_rmdir_r(
Packit Service 20376f
	const char *path, const char *base, uint32_t flags)
Packit Service 20376f
{
Packit Service 20376f
	int error;
Packit Service 20376f
	git_buf fullpath = GIT_BUF_INIT;
Packit Service 20376f
	futils__rmdir_data data;
Packit Service 20376f
Packit Service 20376f
	/* build path and find "root" where we should start calling mkdir */
Packit Service 20376f
	if (git_path_join_unrooted(&fullpath, path, base, NULL) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	memset(&data, 0, sizeof(data));
Packit Service 20376f
	data.base    = base ? base : "";
Packit Service 20376f
	data.baselen = base ? strlen(base) : 0;
Packit Service 20376f
	data.flags   = flags;
Packit Service 20376f
Packit Service 20376f
	error = futils__rmdir_recurs_foreach(&data, &fullpath);
Packit Service 20376f
Packit Service 20376f
	/* remove now-empty parents if requested */
Packit Service 20376f
	if (!error && (flags & GIT_RMDIR_EMPTY_PARENTS) != 0)
Packit Service 20376f
		error = git_path_walk_up(
Packit Service 20376f
			&fullpath, base, futils__rmdir_empty_parent, &data);
Packit Service 20376f
Packit Service 20376f
	if (error == GIT_ITEROVER) {
Packit Service 20376f
		giterr_clear();
Packit Service 20376f
		error = 0;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	git_buf_free(&fullpath);
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_futils_fake_symlink(const char *old, const char *new)
Packit Service 20376f
{
Packit Service 20376f
	int retcode = GIT_ERROR;
Packit Service 20376f
	int fd = git_futils_creat_withpath(new, 0755, 0644);
Packit Service 20376f
	if (fd >= 0) {
Packit Service 20376f
		retcode = p_write(fd, old, strlen(old));
Packit Service 20376f
		p_close(fd);
Packit Service 20376f
	}
Packit Service 20376f
	return retcode;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int cp_by_fd(int ifd, int ofd, bool close_fd_when_done)
Packit Service 20376f
{
Packit Service 20376f
	int error = 0;
Packit Service 20376f
	char buffer[FILEIO_BUFSIZE];
Packit Service 20376f
	ssize_t len = 0;
Packit Service 20376f
Packit Service 20376f
	while (!error && (len = p_read(ifd, buffer, sizeof(buffer))) > 0)
Packit Service 20376f
		/* p_write() does not have the same semantics as write().  It loops
Packit Service 20376f
		 * internally and will return 0 when it has completed writing.
Packit Service 20376f
		 */
Packit Service 20376f
		error = p_write(ofd, buffer, len);
Packit Service 20376f
Packit Service 20376f
	if (len < 0) {
Packit Service 20376f
		giterr_set(GITERR_OS, "read error while copying file");
Packit Service 20376f
		error = (int)len;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (error < 0)
Packit Service 20376f
		giterr_set(GITERR_OS, "write error while copying file");
Packit Service 20376f
Packit Service 20376f
	if (close_fd_when_done) {
Packit Service 20376f
		p_close(ifd);
Packit Service 20376f
		p_close(ofd);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_futils_cp(const char *from, const char *to, mode_t filemode)
Packit Service 20376f
{
Packit Service 20376f
	int ifd, ofd;
Packit Service 20376f
Packit Service 20376f
	if ((ifd = git_futils_open_ro(from)) < 0)
Packit Service 20376f
		return ifd;
Packit Service 20376f
Packit Service 20376f
	if ((ofd = p_open(to, O_WRONLY | O_CREAT | O_EXCL, filemode)) < 0) {
Packit Service 20376f
		p_close(ifd);
Packit Service 20376f
		return git_path_set_error(errno, to, "open for writing");
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return cp_by_fd(ifd, ofd, true);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_futils_touch(const char *path, time_t *when)
Packit Service 20376f
{
Packit Service 20376f
	struct p_timeval times[2];
Packit Service 20376f
	int ret;
Packit Service 20376f
Packit Service 20376f
	times[0].tv_sec =  times[1].tv_sec  = when ? *when : time(NULL);
Packit Service 20376f
	times[0].tv_usec = times[1].tv_usec = 0;
Packit Service 20376f
Packit Service 20376f
	ret = p_utimes(path, times);
Packit Service 20376f
Packit Service 20376f
	return (ret < 0) ? git_path_set_error(errno, path, "touch") : 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int cp_link(const char *from, const char *to, size_t link_size)
Packit Service 20376f
{
Packit Service 20376f
	int error = 0;
Packit Service 20376f
	ssize_t read_len;
Packit Service 20376f
	char *link_data;
Packit Service 20376f
	size_t alloc_size;
Packit Service 20376f
Packit Service 20376f
	GITERR_CHECK_ALLOC_ADD(&alloc_size, link_size, 1);
Packit Service 20376f
	link_data = git__malloc(alloc_size);
Packit Service 20376f
	GITERR_CHECK_ALLOC(link_data);
Packit Service 20376f
Packit Service 20376f
	read_len = p_readlink(from, link_data, link_size);
Packit Service 20376f
	if (read_len != (ssize_t)link_size) {
Packit Service 20376f
		giterr_set(GITERR_OS, "failed to read symlink data for '%s'", from);
Packit Service 20376f
		error = -1;
Packit Service 20376f
	}
Packit Service 20376f
	else {
Packit Service 20376f
		link_data[read_len] = '\0';
Packit Service 20376f
Packit Service 20376f
		if (p_symlink(link_data, to) < 0) {
Packit Service 20376f
			giterr_set(GITERR_OS, "could not symlink '%s' as '%s'",
Packit Service 20376f
				link_data, to);
Packit Service 20376f
			error = -1;
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	git__free(link_data);
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
typedef struct {
Packit Service 20376f
	const char *to_root;
Packit Service 20376f
	git_buf to;
Packit Service 20376f
	ssize_t from_prefix;
Packit Service 20376f
	uint32_t flags;
Packit Service 20376f
	uint32_t mkdir_flags;
Packit Service 20376f
	mode_t dirmode;
Packit Service 20376f
} cp_r_info;
Packit Service 20376f
Packit Service 20376f
#define GIT_CPDIR__MKDIR_DONE_FOR_TO_ROOT (1u << 10)
Packit Service 20376f
Packit Service 20376f
static int _cp_r_mkdir(cp_r_info *info, git_buf *from)
Packit Service 20376f
{
Packit Service 20376f
	int error = 0;
Packit Service 20376f
Packit Service 20376f
	/* create root directory the first time we need to create a directory */
Packit Service 20376f
	if ((info->flags & GIT_CPDIR__MKDIR_DONE_FOR_TO_ROOT) == 0) {
Packit Service 20376f
		error = git_futils_mkdir(
Packit Service 20376f
			info->to_root, info->dirmode,
Packit Service 20376f
			(info->flags & GIT_CPDIR_CHMOD_DIRS) ? GIT_MKDIR_CHMOD : 0);
Packit Service 20376f
Packit Service 20376f
		info->flags |= GIT_CPDIR__MKDIR_DONE_FOR_TO_ROOT;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/* create directory with root as base to prevent excess chmods */
Packit Service 20376f
	if (!error)
Packit Service 20376f
		error = git_futils_mkdir_relative(
Packit Service 20376f
			from->ptr + info->from_prefix, info->to_root,
Packit Service 20376f
			info->dirmode, info->mkdir_flags, NULL);
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int _cp_r_callback(void *ref, git_buf *from)
Packit Service 20376f
{
Packit Service 20376f
	int error = 0;
Packit Service 20376f
	cp_r_info *info = ref;
Packit Service 20376f
	struct stat from_st, to_st;
Packit Service 20376f
	bool exists = false;
Packit Service 20376f
Packit Service 20376f
	if ((info->flags & GIT_CPDIR_COPY_DOTFILES) == 0 &&
Packit Service 20376f
		from->ptr[git_path_basename_offset(from)] == '.')
Packit Service 20376f
		return 0;
Packit Service 20376f
Packit Service 20376f
	if ((error = git_buf_joinpath(
Packit Service 20376f
			&info->to, info->to_root, from->ptr + info->from_prefix)) < 0)
Packit Service 20376f
		return error;
Packit Service 20376f
Packit Service 20376f
	if (!(error = git_path_lstat(info->to.ptr, &to_st)))
Packit Service 20376f
		exists = true;
Packit Service 20376f
	else if (error != GIT_ENOTFOUND)
Packit Service 20376f
		return error;
Packit Service 20376f
	else {
Packit Service 20376f
		giterr_clear();
Packit Service 20376f
		error = 0;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if ((error = git_path_lstat(from->ptr, &from_st)) < 0)
Packit Service 20376f
		return error;
Packit Service 20376f
Packit Service 20376f
	if (S_ISDIR(from_st.st_mode)) {
Packit Service 20376f
		mode_t oldmode = info->dirmode;
Packit Service 20376f
Packit Service 20376f
		/* if we are not chmod'ing, then overwrite dirmode */
Packit Service 20376f
		if ((info->flags & GIT_CPDIR_CHMOD_DIRS) == 0)
Packit Service 20376f
			info->dirmode = from_st.st_mode;
Packit Service 20376f
Packit Service 20376f
		/* make directory now if CREATE_EMPTY_DIRS is requested and needed */
Packit Service 20376f
		if (!exists && (info->flags & GIT_CPDIR_CREATE_EMPTY_DIRS) != 0)
Packit Service 20376f
			error = _cp_r_mkdir(info, from);
Packit Service 20376f
Packit Service 20376f
		/* recurse onto target directory */
Packit Service 20376f
		if (!error && (!exists || S_ISDIR(to_st.st_mode)))
Packit Service 20376f
			error = git_path_direach(from, 0, _cp_r_callback, info);
Packit Service 20376f
Packit Service 20376f
		if (oldmode != 0)
Packit Service 20376f
			info->dirmode = oldmode;
Packit Service 20376f
Packit Service 20376f
		return error;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (exists) {
Packit Service 20376f
		if ((info->flags & GIT_CPDIR_OVERWRITE) == 0)
Packit Service 20376f
			return 0;
Packit Service 20376f
Packit Service 20376f
		if (p_unlink(info->to.ptr) < 0) {
Packit Service 20376f
			giterr_set(GITERR_OS, "cannot overwrite existing file '%s'",
Packit Service 20376f
				info->to.ptr);
Packit Service 20376f
			return GIT_EEXISTS;
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/* Done if this isn't a regular file or a symlink */
Packit Service 20376f
	if (!S_ISREG(from_st.st_mode) &&
Packit Service 20376f
		(!S_ISLNK(from_st.st_mode) ||
Packit Service 20376f
		 (info->flags & GIT_CPDIR_COPY_SYMLINKS) == 0))
Packit Service 20376f
		return 0;
Packit Service 20376f
Packit Service 20376f
	/* Make container directory on demand if needed */
Packit Service 20376f
	if ((info->flags & GIT_CPDIR_CREATE_EMPTY_DIRS) == 0 &&
Packit Service 20376f
		(error = _cp_r_mkdir(info, from)) < 0)
Packit Service 20376f
		return error;
Packit Service 20376f
Packit Service 20376f
	/* make symlink or regular file */
Packit Service 20376f
	if (info->flags & GIT_CPDIR_LINK_FILES) {
Packit Service 20376f
		if ((error = p_link(from->ptr, info->to.ptr)) < 0)
Packit Service 20376f
			giterr_set(GITERR_OS, "failed to link '%s'", from->ptr);
Packit Service 20376f
	} else if (S_ISLNK(from_st.st_mode)) {
Packit Service 20376f
		error = cp_link(from->ptr, info->to.ptr, (size_t)from_st.st_size);
Packit Service 20376f
	} else {
Packit Service 20376f
		mode_t usemode = from_st.st_mode;
Packit Service 20376f
Packit Service 20376f
		if ((info->flags & GIT_CPDIR_SIMPLE_TO_MODE) != 0)
Packit Service 20376f
			usemode = GIT_PERMS_FOR_WRITE(usemode);
Packit Service 20376f
Packit Service 20376f
		error = git_futils_cp(from->ptr, info->to.ptr, usemode);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_futils_cp_r(
Packit Service 20376f
	const char *from,
Packit Service 20376f
	const char *to,
Packit Service 20376f
	uint32_t flags,
Packit Service 20376f
	mode_t dirmode)
Packit Service 20376f
{
Packit Service 20376f
	int error;
Packit Service 20376f
	git_buf path = GIT_BUF_INIT;
Packit Service 20376f
	cp_r_info info;
Packit Service 20376f
Packit Service 20376f
	if (git_buf_joinpath(&path, from, "") < 0) /* ensure trailing slash */
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	memset(&info, 0, sizeof(info));
Packit Service 20376f
	info.to_root = to;
Packit Service 20376f
	info.flags   = flags;
Packit Service 20376f
	info.dirmode = dirmode;
Packit Service 20376f
	info.from_prefix = path.size;
Packit Service 20376f
	git_buf_init(&info.to, 0);
Packit Service 20376f
Packit Service 20376f
	/* precalculate mkdir flags */
Packit Service 20376f
	if ((flags & GIT_CPDIR_CREATE_EMPTY_DIRS) == 0) {
Packit Service 20376f
		/* if not creating empty dirs, then use mkdir to create the path on
Packit Service 20376f
		 * demand right before files are copied.
Packit Service 20376f
		 */
Packit Service 20376f
		info.mkdir_flags = GIT_MKDIR_PATH | GIT_MKDIR_SKIP_LAST;
Packit Service 20376f
		if ((flags & GIT_CPDIR_CHMOD_DIRS) != 0)
Packit Service 20376f
			info.mkdir_flags |= GIT_MKDIR_CHMOD_PATH;
Packit Service 20376f
	} else {
Packit Service 20376f
		/* otherwise, we will do simple mkdir as directories are encountered */
Packit Service 20376f
		info.mkdir_flags =
Packit Service 20376f
			((flags & GIT_CPDIR_CHMOD_DIRS) != 0) ? GIT_MKDIR_CHMOD : 0;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	error = _cp_r_callback(&info, &path);
Packit Service 20376f
Packit Service 20376f
	git_buf_free(&path);
Packit Service 20376f
	git_buf_free(&info.to);
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_futils_filestamp_check(
Packit Service 20376f
	git_futils_filestamp *stamp, const char *path)
Packit Service 20376f
{
Packit Service 20376f
	struct stat st;
Packit Service 20376f
Packit Service 20376f
	/* if the stamp is NULL, then always reload */
Packit Service 20376f
	if (stamp == NULL)
Packit Service 20376f
		return 1;
Packit Service 20376f
Packit Service 20376f
	if (p_stat(path, &st) < 0)
Packit Service 20376f
		return GIT_ENOTFOUND;
Packit Service 20376f
Packit Service 20376f
	if (stamp->mtime.tv_sec == st.st_mtime &&
Packit Service 20376f
#if defined(GIT_USE_NSEC)
Packit Service 20376f
		stamp->mtime.tv_nsec == st.st_mtime_nsec &&
Packit Service 20376f
#endif
Packit Service 20376f
		stamp->size  == (git_off_t)st.st_size   &&
Packit Service 20376f
		stamp->ino   == (unsigned int)st.st_ino)
Packit Service 20376f
		return 0;
Packit Service 20376f
Packit Service 20376f
	stamp->mtime.tv_sec = st.st_mtime;
Packit Service 20376f
#if defined(GIT_USE_NSEC)
Packit Service 20376f
	stamp->mtime.tv_nsec = st.st_mtime_nsec;
Packit Service 20376f
#endif
Packit Service 20376f
	stamp->size  = (git_off_t)st.st_size;
Packit Service 20376f
	stamp->ino   = (unsigned int)st.st_ino;
Packit Service 20376f
Packit Service 20376f
	return 1;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void git_futils_filestamp_set(
Packit Service 20376f
	git_futils_filestamp *target, const git_futils_filestamp *source)
Packit Service 20376f
{
Packit Service 20376f
	assert(target);
Packit Service 20376f
Packit Service 20376f
	if (source)
Packit Service 20376f
		memcpy(target, source, sizeof(*target));
Packit Service 20376f
	else
Packit Service 20376f
		memset(target, 0, sizeof(*target));
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
Packit Service 20376f
void git_futils_filestamp_set_from_stat(
Packit Service 20376f
	git_futils_filestamp *stamp, struct stat *st)
Packit Service 20376f
{
Packit Service 20376f
	if (st) {
Packit Service 20376f
		stamp->mtime.tv_sec = st->st_mtime;
Packit Service 20376f
#if defined(GIT_USE_NSEC)
Packit Service 20376f
		stamp->mtime.tv_nsec = st->st_mtime_nsec;
Packit Service 20376f
#else
Packit Service 20376f
		stamp->mtime.tv_nsec = 0;
Packit Service 20376f
#endif
Packit Service 20376f
		stamp->size  = (git_off_t)st->st_size;
Packit Service 20376f
		stamp->ino   = (unsigned int)st->st_ino;
Packit Service 20376f
	} else {
Packit Service 20376f
		memset(stamp, 0, sizeof(*stamp));
Packit Service 20376f
	}
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_futils_fsync_dir(const char *path)
Packit Service 20376f
{
Packit Service 20376f
#ifdef GIT_WIN32
Packit Service 20376f
	GIT_UNUSED(path);
Packit Service 20376f
	return 0;
Packit Service 20376f
#else
Packit Service 20376f
	int fd, error = -1;
Packit Service 20376f
Packit Service 20376f
	if ((fd = p_open(path, O_RDONLY)) < 0) {
Packit Service 20376f
		giterr_set(GITERR_OS, "failed to open directory '%s' for fsync", path);
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if ((error = p_fsync(fd)) < 0)
Packit Service 20376f
		giterr_set(GITERR_OS, "failed to fsync directory '%s'", path);
Packit Service 20376f
Packit Service 20376f
	p_close(fd);
Packit Service 20376f
	return error;
Packit Service 20376f
#endif
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_futils_fsync_parent(const char *path)
Packit Service 20376f
{
Packit Service 20376f
	char *parent;
Packit Service 20376f
	int error;
Packit Service 20376f
Packit Service 20376f
	if ((parent = git_path_dirname(path)) == NULL)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	error = git_futils_fsync_dir(parent);
Packit Service 20376f
	git__free(parent);
Packit Service 20376f
	return error;
Packit Service 20376f
}