summaryrefslogtreecommitdiff
path: root/src/futils.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/futils.c')
-rw-r--r--src/futils.c94
1 files changed, 49 insertions, 45 deletions
diff --git a/src/futils.c b/src/futils.c
index a44820875..0032d38b5 100644
--- a/src/futils.c
+++ b/src/futils.c
@@ -22,17 +22,17 @@ int git_futils_mkpath2file(const char *file_path, const mode_t 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)
+int git_futils_mktmp(git_str *path_out, const char *filename, mode_t mode)
{
int fd;
mode_t mask;
p_umask(mask = p_umask(0));
- git_buf_sets(path_out, filename);
- git_buf_puts(path_out, "_git2_XXXXXX");
+ git_str_sets(path_out, filename);
+ git_str_puts(path_out, "_git2_XXXXXX");
- if (git_buf_oom(path_out))
+ if (git_str_oom(path_out))
return -1;
if ((fd = p_mkstemp(path_out->ptr)) < 0) {
@@ -145,12 +145,12 @@ mode_t git_futils_canonical_mode(mode_t raw_mode)
return 0;
}
-int git_futils_readbuffer_fd(git_buf *buf, git_file fd, size_t len)
+int git_futils_readbuffer_fd(git_str *buf, git_file fd, size_t len)
{
ssize_t read_size = 0;
size_t alloc_len;
- git_buf_clear(buf);
+ git_str_clear(buf);
if (!git__is_ssizet(len)) {
git_error_set(GIT_ERROR_INVALID, "read too large");
@@ -158,7 +158,7 @@ int git_futils_readbuffer_fd(git_buf *buf, git_file fd, size_t len)
}
GIT_ERROR_CHECK_ALLOC_ADD(&alloc_len, len, 1);
- if (git_buf_grow(buf, alloc_len) < 0)
+ if (git_str_grow(buf, alloc_len) < 0)
return -1;
/* p_read loops internally to read len bytes */
@@ -166,7 +166,7 @@ int git_futils_readbuffer_fd(git_buf *buf, git_file fd, size_t len)
if (read_size != (ssize_t)len) {
git_error_set(GIT_ERROR_OS, "failed to read descriptor");
- git_buf_dispose(buf);
+ git_str_dispose(buf);
return -1;
}
@@ -177,13 +177,16 @@ int git_futils_readbuffer_fd(git_buf *buf, git_file fd, size_t len)
}
int git_futils_readbuffer_updated(
- git_buf *out, const char *path, git_oid *checksum, int *updated)
+ git_str *out,
+ const char *path,
+ unsigned char checksum[GIT_HASH_SHA1_SIZE],
+ int *updated)
{
int error;
git_file fd;
struct stat st;
- git_buf buf = GIT_BUF_INIT;
- git_oid checksum_new;
+ git_str buf = GIT_STR_INIT;
+ unsigned char checksum_new[GIT_HASH_SHA1_SIZE];
GIT_ASSERT_ARG(out);
GIT_ASSERT_ARG(path && *path);
@@ -216,23 +219,24 @@ int git_futils_readbuffer_updated(
p_close(fd);
if (checksum) {
- if ((error = git_hash_buf(&checksum_new, buf.ptr, buf.size)) < 0) {
- git_buf_dispose(&buf);
+ if ((error = git_hash_buf(checksum_new, buf.ptr, buf.size, GIT_HASH_ALGORITHM_SHA1)) < 0) {
+ git_str_dispose(&buf);
return error;
}
/*
* If we were given a checksum, we only want to use it if it's different
*/
- if (!git_oid__cmp(checksum, &checksum_new)) {
- git_buf_dispose(&buf);
+ if (!memcmp(checksum, checksum_new, GIT_HASH_SHA1_SIZE)) {
+ git_str_dispose(&buf);
+
if (updated)
*updated = 0;
return 0;
}
- git_oid_cpy(checksum, &checksum_new);
+ memcpy(checksum, checksum_new, GIT_HASH_SHA1_SIZE);
}
/*
@@ -241,19 +245,19 @@ int git_futils_readbuffer_updated(
if (updated != NULL)
*updated = 1;
- git_buf_swap(out, &buf);
- git_buf_dispose(&buf);
+ git_str_swap(out, &buf);
+ git_str_dispose(&buf);
return 0;
}
-int git_futils_readbuffer(git_buf *buf, const char *path)
+int git_futils_readbuffer(git_str *buf, const char *path)
{
return git_futils_readbuffer_updated(buf, path, NULL, NULL);
}
int git_futils_writebuffer(
- const git_buf *buf, const char *path, int flags, mode_t mode)
+ const git_str *buf, const char *path, int flags, mode_t mode)
{
int fd, do_fsync = 0, error = 0;
@@ -273,7 +277,7 @@ int git_futils_writebuffer(
return fd;
}
- if ((error = p_write(fd, git_buf_cstr(buf), git_buf_len(buf))) < 0) {
+ if ((error = p_write(fd, git_str_cstr(buf), git_str_len(buf))) < 0) {
git_error_set(GIT_ERROR_OS, "could not write to '%s'", path);
(void)p_close(fd);
return error;
@@ -415,7 +419,7 @@ GIT_INLINE(int) mkdir_validate_mode(
}
GIT_INLINE(int) mkdir_canonicalize(
- git_buf *path,
+ git_str *path,
uint32_t flags)
{
ssize_t root_len;
@@ -447,7 +451,7 @@ GIT_INLINE(int) mkdir_canonicalize(
* the root), we don't have anything to do.
*/
if (path->size <= (size_t)root_len)
- git_buf_clear(path);
+ git_str_clear(path);
return 0;
}
@@ -457,16 +461,16 @@ int git_futils_mkdir(
mode_t mode,
uint32_t flags)
{
- git_buf make_path = GIT_BUF_INIT, parent_path = GIT_BUF_INIT;
+ git_str make_path = GIT_STR_INIT, parent_path = GIT_STR_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 ||
+ if ((error = git_str_puts(&make_path, path)) < 0 ||
(error = mkdir_canonicalize(&make_path, flags)) < 0 ||
- (error = git_buf_puts(&parent_path, make_path.ptr)) < 0 ||
+ (error = git_str_puts(&parent_path, make_path.ptr)) < 0 ||
make_path.size == 0)
goto done;
@@ -538,8 +542,8 @@ int git_futils_mkdir(
parent_path.size ? parent_path.ptr : NULL, mode, flags, &opts);
done:
- git_buf_dispose(&make_path);
- git_buf_dispose(&parent_path);
+ git_str_dispose(&make_path);
+ git_str_dispose(&parent_path);
return error;
}
@@ -555,7 +559,7 @@ int git_futils_mkdir_relative(
uint32_t flags,
struct git_futils_mkdir_options *opts)
{
- git_buf make_path = GIT_BUF_INIT;
+ git_str make_path = GIT_STR_INIT;
ssize_t root = 0, min_root_len;
char lastch = '/', *tail;
struct stat st;
@@ -575,7 +579,7 @@ int git_futils_mkdir_relative(
/* if we are not supposed to make the whole path, reset root */
if ((flags & GIT_MKDIR_PATH) == 0)
- root = git_buf_rfind(&make_path, '/');
+ root = git_str_rfind(&make_path, '/');
/* advance root past drive name or network mount prefix */
min_root_len = git_path_root(make_path.ptr);
@@ -670,7 +674,7 @@ retry_lstat:
}
done:
- git_buf_dispose(&make_path);
+ git_str_dispose(&make_path);
return error;
}
@@ -694,13 +698,13 @@ static int futils__error_cannot_rmdir(const char *path, const char *filemsg)
return -1;
}
-static int futils__rm_first_parent(git_buf *path, const char *ceiling)
+static int futils__rm_first_parent(git_str *path, const char *ceiling)
{
int error = GIT_ENOTFOUND;
struct stat st;
while (error == GIT_ENOTFOUND) {
- git_buf_rtruncate_at_char(path, '/');
+ git_str_rtruncate_at_char(path, '/');
if (!path->size || git__prefixcmp(path->ptr, ceiling) != 0)
error = 0;
@@ -719,7 +723,7 @@ static int futils__rm_first_parent(git_buf *path, const char *ceiling)
return error;
}
-static int futils__rmdir_recurs_foreach(void *opaque, git_buf *path)
+static int futils__rmdir_recurs_foreach(void *opaque, git_str *path)
{
int error = 0;
futils__rmdir_data *data = opaque;
@@ -807,7 +811,7 @@ int git_futils_rmdir_r(
const char *path, const char *base, uint32_t flags)
{
int error;
- git_buf fullpath = GIT_BUF_INIT;
+ git_str fullpath = GIT_STR_INIT;
futils__rmdir_data data;
/* build path and find "root" where we should start calling mkdir */
@@ -831,7 +835,7 @@ int git_futils_rmdir_r(
error = 0;
}
- git_buf_dispose(&fullpath);
+ git_str_dispose(&fullpath);
return error;
}
@@ -935,7 +939,7 @@ static int cp_link(const char *from, const char *to, size_t link_size)
typedef struct {
const char *to_root;
- git_buf to;
+ git_str to;
ssize_t from_prefix;
uint32_t flags;
uint32_t mkdir_flags;
@@ -944,7 +948,7 @@ typedef struct {
#define GIT_CPDIR__MKDIR_DONE_FOR_TO_ROOT (1u << 10)
-static int _cp_r_mkdir(cp_r_info *info, git_buf *from)
+static int _cp_r_mkdir(cp_r_info *info, git_str *from)
{
int error = 0;
@@ -966,7 +970,7 @@ static int _cp_r_mkdir(cp_r_info *info, git_buf *from)
return error;
}
-static int _cp_r_callback(void *ref, git_buf *from)
+static int _cp_r_callback(void *ref, git_str *from)
{
int error = 0;
cp_r_info *info = ref;
@@ -977,7 +981,7 @@ static int _cp_r_callback(void *ref, git_buf *from)
from->ptr[git_path_basename_offset(from)] == '.')
return 0;
- if ((error = git_buf_joinpath(
+ if ((error = git_str_joinpath(
&info->to, info->to_root, from->ptr + info->from_prefix)) < 0)
return error;
@@ -1061,10 +1065,10 @@ int git_futils_cp_r(
mode_t dirmode)
{
int error;
- git_buf path = GIT_BUF_INIT;
+ git_str path = GIT_STR_INIT;
cp_r_info info;
- if (git_buf_joinpath(&path, from, "") < 0) /* ensure trailing slash */
+ if (git_str_joinpath(&path, from, "") < 0) /* ensure trailing slash */
return -1;
memset(&info, 0, sizeof(info));
@@ -1072,7 +1076,7 @@ int git_futils_cp_r(
info.flags = flags;
info.dirmode = dirmode;
info.from_prefix = path.size;
- git_buf_init(&info.to, 0);
+ git_str_init(&info.to, 0);
/* precalculate mkdir flags */
if ((flags & GIT_CPDIR_CREATE_EMPTY_DIRS) == 0) {
@@ -1090,8 +1094,8 @@ int git_futils_cp_r(
error = _cp_r_callback(&info, &path);
- git_buf_dispose(&path);
- git_buf_dispose(&info.to);
+ git_str_dispose(&path);
+ git_str_dispose(&info.to);
return error;
}