summaryrefslogtreecommitdiff
path: root/src/futils.c
diff options
context:
space:
mode:
authorEdward Thomson <ethomson@edwardthomson.com>2021-09-07 17:53:49 -0400
committerEdward Thomson <ethomson@edwardthomson.com>2021-10-17 09:49:01 -0400
commitf0e693b18afbe1de37d7da5b5a8967b6c87d8e53 (patch)
treebe5e1cdbfa218ba81ec06bf45e45cfeb7f79a2a5 /src/futils.c
parent5346be3ddd3bcf19779c5d62e71f8442a0171133 (diff)
downloadlibgit2-ethomson/gitstr.tar.gz
str: introduce `git_str` for internal, `git_buf` is externalethomson/gitstr
libgit2 has two distinct requirements that were previously solved by `git_buf`. We require: 1. A general purpose string class that provides a number of utility APIs for manipulating data (eg, concatenating, truncating, etc). 2. A structure that we can use to return strings to callers that they can take ownership of. By using a single class (`git_buf`) for both of these purposes, we have confused the API to the point that refactorings are difficult and reasoning about correctness is also difficult. Move the utility class `git_buf` to be called `git_str`: this represents its general purpose, as an internal string buffer class. The name also is an homage to Junio Hamano ("gitstr"). The public API remains `git_buf`, and has a much smaller footprint. It is generally only used as an "out" param with strict requirements that follow the documentation. (Exceptions exist for some legacy APIs to avoid breaking callers unnecessarily.) Utility functions exist to convert a user-specified `git_buf` to a `git_str` so that we can call internal functions, then converting it back again.
Diffstat (limited to 'src/futils.c')
-rw-r--r--src/futils.c82
1 files changed, 41 insertions, 41 deletions
diff --git a/src/futils.c b/src/futils.c
index d28b231ce..9a15ceeb9 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,7 +177,7 @@ int git_futils_readbuffer_fd(git_buf *buf, git_file fd, size_t len)
}
int git_futils_readbuffer_updated(
- git_buf *out,
+ git_str *out,
const char *path,
unsigned char checksum[GIT_HASH_SHA1_SIZE],
int *updated)
@@ -185,7 +185,7 @@ int git_futils_readbuffer_updated(
int error;
git_file fd;
struct stat st;
- git_buf buf = GIT_BUF_INIT;
+ git_str buf = GIT_STR_INIT;
unsigned char checksum_new[GIT_HASH_SHA1_SIZE];
GIT_ASSERT_ARG(out);
@@ -220,7 +220,7 @@ int git_futils_readbuffer_updated(
if (checksum) {
if ((error = git_hash_buf(checksum_new, buf.ptr, buf.size, GIT_HASH_ALGORITHM_SHA1)) < 0) {
- git_buf_dispose(&buf);
+ git_str_dispose(&buf);
return error;
}
@@ -228,7 +228,7 @@ int git_futils_readbuffer_updated(
* If we were given a checksum, we only want to use it if it's different
*/
if (!memcmp(checksum, checksum_new, GIT_HASH_SHA1_SIZE)) {
- git_buf_dispose(&buf);
+ git_str_dispose(&buf);
if (updated)
*updated = 0;
@@ -244,19 +244,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;
@@ -276,7 +276,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;
@@ -418,7 +418,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;
@@ -450,7 +450,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;
}
@@ -460,16 +460,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;
@@ -541,8 +541,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;
}
@@ -558,7 +558,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;
@@ -578,7 +578,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);
@@ -673,7 +673,7 @@ retry_lstat:
}
done:
- git_buf_dispose(&make_path);
+ git_str_dispose(&make_path);
return error;
}
@@ -697,13 +697,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;
@@ -722,7 +722,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;
@@ -810,7 +810,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 */
@@ -834,7 +834,7 @@ int git_futils_rmdir_r(
error = 0;
}
- git_buf_dispose(&fullpath);
+ git_str_dispose(&fullpath);
return error;
}
@@ -938,7 +938,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;
@@ -947,7 +947,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;
@@ -969,7 +969,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;
@@ -980,7 +980,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;
@@ -1064,10 +1064,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));
@@ -1075,7 +1075,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) {
@@ -1093,8 +1093,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;
}