Merge branch 'jk/pack-name-cleanups' into maint

Code clean-up.

* jk/pack-name-cleanups:
  index-pack: make pointer-alias fallbacks safer
  replace snprintf with odb_pack_name()
  odb_pack_keep(): stop generating keepfile name
  sha1_file.c: make pack-name helper globally accessible
  move odb_* declarations out of git-compat-util.h
This commit is contained in:
Junio C Hamano 2017-03-28 13:52:25 -07:00
commit ba5e05ffef
6 changed files with 57 additions and 46 deletions

View File

@ -1386,7 +1386,9 @@ static void final(const char *final_pack_name, const char *curr_pack_name,
unsigned char *sha1) unsigned char *sha1)
{ {
const char *report = "pack"; const char *report = "pack";
char name[PATH_MAX]; struct strbuf pack_name = STRBUF_INIT;
struct strbuf index_name = STRBUF_INIT;
struct strbuf keep_name_buf = STRBUF_INIT;
int err; int err;
if (!from_stdin) { if (!from_stdin) {
@ -1402,14 +1404,13 @@ static void final(const char *final_pack_name, const char *curr_pack_name,
int keep_fd, keep_msg_len = strlen(keep_msg); int keep_fd, keep_msg_len = strlen(keep_msg);
if (!keep_name) if (!keep_name)
keep_fd = odb_pack_keep(name, sizeof(name), sha1); keep_name = odb_pack_name(&keep_name_buf, sha1, "keep");
else
keep_fd = open(keep_name, O_RDWR|O_CREAT|O_EXCL, 0600);
keep_fd = odb_pack_keep(keep_name);
if (keep_fd < 0) { if (keep_fd < 0) {
if (errno != EEXIST) if (errno != EEXIST)
die_errno(_("cannot write keep file '%s'"), die_errno(_("cannot write keep file '%s'"),
keep_name ? keep_name : name); keep_name);
} else { } else {
if (keep_msg_len > 0) { if (keep_msg_len > 0) {
write_or_die(keep_fd, keep_msg, keep_msg_len); write_or_die(keep_fd, keep_msg, keep_msg_len);
@ -1417,28 +1418,22 @@ static void final(const char *final_pack_name, const char *curr_pack_name,
} }
if (close(keep_fd) != 0) if (close(keep_fd) != 0)
die_errno(_("cannot close written keep file '%s'"), die_errno(_("cannot close written keep file '%s'"),
keep_name ? keep_name : name); keep_name);
report = "keep"; report = "keep";
} }
} }
if (final_pack_name != curr_pack_name) { if (final_pack_name != curr_pack_name) {
if (!final_pack_name) { if (!final_pack_name)
snprintf(name, sizeof(name), "%s/pack/pack-%s.pack", final_pack_name = odb_pack_name(&pack_name, sha1, "pack");
get_object_directory(), sha1_to_hex(sha1));
final_pack_name = name;
}
if (finalize_object_file(curr_pack_name, final_pack_name)) if (finalize_object_file(curr_pack_name, final_pack_name))
die(_("cannot store pack file")); die(_("cannot store pack file"));
} else if (from_stdin) } else if (from_stdin)
chmod(final_pack_name, 0444); chmod(final_pack_name, 0444);
if (final_index_name != curr_index_name) { if (final_index_name != curr_index_name) {
if (!final_index_name) { if (!final_index_name)
snprintf(name, sizeof(name), "%s/pack/pack-%s.idx", final_index_name = odb_pack_name(&index_name, sha1, "idx");
get_object_directory(), sha1_to_hex(sha1));
final_index_name = name;
}
if (finalize_object_file(curr_index_name, final_index_name)) if (finalize_object_file(curr_index_name, final_index_name))
die(_("cannot store index file")); die(_("cannot store index file"));
} else } else
@ -1464,6 +1459,10 @@ static void final(const char *final_pack_name, const char *curr_pack_name,
input_offset += err; input_offset += err;
} }
} }
strbuf_release(&index_name);
strbuf_release(&pack_name);
strbuf_release(&keep_name_buf);
} }
static int git_index_pack_config(const char *k, const char *v, void *cb) static int git_index_pack_config(const char *k, const char *v, void *cb)

21
cache.h
View File

@ -1590,6 +1590,27 @@ extern struct packed_git *find_sha1_pack(const unsigned char *sha1,
extern void pack_report(void); extern void pack_report(void);
/*
* Create a temporary file rooted in the object database directory.
*/
extern int odb_mkstemp(char *template, size_t limit, const char *pattern);
/*
* Generate the filename to be used for a pack file with checksum "sha1" and
* extension "ext". The result is written into the strbuf "buf", overwriting
* any existing contents. A pointer to buf->buf is returned as a convenience.
*
* Example: odb_pack_name(out, sha1, "idx") => ".git/objects/pack/pack-1234..idx"
*/
extern char *odb_pack_name(struct strbuf *buf, const unsigned char *sha1, const char *ext);
/*
* 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.
*/
extern int odb_pack_keep(const char *name);
/* /*
* mmap the index file for the specified packfile (if it is not * mmap the index file for the specified packfile (if it is not
* already mmapped). Return 0 on success. * already mmapped). Return 0 on success.

View File

@ -296,18 +296,16 @@ int odb_mkstemp(char *template, size_t limit, const char *pattern)
return xmkstemp_mode(template, mode); return xmkstemp_mode(template, mode);
} }
int odb_pack_keep(char *name, size_t namesz, const unsigned char *sha1) int odb_pack_keep(const char *name)
{ {
int fd; 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); fd = open(name, O_RDWR|O_CREAT|O_EXCL, 0600);
if (0 <= fd) if (0 <= fd)
return fd; return fd;
/* slow path */ /* slow path */
safe_create_leading_directories(name); safe_create_leading_directories_const(name);
return open(name, O_RDWR|O_CREAT|O_EXCL, 0600); return open(name, O_RDWR|O_CREAT|O_EXCL, 0600);
} }

