From bf592c5057a7e5e1dabd65cd1ddd526a4eca6c53 Mon Sep 17 00:00:00 2001 From: "barkalow@iabervon.org" Date: Sun, 31 Jul 2005 20:53:44 -0400 Subject: [PATCH] [PATCH] Functions for managing the set of packs the library is using (whitespace fixed) This adds support for reading an uninstalled index, and installing a pack file that was added while the program was running, as well as functions for determining where to put the file. Signed-off-by: Daniel Barkalow Signed-off-by: Junio C Hamano --- cache.h | 13 +++++++ sha1_file.c | 123 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 135 insertions(+), 1 deletion(-) diff --git a/cache.h b/cache.h index a3c279d0..381db358 100644 --- a/cache.h +++ b/cache.h @@ -174,6 +174,8 @@ extern void rollback_index_file(struct cache_file *); extern char *mkpath(const char *fmt, ...); extern char *git_path(const char *fmt, ...); extern char *sha1_file_name(const unsigned char *sha1); +extern char *sha1_pack_name(const unsigned char *sha1); +extern char *sha1_pack_index_name(const unsigned char *sha1); int safe_create_leading_directories(char *path); @@ -202,6 +204,9 @@ extern int write_sha1_to_fd(int fd, const unsigned char *sha1); extern int has_sha1_pack(const unsigned char *sha1); extern int has_sha1_file(const unsigned char *sha1); +extern int has_pack_file(const unsigned char *sha1); +extern int has_pack_index(const unsigned char *sha1); + /* Convert to/from hex/sha1 representation */ extern int get_sha1(const char *str, unsigned char *sha1); extern int get_sha1_hex(const char *hex, unsigned char *sha1); @@ -278,6 +283,7 @@ extern struct packed_git { void *pack_base; unsigned int pack_last_used; unsigned int pack_use_cnt; + unsigned char sha1[20]; char pack_name[0]; /* something like ".git/objects/pack/xxxxx.pack" */ } *packed_git; @@ -300,7 +306,14 @@ extern int path_match(const char *path, int nr, char **match); extern int get_ack(int fd, unsigned char *result_sha1); extern struct ref **get_remote_heads(int in, struct ref **list, int nr_match, char **match); +extern struct packed_git *parse_pack_index(unsigned char *sha1); + extern void prepare_packed_git(void); +extern void install_packed_git(struct packed_git *pack); + +extern struct packed_git *find_sha1_pack(const unsigned char *sha1, + struct packed_git *packs); + extern int use_packed_git(struct packed_git *); extern void unuse_packed_git(struct packed_git *); extern struct packed_git *add_packed_git(char *, int); diff --git a/sha1_file.c b/sha1_file.c index 8d3a65f5..e808c913 100644 --- a/sha1_file.c +++ b/sha1_file.c @@ -211,6 +211,56 @@ char *sha1_file_name(const unsigned char *sha1) return base; } +char *sha1_pack_name(const unsigned char *sha1) +{ + static const char hex[] = "0123456789abcdef"; + static char *name, *base, *buf; + int i; + + if (!base) { + const char *sha1_file_directory = get_object_directory(); + int len = strlen(sha1_file_directory); + base = xmalloc(len + 60); + sprintf(base, "%s/pack/pack-1234567890123456789012345678901234567890.pack", sha1_file_directory); + name = base + len + 11; + } + + buf = name; + + for (i = 0; i < 20; i++) { + unsigned int val = *sha1++; + *buf++ = hex[val >> 4]; + *buf++ = hex[val & 0xf]; + } + + return base; +} + +char *sha1_pack_index_name(const unsigned char *sha1) +{ + static const char hex[] = "0123456789abcdef"; + static char *name, *base, *buf; + int i; + + if (!base) { + const char *sha1_file_directory = get_object_directory(); + int len = strlen(sha1_file_directory); + base = xmalloc(len + 60); + sprintf(base, "%s/pack/pack-1234567890123456789012345678901234567890.idx", sha1_file_directory); + name = base + len + 11; + } + + buf = name; + + for (i = 0; i < 20; i++) { + unsigned int val = *sha1++; + *buf++ = hex[val >> 4]; + *buf++ = hex[val & 0xf]; + } + + return base; +} + struct alternate_object_database *alt_odb; /* @@ -371,6 +421,14 @@ void unuse_packed_git(struct packed_git *p) int use_packed_git(struct packed_git *p) { + if (!p->pack_size) { + struct stat st; + // We created the struct before we had the pack + stat(p->pack_name, &st); + if (!S_ISREG(st.st_mode)) + die("packfile %s not a regular file", p->pack_name); + p->pack_size = st.st_size; + } if (!p->pack_base) { int fd; struct stat st; @@ -398,8 +456,10 @@ int use_packed_git(struct packed_git *p) * this is cheap. */ if (memcmp((char*)(p->index_base) + p->index_size - 40, - p->pack_base + p->pack_size - 20, 20)) + p->pack_base + p->pack_size - 20, 20)) { + die("packfile %s does not match index.", p->pack_name); + } } p->pack_last_used = pack_used_ctr++; p->pack_use_cnt++; @@ -437,6 +497,37 @@ struct packed_git *add_packed_git(char *path, int path_len) return p; } +struct packed_git *parse_pack_index(unsigned char *sha1) +{ + struct packed_git *p; + unsigned long idx_size; + void *idx_map; + char *path = sha1_pack_index_name(sha1); + + if (check_packed_git_idx(path, &idx_size, &idx_map)) + return NULL; + + path = sha1_pack_name(sha1); + + p = xmalloc(sizeof(*p) + strlen(path) + 2); + strcpy(p->pack_name, path); + p->index_size = idx_size; + p->pack_size = 0; + p->index_base = idx_map; + p->next = NULL; + p->pack_base = NULL; + p->pack_last_used = 0; + p->pack_use_cnt = 0; + memcpy(p->sha1, sha1, 20); + return p; +} + +void install_packed_git(struct packed_git *pack) +{ + pack->next = packed_git; + packed_git = pack; +} + static void prepare_packed_git_one(char *objdir) { char path[PATH_MAX]; @@ -1000,6 +1091,20 @@ static int find_pack_entry(const unsigned char *sha1, struct pack_entry *e) return 0; } +struct packed_git *find_sha1_pack(const unsigned char *sha1, + struct packed_git *packs) +{ + struct packed_git *p; + struct pack_entry e; + + for (p = packs; p; p = p->next) { + if (find_pack_entry_one(sha1, &e, p)) + return p; + } + return NULL; + +} + int sha1_object_info(const unsigned char *sha1, char *type, unsigned long *sizep) { int status; @@ -1346,6 +1451,22 @@ int write_sha1_from_fd(const unsigned char *sha1, int fd) return 0; } +int has_pack_index(const unsigned char *sha1) +{ + struct stat st; + if (stat(sha1_pack_index_name(sha1), &st)) + return 0; + return 1; +} + +int has_pack_file(const unsigned char *sha1) +{ + struct stat st; + if (stat(sha1_pack_name(sha1), &st)) + return 0; + return 1; +} + int has_sha1_pack(const unsigned char *sha1) { struct pack_entry e; -- 2.11.0