Skip to content

Commit 8fd7a0e

Browse files
pks-tgitster
authored andcommitted
reftable: don't second-guess errors from flock interface
The `flock` interface is implemented as part of "reftable/system.c" and thus needs to be implemented by the integrator between the reftable library and its parent code base. As such, we cannot rely on any specific implementation thereof. Regardless of that, users of the `flock` subsystem rely on `errno` being set to specific values. This is fragile and not documented anywhere and doesn't really make for a good interface. Refactor the code so that the implementations themselves are expected to return reftable-specific error codes. Our implementation of the `flock` subsystem already knows to do this for all error paths except one. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 54d25de commit 8fd7a0e

File tree

3 files changed

+12
-31
lines changed

3 files changed

+12
-31
lines changed

reftable/stack.c

Lines changed: 8 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -698,14 +698,9 @@ static int reftable_stack_init_addition(struct reftable_addition *add,
698698

699699
err = flock_acquire(&add->tables_list_lock, st->list_file,
700700
st->opts.lock_timeout_ms);
701-
if (err < 0) {
702-
if (errno == EEXIST) {
703-
err = REFTABLE_LOCK_ERROR;
704-
} else {
705-
err = REFTABLE_IO_ERROR;
706-
}
701+
if (err < 0)
707702
goto done;
708-
}
703+
709704
if (st->opts.default_permissions) {
710705
if (chmod(add->tables_list_lock.path,
711706
st->opts.default_permissions) < 0) {
@@ -1212,13 +1207,8 @@ static int stack_compact_range(struct reftable_stack *st,
12121207
* which are part of the user-specified range.
12131208
*/
12141209
err = flock_acquire(&tables_list_lock, st->list_file, st->opts.lock_timeout_ms);
1215-
if (err < 0) {
1216-
if (errno == EEXIST)
1217-
err = REFTABLE_LOCK_ERROR;
1218-
else
1219-
err = REFTABLE_IO_ERROR;
1210+
if (err < 0)
12201211
goto done;
1221-
}
12221212

12231213
/*
12241214
* Check whether the stack is up-to-date. We unfortunately cannot
@@ -1272,7 +1262,7 @@ static int stack_compact_range(struct reftable_stack *st,
12721262
* tables, otherwise there would be nothing to compact.
12731263
* In that case, we return a lock error to our caller.
12741264
*/
1275-
if (errno == EEXIST && last - (i - 1) >= 2 &&
1265+
if (err == REFTABLE_LOCK_ERROR && last - (i - 1) >= 2 &&
12761266
flags & STACK_COMPACT_RANGE_BEST_EFFORT) {
12771267
err = 0;
12781268
/*
@@ -1284,13 +1274,9 @@ static int stack_compact_range(struct reftable_stack *st,
12841274
*/
12851275
first = (i - 1) + 1;
12861276
break;
1287-
} else if (errno == EEXIST) {
1288-
err = REFTABLE_LOCK_ERROR;
1289-
goto done;
1290-
} else {
1291-
err = REFTABLE_IO_ERROR;
1292-
goto done;
12931277
}
1278+
1279+
goto done;
12941280
}
12951281

12961282
/*
@@ -1299,10 +1285,8 @@ static int stack_compact_range(struct reftable_stack *st,
12991285
* of tables.
13001286
*/
13011287
err = flock_close(&table_locks[nlocks++]);
1302-
if (err < 0) {
1303-
err = REFTABLE_IO_ERROR;
1288+
if (err < 0)
13041289
goto done;
1305-
}
13061290
}
13071291

13081292
/*
@@ -1334,13 +1318,8 @@ static int stack_compact_range(struct reftable_stack *st,
13341318
* the new table.
13351319
*/
13361320
err = flock_acquire(&tables_list_lock, st->list_file, st->opts.lock_timeout_ms);
1337-
if (err < 0) {
1338-
if (errno == EEXIST)
1339-
err = REFTABLE_LOCK_ERROR;
1340-
else
1341-
err = REFTABLE_IO_ERROR;
1321+
if (err < 0)
13421322
goto done;
1343-
}
13441323

13451324
if (st->opts.default_permissions) {
13461325
if (chmod(tables_list_lock.path,

reftable/system.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ int flock_acquire(struct reftable_flock *l, const char *target_path,
7272
reftable_free(lockfile);
7373
if (errno == EEXIST)
7474
return REFTABLE_LOCK_ERROR;
75-
return -1;
75+
return REFTABLE_IO_ERROR;
7676
}
7777

7878
l->fd = get_lock_file_fd(lockfile);

reftable/system.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,9 @@ struct reftable_flock {
8181
* to acquire the lock. If `timeout_ms` is 0 we don't wait, if it is negative
8282
* we block indefinitely.
8383
*
84-
* Retrun 0 on success, a reftable error code on error.
84+
* Retrun 0 on success, a reftable error code on error. Specifically,
85+
* `REFTABLE_LOCK_ERROR` should be returned in case the target path is already
86+
* locked.
8587
*/
8688
int flock_acquire(struct reftable_flock *l, const char *target_path,
8789
long timeout_ms);

0 commit comments

Comments
 (0)