1 #include "git-compat-util.h"
3 #include "chunk-format.h"
7 * When writing a chunk-based file format, collect the chunks in
8 * an array of chunk_info structs. The size stores the _expected_
9 * amount of data that will be written by write_fn.
14 chunk_write_fn write_fn
;
22 struct chunk_info
*chunks
;
27 struct chunkfile
*init_chunkfile(struct hashfile
*f
)
29 struct chunkfile
*cf
= xcalloc(1, sizeof(*cf
));
34 void free_chunkfile(struct chunkfile
*cf
)
42 int get_num_chunks(struct chunkfile
*cf
)
47 void add_chunk(struct chunkfile
*cf
,
52 ALLOC_GROW(cf
->chunks
, cf
->chunks_nr
+ 1, cf
->chunks_alloc
);
54 cf
->chunks
[cf
->chunks_nr
].id
= id
;
55 cf
->chunks
[cf
->chunks_nr
].write_fn
= fn
;
56 cf
->chunks
[cf
->chunks_nr
].size
= size
;
60 int write_chunkfile(struct chunkfile
*cf
, void *data
)
63 uint64_t cur_offset
= hashfile_total(cf
->f
);
65 trace2_region_enter("chunkfile", "write", the_repository
);
67 /* Add the table of contents to the current offset */
68 cur_offset
+= (cf
->chunks_nr
+ 1) * CHUNK_TOC_ENTRY_SIZE
;
70 for (i
= 0; i
< cf
->chunks_nr
; i
++) {
71 hashwrite_be32(cf
->f
, cf
->chunks
[i
].id
);
72 hashwrite_be64(cf
->f
, cur_offset
);
74 cur_offset
+= cf
->chunks
[i
].size
;
77 /* Trailing entry marks the end of the chunks */
78 hashwrite_be32(cf
->f
, 0);
79 hashwrite_be64(cf
->f
, cur_offset
);
81 for (i
= 0; i
< cf
->chunks_nr
; i
++) {
82 off_t start_offset
= hashfile_total(cf
->f
);
83 result
= cf
->chunks
[i
].write_fn(cf
->f
, data
);
88 if (hashfile_total(cf
->f
) - start_offset
!= cf
->chunks
[i
].size
)
89 BUG("expected to write %"PRId64
" bytes to chunk %"PRIx32
", but wrote %"PRId64
" instead",
90 cf
->chunks
[i
].size
, cf
->chunks
[i
].id
,
91 hashfile_total(cf
->f
) - start_offset
);
95 trace2_region_leave("chunkfile", "write", the_repository
);
99 int read_table_of_contents(struct chunkfile
*cf
,
100 const unsigned char *mfile
,
107 const unsigned char *table_of_contents
= mfile
+ toc_offset
;
109 ALLOC_GROW(cf
->chunks
, toc_length
, cf
->chunks_alloc
);
111 while (toc_length
--) {
112 uint64_t chunk_offset
, next_chunk_offset
;
114 chunk_id
= get_be32(table_of_contents
);
115 chunk_offset
= get_be64(table_of_contents
+ 4);
118 error(_("terminating chunk id appears earlier than expected"));
122 table_of_contents
+= CHUNK_TOC_ENTRY_SIZE
;
123 next_chunk_offset
= get_be64(table_of_contents
+ 4);
125 if (next_chunk_offset
< chunk_offset
||
126 next_chunk_offset
> mfile_size
- the_hash_algo
->rawsz
) {
127 error(_("improper chunk offset(s) %"PRIx64
" and %"PRIx64
""),
128 chunk_offset
, next_chunk_offset
);
132 for (i
= 0; i
< cf
->chunks_nr
; i
++) {
133 if (cf
->chunks
[i
].id
== chunk_id
) {
134 error(_("duplicate chunk ID %"PRIx32
" found"),
140 cf
->chunks
[cf
->chunks_nr
].id
= chunk_id
;
141 cf
->chunks
[cf
->chunks_nr
].start
= mfile
+ chunk_offset
;
142 cf
->chunks
[cf
->chunks_nr
].size
= next_chunk_offset
- chunk_offset
;
146 chunk_id
= get_be32(table_of_contents
);
148 error(_("final chunk has non-zero id %"PRIx32
""), chunk_id
);
155 static int pair_chunk_fn(const unsigned char *chunk_start
,
159 const unsigned char **p
= data
;
164 int pair_chunk(struct chunkfile
*cf
,
166 const unsigned char **p
)
168 return read_chunk(cf
, chunk_id
, pair_chunk_fn
, p
);
171 int read_chunk(struct chunkfile
*cf
,
178 for (i
= 0; i
< cf
->chunks_nr
; i
++) {
179 if (cf
->chunks
[i
].id
== chunk_id
)
180 return fn(cf
->chunks
[i
].start
, cf
->chunks
[i
].size
, data
);
183 return CHUNK_NOT_FOUND
;
186 uint8_t oid_version(const struct git_hash_algo
*algop
)
188 switch (hash_algo_by_ptr(algop
)) {
191 case GIT_HASH_SHA256
:
194 die(_("invalid hash version"));