commit 3fb2130a1acdb6b75c8c39c903f845c49d835a2bed1f53efea0675903367e589 from: Colin Percival via: Stefan Sperling date: Thu Oct 29 20:01:26 2026 UTC add got_worktree_open_shared The function open_worktree now takes a flags argument; and both got_worktree_open and got_worktree_open_shared are wrappers around got_worktree_open_flags. Patch by Colin Percival commit - dbc85498aa04f27ae3e011e0a75f8a73173970e616145f32b49967edbd36fc9d commit + 3fb2130a1acdb6b75c8c39c903f845c49d835a2bed1f53efea0675903367e589 blob - 5f797f951539132a03c0e5589681a7692202a6ef0686363773396dd3fc220b28 blob + 455cb98413dcedd0f41144e8041661b24f4079bab2501680cc5b5d0b880d0ed2 --- include/got_worktree.h +++ include/got_worktree.h @@ -66,6 +66,10 @@ const struct got_error *got_worktree_init(const char * const struct got_error *got_worktree_open(struct got_worktree **, const char *path, const char *meta_dir); +/* As got_worktree_open except take a shared lock. */ +const struct got_error *got_worktree_open_shared(struct got_worktree **, + const char *path, const char *meta_dir); + /* Dispose of an open work tree. */ const struct got_error *got_worktree_close(struct got_worktree *); blob - 56b3f6adb0e425c98b269eef35e87693e7d12335e02d83310f492baa70be95f0 blob + 15e1b914dde5169ed1c033152acf76fd92429783d3e969d58aa476ef74c7735f --- lib/worktree_open.c +++ lib/worktree_open.c @@ -117,7 +117,7 @@ done: static const struct got_error * open_worktree(struct got_worktree **worktree, const char *path, - const char *meta_dir) + const char *meta_dir, int flags) { const struct got_error *err = NULL; char *path_meta; @@ -150,7 +150,7 @@ open_worktree(struct got_worktree **worktree, const ch goto done; } - fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK | O_CLOEXEC); + fd = open(path_lock, O_RDWR | flags | O_NONBLOCK | O_CLOEXEC); if (fd == -1) { err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY) : got_error_from_errno2("open", path_lock)); @@ -279,9 +279,9 @@ done: return err; } -const struct got_error * -got_worktree_open(struct got_worktree **worktree, const char *path, - const char *meta_dir) +static const struct got_error * +got_worktree_open_flags(struct got_worktree **worktree, const char *path, + const char *meta_dir, int flags) { const struct got_error *err = NULL; char *worktree_path; @@ -301,7 +301,7 @@ got_worktree_open(struct got_worktree **worktree, cons if (meta_dir == NULL) { for (i = 0; i < nitems(meta_dirs); i++) { err = open_worktree(worktree, worktree_path, - meta_dirs[i]); + meta_dirs[i], flags); if (err == NULL || err->code == GOT_ERR_WORKTREE_BUSY) break; @@ -312,7 +312,8 @@ got_worktree_open(struct got_worktree **worktree, cons } } } else - err = open_worktree(worktree, worktree_path, meta_dir); + err = open_worktree(worktree, worktree_path, meta_dir, + flags); if (err && !(err->code == GOT_ERR_ERRNO && errno == ENOENT)) { free(worktree_path); return err; @@ -340,6 +341,20 @@ got_worktree_open(struct got_worktree **worktree, cons } const struct got_error * +got_worktree_open(struct got_worktree **worktree, const char *path, + const char *meta_dir) +{ + return got_worktree_open_flags(worktree, path, meta_dir, O_EXLOCK); +} + +const struct got_error * +got_worktree_open_shared(struct got_worktree **worktree, const char *path, + const char *meta_dir) +{ + return got_worktree_open_flags(worktree, path, meta_dir, O_SHLOCK); +} + +const struct got_error * got_worktree_close(struct got_worktree *worktree) { const struct got_error *err = NULL;