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_tmp_packfile(struct strbuf
*basename
,
27 const char *pack_tmp_name
,
28 struct pack_idx_entry
**written_list
,
30 struct pack_idx_option
*pack_idx_opts
,
33 char *idx_tmp_name
= NULL
;
35 stage_tmp_packfiles(basename
, pack_tmp_name
, written_list
, nr_written
,
36 pack_idx_opts
, hash
, &idx_tmp_name
);
37 rename_tmp_packfile_idx(basename
, &idx_tmp_name
);
42 static void finish_bulk_checkin(struct bulk_checkin_state
*state
)
44 unsigned char hash
[GIT_MAX_RAWSZ
];
45 struct strbuf packname
= STRBUF_INIT
;
51 if (state
->nr_written
== 0) {
53 unlink(state
->pack_tmp_name
);
55 } else if (state
->nr_written
== 1) {
56 finalize_hashfile(state
->f
, hash
, CSUM_HASH_IN_STREAM
| CSUM_FSYNC
| CSUM_CLOSE
);
58 int fd
= finalize_hashfile(state
->f
, hash
, 0);
59 fixup_pack_header_footer(fd
, hash
, state
->pack_tmp_name
,
60 state
->nr_written
, hash
,
65 strbuf_addf(&packname
, "%s/pack/pack-%s.", get_object_directory(),
67 finish_tmp_packfile(&packname
, state
->pack_tmp_name
,
68 state
->written
, state
->nr_written
,
69 &state
->pack_idx_opts
, hash
);
70 for (i
= 0; i
< state
->nr_written
; i
++)
71 free(state
->written
[i
]);
75 memset(state
, 0, sizeof(*state
));
77 strbuf_release(&packname
);
78 /* Make objects we just wrote available to ourselves */
79 reprepare_packed_git(the_repository
);
82 static int already_written(struct bulk_checkin_state
*state
, struct object_id
*oid
)
86 /* The object may already exist in the repository */
87 if (has_object_file(oid
))
90 /* Might want to keep the list sorted */
91 for (i
= 0; i
< state
->nr_written
; i
++)
92 if (oideq(&state
->written
[i
]->oid
, oid
))
95 /* This is a new object we need to keep */
100 * Read the contents from fd for size bytes, streaming it to the
101 * packfile in state while updating the hash in ctx. Signal a failure
102 * by returning a negative value when the resulting pack would exceed
103 * the pack size limit and this is not the first object in the pack,
104 * so that the caller can discard what we wrote from the current pack
105 * by truncating it and opening a new one. The caller will then call
106 * us again after rewinding the input fd.
108 * The already_hashed_to pointer is kept untouched by the caller to
109 * make sure we do not hash the same byte when we are called
110 * again. This way, the caller does not have to checkpoint its hash
111 * status before calling us just in case we ask it to call us again
114 static int stream_to_pack(struct bulk_checkin_state
*state
,
115 git_hash_ctx
*ctx
, off_t
*already_hashed_to
,
116 int fd
, size_t size
, enum object_type type
,
117 const char *path
, unsigned flags
)
120 unsigned char ibuf
[16384];
121 unsigned char obuf
[16384];
124 int write_object
= (flags
& HASH_WRITE_OBJECT
);
127 git_deflate_init(&s
, pack_compression_level
);
129 hdrlen
= encode_in_pack_object_header(obuf
, sizeof(obuf
), type
, size
);
130 s
.next_out
= obuf
+ hdrlen
;
131 s
.avail_out
= sizeof(obuf
) - hdrlen
;
133 while (status
!= Z_STREAM_END
) {
134 if (size
&& !s
.avail_in
) {
135 ssize_t rsize
= size
< sizeof(ibuf
) ? size
: sizeof(ibuf
);
136 ssize_t read_result
= read_in_full(fd
, ibuf
, rsize
);
138 die_errno("failed to read from '%s'", path
);
139 if (read_result
!= rsize
)
140 die("failed to read %d bytes from '%s'",
143 if (*already_hashed_to
< offset
) {
144 size_t hsize
= offset
- *already_hashed_to
;
148 the_hash_algo
->update_fn(ctx
, ibuf
, hsize
);
149 *already_hashed_to
= offset
;
156 status
= git_deflate(&s
, size
? 0 : Z_FINISH
);
158 if (!s
.avail_out
|| status
== Z_STREAM_END
) {
160 size_t written
= s
.next_out
- obuf
;
162 /* would we bust the size limit? */
163 if (state
->nr_written
&&
164 pack_size_limit_cfg
&&
165 pack_size_limit_cfg
< state
->offset
+ written
) {
166 git_deflate_abort(&s
);
170 hashwrite(state
->f
, obuf
, written
);
171 state
->offset
+= written
;
174 s
.avail_out
= sizeof(obuf
);
183 die("unexpected deflate failure: %d", status
);
190 /* Lazily create backing packfile for the state */
191 static void prepare_to_stream(struct bulk_checkin_state
*state
,
194 if (!(flags
& HASH_WRITE_OBJECT
) || state
->f
)
197 state
->f
= create_tmp_packfile(&state
->pack_tmp_name
);
198 reset_pack_idx_option(&state
->pack_idx_opts
);
200 /* Pretend we are going to write only one object */
201 state
->offset
= write_pack_header(state
->f
, 1);
203 die_errno("unable to write pack header");
206 static int deflate_to_pack(struct bulk_checkin_state
*state
,
207 struct object_id
*result_oid
,
209 enum object_type type
, const char *path
,
212 off_t seekback
, already_hashed_to
;
214 unsigned char obuf
[16384];
216 struct hashfile_checkpoint checkpoint
= {0};
217 struct pack_idx_entry
*idx
= NULL
;
219 seekback
= lseek(fd
, 0, SEEK_CUR
);
220 if (seekback
== (off_t
) -1)
221 return error("cannot find the current offset");
223 header_len
= xsnprintf((char *)obuf
, sizeof(obuf
), "%s %" PRIuMAX
,
224 type_name(type
), (uintmax_t)size
) + 1;
225 the_hash_algo
->init_fn(&ctx
);
226 the_hash_algo
->update_fn(&ctx
, obuf
, header_len
);
228 /* Note: idx is non-NULL when we are writing */
229 if ((flags
& HASH_WRITE_OBJECT
) != 0)
230 CALLOC_ARRAY(idx
, 1);
232 already_hashed_to
= 0;
235 prepare_to_stream(state
, flags
);
237 hashfile_checkpoint(state
->f
, &checkpoint
);
238 idx
->offset
= state
->offset
;
239 crc32_begin(state
->f
);
241 if (!stream_to_pack(state
, &ctx
, &already_hashed_to
,
242 fd
, size
, type
, path
, flags
))
245 * Writing this object to the current pack will make
246 * it too big; we need to truncate it, start a new
247 * pack, and write into it.
250 BUG("should not happen");
251 hashfile_truncate(state
->f
, &checkpoint
);
252 state
->offset
= checkpoint
.offset
;
253 finish_bulk_checkin(state
);
254 if (lseek(fd
, seekback
, SEEK_SET
) == (off_t
) -1)
255 return error("cannot seek back");
257 the_hash_algo
->final_oid_fn(result_oid
, &ctx
);
261 idx
->crc32
= crc32_end(state
->f
);
262 if (already_written(state
, result_oid
)) {
263 hashfile_truncate(state
->f
, &checkpoint
);
264 state
->offset
= checkpoint
.offset
;
267 oidcpy(&idx
->oid
, result_oid
);
268 ALLOC_GROW(state
->written
,
269 state
->nr_written
+ 1,
270 state
->alloc_written
);
271 state
->written
[state
->nr_written
++] = idx
;
276 int index_bulk_checkin(struct object_id
*oid
,
277 int fd
, size_t size
, enum object_type type
,
278 const char *path
, unsigned flags
)
280 int status
= deflate_to_pack(&state
, oid
, fd
, size
, type
,
283 finish_bulk_checkin(&state
);
287 void plug_bulk_checkin(void)
292 void unplug_bulk_checkin(void)
296 finish_bulk_checkin(&state
);