View File

@ -940,41 +940,40 @@ static const char *create_index(void)
static char *keep_pack(const char *curr_index_name) static char *keep_pack(const char *curr_index_name)
{ {
static char name[PATH_MAX];
static const char *keep_msg = "fast-import"; static const char *keep_msg = "fast-import";
struct strbuf name = STRBUF_INIT;
int keep_fd; int keep_fd;
keep_fd = odb_pack_keep(name, sizeof(name), pack_data->sha1); odb_pack_name(&name, pack_data->sha1, "keep");
keep_fd = odb_pack_keep(name.buf);
if (keep_fd < 0) if (keep_fd < 0)
die_errno("cannot create keep file"); die_errno("cannot create keep file");
write_or_die(keep_fd, keep_msg, strlen(keep_msg)); write_or_die(keep_fd, keep_msg, strlen(keep_msg));
if (close(keep_fd)) if (close(keep_fd))
die_errno("failed to write keep file"); die_errno("failed to write keep file");
snprintf(name, sizeof(name), "%s/pack/pack-%s.pack", odb_pack_name(&name, pack_data->sha1, "pack");
get_object_directory(), sha1_to_hex(pack_data->sha1)); if (finalize_object_file(pack_data->pack_name, name.buf))
if (finalize_object_file(pack_data->pack_name, name))
die("cannot store pack file"); die("cannot store pack file");
snprintf(name, sizeof(name), "%s/pack/pack-%s.idx", odb_pack_name(&name, pack_data->sha1, "idx");
get_object_directory(), sha1_to_hex(pack_data->sha1)); if (finalize_object_file(curr_index_name, name.buf))
if (finalize_object_file(curr_index_name, name))
die("cannot store index file"); die("cannot store index file");
free((void *)curr_index_name); free((void *)curr_index_name);
return name; return strbuf_detach(&name, NULL);
} }
static void unkeep_all_packs(void) static void unkeep_all_packs(void)
{ {
static char name[PATH_MAX]; struct strbuf name = STRBUF_INIT;
int k; int k;
for (k = 0; k < pack_id; k++) { for (k = 0; k < pack_id; k++) {
struct packed_git *p = all_packs[k]; struct packed_git *p = all_packs[k];
snprintf(name, sizeof(name), "%s/pack/pack-%s.keep", odb_pack_name(&name, p->sha1, "keep");
get_object_directory(), sha1_to_hex(p->sha1)); unlink_or_warn(name.buf);
unlink_or_warn(name);
} }
strbuf_release(&name);
} }
static int loosen_small_pack(const struct packed_git *p) static int loosen_small_pack(const struct packed_git *p)
@ -1033,6 +1032,7 @@ static void end_packfile(void)
die("core git rejected index %s", idx_name); die("core git rejected index %s", idx_name);
all_packs[pack_id] = new_p; all_packs[pack_id] = new_p;
install_packed_git(new_p); install_packed_git(new_p);
free(idx_name);
/* Print the boundary */ /* Print the boundary */
if (pack_edges) { if (pack_edges) {

View File

@ -798,8 +798,6 @@ extern FILE *xfopen(const char *path, const char *mode);
extern FILE *xfdopen(int fd, const char *mode); extern FILE *xfdopen(int fd, const char *mode);
extern int xmkstemp(char *template); extern int xmkstemp(char *template);
extern int xmkstemp_mode(char *template, int mode); extern int xmkstemp_mode(char *template, int mode);
extern int odb_mkstemp(char *template, size_t limit, const char *pattern);
extern int odb_pack_keep(char *name, size_t namesz, const unsigned char *sha1);
extern char *xgetcwd(void); extern char *xgetcwd(void);
extern FILE *fopen_for_writing(const char *path); extern FILE *fopen_for_writing(const char *path);

View File

@ -203,31 +203,26 @@ static const char *alt_sha1_path(struct alternate_object_database *alt,
return buf->buf; return buf->buf;
} }
/* char *odb_pack_name(struct strbuf *buf,
* Return the name of the pack or index file with the specified sha1 const unsigned char *sha1,
* in its filename. *base and *name are scratch space that must be const char *ext)
* provided by the caller. which should be "pack" or "idx".
*/
static char *sha1_get_pack_name(const unsigned char *sha1,
struct strbuf *buf,
const char *which)
{ {
strbuf_reset(buf); strbuf_reset(buf);
strbuf_addf(buf, "%s/pack/pack-%s.%s", get_object_directory(), strbuf_addf(buf, "%s/pack/pack-%s.%s", get_object_directory(),
sha1_to_hex(sha1), which); sha1_to_hex(sha1), ext);
return buf->buf; return buf->buf;
} }
char *sha1_pack_name(const unsigned char *sha1) char *sha1_pack_name(const unsigned char *sha1)
{ {
static struct strbuf buf = STRBUF_INIT; static struct strbuf buf = STRBUF_INIT;
return sha1_get_pack_name(sha1, &buf, "pack"); return odb_pack_name(&buf, sha1, "pack");
} }
char *sha1_pack_index_name(const unsigned char *sha1) char *sha1_pack_index_name(const unsigned char *sha1)
{ {
static struct strbuf buf = STRBUF_INIT; static struct strbuf buf = STRBUF_INIT;
return sha1_get_pack_name(sha1, &buf, "idx"); return odb_pack_name(&buf, sha1, "idx");
} }
struct alternate_object_database *alt_odb_list; struct alternate_object_database *alt_odb_list;