Skip to content

Commit e9dcc30

Browse files
pcloudsgitster
authored andcommitted
files-backend: convert git_path() to strbuf_git_path()
git_path() and friends are going to be killed in files-backend.c in near future. And because there's a risk with overwriting buffer in git_path(), let's convert them all to strbuf_git_path(). We'll have easier time killing/converting strbuf_git_path() then because we won't have to worry about memory management again. Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 0a3f07d commit e9dcc30

File tree

1 file changed

+97
-33
lines changed

1 file changed

+97
-33
lines changed

refs/files-backend.c

Lines changed: 97 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2316,6 +2316,7 @@ enum {
23162316
static void try_remove_empty_parents(const char *refname, unsigned int flags)
23172317
{
23182318
struct strbuf buf = STRBUF_INIT;
2319+
struct strbuf sb = STRBUF_INIT;
23192320
char *p, *q;
23202321
int i;
23212322

@@ -2337,14 +2338,19 @@ static void try_remove_empty_parents(const char *refname, unsigned int flags)
23372338
if (q == p)
23382339
break;
23392340
strbuf_setlen(&buf, q - buf.buf);
2340-
if ((flags & REMOVE_EMPTY_PARENTS_REF) &&
2341-
rmdir(git_path("%s", buf.buf)))
2341+
2342+
strbuf_reset(&sb);
2343+
strbuf_git_path(&sb, "%s", buf.buf);
2344+
if ((flags & REMOVE_EMPTY_PARENTS_REF) && rmdir(sb.buf))
23422345
flags &= ~REMOVE_EMPTY_PARENTS_REF;
2343-
if ((flags & REMOVE_EMPTY_PARENTS_REFLOG) &&
2344-
rmdir(git_path("logs/%s", buf.buf)))
2346+
2347+
strbuf_reset(&sb);
2348+
strbuf_git_path(&sb, "logs/%s", buf.buf);
2349+
if ((flags & REMOVE_EMPTY_PARENTS_REFLOG) && rmdir(sb.buf))
23452350
flags &= ~REMOVE_EMPTY_PARENTS_REFLOG;
23462351
}
23472352
strbuf_release(&buf);
2353+
strbuf_release(&sb);
23482354
}
23492355

