path: adjust last remaining users of `the_repository`

With the preceding refactorings we now only have a couple of implicit
users of `the_repository` left in the "path" subsystem, all of which
depend on global state via `calc_shared_perm()`. Make the dependency on
`the_repository` explicit by passing the repo as a parameter instead and
adjust callers accordingly.

Note that this change bubbles up into a couple of subsystems that were
previously declared as free from `the_repository`. Instead of marking
all of them as `the_repository`-dependent again, we instead use the
repository that is available in the calling context. There are three
exceptions though with "copy.c", "pack-write.c" and "tempfile.c".
Adjusting these would require us to adapt callsites all over the place,
so this is left for a future iteration.

Mark "path.c" as free from `the_repository`.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
maint
Patrick Steinhardt 2025-02-07 12:03:41 +01:00 committed by Junio C Hamano
parent f1ce861c34
commit 028f618658
17 changed files with 64 additions and 56 deletions

View File

@ -1220,7 +1220,7 @@ int cmd_clone(int argc,


strbuf_reset(&buf); strbuf_reset(&buf);
strbuf_addf(&buf, "%s/refs", git_dir); strbuf_addf(&buf, "%s/refs", git_dir);
safe_create_dir(buf.buf, 1); safe_create_dir(the_repository, buf.buf, 1);


/* /*
* additional config can be injected with -c, make sure it's included * additional config can be injected with -c, make sure it's included

View File

@ -2084,7 +2084,7 @@ static int write_commit_graph_file(struct write_commit_graph_context *ctx)
return -1; return -1;
} }


if (adjust_shared_perm(get_tempfile_path(graph_layer))) { if (adjust_shared_perm(the_repository, get_tempfile_path(graph_layer))) {
error(_("unable to adjust shared permissions for '%s'"), error(_("unable to adjust shared permissions for '%s'"),
get_tempfile_path(graph_layer)); get_tempfile_path(graph_layer));
return -1; return -1;

4
copy.c
View File

@ -1,3 +1,5 @@
#define USE_THE_REPOSITORY_VARIABLE

#include "git-compat-util.h" #include "git-compat-util.h"
#include "copy.h" #include "copy.h"
#include "path.h" #include "path.h"
@ -57,7 +59,7 @@ int copy_file(const char *dst, const char *src, int mode)
if (close(fdo) != 0) if (close(fdo) != 0)
return error_errno("%s: close error", dst); return error_errno("%s: close error", dst);


if (!status && adjust_shared_perm(dst)) if (!status && adjust_shared_perm(the_repository, dst))
return -1; return -1;


return status; return status;

View File

@ -190,7 +190,7 @@ static int write_one_object(struct repository *repo, const struct object_id *oid
goto errout; goto errout;
if (close(fd)) if (close(fd))
goto errout; goto errout;
adjust_shared_perm(path.buf); adjust_shared_perm(repo, path.buf);
rollback_lock_file(&lock); rollback_lock_file(&lock);
strbuf_release(&buf); strbuf_release(&buf);
strbuf_release(&path); strbuf_release(&path);

View File

@ -1336,7 +1336,7 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
return -1; return -1;
} }


if (adjust_shared_perm(get_tempfile_path(incr))) { if (adjust_shared_perm(r, get_tempfile_path(incr))) {
error(_("unable to adjust shared permissions for '%s'"), error(_("unable to adjust shared permissions for '%s'"),
get_tempfile_path(incr)); get_tempfile_path(incr));
return -1; return -1;

View File

@ -388,7 +388,7 @@ int mkdir_in_gitdir(const char *path)
} }
strbuf_release(&sb); strbuf_release(&sb);
} }
return adjust_shared_perm(path); return adjust_shared_perm(the_repository, path);
} }


static enum scld_error safe_create_leading_directories_1(char *path, int share) static enum scld_error safe_create_leading_directories_1(char *path, int share)
@ -437,7 +437,7 @@ static enum scld_error safe_create_leading_directories_1(char *path, int share)
ret = SCLD_VANISHED; ret = SCLD_VANISHED;
else else
ret = SCLD_FAILED; ret = SCLD_FAILED;
} else if (share && adjust_shared_perm(path)) { } else if (share && adjust_shared_perm(the_repository, path)) {
ret = SCLD_PERMS; ret = SCLD_PERMS;
} }
*slash = slash_character; *slash = slash_character;
@ -2105,7 +2105,7 @@ retry:
} }


out: out:
if (adjust_shared_perm(filename)) if (adjust_shared_perm(the_repository, filename))
return error(_("unable to set permission to '%s'"), filename); return error(_("unable to set permission to '%s'"), filename);
return 0; return 0;
} }
@ -2181,7 +2181,7 @@ static int create_tmpfile(struct strbuf *tmp, const char *filename)
strbuf_add(tmp, filename, dirlen - 1); strbuf_add(tmp, filename, dirlen - 1);
if (mkdir(tmp->buf, 0777) && errno != EEXIST) if (mkdir(tmp->buf, 0777) && errno != EEXIST)
return -1; return -1;
if (adjust_shared_perm(tmp->buf)) if (adjust_shared_perm(the_repository, tmp->buf))
return -1; return -1;


/* Try again */ /* Try again */

View File

@ -1072,7 +1072,7 @@ void bitmap_writer_finish(struct bitmap_writer *writer,
finalize_hashfile(f, NULL, FSYNC_COMPONENT_PACK_METADATA, finalize_hashfile(f, NULL, FSYNC_COMPONENT_PACK_METADATA,
CSUM_HASH_IN_STREAM | CSUM_FSYNC | CSUM_CLOSE); CSUM_HASH_IN_STREAM | CSUM_FSYNC | CSUM_CLOSE);


if (adjust_shared_perm(tmp_file.buf)) if (adjust_shared_perm(the_repository, tmp_file.buf))
die_errno("unable to make temporary bitmap file readable"); die_errno("unable to make temporary bitmap file readable");


if (rename(tmp_file.buf, filename)) if (rename(tmp_file.buf, filename))

View File

@ -1,3 +1,5 @@
#define USE_THE_REPOSITORY_VARIABLE

#include "git-compat-util.h" #include "git-compat-util.h"
#include "environment.h" #include "environment.h"
#include "gettext.h" #include "gettext.h"
@ -287,7 +289,7 @@ char *write_rev_file_order(const struct git_hash_algo *hash_algo,
write_rev_index_positions(f, pack_order, nr_objects); write_rev_index_positions(f, pack_order, nr_objects);
write_rev_trailer(hash_algo, f, hash); write_rev_trailer(hash_algo, f, hash);


if (adjust_shared_perm(path) < 0) if (adjust_shared_perm(the_repository, path) < 0)
die(_("failed to make %s readable"), path); die(_("failed to make %s readable"), path);


finalize_hashfile(f, NULL, FSYNC_COMPONENT_PACK_METADATA, finalize_hashfile(f, NULL, FSYNC_COMPONENT_PACK_METADATA,
@ -350,7 +352,7 @@ static char *write_mtimes_file(const struct git_hash_algo *hash_algo,
write_mtimes_objects(f, to_pack, objects, nr_objects); write_mtimes_objects(f, to_pack, objects, nr_objects);
write_mtimes_trailer(hash_algo, f, hash); write_mtimes_trailer(hash_algo, f, hash);


if (adjust_shared_perm(mtimes_name) < 0) if (adjust_shared_perm(the_repository, mtimes_name) < 0)
die(_("failed to make %s readable"), mtimes_name); die(_("failed to make %s readable"), mtimes_name);


finalize_hashfile(f, NULL, FSYNC_COMPONENT_PACK_METADATA, finalize_hashfile(f, NULL, FSYNC_COMPONENT_PACK_METADATA,
@ -565,12 +567,12 @@ void stage_tmp_packfiles(const struct git_hash_algo *hash_algo,
char *rev_tmp_name = NULL; char *rev_tmp_name = NULL;
char *mtimes_tmp_name = NULL; char *mtimes_tmp_name = NULL;


if (adjust_shared_perm(pack_tmp_name)) if (adjust_shared_perm(the_repository, pack_tmp_name))
die_errno("unable to make temporary pack file readable"); die_errno("unable to make temporary pack file readable");


*idx_tmp_name = (char *)write_idx_file(hash_algo, NULL, written_list, *idx_tmp_name = (char *)write_idx_file(hash_algo, NULL, written_list,
nr_written, pack_idx_opts, hash); nr_written, pack_idx_opts, hash);
if (adjust_shared_perm(*idx_tmp_name)) if (adjust_shared_perm(the_repository, *idx_tmp_name))
die_errno("unable to make temporary index file readable"); die_errno("unable to make temporary index file readable");


rev_tmp_name = write_rev_file(hash_algo, NULL, written_list, nr_written, rev_tmp_name = write_rev_file(hash_algo, NULL, written_list, nr_written,

25
path.c
View File

@ -2,8 +2,6 @@
* Utilities for paths and pathnames * Utilities for paths and pathnames
*/ */


#define USE_THE_REPOSITORY_VARIABLE

#include "git-compat-util.h" #include "git-compat-util.h"
#include "abspath.h" #include "abspath.h"
#include "environment.h" #include "environment.h"
@ -840,21 +838,22 @@ const char *enter_repo(const char *path, unsigned flags)
return NULL; return NULL;
} }


int calc_shared_perm(int mode) int calc_shared_perm(struct repository *repo,
int mode)
{ {
int tweak; int tweak;


if (repo_settings_get_shared_repository(the_repository) < 0) if (repo_settings_get_shared_repository(repo) < 0)
tweak = -repo_settings_get_shared_repository(the_repository); tweak = -repo_settings_get_shared_repository(repo);
else else
tweak = repo_settings_get_shared_repository(the_repository); tweak = repo_settings_get_shared_repository(repo);


if (!(mode & S_IWUSR)) if (!(mode & S_IWUSR))
tweak &= ~0222; tweak &= ~0222;
if (mode & S_IXUSR) if (mode & S_IXUSR)
/* Copy read bits to execute bits */ /* Copy read bits to execute bits */
tweak |= (tweak & 0444) >> 2; tweak |= (tweak & 0444) >> 2;
if (repo_settings_get_shared_repository(the_repository) < 0) if (repo_settings_get_shared_repository(repo) < 0)
mode = (mode & ~0777) | tweak; mode = (mode & ~0777) | tweak;
else else
mode |= tweak; mode |= tweak;
@ -862,17 +861,17 @@ int calc_shared_perm(int mode)
return mode; return mode;
} }



int adjust_shared_perm(struct repository *repo,
int adjust_shared_perm(const char *path) const char *path)
{ {
int old_mode, new_mode; int old_mode, new_mode;


if (!repo_settings_get_shared_repository(the_repository)) if (!repo_settings_get_shared_repository(repo))
return 0; return 0;
if (get_st_mode_bits(path, &old_mode) < 0) if (get_st_mode_bits(path, &old_mode) < 0)
return -1; return -1;


new_mode = calc_shared_perm(old_mode); new_mode = calc_shared_perm(repo, old_mode);
if (S_ISDIR(old_mode)) { if (S_ISDIR(old_mode)) {
/* Copy read bits to execute bits */ /* Copy read bits to execute bits */
new_mode |= (new_mode & 0444) >> 2; new_mode |= (new_mode & 0444) >> 2;
@ -891,7 +890,7 @@ int adjust_shared_perm(const char *path)
return 0; return 0;
} }


void safe_create_dir(const char *dir, int share) void safe_create_dir(struct repository *repo, const char *dir, int share)
{ {
if (mkdir(dir, 0777) < 0) { if (mkdir(dir, 0777) < 0) {
if (errno != EEXIST) { if (errno != EEXIST) {
@ -899,7 +898,7 @@ void safe_create_dir(const char *dir, int share)
exit(1); exit(1);
} }
} }
else if (share && adjust_shared_perm(dir)) else if (share && adjust_shared_perm(repo, dir))
die(_("Could not make %s writable by group"), dir); die(_("Could not make %s writable by group"), dir);
} }



6
path.h
View File

@ -141,8 +141,8 @@ const char *git_path_shallow(struct repository *r);


int ends_with_path_components(const char *path, const char *components); int ends_with_path_components(const char *path, const char *components);


int calc_shared_perm(int mode); int calc_shared_perm(struct repository *repo, int mode);
int adjust_shared_perm(const char *path); int adjust_shared_perm(struct repository *repo, const char *path);


char *interpolate_path(const char *path, int real_home); char *interpolate_path(const char *path, int real_home);


@ -219,7 +219,7 @@ char *xdg_cache_home(const char *filename);
* directories under $GIT_DIR. Don't use it for working tree * directories under $GIT_DIR. Don't use it for working tree
* directories. * directories.
*/ */
void safe_create_dir(const char *dir, int share); void safe_create_dir(struct repository *repo, const char *dir, int share);


# ifdef USE_THE_REPOSITORY_VARIABLE # ifdef USE_THE_REPOSITORY_VARIABLE
# include "strbuf.h" # include "strbuf.h"

View File

@ -3290,7 +3290,7 @@ static int write_shared_index(struct index_state *istate,


if (ret) if (ret)
return ret; return ret;
ret = adjust_shared_perm(get_tempfile_path(*temp)); ret = adjust_shared_perm(the_repository, get_tempfile_path(*temp));
if (ret) { if (ret) {
error(_("cannot fix permission bits on '%s'"), get_tempfile_path(*temp)); error(_("cannot fix permission bits on '%s'"), get_tempfile_path(*temp));
return ret; return ret;

View File

@ -1831,7 +1831,7 @@ static int log_ref_setup(struct files_ref_store *refs,
} }


if (*logfd >= 0) if (*logfd >= 0)
adjust_shared_perm(logfile); adjust_shared_perm(the_repository, logfile);


free(logfile); free(logfile);
return 0; return 0;
@ -3488,8 +3488,8 @@ static int files_ref_store_create_on_disk(struct ref_store *ref_store,
* they do not understand the reference format extension. * they do not understand the reference format extension.
*/ */
strbuf_addf(&sb, "%s/refs", ref_store->gitdir); strbuf_addf(&sb, "%s/refs", ref_store->gitdir);
safe_create_dir(sb.buf, 1); safe_create_dir(the_repository, sb.buf, 1);
adjust_shared_perm(sb.buf); adjust_shared_perm(the_repository, sb.buf);


/* /*
* There is no need to create directories for common refs when creating * There is no need to create directories for common refs when creating
@ -3501,11 +3501,11 @@ static int files_ref_store_create_on_disk(struct ref_store *ref_store,
*/ */
strbuf_reset(&sb); strbuf_reset(&sb);
files_ref_path(refs, &sb, "refs/heads"); files_ref_path(refs, &sb, "refs/heads");
safe_create_dir(sb.buf, 1); safe_create_dir(the_repository, sb.buf, 1);


strbuf_reset(&sb); strbuf_reset(&sb);
files_ref_path(refs, &sb, "refs/tags"); files_ref_path(refs, &sb, "refs/tags");
safe_create_dir(sb.buf, 1); safe_create_dir(the_repository, sb.buf, 1);
} }


strbuf_release(&sb); strbuf_release(&sb);

View File

@ -380,7 +380,7 @@ static struct ref_store *reftable_be_init(struct repository *repo,
default: default:
BUG("unknown hash algorithm %d", repo->hash_algo->format_id); BUG("unknown hash algorithm %d", repo->hash_algo->format_id);
} }
refs->write_options.default_permissions = calc_shared_perm(0666 & ~mask); refs->write_options.default_permissions = calc_shared_perm(the_repository, 0666 & ~mask);
refs->write_options.disable_auto_compact = refs->write_options.disable_auto_compact =
!git_env_bool("GIT_TEST_REFTABLE_AUTOCOMPACTION", 1); !git_env_bool("GIT_TEST_REFTABLE_AUTOCOMPACTION", 1);
refs->write_options.lock_timeout_ms = 100; refs->write_options.lock_timeout_ms = 100;
@ -470,21 +470,21 @@ static int reftable_be_create_on_disk(struct ref_store *ref_store,
struct strbuf sb = STRBUF_INIT; struct strbuf sb = STRBUF_INIT;


strbuf_addf(&sb, "%s/reftable", refs->base.gitdir); strbuf_addf(&sb, "%s/reftable", refs->base.gitdir);
safe_create_dir(sb.buf, 1); safe_create_dir(the_repository, sb.buf, 1);
strbuf_reset(&sb); strbuf_reset(&sb);


strbuf_addf(&sb, "%s/HEAD", refs->base.gitdir); strbuf_addf(&sb, "%s/HEAD", refs->base.gitdir);
write_file(sb.buf, "ref: refs/heads/.invalid"); write_file(sb.buf, "ref: refs/heads/.invalid");
adjust_shared_perm(sb.buf); adjust_shared_perm(the_repository, sb.buf);
strbuf_reset(&sb); strbuf_reset(&sb);


strbuf_addf(&sb, "%s/refs", refs->base.gitdir); strbuf_addf(&sb, "%s/refs", refs->base.gitdir);
safe_create_dir(sb.buf, 1); safe_create_dir(the_repository, sb.buf, 1);
strbuf_reset(&sb); strbuf_reset(&sb);


strbuf_addf(&sb, "%s/refs/heads", refs->base.gitdir); strbuf_addf(&sb, "%s/refs/heads", refs->base.gitdir);
write_file(sb.buf, "this repository uses the reftable format"); write_file(sb.buf, "this repository uses the reftable format");
adjust_shared_perm(sb.buf); adjust_shared_perm(the_repository, sb.buf);


strbuf_release(&sb); strbuf_release(&sb);
return 0; return 0;

View File

@ -125,7 +125,7 @@ static int update_info_file(struct repository *r, char *path,
uic.cur_fp = NULL; uic.cur_fp = NULL;


if (uic_is_stale(&uic)) { if (uic_is_stale(&uic)) {
if (adjust_shared_perm(get_tempfile_path(f)) < 0) if (adjust_shared_perm(r, get_tempfile_path(f)) < 0)
goto out; goto out;
if (rename_tempfile(&f, path) < 0) if (rename_tempfile(&f, path) < 0)
goto out; goto out;

12
setup.c
View File

@ -2088,7 +2088,7 @@ static void copy_templates_1(struct strbuf *path, struct strbuf *template_path,
* with the way the namespace under .git/ is organized, should * with the way the namespace under .git/ is organized, should
* be really carefully chosen. * be really carefully chosen.
*/ */
safe_create_dir(path->buf, 1); safe_create_dir(the_repository, path->buf, 1);
while ((de = readdir(dir)) != NULL) { while ((de = readdir(dir)) != NULL) {
struct stat st_git, st_template; struct stat st_git, st_template;
int exists = 0; int exists = 0;
@ -2352,7 +2352,7 @@ static int create_default_files(const char *template_path,
* shared-repository settings, we would need to fix them up. * shared-repository settings, we would need to fix them up.
*/ */
if (repo_settings_get_shared_repository(the_repository)) { if (repo_settings_get_shared_repository(the_repository)) {
adjust_shared_perm(repo_get_git_dir(the_repository)); adjust_shared_perm(the_repository, repo_get_git_dir(the_repository));
} }


initialize_repository_version(fmt->hash_algo, fmt->ref_storage_format, reinit); initialize_repository_version(fmt->hash_algo, fmt->ref_storage_format, reinit);
@ -2413,15 +2413,15 @@ static void create_object_directory(void)
strbuf_addstr(&path, repo_get_object_directory(the_repository)); strbuf_addstr(&path, repo_get_object_directory(the_repository));
baselen = path.len; baselen = path.len;


safe_create_dir(path.buf, 1); safe_create_dir(the_repository, path.buf, 1);


strbuf_setlen(&path, baselen); strbuf_setlen(&path, baselen);
strbuf_addstr(&path, "/pack"); strbuf_addstr(&path, "/pack");
safe_create_dir(path.buf, 1); safe_create_dir(the_repository, path.buf, 1);


strbuf_setlen(&path, baselen); strbuf_setlen(&path, baselen);
strbuf_addstr(&path, "/info"); strbuf_addstr(&path, "/info");
safe_create_dir(path.buf, 1); safe_create_dir(the_repository, path.buf, 1);


strbuf_release(&path); strbuf_release(&path);
} }
@ -2588,7 +2588,7 @@ int init_db(const char *git_dir, const char *real_git_dir,
*/ */
git_config(platform_core_config, NULL); git_config(platform_core_config, NULL);


safe_create_dir(git_dir, 0); safe_create_dir(the_repository, git_dir, 0);


reinit = create_default_files(template_dir, original_git_dir, reinit = create_default_files(template_dir, original_git_dir,
&repo_fmt, init_shared_repository); &repo_fmt, init_shared_repository);

View File

@ -42,6 +42,8 @@
* file created by its parent. * file created by its parent.
*/ */


#define USE_THE_REPOSITORY_VARIABLE

#include "git-compat-util.h" #include "git-compat-util.h"
#include "abspath.h" #include "abspath.h"
#include "path.h" #include "path.h"
@ -148,7 +150,7 @@ struct tempfile *create_tempfile_mode(const char *path, int mode)
return NULL; return NULL;
} }
activate_tempfile(tempfile); activate_tempfile(tempfile);
if (adjust_shared_perm(tempfile->filename.buf)) { if (adjust_shared_perm(the_repository, tempfile->filename.buf)) {
int save_errno = errno; int save_errno = errno;
error("cannot fix permission bits on %s", tempfile->filename.buf); error("cannot fix permission bits on %s", tempfile->filename.buf);
delete_tempfile(&tempfile); delete_tempfile(&tempfile);

View File

@ -207,10 +207,12 @@ static int read_dir_paths(struct string_list *out, const char *path)
return 0; return 0;
} }


static int migrate_paths(struct strbuf *src, struct strbuf *dst, static int migrate_paths(struct tmp_objdir *t,
struct strbuf *src, struct strbuf *dst,
enum finalize_object_file_flags flags); enum finalize_object_file_flags flags);


static int migrate_one(struct strbuf *src, struct strbuf *dst, static int migrate_one(struct tmp_objdir *t,
struct strbuf *src, struct strbuf *dst,
enum finalize_object_file_flags flags) enum finalize_object_file_flags flags)
{ {
struct stat st; struct stat st;
@ -219,11 +221,11 @@ static int migrate_one(struct strbuf *src, struct strbuf *dst,
return -1; return -1;
if (S_ISDIR(st.st_mode)) { if (S_ISDIR(st.st_mode)) {
if (!mkdir(dst->buf, 0777)) { if (!mkdir(dst->buf, 0777)) {
if (adjust_shared_perm(dst->buf)) if (adjust_shared_perm(t->repo, dst->buf))
return -1; return -1;
} else if (errno != EEXIST) } else if (errno != EEXIST)
return -1; return -1;
return migrate_paths(src, dst, flags); return migrate_paths(t, src, dst, flags);
} }
return finalize_object_file_flags(src->buf, dst->buf, flags); return finalize_object_file_flags(src->buf, dst->buf, flags);
} }
@ -233,7 +235,8 @@ static int is_loose_object_shard(const char *name)
return strlen(name) == 2 && isxdigit(name[0]) && isxdigit(name[1]); return strlen(name) == 2 && isxdigit(name[0]) && isxdigit(name[1]);
} }


static int migrate_paths(struct strbuf *src, struct strbuf *dst, static int migrate_paths(struct tmp_objdir *t,
struct strbuf *src, struct strbuf *dst,
enum finalize_object_file_flags flags) enum finalize_object_file_flags flags)
{ {
size_t src_len = src->len, dst_len = dst->len; size_t src_len = src->len, dst_len = dst->len;
@ -255,7 +258,7 @@ static int migrate_paths(struct strbuf *src, struct strbuf *dst,
if (is_loose_object_shard(name)) if (is_loose_object_shard(name))
flags_copy |= FOF_SKIP_COLLISION_CHECK; flags_copy |= FOF_SKIP_COLLISION_CHECK;


ret |= migrate_one(src, dst, flags_copy); ret |= migrate_one(t, src, dst, flags_copy);


strbuf_setlen(src, src_len); strbuf_setlen(src, src_len);
strbuf_setlen(dst, dst_len); strbuf_setlen(dst, dst_len);
@ -283,7 +286,7 @@ int tmp_objdir_migrate(struct tmp_objdir *t)
strbuf_addbuf(&src, &t->path); strbuf_addbuf(&src, &t->path);
strbuf_addstr(&dst, repo_get_object_directory(t->repo)); strbuf_addstr(&dst, repo_get_object_directory(t->repo));


ret = migrate_paths(&src, &dst, 0); ret = migrate_paths(t, &src, &dst, 0);


strbuf_release(&src); strbuf_release(&src);
strbuf_release(&dst); strbuf_release(&dst);