2 * Copyright (c) 2011, Google Inc.
5 #include "bulk-checkin.h"
6 #include "repository.h"
11 #include "object-store.h"
13 static struct bulk_checkin_state
{
19 struct pack_idx_option pack_idx_opts
;
21 struct pack_idx_entry
**written
;
22 uint32_t alloc_written
;
26 static void finish_bulk_checkin(struct bulk_checkin_state
*state
)
29 struct strbuf packname
= STRBUF_INIT
;
35 if (state
->nr_written
== 0) {
37 unlink(state
->pack_tmp_name
);
39 } else if (state
->nr_written
== 1) {
40 finalize_hashfile(state
->f
, oid
.hash
, CSUM_HASH_IN_STREAM
| CSUM_FSYNC
| CSUM_CLOSE
);
42 int fd
= finalize_hashfile(state
->f
, oid
.hash
, 0);
43 fixup_pack_header_footer(fd
, oid
.hash
, state
->pack_tmp_name
,
44 state
->nr_written
, oid
.hash
,
49 strbuf_addf(&packname
, "%s/pack/pack-", get_object_directory());
50 finish_tmp_packfile(&packname
, state
->pack_tmp_name
,
51 state
->written
, state
->nr_written
,
52 &state
->pack_idx_opts
, oid
.hash
);
53 for (i
= 0; i
< state
->nr_written
; i
++)
54 free(state
->written
[i
]);
58 memset(state
, 0, sizeof(*state
));
60 strbuf_release(&packname
);
61 /* Make objects we just wrote available to ourselves */
62 reprepare_packed_git(the_repository
);
65 static int already_written(struct bulk_checkin_state
*state
, struct object_id
*oid
)
69 /* The object may already exist in the repository */
70 if (has_sha1_file(oid
->hash
))
73 /* Might want to keep the list sorted */
74 for (i
= 0; i
< state
->nr_written
; i
++)
75 if (oideq(&state
->written
[i
]->oid
, oid
))
78 /* This is a new object we need to keep */
83 * Read the contents from fd for size bytes, streaming it to the
84 * packfile in state while updating the hash in ctx. Signal a failure
85 * by returning a negative value when the resulting pack would exceed
86 * the pack size limit and this is not the first object in the pack,
87 * so that the caller can discard what we wrote from the current pack
88 * by truncating it and opening a new one. The caller will then call
89 * us again after rewinding the input fd.
91 * The already_hashed_to pointer is kept untouched by the caller to
92 * make sure we do not hash the same byte when we are called
93 * again. This way, the caller does not have to checkpoint its hash
94 * status before calling us just in case we ask it to call us again
97 static int stream_to_pack(struct bulk_checkin_state
*state
,
98 git_hash_ctx
*ctx
, off_t
*already_hashed_to
,
99 int fd
, size_t size
, enum object_type type
,
100 const char *path
, unsigned flags
)
103 unsigned char obuf
[16384];
106 int write_object
= (flags
& HASH_WRITE_OBJECT
);
109 git_deflate_init(&s
, pack_compression_level
);
111 hdrlen
= encode_in_pack_object_header(obuf
, sizeof(obuf
), type
, size
);
112 s
.next_out
= obuf
+ hdrlen
;
113 s
.avail_out
= sizeof(obuf
) - hdrlen
;
115 while (status
!= Z_STREAM_END
) {
116 unsigned char ibuf
[16384];
118 if (size
&& !s
.avail_in
) {
119 ssize_t rsize
= size
< sizeof(ibuf
) ? size
: sizeof(ibuf
);
120 ssize_t read_result
= read_in_full(fd
, ibuf
, rsize
);
122 die_errno("failed to read from '%s'", path
);
123 if (read_result
!= rsize
)
124 die("failed to read %d bytes from '%s'",
127 if (*already_hashed_to
< offset
) {
128 size_t hsize
= offset
- *already_hashed_to
;
132 the_hash_algo
->update_fn(ctx
, ibuf
, hsize
);
133 *already_hashed_to
= offset
;
140 status
= git_deflate(&s
, size
? 0 : Z_FINISH
);
142 if (!s
.avail_out
|| status
== Z_STREAM_END
) {
144 size_t written
= s
.next_out
- obuf
;
146 /* would we bust the size limit? */
147 if (state
->nr_written
&&
148 pack_size_limit_cfg
&&
149 pack_size_limit_cfg
< state
->offset
+ written
) {
150 git_deflate_abort(&s
);
154 hashwrite(state
->f
, obuf
, written
);
155 state
->offset
+= written
;
158 s
.avail_out
= sizeof(obuf
);
167 die("unexpected deflate failure: %d", status
);
174 /* Lazily create backing packfile for the state */
175 static void prepare_to_stream(struct bulk_checkin_state
*state
,
178 if (!(flags
& HASH_WRITE_OBJECT
) || state
->f
)
181 state
->f
= create_tmp_packfile(&state
->pack_tmp_name
);
182 reset_pack_idx_option(&state
->pack_idx_opts
);
184 /* Pretend we are going to write only one object */
185 state
->offset
= write_pack_header(state
->f
, 1);
187 die_errno("unable to write pack header");
190 static int deflate_to_pack(struct bulk_checkin_state
*state
,
191 struct object_id
*result_oid
,
193 enum object_type type
, const char *path
,
196 off_t seekback
, already_hashed_to
;
198 unsigned char obuf
[16384];
200 struct hashfile_checkpoint checkpoint
;
201 struct pack_idx_entry
*idx
= NULL
;
203 seekback
= lseek(fd
, 0, SEEK_CUR
);
204 if (seekback
== (off_t
) -1)
205 return error("cannot find the current offset");
207 header_len
= xsnprintf((char *)obuf
, sizeof(obuf
), "%s %" PRIuMAX
,
208 type_name(type
), (uintmax_t)size
) + 1;
209 the_hash_algo
->init_fn(&ctx
);
210 the_hash_algo
->update_fn(&ctx
, obuf
, header_len
);
212 /* Note: idx is non-NULL when we are writing */
213 if ((flags
& HASH_WRITE_OBJECT
) != 0)
214 idx
= xcalloc(1, sizeof(*idx
));
216 already_hashed_to
= 0;
219 prepare_to_stream(state
, flags
);
221 hashfile_checkpoint(state
->f
, &checkpoint
);
222 idx
->offset
= state
->offset
;
223 crc32_begin(state
->f
);
225 if (!stream_to_pack(state
, &ctx
, &already_hashed_to
,
226 fd
, size
, type
, path
, flags
))
229 * Writing this object to the current pack will make
230 * it too big; we need to truncate it, start a new
231 * pack, and write into it.
234 BUG("should not happen");
235 hashfile_truncate(state
->f
, &checkpoint
);
236 state
->offset
= checkpoint
.offset
;
237 finish_bulk_checkin(state
);
238 if (lseek(fd
, seekback
, SEEK_SET
) == (off_t
) -1)
239 return error("cannot seek back");
241 the_hash_algo
->final_fn(result_oid
->hash
, &ctx
);
245 idx
->crc32
= crc32_end(state
->f
);
246 if (already_written(state
, result_oid
)) {
247 hashfile_truncate(state
->f
, &checkpoint
);
248 state
->offset
= checkpoint
.offset
;
251 oidcpy(&idx
->oid
, result_oid
);
252 ALLOC_GROW(state
->written
,
253 state
->nr_written
+ 1,
254 state
->alloc_written
);
255 state
->written
[state
->nr_written
++] = idx
;
260 int index_bulk_checkin(struct object_id
*oid
,
261 int fd
, size_t size
, enum object_type type
,
262 const char *path
, unsigned flags
)
264 int status
= deflate_to_pack(&state
, oid
, fd
, size
, type
,
267 finish_bulk_checkin(&state
);
271 void plug_bulk_checkin(void)
276 void unplug_bulk_checkin(void)
280 finish_bulk_checkin(&state
);