write_entry: avoid reading blobs in CE_RETRY case
[git.git] / entry.c
blobbec51e37a2c162456217b3a7f52e44edbb0eabb0
1 #include "cache.h"
2 #include "blob.h"
3 #include "dir.h"
4 #include "streaming.h"
5 #include "submodule.h"
7 static void create_directories(const char *path, int path_len,
8 const struct checkout *state)
10 char *buf = xmallocz(path_len);
11 int len = 0;
13 while (len < path_len) {
14 do {
15 buf[len] = path[len];
16 len++;
17 } while (len < path_len && path[len] != '/');
18 if (len >= path_len)
19 break;
20 buf[len] = 0;
23 * For 'checkout-index --prefix=<dir>', <dir> is
24 * allowed to be a symlink to an existing directory,
25 * and we set 'state->base_dir_len' below, such that
26 * we test the path components of the prefix with the
27 * stat() function instead of the lstat() function.
29 if (has_dirs_only_path(buf, len, state->base_dir_len))
30 continue; /* ok, it is already a directory. */
33 * If this mkdir() would fail, it could be that there
34 * is already a symlink or something else exists
35 * there, therefore we then try to unlink it and try
36 * one more time to create the directory.
38 if (mkdir(buf, 0777)) {
39 if (errno == EEXIST && state->force &&
40 !unlink_or_warn(buf) && !mkdir(buf, 0777))
41 continue;
42 die_errno("cannot create directory at '%s'", buf);
45 free(buf);
48 static void remove_subtree(struct strbuf *path)
50 DIR *dir = opendir(path->buf);
51 struct dirent *de;
52 int origlen = path->len;
54 if (!dir)
55 die_errno("cannot opendir '%s'", path->buf);
56 while ((de = readdir(dir)) != NULL) {
57 struct stat st;
59 if (is_dot_or_dotdot(de->d_name))
60 continue;
62 strbuf_addch(path, '/');
63 strbuf_addstr(path, de->d_name);
64 if (lstat(path->buf, &st))
65 die_errno("cannot lstat '%s'", path->buf);
66 if (S_ISDIR(st.st_mode))
67 remove_subtree(path);
68 else if (unlink(path->buf))
69 die_errno("cannot unlink '%s'", path->buf);
70 strbuf_setlen(path, origlen);
72 closedir(dir);
73 if (rmdir(path->buf))
74 die_errno("cannot rmdir '%s'", path->buf);
77 static int create_file(const char *path, unsigned int mode)
79 mode = (mode & 0100) ? 0777 : 0666;
80 return open(path, O_WRONLY | O_CREAT | O_EXCL, mode);
83 static void *read_blob_entry(const struct cache_entry *ce, unsigned long *size)
85 enum object_type type;
86 void *new = read_sha1_file(ce->oid.hash, &type, size);
88 if (new) {
89 if (type == OBJ_BLOB)
90 return new;
91 free(new);
93 return NULL;
96 static int open_output_fd(char *path, const struct cache_entry *ce, int to_tempfile)
98 int symlink = (ce->ce_mode & S_IFMT) != S_IFREG;
99 if (to_tempfile) {
100 xsnprintf(path, TEMPORARY_FILENAME_LENGTH, "%s",
101 symlink ? ".merge_link_XXXXXX" : ".merge_file_XXXXXX");
102 return mkstemp(path);
103 } else {
104 return create_file(path, !symlink ? ce->ce_mode : 0666);
108 static int fstat_output(int fd, const struct checkout *state, struct stat *st)
110 /* use fstat() only when path == ce->name */
111 if (fstat_is_reliable() &&
112 state->refresh_cache && !state->base_dir_len) {
113 fstat(fd, st);
114 return 1;
116 return 0;
119 static int streaming_write_entry(const struct cache_entry *ce, char *path,
120 struct stream_filter *filter,
121 const struct checkout *state, int to_tempfile,
122 int *fstat_done, struct stat *statbuf)
124 int result = 0;
125 int fd;
127 fd = open_output_fd(path, ce, to_tempfile);
128 if (fd < 0)
129 return -1;
131 result |= stream_blob_to_fd(fd, &ce->oid, filter, 1);
132 *fstat_done = fstat_output(fd, state, statbuf);
133 result |= close(fd);
135 if (result)
136 unlink(path);
137 return result;
140 void enable_delayed_checkout(struct checkout *state)
142 if (!state->delayed_checkout) {
143 state->delayed_checkout = xmalloc(sizeof(*state->delayed_checkout));
144 state->delayed_checkout->state = CE_CAN_DELAY;
145 string_list_init(&state->delayed_checkout->filters, 0);
146 string_list_init(&state->delayed_checkout->paths, 0);
150 static int remove_available_paths(struct string_list_item *item, void *cb_data)
152 struct string_list *available_paths = cb_data;
153 struct string_list_item *available;
155 available = string_list_lookup(available_paths, item->string);
156 if (available)
157 available->util = (void *)item->string;
158 return !available;
161 int finish_delayed_checkout(struct checkout *state)
163 int errs = 0;
164 struct string_list_item *filter, *path;
165 struct delayed_checkout *dco = state->delayed_checkout;
167 if (!state->delayed_checkout)
168 return errs;
170 dco->state = CE_RETRY;
171 while (dco->filters.nr > 0) {
172 for_each_string_list_item(filter, &dco->filters) {
173 struct string_list available_paths = STRING_LIST_INIT_NODUP;
175 if (!async_query_available_blobs(filter->string, &available_paths)) {
176 /* Filter reported an error */
177 errs = 1;
178 filter->string = "";
179 continue;
181 if (available_paths.nr <= 0) {
183 * Filter responded with no entries. That means
184 * the filter is done and we can remove the
185 * filter from the list (see
186 * "string_list_remove_empty_items" call below).
188 filter->string = "";
189 continue;
193 * In dco->paths we store a list of all delayed paths.
194 * The filter just send us a list of available paths.
195 * Remove them from the list.
197 filter_string_list(&dco->paths, 0,
198 &remove_available_paths, &available_paths);
200 for_each_string_list_item(path, &available_paths) {
201 struct cache_entry* ce;
203 if (!path->util) {
204 error("external filter '%s' signaled that '%s' "
205 "is now available although it has not been "
206 "delayed earlier",
207 filter->string, path->string);
208 errs |= 1;
211 * Do not ask the filter for available blobs,
212 * again, as the filter is likely buggy.
214 filter->string = "";
215 continue;
217 ce = index_file_exists(state->istate, path->string,
218 strlen(path->string), 0);
219 errs |= (ce ? checkout_entry(ce, state, NULL) : 1);
222 string_list_remove_empty_items(&dco->filters, 0);
224 string_list_clear(&dco->filters, 0);
226 /* At this point we should not have any delayed paths anymore. */
227 errs |= dco->paths.nr;
228 for_each_string_list_item(path, &dco->paths) {
229 error("'%s' was not filtered properly", path->string);
231 string_list_clear(&dco->paths, 0);
233 free(dco);
234 state->delayed_checkout = NULL;
236 return errs;
239 static int write_entry(struct cache_entry *ce,
240 char *path, const struct checkout *state, int to_tempfile)
242 unsigned int ce_mode_s_ifmt = ce->ce_mode & S_IFMT;
243 struct delayed_checkout *dco = state->delayed_checkout;
244 int fd, ret, fstat_done = 0;
245 char *new;
246 struct strbuf buf = STRBUF_INIT;
247 unsigned long size;
248 size_t wrote, newsize = 0;
249 struct stat st;
250 const struct submodule *sub;
252 if (ce_mode_s_ifmt == S_IFREG) {
253 struct stream_filter *filter = get_stream_filter(ce->name,
254 ce->oid.hash);
255 if (filter &&
256 !streaming_write_entry(ce, path, filter,
257 state, to_tempfile,
258 &fstat_done, &st))
259 goto finish;
262 switch (ce_mode_s_ifmt) {
263 case S_IFREG:
264 case S_IFLNK:
266 * We do not send the blob in case of a retry, so do not
267 * bother reading it at all.
269 if (ce_mode_s_ifmt == S_IFREG && dco && dco->state == CE_RETRY) {
270 new = NULL;
271 size = 0;
272 } else {
273 new = read_blob_entry(ce, &size);
274 if (!new)
275 return error("unable to read sha1 file of %s (%s)",
276 path, oid_to_hex(&ce->oid));
279 if (ce_mode_s_ifmt == S_IFLNK && has_symlinks && !to_tempfile) {
280 ret = symlink(new, path);
281 free(new);
282 if (ret)
283 return error_errno("unable to create symlink %s",
284 path);
285 break;
289 * Convert from git internal format to working tree format
291 if (ce_mode_s_ifmt == S_IFREG) {
292 if (dco && dco->state != CE_NO_DELAY) {
293 ret = async_convert_to_working_tree(
294 ce->name, new, size, &buf, dco);
295 if (ret && string_list_has_string(&dco->paths, ce->name)) {
296 free(new);
297 goto delayed;
299 } else
300 ret = convert_to_working_tree(
301 ce->name, new, size, &buf);
303 if (ret) {
304 free(new);
305 new = strbuf_detach(&buf, &newsize);
306 size = newsize;
309 * No "else" here as errors from convert are OK at this
310 * point. If the error would have been fatal (e.g.
311 * filter is required), then we would have died already.
315 fd = open_output_fd(path, ce, to_tempfile);
316 if (fd < 0) {
317 free(new);
318 return error_errno("unable to create file %s", path);
321 wrote = write_in_full(fd, new, size);
322 if (!to_tempfile)
323 fstat_done = fstat_output(fd, state, &st);
324 close(fd);
325 free(new);
326 if (wrote != size)
327 return error("unable to write file %s", path);
328 break;
329 case S_IFGITLINK:
330 if (to_tempfile)
331 return error("cannot create temporary submodule %s", path);
332 if (mkdir(path, 0777) < 0)
333 return error("cannot create submodule directory %s", path);
334 sub = submodule_from_ce(ce);
335 if (sub)
336 return submodule_move_head(ce->name,
337 NULL, oid_to_hex(&ce->oid),
338 state->force ? SUBMODULE_MOVE_HEAD_FORCE : 0);
339 break;
340 default:
341 return error("unknown file mode for %s in index", path);
344 finish:
345 if (state->refresh_cache) {
346 assert(state->istate);
347 if (!fstat_done)
348 if (lstat(ce->name, &st) < 0)
349 return error_errno("unable to stat just-written file %s",
350 ce->name);
351 fill_stat_cache_info(ce, &st);
352 ce->ce_flags |= CE_UPDATE_IN_BASE;
353 state->istate->cache_changed |= CE_ENTRY_CHANGED;
355 delayed:
356 return 0;
360 * This is like 'lstat()', except it refuses to follow symlinks
361 * in the path, after skipping "skiplen".
363 static int check_path(const char *path, int len, struct stat *st, int skiplen)
365 const char *slash = path + len;
367 while (path < slash && *slash != '/')
368 slash--;
369 if (!has_dirs_only_path(path, slash - path, skiplen)) {
370 errno = ENOENT;
371 return -1;
373 return lstat(path, st);
377 * Write the contents from ce out to the working tree.
379 * When topath[] is not NULL, instead of writing to the working tree
380 * file named by ce, a temporary file is created by this function and
381 * its name is returned in topath[], which must be able to hold at
382 * least TEMPORARY_FILENAME_LENGTH bytes long.
384 int checkout_entry(struct cache_entry *ce,
385 const struct checkout *state, char *topath)
387 static struct strbuf path = STRBUF_INIT;
388 struct stat st;
390 if (topath)
391 return write_entry(ce, topath, state, 1);
393 strbuf_reset(&path);
394 strbuf_add(&path, state->base_dir, state->base_dir_len);
395 strbuf_add(&path, ce->name, ce_namelen(ce));
397 if (!check_path(path.buf, path.len, &st, state->base_dir_len)) {
398 const struct submodule *sub;
399 unsigned changed = ce_match_stat(ce, &st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE);
401 * Needs to be checked before !changed returns early,
402 * as the possibly empty directory was not changed
404 sub = submodule_from_ce(ce);
405 if (sub) {
406 int err;
407 if (!is_submodule_populated_gently(ce->name, &err)) {
408 struct stat sb;
409 if (lstat(ce->name, &sb))
410 die(_("could not stat file '%s'"), ce->name);
411 if (!(st.st_mode & S_IFDIR))
412 unlink_or_warn(ce->name);
414 return submodule_move_head(ce->name,
415 NULL, oid_to_hex(&ce->oid), 0);
416 } else
417 return submodule_move_head(ce->name,
418 "HEAD", oid_to_hex(&ce->oid),
419 state->force ? SUBMODULE_MOVE_HEAD_FORCE : 0);
422 if (!changed)
423 return 0;
424 if (!state->force) {
425 if (!state->quiet)
426 fprintf(stderr,
427 "%s already exists, no checkout\n",
428 path.buf);
429 return -1;
433 * We unlink the old file, to get the new one with the
434 * right permissions (including umask, which is nasty
435 * to emulate by hand - much easier to let the system
436 * just do the right thing)
438 if (S_ISDIR(st.st_mode)) {
439 /* If it is a gitlink, leave it alone! */
440 if (S_ISGITLINK(ce->ce_mode))
441 return 0;
442 if (!state->force)
443 return error("%s is a directory", path.buf);
444 remove_subtree(&path);
445 } else if (unlink(path.buf))
446 return error_errno("unable to unlink old '%s'", path.buf);
447 } else if (state->not_new)
448 return 0;
450 create_directories(path.buf, path.len, state);
451 return write_entry(ce, path.buf, state, 0);