23502356
/* make sure nobody touched the ref, and unlink */
@@ -2506,19 +2512,24 @@ static int files_delete_refs(struct ref_store *ref_store,
25062512
*/
25072513
#define TMP_RENAMED_LOG "logs/refs/.tmp-renamed-log"
25082514

2509-
static int rename_tmp_log_callback(const char *path, void *cb)
2515+
struct rename_cb {
2516+
const char *tmp_renamed_log;
2517+
int true_errno;
2518+
};
2519+
2520+
static int rename_tmp_log_callback(const char *path, void *cb_data)
25102521
{
2511-
int *true_errno = cb;
2522+
struct rename_cb *cb = cb_data;
25122523

2513-
if (rename(git_path(TMP_RENAMED_LOG), path)) {
2524+
if (rename(cb->tmp_renamed_log, path)) {
25142525
/*
25152526
* rename(a, b) when b is an existing directory ought
25162527
* to result in ISDIR, but Solaris 5.8 gives ENOTDIR.
25172528
* Sheesh. Record the true errno for error reporting,
25182529
* but report EISDIR to raceproof_create_file() so
25192530
* that it knows to retry.
25202531
*/
2521-
*true_errno = errno;
2532+
cb->true_errno = errno;
25222533
if (errno == ENOTDIR)
25232534
errno = EISDIR;
25242535
return -1;
@@ -2529,20 +2540,26 @@ static int rename_tmp_log_callback(const char *path, void *cb)
25292540

25302541
static int rename_tmp_log(const char *newrefname)
25312542
{
2532-
char *path = git_pathdup("logs/%s", newrefname);
2533-
int ret, true_errno;
2543+
struct strbuf path = STRBUF_INIT;
2544+
struct strbuf tmp = STRBUF_INIT;
2545+
struct rename_cb cb;
2546+
int ret;
25342547

2535-
ret = raceproof_create_file(path, rename_tmp_log_callback, &true_errno);
2548+
strbuf_git_path(&path, "logs/%s", newrefname);
2549+
strbuf_git_path(&tmp, TMP_RENAMED_LOG);
2550+
cb.tmp_renamed_log = tmp.buf;
2551+
ret = raceproof_create_file(path.buf, rename_tmp_log_callback, &cb);
25362552
if (ret) {
25372553
if (errno == EISDIR)
2538-
error("directory not empty: %s", path);
2554+
error("directory not empty: %s", path.buf);
25392555
else
25402556
error("unable to move logfile %s to %s: %s",
2541-
git_path(TMP_RENAMED_LOG), path,
2542-
strerror(true_errno));
2557+
tmp.buf, path.buf,
2558+
strerror(cb.true_errno));
25432559
}
25442560

2545-
free(path);
2561+
strbuf_release(&path);
2562+
strbuf_release(&tmp);
25462563
return ret;
25472564
}
25482565

@@ -2583,10 +2600,17 @@ static int files_rename_ref(struct ref_store *ref_store,
25832600
int flag = 0, logmoved = 0;
25842601
struct ref_lock *lock;
25852602
struct stat loginfo;
2586-
int log = !lstat(git_path("logs/%s", oldrefname), &loginfo);
2603+
struct strbuf sb_oldref = STRBUF_INIT;
2604+
struct strbuf sb_newref = STRBUF_INIT;
2605+
struct strbuf tmp_renamed_log = STRBUF_INIT;
2606+
int log, ret;
25872607
struct strbuf err = STRBUF_INIT;
2588-
int ret;
25892608

2609+
strbuf_git_path(&sb_oldref, "logs/%s", oldrefname);
2610+
strbuf_git_path(&sb_newref, "logs/%s", newrefname);
2611+
strbuf_git_path(&tmp_renamed_log, TMP_RENAMED_LOG);
2612+
2613+
log = !lstat(sb_oldref.buf, &loginfo);
25902614
if (log && S_ISLNK(loginfo.st_mode)) {
25912615
ret = error("reflog for %s is a symlink", oldrefname);
25922616
goto out;
@@ -2608,7 +2632,7 @@ static int files_rename_ref(struct ref_store *ref_store,
26082632
goto out;
26092633
}
26102634

2611-
if (log && rename(git_path("logs/%s", oldrefname), git_path(TMP_RENAMED_LOG))) {
2635+
if (log && rename(sb_oldref.buf, tmp_renamed_log.buf)) {
26122636
ret = error("unable to move logfile logs/%s to "TMP_RENAMED_LOG": %s",
26132637
oldrefname, strerror(errno));
26142638
goto out;
@@ -2690,16 +2714,19 @@ static int files_rename_ref(struct ref_store *ref_store,
26902714
log_all_ref_updates = flag;
26912715

26922716
rollbacklog:
2693-
if (logmoved && rename(git_path("logs/%s", newrefname), git_path("logs/%s", oldrefname)))
2717+
if (logmoved && rename(sb_newref.buf, sb_oldref.buf))
26942718
error("unable to restore logfile %s from %s: %s",
26952719
oldrefname, newrefname, strerror(errno));
26962720
if (!logmoved && log &&
2697-
rename(git_path(TMP_RENAMED_LOG), git_path("logs/%s", oldrefname)))
2721+
rename(tmp_renamed_log.buf, sb_oldref.buf))
26982722
error("unable to restore logfile %s from "TMP_RENAMED_LOG": %s",
26992723
oldrefname, strerror(errno));
2700-
27012724
ret = 1;
27022725
out:
2726+
strbuf_release(&sb_newref);
2727+
strbuf_release(&sb_oldref);
2728+
strbuf_release(&tmp_renamed_log);
2729+
27032730
return ret;
27042731
}
27052732

@@ -2872,18 +2899,24 @@ static int files_log_ref_write(const char *refname, const unsigned char *old_sha
28722899
result = log_ref_write_fd(logfd, old_sha1, new_sha1,
28732900
git_committer_info(0), msg);
28742901
if (result) {
2902+
struct strbuf sb = STRBUF_INIT;
28752903
int save_errno = errno;
28762904

2905+
strbuf_git_path(&sb, "logs/%s", refname);
28772906
strbuf_addf(err, "unable to append to '%s': %s",
2878-
git_path("logs/%s", refname), strerror(save_errno));
2907+
sb.buf, strerror(save_errno));
2908+
strbuf_release(&sb);
28792909
close(logfd);
28802910
return -1;
28812911
}
28822912
if (close(logfd)) {
2913+
struct strbuf sb = STRBUF_INIT;
28832914
int save_errno = errno;
28842915

2916+
strbuf_git_path(&sb, "logs/%s", refname);
28852917
strbuf_addf(err, "unable to append to '%s': %s",
2886-
git_path("logs/%s", refname), strerror(save_errno));
2918+
sb.buf, strerror(save_errno));
2919+
strbuf_release(&sb);
28872920
return -1;
28882921
}
28892922
return 0;
@@ -3103,22 +3136,32 @@ int set_worktree_head_symref(const char *gitdir, const char *target, const char
31033136
static int files_reflog_exists(struct ref_store *ref_store,
31043137
const char *refname)
31053138
{
3139+
struct strbuf sb = STRBUF_INIT;
31063140
struct stat st;
3141+
int ret;
31073142

31083143
/* Check validity (but we don't need the result): */
31093144
files_downcast(ref_store, 0, "reflog_exists");
31103145

3111-
return !lstat(git_path("logs/%s", refname), &st) &&
3112-
S_ISREG(st.st_mode);
3146+
strbuf_git_path(&sb, "logs/%s", refname);
3147+
ret = !lstat(sb.buf, &st) && S_ISREG(st.st_mode);
3148+
strbuf_release(&sb);
3149+
return ret;
31133150
}
31143151

31153152
static int files_delete_reflog(struct ref_store *ref_store,
31163153
const char *refname)
31173154
{
3155+
struct strbuf sb = STRBUF_INIT;
3156+
int ret;
3157+
31183158
/* Check validity (but we don't need the result): */
31193159
files_downcast(ref_store, 0, "delete_reflog");
31203160

3121-
return remove_path(git_path("logs/%s", refname));
3161+
strbuf_git_path(&sb, "logs/%s", refname);
3162+
ret = remove_path(sb.buf);
3163+
strbuf_release(&sb);
3164+
return ret;
31223165
}
31233166

31243167
static int show_one_reflog_ent(struct strbuf *sb, each_reflog_ent_fn fn, void *cb_data)
@@ -3174,7 +3217,9 @@ static int files_for_each_reflog_ent_reverse(struct ref_store *ref_store,
31743217
/* Check validity (but we don't need the result): */
31753218
files_downcast(ref_store, 0, "for_each_reflog_ent_reverse");
31763219

3177-
logfp = fopen(git_path("logs/%s", refname), "r");
3220+
strbuf_git_path(&sb, "logs/%s", refname);
3221+
logfp = fopen(sb.buf, "r");
3222+
strbuf_release(&sb);
31783223
if (!logfp)
31793224
return -1;
31803225

@@ -3280,7 +3325,9 @@ static int files_for_each_reflog_ent(struct ref_store *ref_store,
32803325
/* Check validity (but we don't need the result): */
32813326
files_downcast(ref_store, 0, "for_each_reflog_ent");
32823327

3283-
logfp = fopen(git_path("logs/%s", refname), "r");
3328+
strbuf_git_path(&sb, "logs/%s", refname);
3329+
logfp = fopen(sb.buf, "r");
3330+
strbuf_release(&sb);
32843331
if (!logfp)
32853332
return -1;
32863333

@@ -3362,12 +3409,15 @@ static struct ref_iterator *files_reflog_iterator_begin(struct ref_store *ref_st
33623409
{
33633410
struct files_reflog_iterator *iter = xcalloc(1, sizeof(*iter));
33643411
struct ref_iterator *ref_iterator = &iter->base;
3412+
struct strbuf sb = STRBUF_INIT;
33653413

33663414
/* Check validity (but we don't need the result): */
33673415
files_downcast(ref_store, 0, "reflog_iterator_begin");
33683416

33693417
base_ref_iterator_init(ref_iterator, &files_reflog_iterator_vtable);
3370-
iter->dir_iterator = dir_iterator_begin(git_path("logs"));
3418+
strbuf_git_path(&sb, "logs");
3419+
iter->dir_iterator = dir_iterator_begin(sb.buf);
3420+
strbuf_release(&sb);
33713421
return ref_iterator;
33723422
}
33733423

@@ -3705,6 +3755,7 @@ static int files_transaction_commit(struct ref_store *ref_store,
37053755
char *head_ref = NULL;
37063756
int head_type;
37073757
struct object_id head_oid;
3758+
struct strbuf sb = STRBUF_INIT;
37083759

37093760
assert(err);
37103761

@@ -3826,7 +3877,9 @@ static int files_transaction_commit(struct ref_store *ref_store,
38263877
if (!(update->type & REF_ISPACKED) ||
38273878
update->type & REF_ISSYMREF) {
38283879
/* It is a loose reference. */
3829-
if (unlink_or_msg(git_path("%s", lock->ref_name), err)) {
3880+
strbuf_reset(&sb);
3881+
strbuf_git_path(&sb, "%s", lock->ref_name);
3882+
if (unlink_or_msg(sb.buf, err)) {
38303883
ret = TRANSACTION_GENERIC_ERROR;
38313884
goto cleanup;
38323885
}
@@ -3846,14 +3899,17 @@ static int files_transaction_commit(struct ref_store *ref_store,
38463899

38473900
/* Delete the reflogs of any references that were deleted: */
38483901
for_each_string_list_item(ref_to_delete, &refs_to_delete) {
3849-
if (!unlink_or_warn(git_path("logs/%s", ref_to_delete->string)))
3902+
strbuf_reset(&sb);
3903+
strbuf_git_path(&sb, "logs/%s", ref_to_delete->string);
3904+
if (!unlink_or_warn(sb.buf))
38503905
try_remove_empty_parents(ref_to_delete->string,
38513906
REMOVE_EMPTY_PARENTS_REFLOG);
38523907
}
38533908

38543909
clear_loose_ref_cache(refs);
38553910

38563911
cleanup:
3912+
strbuf_release(&sb);
38573913
transaction->state = REF_TRANSACTION_CLOSED;
38583914

38593915
for (i = 0; i < transaction->nr; i++) {
@@ -4120,14 +4176,22 @@ static int files_reflog_expire(struct ref_store *ref_store,
41204176

41214177
static int files_init_db(struct ref_store *ref_store, struct strbuf *err)
41224178
{
4179+
struct strbuf sb = STRBUF_INIT;
4180+
41234181
/* Check validity (but we don't need the result): */
41244182
files_downcast(ref_store, 0, "init_db");
41254183

41264184
/*
41274185
* Create .git/refs/{heads,tags}
41284186
*/
4129-
safe_create_dir(git_path("refs/heads"), 1);
4130-
safe_create_dir(git_path("refs/tags"), 1);
4187+
strbuf_git_path(&sb, "refs/heads");
4188+
safe_create_dir(sb.buf, 1);
4189+
4190+
strbuf_reset(&sb);
4191+
strbuf_git_path(&sb, "refs/tags");
4192+
safe_create_dir(sb.buf, 1);
4193+
4194+
strbuf_release(&sb);
41314195
return 0;
41324196
}
41334197

0 commit comments

Comments
 (0)