1
0
mirror of https://github.com/git/git synced 2024-07-05 00:58:49 +00:00

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)
{
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;
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);
if (!keep_name)
keep_fd = odb_pack_keep(name, sizeof(name), sha1);
else
keep_fd = open(keep_name, O_RDWR|O_CREAT|O_EXCL, 0600);
keep_name = odb_pack_name(&keep_name_buf, sha1, "keep");
keep_fd = odb_pack_keep(keep_name);
if (keep_fd < 0) {
if (errno != EEXIST)
die_errno(_("cannot write keep file '%s'"),
keep_name ? keep_name : name);
keep_name);
} else {
if (keep_msg_len > 0) {
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)
die_errno(_("cannot close written keep file '%s'"),
keep_name ? keep_name : name);
keep_name);
report = "keep";
}
}
if (final_pack_name != curr_pack_name) {
if (!final_pack_name) {
snprintf(name, sizeof(name), "%s/pack/pack-%s.pack",
get_object_directory(), sha1_to_hex(sha1));
final_pack_name = name;
}
if (!final_pack_name)
final_pack_name = odb_pack_name(&pack_name, sha1, "pack");
if (finalize_object_file(curr_pack_name, final_pack_name))
die(_("cannot store pack file"));
} else if (from_stdin)
chmod(final_pack_name, 0444);
if (final_index_name != curr_index_name) {
if (!final_index_name) {
snprintf(name, sizeof(name), "%s/pack/pack-%s.idx",
get_object_directory(), sha1_to_hex(sha1));
final_index_name = name;
}
if (!final_index_name)
final_index_name = odb_pack_name(&index_name, sha1, "idx");
if (finalize_object_file(curr_index_name, final_index_name))
die(_("cannot store index file"));
} else
@ -1464,6 +1459,10 @@ static void final(const char *final_pack_name, const char *curr_pack_name,
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)

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);
/*
* 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
* 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);
}
int odb_pack_keep(char *name, size_t namesz, const unsigned char *sha1)
int odb_pack_keep(const char *name)
{
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);
safe_create_leading_directories_const(name);
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 name[PATH_MAX];
static const char *keep_msg = "fast-import";
struct strbuf name = STRBUF_INIT;
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)
die_errno("cannot create keep file");
write_or_die(keep_fd, keep_msg, strlen(keep_msg));
if (close(keep_fd))
die_errno("failed to write keep file");
snprintf(name, sizeof(name), "%s/pack/pack-%s.pack",
get_object_directory(), sha1_to_hex(pack_data->sha1));
if (finalize_object_file(pack_data->pack_name, name))
odb_pack_name(&name, pack_data->sha1, "pack");
if (finalize_object_file(pack_data->pack_name, name.buf))
die("cannot store pack file");
snprintf(name, sizeof(name), "%s/pack/pack-%s.idx",
get_object_directory(), sha1_to_hex(pack_data->sha1));
if (finalize_object_file(curr_index_name, name))
odb_pack_name(&name, pack_data->sha1, "idx");
if (finalize_object_file(curr_index_name, name.buf))
die("cannot store index file");
free((void *)curr_index_name);
return name;
return strbuf_detach(&name, NULL);
}
static void unkeep_all_packs(void)
{
static char name[PATH_MAX];
struct strbuf name = STRBUF_INIT;
int k;
for (k = 0; k < pack_id; k++) {
struct packed_git *p = all_packs[k];
snprintf(name, sizeof(name), "%s/pack/pack-%s.keep",
get_object_directory(), sha1_to_hex(p->sha1));
unlink_or_warn(name);
odb_pack_name(&name, p->sha1, "keep");
unlink_or_warn(name.buf);
}
strbuf_release(&name);
}
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);
all_packs[pack_id] = new_p;
install_packed_git(new_p);
free(idx_name);
/* Print the boundary */
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 int xmkstemp(char *template);
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 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 the name of the pack or index file with the specified sha1
* in its filename. *base and *name are scratch space that must be
* 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)
char *odb_pack_name(struct strbuf *buf,
const unsigned char *sha1,
const char *ext)
{
strbuf_reset(buf);
strbuf_addf(buf, "%s/pack/pack-%s.%s", get_object_directory(),
sha1_to_hex(sha1), which);
sha1_to_hex(sha1), ext);
return buf->buf;
}
char *sha1_pack_name(const unsigned char *sha1)
{
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)
{
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;