summaryrefslogtreecommitdiff
path: root/pack-mtimes.c
diff options
context:
space:
mode:
authorTaylor Blau <me@ttaylorr.com>2022-05-20 23:17:35 (GMT)
committerJunio C Hamano <gitster@pobox.com>2022-05-26 22:48:26 (GMT)
commit94cd775a6c52a99caeb1278c3d8044ee109e2d3e (patch)
tree82356eb7d022d44f0bc0a290f3ffc2685fdcebb2 /pack-mtimes.c
parent3d89a8c11801af1f7aae9d009240fd43cf322845 (diff)
downloadgit-94cd775a6c52a99caeb1278c3d8044ee109e2d3e.zip
git-94cd775a6c52a99caeb1278c3d8044ee109e2d3e.tar.gz
git-94cd775a6c52a99caeb1278c3d8044ee109e2d3e.tar.bz2
pack-mtimes: support reading .mtimes files
To store the individual mtimes of objects in a cruft pack, introduce a new `.mtimes` format that can optionally accompany a single pack in the repository. The format is defined in Documentation/technical/pack-format.txt, and stores a 4-byte network order timestamp for each object in name (index) order. This patch prepares for cruft packs by defining the `.mtimes` format, and introducing a basic API that callers can use to read out individual mtimes. Signed-off-by: Taylor Blau <me@ttaylorr.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'pack-mtimes.c')
-rw-r--r--pack-mtimes.c129
1 files changed, 129 insertions, 0 deletions
diff --git a/pack-mtimes.c b/pack-mtimes.c
new file mode 100644
index 0000000..0e0aafd
--- /dev/null
+++ b/pack-mtimes.c
@@ -0,0 +1,129 @@
+#include "git-compat-util.h"
+#include "pack-mtimes.h"
+#include "object-store.h"
+#include "packfile.h"
+
+static char *pack_mtimes_filename(struct packed_git *p)
+{
+ size_t len;
+ if (!strip_suffix(p->pack_name, ".pack", &len))
+ BUG("pack_name does not end in .pack");
+ return xstrfmt("%.*s.mtimes", (int)len, p->pack_name);
+}
+
+#define MTIMES_HEADER_SIZE (12)
+
+struct mtimes_header {
+ uint32_t signature;
+ uint32_t version;
+ uint32_t hash_id;
+};
+
+static int load_pack_mtimes_file(char *mtimes_file,
+ uint32_t num_objects,
+ const uint32_t **data_p, size_t *len_p)
+{
+ int fd, ret = 0;
+ struct stat st;
+ uint32_t *data = NULL;
+ size_t mtimes_size, expected_size;
+ struct mtimes_header header;
+
+ fd = git_open(mtimes_file);
+
+ if (fd < 0) {
+ ret = -1;
+ goto cleanup;
+ }
+ if (fstat(fd, &st)) {
+ ret = error_errno(_("failed to read %s"), mtimes_file);
+ goto cleanup;
+ }
+
+ mtimes_size = xsize_t(st.st_size);
+
+ if (mtimes_size < MTIMES_HEADER_SIZE) {
+ ret = error(_("mtimes file %s is too small"), mtimes_file);
+ goto cleanup;
+ }
+
+ data = xmmap(NULL, mtimes_size, PROT_READ, MAP_PRIVATE, fd, 0);
+
+ header.signature = ntohl(data[0]);
+ header.version = ntohl(data[1]);
+ header.hash_id = ntohl(data[2]);
+
+ if (header.signature != MTIMES_SIGNATURE) {
+ ret = error(_("mtimes file %s has unknown signature"), mtimes_file);
+ goto cleanup;
+ }
+
+ if (header.version != 1) {
+ ret = error(_("mtimes file %s has unsupported version %"PRIu32),
+ mtimes_file, header.version);
+ goto cleanup;
+ }
+
+ if (!(header.hash_id == 1 || header.hash_id == 2)) {
+ ret = error(_("mtimes file %s has unsupported hash id %"PRIu32),
+ mtimes_file, header.hash_id);
+ goto cleanup;
+ }
+
+
+ expected_size = MTIMES_HEADER_SIZE;
+ expected_size = st_add(expected_size, st_mult(sizeof(uint32_t), num_objects));
+ expected_size = st_add(expected_size, 2 * (header.hash_id == 1 ? GIT_SHA1_RAWSZ : GIT_SHA256_RAWSZ));
+
+ if (mtimes_size != expected_size) {
+ ret = error(_("mtimes file %s is corrupt"), mtimes_file);
+ goto cleanup;
+ }
+
+cleanup:
+ if (ret) {
+ if (data)
+ munmap(data, mtimes_size);
+ } else {
+ *len_p = mtimes_size;
+ *data_p = data;
+ }
+
+ close(fd);
+ return ret;
+}
+
+int load_pack_mtimes(struct packed_git *p)
+{
+ char *mtimes_name = NULL;
+ int ret = 0;
+
+ if (!p->is_cruft)
+ return ret; /* not a cruft pack */
+ if (p->mtimes_map)
+ return ret; /* already loaded */
+
+ ret = open_pack_index(p);
+ if (ret < 0)
+ goto cleanup;
+
+ mtimes_name = pack_mtimes_filename(p);
+ ret = load_pack_mtimes_file(mtimes_name,
+ p->num_objects,
+ &p->mtimes_map,
+ &p->mtimes_size);
+cleanup:
+ free(mtimes_name);
+ return ret;
+}
+
+uint32_t nth_packed_mtime(struct packed_git *p, uint32_t pos)
+{
+ if (!p->mtimes_map)
+ BUG("pack .mtimes file not loaded for %s", p->pack_name);
+ if (p->num_objects <= pos)
+ BUG("pack .mtimes out-of-bounds (%"PRIu32" vs %"PRIu32")",
+ pos, p->num_objects);
+
+ return get_be32(p->mtimes_map + pos + 3);
+}