p = alloc_packed_git(r, alloc);
memcpy(p->pack_name, path, path_len);
+ /*
+ * Note that we have to check auxiliary data structures before we check
+ * for the ".pack" file to exist to avoid races with a packfile that is
+ * in the process of being deleted. The ".pack" file is unlinked before
+ * its auxiliary data structures, so we know that we either get a
+ * consistent snapshot of all data structures or that we'll fail to
+ * stat(3p) the packfile itself and thus return `NULL`.
+ *
+ * As such, we cannot bail out before the access(3p) calls in case the
+ * packfile doesn't exist without doing two stat(3p) calls for it.
+ */
xsnprintf(p->pack_name + path_len, alloc - path_len, ".keep");
if (!access(p->pack_name, F_OK))
p->pack_keep = 1;