2 * Copyright (c) 2011, Google Inc.
4 #include "git-compat-util.h"
5 #include "bulk-checkin.h"
6 #include "environment.h"
10 #include "repository.h"
11 #include "csum-file.h"
14 #include "string-list.h"
15 #include "tmp-objdir.h"
17 #include "object-file.h"
18 #include "object-store-ll.h"
20 static int odb_transaction_nesting
;
22 static struct tmp_objdir
*bulk_fsync_objdir
;
24 static struct bulk_checkin_packfile
{
28 struct pack_idx_option pack_idx_opts
;
30 struct pack_idx_entry
**written
;
31 uint32_t alloc_written
;
33 } bulk_checkin_packfile
;
35 static void finish_tmp_packfile(struct strbuf
*basename
,
36 const char *pack_tmp_name
,
37 struct pack_idx_entry
**written_list
,
39 struct pack_idx_option
*pack_idx_opts
,
42 char *idx_tmp_name
= NULL
;
44 stage_tmp_packfiles(basename
, pack_tmp_name
, written_list
, nr_written
,
45 NULL
, pack_idx_opts
, hash
, &idx_tmp_name
);
46 rename_tmp_packfile_idx(basename
, &idx_tmp_name
);
51 static void flush_bulk_checkin_packfile(struct bulk_checkin_packfile
*state
)
53 unsigned char hash
[GIT_MAX_RAWSZ
];
54 struct strbuf packname
= STRBUF_INIT
;
60 if (state
->nr_written
== 0) {
62 unlink(state
->pack_tmp_name
);
64 } else if (state
->nr_written
== 1) {
65 finalize_hashfile(state
->f
, hash
, FSYNC_COMPONENT_PACK
,
66 CSUM_HASH_IN_STREAM
| CSUM_FSYNC
| CSUM_CLOSE
);
68 int fd
= finalize_hashfile(state
->f
, hash
, FSYNC_COMPONENT_PACK
, 0);
69 fixup_pack_header_footer(fd
, hash
, state
->pack_tmp_name
,
70 state
->nr_written
, hash
,
75 strbuf_addf(&packname
, "%s/pack/pack-%s.", get_object_directory(),
77 finish_tmp_packfile(&packname
, state
->pack_tmp_name
,
78 state
->written
, state
->nr_written
,
79 &state
->pack_idx_opts
, hash
);
80 for (i
= 0; i
< state
->nr_written
; i
++)
81 free(state
->written
[i
]);
85 memset(state
, 0, sizeof(*state
));
87 strbuf_release(&packname
);
88 /* Make objects we just wrote available to ourselves */
89 reprepare_packed_git(the_repository
);
93 * Cleanup after batch-mode fsync_object_files.
95 static void flush_batch_fsync(void)
97 struct strbuf temp_path
= STRBUF_INIT
;
98 struct tempfile
*temp
;
100 if (!bulk_fsync_objdir
)
104 * Issue a full hardware flush against a temporary file to ensure
105 * that all objects are durable before any renames occur. The code in
106 * fsync_loose_object_bulk_checkin has already issued a writeout
107 * request, but it has not flushed any writeback cache in the storage
108 * hardware or any filesystem logs. This fsync call acts as a barrier
109 * to ensure that the data in each new object file is durable before
110 * the final name is visible.
112 strbuf_addf(&temp_path
, "%s/bulk_fsync_XXXXXX", get_object_directory());
113 temp
= xmks_tempfile(temp_path
.buf
);
114 fsync_or_die(get_tempfile_fd(temp
), get_tempfile_path(temp
));
115 delete_tempfile(&temp
);
116 strbuf_release(&temp_path
);
119 * Make the object files visible in the primary ODB after their data is
122 tmp_objdir_migrate(bulk_fsync_objdir
);
123 bulk_fsync_objdir
= NULL
;
126 static int already_written(struct bulk_checkin_packfile
*state
, struct object_id
*oid
)
130 /* The object may already exist in the repository */
131 if (repo_has_object_file(the_repository
, oid
))
134 /* Might want to keep the list sorted */
135 for (i
= 0; i
< state
->nr_written
; i
++)
136 if (oideq(&state
->written
[i
]->oid
, oid
))
139 /* This is a new object we need to keep */
144 * Read the contents from fd for size bytes, streaming it to the
145 * packfile in state while updating the hash in ctx. Signal a failure
146 * by returning a negative value when the resulting pack would exceed
147 * the pack size limit and this is not the first object in the pack,
148 * so that the caller can discard what we wrote from the current pack
149 * by truncating it and opening a new one. The caller will then call
150 * us again after rewinding the input fd.
152 * The already_hashed_to pointer is kept untouched by the caller to
153 * make sure we do not hash the same byte when we are called
154 * again. This way, the caller does not have to checkpoint its hash
155 * status before calling us just in case we ask it to call us again
158 static int stream_to_pack(struct bulk_checkin_packfile
*state
,
159 git_hash_ctx
*ctx
, off_t
*already_hashed_to
,
160 int fd
, size_t size
, enum object_type type
,
161 const char *path
, unsigned flags
)
164 unsigned char ibuf
[16384];
165 unsigned char obuf
[16384];
168 int write_object
= (flags
& HASH_WRITE_OBJECT
);
171 git_deflate_init(&s
, pack_compression_level
);
173 hdrlen
= encode_in_pack_object_header(obuf
, sizeof(obuf
), type
, size
);
174 s
.next_out
= obuf
+ hdrlen
;
175 s
.avail_out
= sizeof(obuf
) - hdrlen
;
177 while (status
!= Z_STREAM_END
) {
178 if (size
&& !s
.avail_in
) {
179 ssize_t rsize
= size
< sizeof(ibuf
) ? size
: sizeof(ibuf
);
180 ssize_t read_result
= read_in_full(fd
, ibuf
, rsize
);
182 die_errno("failed to read from '%s'", path
);
183 if (read_result
!= rsize
)
184 die("failed to read %d bytes from '%s'",
187 if (*already_hashed_to
< offset
) {
188 size_t hsize
= offset
- *already_hashed_to
;
192 the_hash_algo
->update_fn(ctx
, ibuf
, hsize
);
193 *already_hashed_to
= offset
;
200 status
= git_deflate(&s
, size
? 0 : Z_FINISH
);
202 if (!s
.avail_out
|| status
== Z_STREAM_END
) {
204 size_t written
= s
.next_out
- obuf
;
206 /* would we bust the size limit? */
207 if (state
->nr_written
&&
208 pack_size_limit_cfg
&&
209 pack_size_limit_cfg
< state
->offset
+ written
) {
210 git_deflate_abort(&s
);
214 hashwrite(state
->f
, obuf
, written
);
215 state
->offset
+= written
;
218 s
.avail_out
= sizeof(obuf
);
227 die("unexpected deflate failure: %d", status
);
234 /* Lazily create backing packfile for the state */
235 static void prepare_to_stream(struct bulk_checkin_packfile
*state
,
238 if (!(flags
& HASH_WRITE_OBJECT
) || state
->f
)
241 state
->f
= create_tmp_packfile(&state
->pack_tmp_name
);
242 reset_pack_idx_option(&state
->pack_idx_opts
);
244 /* Pretend we are going to write only one object */
245 state
->offset
= write_pack_header(state
->f
, 1);
247 die_errno("unable to write pack header");
250 static int deflate_to_pack(struct bulk_checkin_packfile
*state
,
251 struct object_id
*result_oid
,
253 enum object_type type
, const char *path
,
256 off_t seekback
, already_hashed_to
;
258 unsigned char obuf
[16384];
260 struct hashfile_checkpoint checkpoint
= {0};
261 struct pack_idx_entry
*idx
= NULL
;
263 seekback
= lseek(fd
, 0, SEEK_CUR
);
264 if (seekback
== (off_t
) -1)
265 return error("cannot find the current offset");
267 header_len
= format_object_header((char *)obuf
, sizeof(obuf
),
269 the_hash_algo
->init_fn(&ctx
);
270 the_hash_algo
->update_fn(&ctx
, obuf
, header_len
);
271 the_hash_algo
->init_fn(&checkpoint
.ctx
);
273 /* Note: idx is non-NULL when we are writing */
274 if ((flags
& HASH_WRITE_OBJECT
) != 0)
275 CALLOC_ARRAY(idx
, 1);
277 already_hashed_to
= 0;
280 prepare_to_stream(state
, flags
);
282 hashfile_checkpoint(state
->f
, &checkpoint
);
283 idx
->offset
= state
->offset
;
284 crc32_begin(state
->f
);
286 if (!stream_to_pack(state
, &ctx
, &already_hashed_to
,
287 fd
, size
, type
, path
, flags
))
290 * Writing this object to the current pack will make
291 * it too big; we need to truncate it, start a new
292 * pack, and write into it.
295 BUG("should not happen");
296 hashfile_truncate(state
->f
, &checkpoint
);
297 state
->offset
= checkpoint
.offset
;
298 flush_bulk_checkin_packfile(state
);
299 if (lseek(fd
, seekback
, SEEK_SET
) == (off_t
) -1)
300 return error("cannot seek back");
302 the_hash_algo
->final_oid_fn(result_oid
, &ctx
);
306 idx
->crc32
= crc32_end(state
->f
);
307 if (already_written(state
, result_oid
)) {
308 hashfile_truncate(state
->f
, &checkpoint
);
309 state
->offset
= checkpoint
.offset
;
312 oidcpy(&idx
->oid
, result_oid
);
313 ALLOC_GROW(state
->written
,
314 state
->nr_written
+ 1,
315 state
->alloc_written
);
316 state
->written
[state
->nr_written
++] = idx
;
321 void prepare_loose_object_bulk_checkin(void)
324 * We lazily create the temporary object directory
325 * the first time an object might be added, since
326 * callers may not know whether any objects will be
327 * added at the time they call begin_odb_transaction.
329 if (!odb_transaction_nesting
|| bulk_fsync_objdir
)
332 bulk_fsync_objdir
= tmp_objdir_create("bulk-fsync");
333 if (bulk_fsync_objdir
)
334 tmp_objdir_replace_primary_odb(bulk_fsync_objdir
, 0);
337 void fsync_loose_object_bulk_checkin(int fd
, const char *filename
)
340 * If we have an active ODB transaction, we issue a call that
341 * cleans the filesystem page cache but avoids a hardware flush
342 * command. Later on we will issue a single hardware flush
343 * before renaming the objects to their final names as part of
346 if (!bulk_fsync_objdir
||
347 git_fsync(fd
, FSYNC_WRITEOUT_ONLY
) < 0) {
349 warning(_("core.fsyncMethod = batch is unsupported on this platform"));
350 fsync_or_die(fd
, filename
);
354 int index_bulk_checkin(struct object_id
*oid
,
355 int fd
, size_t size
, enum object_type type
,
356 const char *path
, unsigned flags
)
358 int status
= deflate_to_pack(&bulk_checkin_packfile
, oid
, fd
, size
, type
,
360 if (!odb_transaction_nesting
)
361 flush_bulk_checkin_packfile(&bulk_checkin_packfile
);
365 void begin_odb_transaction(void)
367 odb_transaction_nesting
+= 1;
370 void flush_odb_transaction(void)
373 flush_bulk_checkin_packfile(&bulk_checkin_packfile
);
376 void end_odb_transaction(void)
378 odb_transaction_nesting
-= 1;
379 if (odb_transaction_nesting
< 0)
380 BUG("Unbalanced ODB transaction nesting");
382 if (odb_transaction_nesting
)
385 flush_odb_transaction();