]> git.ipfire.org Git - thirdparty/git.git/blame - pack-mtimes.c
object-file: remove OBJECT_INFO_IGNORE_LOOSE
[thirdparty/git.git] / pack-mtimes.c
CommitLineData
94cd775a
TB
1#include "git-compat-util.h"
2#include "pack-mtimes.h"
3#include "object-store.h"
4#include "packfile.h"
5
6static char *pack_mtimes_filename(struct packed_git *p)
7{
8 size_t len;
9 if (!strip_suffix(p->pack_name, ".pack", &len))
10 BUG("pack_name does not end in .pack");
11 return xstrfmt("%.*s.mtimes", (int)len, p->pack_name);
12}
13
14#define MTIMES_HEADER_SIZE (12)
15
16struct mtimes_header {
17 uint32_t signature;
18 uint32_t version;
19 uint32_t hash_id;
20};
21
22static int load_pack_mtimes_file(char *mtimes_file,
23 uint32_t num_objects,
24 const uint32_t **data_p, size_t *len_p)
25{
26 int fd, ret = 0;
27 struct stat st;
28 uint32_t *data = NULL;
29 size_t mtimes_size, expected_size;
30 struct mtimes_header header;
31
32 fd = git_open(mtimes_file);
33
34 if (fd < 0) {
35 ret = -1;
36 goto cleanup;
37 }
38 if (fstat(fd, &st)) {
39 ret = error_errno(_("failed to read %s"), mtimes_file);
40 goto cleanup;
41 }
42
43 mtimes_size = xsize_t(st.st_size);
44
45 if (mtimes_size < MTIMES_HEADER_SIZE) {
46 ret = error(_("mtimes file %s is too small"), mtimes_file);
47 goto cleanup;
48 }
49
50 data = xmmap(NULL, mtimes_size, PROT_READ, MAP_PRIVATE, fd, 0);
51
52 header.signature = ntohl(data[0]);
53 header.version = ntohl(data[1]);
54 header.hash_id = ntohl(data[2]);
55
56 if (header.signature != MTIMES_SIGNATURE) {
57 ret = error(_("mtimes file %s has unknown signature"), mtimes_file);
58 goto cleanup;
59 }
60
61 if (header.version != 1) {
62 ret = error(_("mtimes file %s has unsupported version %"PRIu32),
63 mtimes_file, header.version);
64 goto cleanup;
65 }
66
67 if (!(header.hash_id == 1 || header.hash_id == 2)) {
68 ret = error(_("mtimes file %s has unsupported hash id %"PRIu32),
69 mtimes_file, header.hash_id);
70 goto cleanup;
71 }
72
73
74 expected_size = MTIMES_HEADER_SIZE;
75 expected_size = st_add(expected_size, st_mult(sizeof(uint32_t), num_objects));
76 expected_size = st_add(expected_size, 2 * (header.hash_id == 1 ? GIT_SHA1_RAWSZ : GIT_SHA256_RAWSZ));
77
78 if (mtimes_size != expected_size) {
79 ret = error(_("mtimes file %s is corrupt"), mtimes_file);
80 goto cleanup;
81 }
82
83cleanup:
84 if (ret) {
85 if (data)
86 munmap(data, mtimes_size);
87 } else {
88 *len_p = mtimes_size;
89 *data_p = data;
90 }
91
41f1a8e6
JS
92 if (fd >= 0)
93 close(fd);
94cd775a
TB
94 return ret;
95}
96
97int load_pack_mtimes(struct packed_git *p)
98{
99 char *mtimes_name = NULL;
100 int ret = 0;
101
102 if (!p->is_cruft)
103 return ret; /* not a cruft pack */
104 if (p->mtimes_map)
105 return ret; /* already loaded */
106
107 ret = open_pack_index(p);
108 if (ret < 0)
109 goto cleanup;
110
111 mtimes_name = pack_mtimes_filename(p);
112 ret = load_pack_mtimes_file(mtimes_name,
113 p->num_objects,
114 &p->mtimes_map,
115 &p->mtimes_size);
116cleanup:
117 free(mtimes_name);
118 return ret;
119}
120
121uint32_t nth_packed_mtime(struct packed_git *p, uint32_t pos)
122{
123 if (!p->mtimes_map)
124 BUG("pack .mtimes file not loaded for %s", p->pack_name);
125 if (p->num_objects <= pos)
126 BUG("pack .mtimes out-of-bounds (%"PRIu32" vs %"PRIu32")",
127 pos, p->num_objects);
128
129 return get_be32(p->mtimes_map + pos + 3);
130}