Skip to content

Commit cac4b8e

Browse files
ttaylorrgitster
authored andcommitted
shallow: use struct 'shallow_lock' for additional safety
In previous patches, the functions 'commit_shallow_file' and 'rollback_shallow_file' were introduced to reset the shallowness validity checks on a repository after potentially modifying '.git/shallow'. These functions can be made safer by wrapping the 'struct lockfile *' in a new type, 'shallow_lock', so that they cannot be called with a raw lock (and potentially misused by other code that happens to possess a lockfile, but has nothing to do with shallowness). This patch introduces that type as a thin wrapper around 'struct lockfile', and updates the two aforementioned functions and their callers to use it. Suggested-by: Junio C Hamano <[email protected]> Helped-by: Jonathan Nieder <[email protected]> Signed-off-by: Taylor Blau <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a410161 commit cac4b8e

File tree

4 files changed

+31
-16
lines changed

4 files changed

+31
-16
lines changed

builtin/receive-pack.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -856,7 +856,7 @@ static void refuse_unconfigured_deny_delete_current(void)
856856
static int command_singleton_iterator(void *cb_data, struct object_id *oid);
857857
static int update_shallow_ref(struct command *cmd, struct shallow_info *si)
858858
{
859-
struct lock_file shallow_lock = LOCK_INIT;
859+
struct shallow_lock shallow_lock = SHALLOW_LOCK_INIT;
860860
struct oid_array extra = OID_ARRAY_INIT;
861861
struct check_connected_options opt = CHECK_CONNECTED_INIT;
862862
uint32_t mask = 1 << (cmd->index % 32);

fetch-pack.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ static int fetch_fsck_objects = -1;
3535
static int transfer_fsck_objects = -1;
3636
static int agent_supported;
3737
static int server_supports_filtering;
38-
static struct lock_file shallow_lock;
38+
static struct shallow_lock shallow_lock;
3939
static const char *alternate_shallow_file;
4040
static struct strbuf fsck_msg_types = STRBUF_INIT;
4141

shallow.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -92,16 +92,16 @@ static void reset_repository_shallow(struct repository *r)
9292
stat_validity_clear(r->parsed_objects->shallow_stat);
9393
}
9494

95-
int commit_shallow_file(struct repository *r, struct lock_file *lk)
95+
int commit_shallow_file(struct repository *r, struct shallow_lock *lk)
9696
{
97-
int res = commit_lock_file(lk);
97+
int res = commit_lock_file(&lk->lock);
9898
reset_repository_shallow(r);
9999
return res;
100100
}
101101

102-
void rollback_shallow_file(struct repository *r, struct lock_file *lk)
102+
void rollback_shallow_file(struct repository *r, struct shallow_lock *lk)
103103
{
104-
rollback_lock_file(lk);
104+
rollback_lock_file(&lk->lock);
105105
reset_repository_shallow(r);
106106
}
107107

@@ -366,22 +366,22 @@ const char *setup_temporary_shallow(const struct oid_array *extra)
366366
return "";
367367
}
368368

369-
void setup_alternate_shallow(struct lock_file *shallow_lock,
369+
void setup_alternate_shallow(struct shallow_lock *shallow_lock,
370370
const char **alternate_shallow_file,
371371
const struct oid_array *extra)
372372
{
373373
struct strbuf sb = STRBUF_INIT;
374374
int fd;
375375

376-
fd = hold_lock_file_for_update(shallow_lock,
376+
fd = hold_lock_file_for_update(&shallow_lock->lock,
377377
git_path_shallow(the_repository),
378378
LOCK_DIE_ON_ERROR);
379379
check_shallow_file_for_update(the_repository);
380380
if (write_shallow_commits(&sb, 0, extra)) {
381381
if (write_in_full(fd, sb.buf, sb.len) < 0)
382382
die_errno("failed to write to %s",
383-
get_lock_file_path(shallow_lock));
384-
*alternate_shallow_file = get_lock_file_path(shallow_lock);
383+
get_lock_file_path(&shallow_lock->lock));
384+
*alternate_shallow_file = get_lock_file_path(&shallow_lock->lock);
385385
} else
386386
/*
387387
* is_repository_shallow() sees empty string as "no
@@ -414,7 +414,7 @@ void advertise_shallow_grafts(int fd)
414414
*/
415415
void prune_shallow(unsigned options)
416416
{
417-
struct lock_file shallow_lock = LOCK_INIT;
417+
struct shallow_lock shallow_lock = SHALLOW_LOCK_INIT;
418418
struct strbuf sb = STRBUF_INIT;
419419
unsigned flags = SEEN_ONLY;
420420
int fd;
@@ -428,14 +428,14 @@ void prune_shallow(unsigned options)
428428
strbuf_release(&sb);
429429
return;
430430
}
431-
fd = hold_lock_file_for_update(&shallow_lock,
431+
fd = hold_lock_file_for_update(&shallow_lock.lock,
432432
git_path_shallow(the_repository),
433433
LOCK_DIE_ON_ERROR);
434434
check_shallow_file_for_update(the_repository);
435435
if (write_shallow_commits_1(&sb, 0, NULL, flags)) {
436436
if (write_in_full(fd, sb.buf, sb.len) < 0)
437437
die_errno("failed to write to %s",
438-
get_lock_file_path(&shallow_lock));
438+
get_lock_file_path(&shallow_lock.lock));
439439
commit_shallow_file(the_repository, &shallow_lock);
440440
} else {
441441
unlink(git_path_shallow(the_repository));

shallow.h

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,25 @@ void set_alternate_shallow_file(struct repository *r, const char *path, int over
1010
int register_shallow(struct repository *r, const struct object_id *oid);
1111
int unregister_shallow(const struct object_id *oid);
1212
int is_repository_shallow(struct repository *r);
13+
14+
/*
15+
* Lock for updating the $GIT_DIR/shallow file.
16+
*
17+
* Use `commit_shallow_file()` to commit an update, or
18+
* `rollback_shallow_file()` to roll it back. In either case, any
19+
* in-memory cached information about which commits are shallow will be
20+
* appropriately invalidated so that future operations reflect the new
21+
* state.
22+
*/
23+
struct shallow_lock {
24+
struct lock_file lock;
25+
};
26+
#define SHALLOW_LOCK_INIT { LOCK_INIT }
27+
1328
/* commit $GIT_DIR/shallow and reset stat-validity checks */
14-
int commit_shallow_file(struct repository *r, struct lock_file *lk);
29+
int commit_shallow_file(struct repository *r, struct shallow_lock *lk);
1530
/* rollback $GIT_DIR/shallow and reset stat-validity checks */
16-
void rollback_shallow_file(struct repository *r, struct lock_file *lk);
31+
void rollback_shallow_file(struct repository *r, struct shallow_lock *lk);
1732

1833
struct commit_list *get_shallow_commits(struct object_array *heads,
1934
int depth, int shallow_flag, int not_shallow_flag);
@@ -22,7 +37,7 @@ struct commit_list *get_shallow_commits_by_rev_list(
2237
int write_shallow_commits(struct strbuf *out, int use_pack_protocol,
2338
const struct oid_array *extra);
2439

25-
void setup_alternate_shallow(struct lock_file *shallow_lock,
40+
void setup_alternate_shallow(struct shallow_lock *shallow_lock,
2641
const char **alternate_shallow_file,
2742
const struct oid_array *extra);
2843

0 commit comments

Comments
 (0)