2 * Copyright (c) 2011, Google Inc.
5 #include "bulk-checkin.h"
10 static int pack_compression_level
= Z_DEFAULT_COMPRESSION
;
12 static struct bulk_checkin_state
{
18 struct pack_idx_option pack_idx_opts
;
20 struct pack_idx_entry
**written
;
21 uint32_t alloc_written
;
25 static void finish_bulk_checkin(struct bulk_checkin_state
*state
)
28 struct strbuf packname
= STRBUF_INIT
;
34 if (state
->nr_written
== 0) {
36 unlink(state
->pack_tmp_name
);
38 } else if (state
->nr_written
== 1) {
39 sha1close(state
->f
, oid
.hash
, CSUM_FSYNC
);
41 int fd
= sha1close(state
->f
, oid
.hash
, 0);
42 fixup_pack_header_footer(fd
, oid
.hash
, state
->pack_tmp_name
,
43 state
->nr_written
, oid
.hash
,
48 strbuf_addf(&packname
, "%s/pack/pack-", get_object_directory());
49 finish_tmp_packfile(&packname
, state
->pack_tmp_name
,
50 state
->written
, state
->nr_written
,
51 &state
->pack_idx_opts
, oid
.hash
);
52 for (i
= 0; i
< state
->nr_written
; i
++)
53 free(state
->written
[i
]);
57 memset(state
, 0, sizeof(*state
));
59 strbuf_release(&packname
);
60 /* Make objects we just wrote available to ourselves */
61 reprepare_packed_git();
64 static int already_written(struct bulk_checkin_state
*state
, unsigned char sha1
[])
68 /* The object may already exist in the repository */
69 if (has_sha1_file(sha1
))
72 /* Might want to keep the list sorted */
73 for (i
= 0; i
< state
->nr_written
; i
++)
74 if (!hashcmp(state
->written
[i
]->sha1
, sha1
))
77 /* This is a new object we need to keep */
82 * Read the contents from fd for size bytes, streaming it to the
83 * packfile in state while updating the hash in ctx. Signal a failure
84 * by returning a negative value when the resulting pack would exceed
85 * the pack size limit and this is not the first object in the pack,
86 * so that the caller can discard what we wrote from the current pack
87 * by truncating it and opening a new one. The caller will then call
88 * us again after rewinding the input fd.
90 * The already_hashed_to pointer is kept untouched by the caller to
91 * make sure we do not hash the same byte when we are called
92 * again. This way, the caller does not have to checkpoint its hash
93 * status before calling us just in case we ask it to call us again
96 static int stream_to_pack(struct bulk_checkin_state
*state
,
97 git_SHA_CTX
*ctx
, off_t
*already_hashed_to
,
98 int fd
, size_t size
, enum object_type type
,
99 const char *path
, unsigned flags
)
102 unsigned char obuf
[16384];
105 int write_object
= (flags
& HASH_WRITE_OBJECT
);
108 git_deflate_init(&s
, pack_compression_level
);
110 hdrlen
= encode_in_pack_object_header(type
, size
, obuf
);
111 s
.next_out
= obuf
+ hdrlen
;
112 s
.avail_out
= sizeof(obuf
) - hdrlen
;
114 while (status
!= Z_STREAM_END
) {
115 unsigned char ibuf
[16384];
117 if (size
&& !s
.avail_in
) {
118 ssize_t rsize
= size
< sizeof(ibuf
) ? size
: sizeof(ibuf
);
119 if (read_in_full(fd
, ibuf
, rsize
) != rsize
)
120 die("failed to read %d bytes from '%s'",
123 if (*already_hashed_to
< offset
) {
124 size_t hsize
= offset
- *already_hashed_to
;
128 git_SHA1_Update(ctx
, ibuf
, hsize
);
129 *already_hashed_to
= offset
;
136 status
= git_deflate(&s
, size
? 0 : Z_FINISH
);
138 if (!s
.avail_out
|| status
== Z_STREAM_END
) {
140 size_t written
= s
.next_out
- obuf
;
142 /* would we bust the size limit? */
143 if (state
->nr_written
&&
144 pack_size_limit_cfg
&&
145 pack_size_limit_cfg
< state
->offset
+ written
) {
146 git_deflate_abort(&s
);
150 sha1write(state
->f
, obuf
, written
);
151 state
->offset
+= written
;
154 s
.avail_out
= sizeof(obuf
);
163 die("unexpected deflate failure: %d", status
);
170 /* Lazily create backing packfile for the state */
171 static void prepare_to_stream(struct bulk_checkin_state
*state
,
174 if (!(flags
& HASH_WRITE_OBJECT
) || state
->f
)
177 state
->f
= create_tmp_packfile(&state
->pack_tmp_name
);
178 reset_pack_idx_option(&state
->pack_idx_opts
);
180 /* Pretend we are going to write only one object */
181 state
->offset
= write_pack_header(state
->f
, 1);
183 die_errno("unable to write pack header");
186 static int deflate_to_pack(struct bulk_checkin_state
*state
,
187 unsigned char result_sha1
[],
189 enum object_type type
, const char *path
,
192 off_t seekback
, already_hashed_to
;
194 unsigned char obuf
[16384];
196 struct sha1file_checkpoint checkpoint
;
197 struct pack_idx_entry
*idx
= NULL
;
199 seekback
= lseek(fd
, 0, SEEK_CUR
);
200 if (seekback
== (off_t
) -1)
201 return error("cannot find the current offset");
203 header_len
= xsnprintf((char *)obuf
, sizeof(obuf
), "%s %" PRIuMAX
,
204 typename(type
), (uintmax_t)size
) + 1;
206 git_SHA1_Update(&ctx
, obuf
, header_len
);
208 /* Note: idx is non-NULL when we are writing */
209 if ((flags
& HASH_WRITE_OBJECT
) != 0)
210 idx
= xcalloc(1, sizeof(*idx
));
212 already_hashed_to
= 0;
215 prepare_to_stream(state
, flags
);
217 sha1file_checkpoint(state
->f
, &checkpoint
);
218 idx
->offset
= state
->offset
;
219 crc32_begin(state
->f
);
221 if (!stream_to_pack(state
, &ctx
, &already_hashed_to
,
222 fd
, size
, type
, path
, flags
))
225 * Writing this object to the current pack will make
226 * it too big; we need to truncate it, start a new
227 * pack, and write into it.
230 die("BUG: should not happen");
231 sha1file_truncate(state
->f
, &checkpoint
);
232 state
->offset
= checkpoint
.offset
;
233 finish_bulk_checkin(state
);
234 if (lseek(fd
, seekback
, SEEK_SET
) == (off_t
) -1)
235 return error("cannot seek back");
237 git_SHA1_Final(result_sha1
, &ctx
);
241 idx
->crc32
= crc32_end(state
->f
);
242 if (already_written(state
, result_sha1
)) {
243 sha1file_truncate(state
->f
, &checkpoint
);
244 state
->offset
= checkpoint
.offset
;
247 hashcpy(idx
->sha1
, result_sha1
);
248 ALLOC_GROW(state
->written
,
249 state
->nr_written
+ 1,
250 state
->alloc_written
);
251 state
->written
[state
->nr_written
++] = idx
;
256 int index_bulk_checkin(unsigned char *sha1
,
257 int fd
, size_t size
, enum object_type type
,
258 const char *path
, unsigned flags
)
260 int status
= deflate_to_pack(&state
, sha1
, fd
, size
, type
,
263 finish_bulk_checkin(&state
);
267 void plug_bulk_checkin(void)
272 void unplug_bulk_checkin(void)
276 finish_bulk_checkin(&state
);