diff options
Diffstat (limited to 'server-info.c')
-rw-r--r-- | server-info.c | 137 |
1 files changed, 86 insertions, 51 deletions
diff --git a/server-info.c b/server-info.c index f9be5a7f60..c82e9ee396 100644 --- a/server-info.c +++ b/server-info.c @@ -4,44 +4,81 @@ #include "commit.h" #include "tag.h" -/* refs */ -static FILE *info_ref_fp; +/* + * Create the file "path" by writing to a temporary file and renaming + * it into place. The contents of the file come from "generate", which + * should return non-zero if it encounters an error. + */ +static int update_info_file(char *path, int (*generate)(FILE *)) +{ + char *tmp = mkpathdup("%s_XXXXXX", path); + int ret = -1; + int fd = -1; + FILE *fp = NULL; + + safe_create_leading_directories(path); + fd = git_mkstemp_mode(tmp, 0666); + if (fd < 0) + goto out; + fp = fdopen(fd, "w"); + if (!fp) + goto out; + ret = generate(fp); + if (ret) + goto out; + if (fclose(fp)) + goto out; + if (adjust_shared_perm(tmp) < 0) + goto out; + if (rename(tmp, path) < 0) + goto out; + ret = 0; + +out: + if (ret) { + error("unable to update %s: %s", path, strerror(errno)); + if (fp) + fclose(fp); + else if (fd >= 0) + close(fd); + unlink(tmp); + } + free(tmp); + return ret; +} -static int add_info_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data) +static int add_info_ref(const char *path, const struct object_id *oid, + int flag, void *cb_data) { - struct object *o = parse_object(sha1); + FILE *fp = cb_data; + struct object *o = parse_object(oid->hash); if (!o) return -1; - fprintf(info_ref_fp, "%s %s\n", sha1_to_hex(sha1), path); + if (fprintf(fp, "%s %s\n", oid_to_hex(oid), path) < 0) + return -1; + if (o->type == OBJ_TAG) { o = deref_tag(o, path, 0); if (o) - fprintf(info_ref_fp, "%s %s^{}\n", - sha1_to_hex(o->sha1), path); + if (fprintf(fp, "%s %s^{}\n", + sha1_to_hex(o->sha1), path) < 0) + return -1; } return 0; } +static int generate_info_refs(FILE *fp) +{ + return for_each_ref(add_info_ref, fp); +} + static int update_info_refs(int force) { - char *path0 = xstrdup(git_path("info/refs")); - int len = strlen(path0); - char *path1 = xmalloc(len + 2); - - strcpy(path1, path0); - strcpy(path1 + len, "+"); - - safe_create_leading_directories(path0); - info_ref_fp = fopen(path1, "w"); - if (!info_ref_fp) - return error("unable to update %s", path0); - for_each_ref(add_info_ref, NULL); - fclose(info_ref_fp); - rename(path1, path0); - free(path0); - free(path1); - return 0; + char *path = git_pathdup("info/refs"); + int ret = update_info_file(path, generate_info_refs); + free(path); + return ret; } /* packs */ @@ -100,7 +137,7 @@ static int read_pack_info_file(const char *infofile) while (fgets(line, sizeof(line), fp)) { int len = strlen(line); - if (line[len-1] == '\n') + if (len && line[len-1] == '\n') line[--len] = 0; if (!len) @@ -112,11 +149,8 @@ static int read_pack_info_file(const char *infofile) goto out_stale; break; case 'D': /* we used to emit D but that was misguided. */ - goto out_stale; - break; case 'T': /* we used to emit T but nobody uses it. */ goto out_stale; - break; default: error("unrecognized: %s", line); break; @@ -131,8 +165,8 @@ static int read_pack_info_file(const char *infofile) static int compare_info(const void *a_, const void *b_) { - struct pack_info * const* a = a_; - struct pack_info * const* b = b_; + struct pack_info *const *a = a_; + struct pack_info *const *b = b_; if (0 <= (*a)->old_num && 0 <= (*b)->old_num) /* Keep the order in the original */ @@ -200,35 +234,36 @@ static void init_pack_info(const char *infofile, int force) info[i]->new_num = i; } -static void write_pack_info_file(FILE *fp) +static void free_pack_info(void) { int i; for (i = 0; i < num_pack; i++) - fprintf(fp, "P %s\n", info[i]->p->pack_name + objdirlen + 6); - fputc('\n', fp); + free(info[i]); + free(info); } -static int update_info_packs(int force) +static int write_pack_info_file(FILE *fp) { - char infofile[PATH_MAX]; - char name[PATH_MAX]; - int namelen; - FILE *fp; + int i; + for (i = 0; i < num_pack; i++) { + if (fprintf(fp, "P %s\n", info[i]->p->pack_name + objdirlen + 6) < 0) + return -1; + } + if (fputc('\n', fp) == EOF) + return -1; + return 0; +} - namelen = sprintf(infofile, "%s/info/packs", get_object_directory()); - strcpy(name, infofile); - strcpy(name + namelen, "+"); +static int update_info_packs(int force) +{ + char *infofile = mkpathdup("%s/info/packs", get_object_directory()); + int ret; init_pack_info(infofile, force); - - safe_create_leading_directories(name); - fp = fopen(name, "w"); - if (!fp) - return error("cannot open %s", name); - write_pack_info_file(fp); - fclose(fp); - rename(name, infofile); - return 0; + ret = update_info_file(infofile, write_pack_info_file); + free_pack_info(); + free(infofile); + return ret; } /* public */ @@ -244,7 +279,7 @@ int update_server_info(int force) errs = errs | update_info_packs(force); /* remove leftover rev-cache file if there is any */ - unlink(git_path("info/rev-cache")); + unlink_or_warn(git_path("info/rev-cache")); return errs; } |