657
}
658
659
struct reftable_addition {
660
- struct lock_file tables_list_lock;
660
+ struct reftable_flock tables_list_lock;
661
struct reftable_stack *stack;
662
663
char **new_tables;
676
677
add->stack = st;
678
679
- err = hold_lock_file_for_update_timeout(&add->tables_list_lock,
680
- st->list_file,
681
- LOCK_NO_DEREF,
682
- st->opts.lock_timeout_ms);
679
+ err = flock_acquire(&add->tables_list_lock, st->list_file,
680
+ st->opts.lock_timeout_ms);
681
if (err < 0) {
682
if (errno == EEXIST) {
683
err = REFTABLE_LOCK_ERROR;
687
goto done;
688
}
689
if (st->opts.default_permissions) {
692
- if (chmod(get_lock_file_path(&add->tables_list_lock),
690
+ if (chmod(add->tables_list_lock.path,
691
st->opts.default_permissions) < 0) {
692
err = REFTABLE_IO_ERROR;
693
goto done;
731
add->new_tables_len = 0;
732
add->new_tables_cap = 0;
733
736
- rollback_lock_file(&add->tables_list_lock);
734
+ flock_release(&add->tables_list_lock);
735
reftable_buf_release(&nm);
736
}
737
747
int reftable_addition_commit(struct reftable_addition *add)
748
{
749
struct reftable_buf table_list = REFTABLE_BUF_INIT;
752
- int lock_file_fd = get_lock_file_fd(&add->tables_list_lock);
750
int err = 0;
751
size_t i;
752
764
goto done;
765
}
766
770
- err = write_in_full(lock_file_fd, table_list.buf, table_list.len);
767
+ err = write_in_full(add->tables_list_lock.fd, table_list.buf, table_list.len);
768
reftable_buf_release(&table_list);
769
if (err < 0) {
770
err = REFTABLE_IO_ERROR;
771
goto done;
772
}
773
777
- err = stack_fsync(&add->stack->opts, lock_file_fd);
774
+ err = stack_fsync(&add->stack->opts, add->tables_list_lock.fd);
775
if (err < 0) {
776
err = REFTABLE_IO_ERROR;
777
goto done;
778
}
779
783
- err = commit_lock_file(&add->tables_list_lock);
780
+ err = flock_commit(&add->tables_list_lock);
781
if (err < 0) {
782
err = REFTABLE_IO_ERROR;
783
goto done;
1157
struct reftable_buf new_table_name = REFTABLE_BUF_INIT;
1158
struct reftable_buf new_table_path = REFTABLE_BUF_INIT;
1159
struct reftable_buf table_name = REFTABLE_BUF_INIT;
1163
- struct lock_file tables_list_lock = LOCK_INIT;
1164
- struct lock_file *table_locks = NULL;
1160
+ struct reftable_flock tables_list_lock = REFTABLE_FLOCK_INIT;
1161
+ struct reftable_flock *table_locks = NULL;
1162
struct reftable_tmpfile new_table = REFTABLE_TMPFILE_INIT;
1163
int is_empty_table = 0, err = 0;
1164
size_t first_to_replace, last_to_replace;
1176
* Hold the lock so that we can read "tables.list" and lock all tables
1177
* which are part of the user-specified range.
1178
*/
1182
- err = hold_lock_file_for_update_timeout(&tables_list_lock,
1183
- st->list_file,
1184
- LOCK_NO_DEREF,
1185
- st->opts.lock_timeout_ms);
1179
+ err = flock_acquire(&tables_list_lock, st->list_file, st->opts.lock_timeout_ms);
1180
if (err < 0) {
1181
if (errno == EEXIST)
1182
err = REFTABLE_LOCK_ERROR;
1199
* older process is still busy compacting tables which are preexisting
1200
* from the point of view of the newer process.
1201
*/
1208
- REFTABLE_CALLOC_ARRAY(table_locks, last - first + 1);
1202
+ REFTABLE_ALLOC_ARRAY(table_locks, last - first + 1);
1203
if (!table_locks) {
1204
err = REFTABLE_OUT_OF_MEMORY_ERROR;
1205
goto done;
1206
}
1207
+ for (i = 0; i < last - first + 1; i++)
1208
+ table_locks[i] = REFTABLE_FLOCK_INIT;
1209
1210
for (i = last + 1; i > first; i--) {
1211
err = stack_filename(&table_name, st, reader_name(st->readers[i - 1]));
1212
if (err < 0)
1213
goto done;
1214
1219
- err = hold_lock_file_for_update(&table_locks[nlocks],
1220
- table_name.buf, LOCK_NO_DEREF);
1215
+ err = flock_acquire(&table_locks[nlocks], table_name.buf, 0);
1216
if (err < 0) {
1217
/*
1218
* When the table is locked already we may do a
1248
* run into file descriptor exhaustion when we compress a lot
1249
* of tables.
1250
*/
1256
- err = close_lock_file_gently(&table_locks[nlocks++]);
1251
+ err = flock_close(&table_locks[nlocks++]);
1252
if (err < 0) {
1253
err = REFTABLE_IO_ERROR;
1254
goto done;
1260
* "tables.list" lock while compacting the locked tables. This allows
1261
* concurrent updates to the stack to proceed.
1262
*/
1268
- err = rollback_lock_file(&tables_list_lock);
1263
+ err = flock_release(&tables_list_lock);
1264
if (err < 0) {
1265
err = REFTABLE_IO_ERROR;
1266
goto done;
1283
* "tables.list". We'll then replace the compacted range of tables with
1284
* the new table.
1285
*/
1291
- err = hold_lock_file_for_update_timeout(&tables_list_lock,
1292
- st->list_file,
1293
- LOCK_NO_DEREF,
1294
- st->opts.lock_timeout_ms);
1286
+ err = flock_acquire(&tables_list_lock, st->list_file, st->opts.lock_timeout_ms);
1287
if (err < 0) {
1288
if (errno == EEXIST)
1289
err = REFTABLE_LOCK_ERROR;
1293
}
1294
1295
if (st->opts.default_permissions) {
1304
- if (chmod(get_lock_file_path(&tables_list_lock),
1296
+ if (chmod(tables_list_lock.path,
1297
st->opts.default_permissions) < 0) {
1298
err = REFTABLE_IO_ERROR;
1299
goto done;
1448
goto done;
1449
}
1450
1459
- err = write_in_full(get_lock_file_fd(&tables_list_lock),
1451
+ err = write_in_full(tables_list_lock.fd,
1452
tables_list_buf.buf, tables_list_buf.len);
1453
if (err < 0) {
1454
err = REFTABLE_IO_ERROR;
1456
goto done;
1457
}
1458
1467
- err = stack_fsync(&st->opts, get_lock_file_fd(&tables_list_lock));
1459
+ err = stack_fsync(&st->opts, tables_list_lock.fd);
1460
if (err < 0) {
1461
err = REFTABLE_IO_ERROR;
1462
unlink(new_table_path.buf);
1463
goto done;
1464
}
1465
1474
- err = commit_lock_file(&tables_list_lock);
1466
+ err = flock_commit(&tables_list_lock);
1467
if (err < 0) {
1468
err = REFTABLE_IO_ERROR;
1469
unlink(new_table_path.buf);
1484
* readers, so it is expected that unlinking tables may fail.
1485
*/
1486
for (i = 0; i < nlocks; i++) {
1495
- struct lock_file *table_lock = &table_locks[i];
1496
- const char *lock_path = get_lock_file_path(table_lock);
1487
+ struct reftable_flock *table_lock = &table_locks[i];
1488
1489
reftable_buf_reset(&table_name);
1499
- err = reftable_buf_add(&table_name, lock_path,
1500
- strlen(lock_path) - strlen(".lock"));
1490
+ err = reftable_buf_add(&table_name, table_lock->path,
1491
+ strlen(table_lock->path) - strlen(".lock"));
1492
if (err)
1493
continue;
1494
1496
}
1497
1498
done:
1508
- rollback_lock_file(&tables_list_lock);
1499
+ flock_release(&tables_list_lock);
1500
for (i = 0; table_locks && i < nlocks; i++)
1510
- rollback_lock_file(&table_locks[i]);
1501
+ flock_release(&table_locks[i]);
1502
reftable_free(table_locks);
1503
1504
tmpfile_delete(&new_table);