Make sure objects/pack exists before creating a new pack
[gitweb.git] / wrapper.c
index 93562f03eef21b26945d2d9bbdc96818f4de6567..231a58f1a9581b2f7e150ffe4e2f908dca3e3e61 100644 (file)
--- a/wrapper.c
+++ b/wrapper.c
@@ -196,3 +196,35 @@ int xmkstemp(char *template)
                die("Unable to create temporary file: %s", strerror(errno));
        return fd;
 }
+
+int odb_mkstemp(char *template, size_t limit, const char *pattern)
+{
+       int fd;
+
+       snprintf(template, limit, "%s/%s",
+                get_object_directory(), pattern);
+       fd = mkstemp(template);
+       if (0 <= fd)
+               return fd;
+
+       /* slow path */
+       safe_create_leading_directories(template);
+       snprintf(template, limit, "%s/%s",
+                get_object_directory(), pattern);
+       return xmkstemp(template);
+}
+
+int odb_pack_keep(char *name, size_t namesz, unsigned char *sha1)
+{
+       int fd;
+
+       snprintf(name, namesz, "%s/pack/pack-%s.keep",
+                get_object_directory(), sha1_to_hex(sha1));
+       fd = open(name, O_RDWR|O_CREAT|O_EXCL, 0600);
+       if (0 <= fd)
+               return fd;
+
+       /* slow path */
+       safe_create_leading_directories(name);
+       return open(name, O_RDWR|O_CREAT|O_EXCL, 0600);
+}