]> git.ipfire.org Git - thirdparty/git.git/commitdiff
object-store: move and rename `odb_pack_keep()`
authorPatrick Steinhardt <ps@pks.im>
Tue, 29 Apr 2025 07:52:17 +0000 (09:52 +0200)
committerJunio C Hamano <gitster@pobox.com>
Tue, 29 Apr 2025 17:08:12 +0000 (10:08 -0700)
The function `odb_pack_keep()` creates a file at the passed-in path. If
this fails, then the function re-tries by first creating any potentially
missing leading directories and then trying to create the file once
again. As such, this function doesn't host any kind of logic that is
specific to the object store, but is rather a generic helper function.

Rename the function to `safe_create_file_with_leading_directories()` and
move it into "path.c". While at it, refactor it so that it loses its
dependency on `the_repository`.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/fast-import.c
builtin/index-pack.c
object-store.c
object-store.h
path.c
path.h

index c1e198f4e34df901ce5c0bd145d3b9e213004372..b2839c5f439b7bf5bbee70eb6ff652b1bca89ac6 100644 (file)
@@ -811,7 +811,8 @@ static char *keep_pack(const char *curr_index_name)
        int keep_fd;
 
        odb_pack_name(pack_data->repo, &name, pack_data->hash, "keep");
-       keep_fd = odb_pack_keep(name.buf);
+       keep_fd = safe_create_file_with_leading_directories(pack_data->repo,
+                                                           name.buf);
        if (keep_fd < 0)
                die_errno("cannot create keep file");
        write_or_die(keep_fd, keep_msg, strlen(keep_msg));
index 60a8ee05dbc9822fdf176fde8acfc1884b2f4feb..f49431d626b173d623949f6fa42ff68311e43106 100644 (file)
@@ -1565,7 +1565,7 @@ static void write_special_file(const char *suffix, const char *msg,
        else
                filename = odb_pack_name(the_repository, &name_buf, hash, suffix);
 
-       fd = odb_pack_keep(filename);
+       fd = safe_create_file_with_leading_directories(the_repository, filename);
        if (fd < 0) {
                if (errno != EEXIST)
                        die_errno(_("cannot write %s file '%s'"),
index e5cfb8c007915ad55c9fde4ab4cf988cf9867a22..0cbad5a19a0c045b56104ca0ecd79289d52e54c7 100644 (file)
@@ -83,19 +83,6 @@ int odb_mkstemp(struct strbuf *temp_filename, const char *pattern)
        return xmkstemp_mode(temp_filename->buf, mode);
 }
 
-int odb_pack_keep(const char *name)
-{
-       int fd;
-
-       fd = open(name, O_RDWR|O_CREAT|O_EXCL, 0600);
-       if (0 <= fd)
-               return fd;
-
-       /* slow path */
-       safe_create_leading_directories_const(the_repository, name);
-       return open(name, O_RDWR|O_CREAT|O_EXCL, 0600);
-}
-
 /*
  * Return non-zero iff the path is usable as an alternate object database.
  */
index 5668de62d01a5df8d4b5f84373e85c06e5f3bf51..aa8fc63043ec87747b33ba60c9f0f36e2da2abee 100644 (file)
@@ -189,13 +189,6 @@ void raw_object_store_clear(struct raw_object_store *o);
  */
 int odb_mkstemp(struct strbuf *temp_filename, const char *pattern);
 
-/*
- * Create a pack .keep file named "name" (which should generally be the output
- * of odb_pack_name). Returns a file descriptor opened for writing, or -1 on
- * error.
- */
-int odb_pack_keep(const char *name);
-
 void *map_loose_object(struct repository *r, const struct object_id *oid,
                       unsigned long *size);
 
diff --git a/path.c b/path.c
index 4505bb78e8b4706b5f04431a8778b8e01f10d781..3b598b2847ff03a794bd7b3ffc5d728049e757e0 100644 (file)
--- a/path.c
+++ b/path.c
@@ -1011,6 +1011,20 @@ enum scld_error safe_create_leading_directories_const(struct repository *repo,
        return result;
 }
 
+int safe_create_file_with_leading_directories(struct repository *repo,
+                                             const char *path)
+{
+       int fd;
+
+       fd = open(path, O_RDWR|O_CREAT|O_EXCL, 0600);
+       if (0 <= fd)
+               return fd;
+
+       /* slow path */
+       safe_create_leading_directories_const(repo, path);
+       return open(path, O_RDWR|O_CREAT|O_EXCL, 0600);
+}
+
 static int have_same_root(const char *path1, const char *path2)
 {
        int is_abs1, is_abs2;
diff --git a/path.h b/path.h
index fd1a194b060135e4c3fd7a4421b0aff20cf4c4c2..e67348f25397cc53bfa24c1908283e6f5420a2c3 100644 (file)
--- a/path.h
+++ b/path.h
@@ -266,6 +266,13 @@ enum scld_error safe_create_leading_directories_const(struct repository *repo,
                                                      const char *path);
 enum scld_error safe_create_leading_directories_no_share(char *path);
 
+/*
+ * Create a file, potentially creating its leading directories in case they
+ * don't exist. Returns the return value of the open(3p) call.
+ */
+int safe_create_file_with_leading_directories(struct repository *repo,
+                                             const char *path);
+
 # ifdef USE_THE_REPOSITORY_VARIABLE
 #  include "strbuf.h"
 #  include "repository.h"