summaryrefslogtreecommitdiff
path: root/wrapper.c
diff options
context:
space:
mode:
authorJunio C Hamano <gitster@pobox.com>2009-02-24 23:11:29 -0800
committerJunio C Hamano <gitster@pobox.com>2009-02-25 14:39:42 -0800
commit6e180cdcecbb3e828aa892925d7ef67abf81ad80 (patch)
treea351bdda2ecb3ba6db58a56fe81daa1a24024590 /wrapper.c
parent718258e256b74622aa55f5ee0cb9cff4cce6bf9f (diff)
downloadgit-6e180cdcecbb3e828aa892925d7ef67abf81ad80.tar.gz
Make sure objects/pack exists before creating a new pack
In a repository created with git older than f49fb35 (git-init-db: create "pack" subdirectory under objects, 2005-06-27), objects/pack/ directory is not created upon initialization. It was Ok because subdirectories are created as needed inside directories init-db creates, and back then, packfiles were recent invention. After the said commit, new codepaths started relying on the presense of objects/pack/ directory in the repository. This was exacerbated with 8b4eb6b (Do not perform cross-directory renames when creating packs, 2008-09-22) that moved the location temporary pack files are created from objects/ directory to objects/pack/ directory, because moving temporary to the final location was done carefully with lazy leading directory creation. Many packfile related operations in such an old repository can fail mysteriously because of this. This commit introduces two helper functions to make things work better. - odb_mkstemp() is a specialized version of mkstemp() to refactor the code and teach it to create leading directories as needed; - odb_pack_keep() refactors the code to create a ".keep" file while create leading directories as needed. Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'wrapper.c')
-rw-r--r--wrapper.c32
1 files changed, 32 insertions, 0 deletions
diff --git a/wrapper.c b/wrapper.c
index 93562f03ee..231a58f1a9 100644
--- 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);
+}