entry.c: update cache entry only for existing files
[git/git.git] / entry.c
CommitLineData
12dccc16 1#include "cache.h"
8e440259 2#include "blob.h"
8ca12c0d 3#include "dir.h"
dd8e9121 4#include "streaming.h"
6d14eac3 5#include "submodule.h"
12dccc16 6
81a9aa60
KB
7static void create_directories(const char *path, int path_len,
8 const struct checkout *state)
12dccc16 9{
3733e694 10 char *buf = xmallocz(path_len);
81a9aa60
KB
11 int len = 0;
12
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;
12dccc16 20 buf[len] = 0;
fa2e71c9 21
bad4a54f
KB
22 /*
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.
28 */
57199892 29 if (has_dirs_only_path(buf, len, state->base_dir_len))
fa2e71c9
JH
30 continue; /* ok, it is already a directory. */
31
32 /*
bad4a54f
KB
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.
fa2e71c9 37 */
f312de01 38 if (mkdir(buf, 0777)) {
fa2e71c9 39 if (errno == EEXIST && state->force &&
691f1a28 40 !unlink_or_warn(buf) && !mkdir(buf, 0777))
fa2e71c9 41 continue;
0721c314 42 die_errno("cannot create directory at '%s'", buf);
12dccc16
LT
43 }
44 }
45 free(buf);
46}
47
2f29e0c6 48static void remove_subtree(struct strbuf *path)
12dccc16 49{
2f29e0c6 50 DIR *dir = opendir(path->buf);
12dccc16 51 struct dirent *de;
2f29e0c6 52 int origlen = path->len;
a6080a0a 53
12dccc16 54 if (!dir)
2f29e0c6 55 die_errno("cannot opendir '%s'", path->buf);
12dccc16
LT
56 while ((de = readdir(dir)) != NULL) {
57 struct stat st;
2f29e0c6 58
8ca12c0d 59 if (is_dot_or_dotdot(de->d_name))
12dccc16 60 continue;
2f29e0c6
MH
61
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);
12dccc16 66 if (S_ISDIR(st.st_mode))
2f29e0c6
MH
67 remove_subtree(path);
68 else if (unlink(path->buf))
69 die_errno("cannot unlink '%s'", path->buf);
70 strbuf_setlen(path, origlen);
12dccc16
LT
71 }
72 closedir(dir);
2f29e0c6
MH
73 if (rmdir(path->buf))
74 die_errno("cannot rmdir '%s'", path->buf);
12dccc16
LT
75}
76
d48a72f3 77static int create_file(const char *path, unsigned int mode)
12dccc16 78{
12dccc16 79 mode = (mode & 0100) ? 0777 : 0666;
781411ed 80 return open(path, O_WRONLY | O_CREAT | O_EXCL, mode);
12dccc16
LT
81}
82
9c5e6c80 83static void *read_blob_entry(const struct cache_entry *ce, unsigned long *size)
f0807e62
LT
84{
85 enum object_type type;
99d1a986 86 void *new = read_sha1_file(ce->oid.hash, &type, size);
f0807e62
LT
87
88 if (new) {
89 if (type == OBJ_BLOB)
90 return new;
91 free(new);
92 }
93 return NULL;
94}
95
9c5e6c80 96static int open_output_fd(char *path, const struct cache_entry *ce, int to_tempfile)
fd5db55d
JH
97{
98 int symlink = (ce->ce_mode & S_IFMT) != S_IFREG;
99 if (to_tempfile) {
330c8e26
JK
100 xsnprintf(path, TEMPORARY_FILENAME_LENGTH, "%s",
101 symlink ? ".merge_link_XXXXXX" : ".merge_file_XXXXXX");
fd5db55d
JH
102 return mkstemp(path);
103 } else {
104 return create_file(path, !symlink ? ce->ce_mode : 0666);
105 }
106}
107
108static int fstat_output(int fd, const struct checkout *state, struct stat *st)
109{
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;
115 }
116 return 0;
117}
118
9c5e6c80 119static int streaming_write_entry(const struct cache_entry *ce, char *path,
b6691092 120 struct stream_filter *filter,
dd8e9121
JH
121 const struct checkout *state, int to_tempfile,
122 int *fstat_done, struct stat *statbuf)
123{
d9c31e14 124 int result = 0;
47a02ff2 125 int fd;
dd8e9121
JH
126
127 fd = open_output_fd(path, ce, to_tempfile);
d9c31e14
JK
128 if (fd < 0)
129 return -1;
130
7eda0e4f 131 result |= stream_blob_to_fd(fd, &ce->oid, filter, 1);
d9c31e14
JK
132 *fstat_done = fstat_output(fd, state, statbuf);
133 result |= close(fd);
134
135 if (result)
dd8e9121
JH
136 unlink(path);
137 return result;
138}
139
2841e8f8
LS
140void enable_delayed_checkout(struct checkout *state)
141{
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);
147 }
148}
149
150static int remove_available_paths(struct string_list_item *item, void *cb_data)
151{
152 struct string_list *available_paths = cb_data;
153 struct string_list_item *available;
154
155 available = string_list_lookup(available_paths, item->string);
156 if (available)
157 available->util = (void *)item->string;
158 return !available;
159}
160
161int finish_delayed_checkout(struct checkout *state)
162{
163 int errs = 0;
164 struct string_list_item *filter, *path;
165 struct delayed_checkout *dco = state->delayed_checkout;
166
167 if (!state->delayed_checkout)
168 return errs;
169
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;
174
175 if (!async_query_available_blobs(filter->string, &available_paths)) {
176 /* Filter reported an error */
177 errs = 1;
178 filter->string = "";
179 continue;
180 }
181 if (available_paths.nr <= 0) {
182 /*
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).
187 */
188 filter->string = "";
189 continue;
190 }
191
192 /*
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.
196 */
197 filter_string_list(&dco->paths, 0,
198 &remove_available_paths, &available_paths);
199
200 for_each_string_list_item(path, &available_paths) {
201 struct cache_entry* ce;
202
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;
209
210 /*
211 * Do not ask the filter for available blobs,
212 * again, as the filter is likely buggy.
213 */
214 filter->string = "";
215 continue;
216 }
217 ce = index_file_exists(state->istate, path->string,
218 strlen(path->string), 0);
219 errs |= (ce ? checkout_entry(ce, state, NULL) : 1);
220 }
221 }
222 string_list_remove_empty_items(&dco->filters, 0);
223 }
224 string_list_clear(&dco->filters, 0);
225
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);
230 }
231 string_list_clear(&dco->paths, 0);
232
233 free(dco);
234 state->delayed_checkout = NULL;
235
236 return errs;
237}
238
9c5e6c80
NTND
239static int write_entry(struct cache_entry *ce,
240 char *path, const struct checkout *state, int to_tempfile)
12dccc16 241{
4857c761 242 unsigned int ce_mode_s_ifmt = ce->ce_mode & S_IFMT;
e4c72923 243 int fd, ret, fstat_done = 0;
4857c761
KB
244 char *new;
245 struct strbuf buf = STRBUF_INIT;
246 unsigned long size;
247 size_t wrote, newsize = 0;
e4c72923 248 struct stat st;
6d14eac3 249 const struct submodule *sub;
4857c761 250
b6691092 251 if (ce_mode_s_ifmt == S_IFREG) {
99d1a986 252 struct stream_filter *filter = get_stream_filter(ce->name,
253 ce->oid.hash);
b6691092
JH
254 if (filter &&
255 !streaming_write_entry(ce, path, filter,
256 state, to_tempfile,
257 &fstat_done, &st))
258 goto finish;
259 }
dd8e9121 260
4857c761 261 switch (ce_mode_s_ifmt) {
12dccc16 262 case S_IFREG:
4857c761
KB
263 case S_IFLNK:
264 new = read_blob_entry(ce, &size);
f0807e62 265 if (!new)
d43e9073 266 return error("unable to read sha1 file of %s (%s)",
99d1a986 267 path, oid_to_hex(&ce->oid));
1a9d7e9b 268
4857c761
KB
269 if (ce_mode_s_ifmt == S_IFLNK && has_symlinks && !to_tempfile) {
270 ret = symlink(new, path);
271 free(new);
272 if (ret)
e1ebb3c2
NTND
273 return error_errno("unable to create symlink %s",
274 path);
4857c761
KB
275 break;
276 }
277
1a9d7e9b
JH
278 /*
279 * Convert from git internal format to working tree format
280 */
2841e8f8
LS
281 if (ce_mode_s_ifmt == S_IFREG) {
282 struct delayed_checkout *dco = state->delayed_checkout;
283 if (dco && dco->state != CE_NO_DELAY) {
284 /* Do not send the blob in case of a retry. */
285 if (dco->state == CE_RETRY) {
286 new = NULL;
287 size = 0;
288 }
289 ret = async_convert_to_working_tree(
290 ce->name, new, size, &buf, dco);
291 if (ret && string_list_has_string(&dco->paths, ce->name)) {
292 free(new);
03b95333 293 goto delayed;
2841e8f8
LS
294 }
295 } else
296 ret = convert_to_working_tree(
297 ce->name, new, size, &buf);
298
299 if (ret) {
300 free(new);
301 new = strbuf_detach(&buf, &newsize);
302 size = newsize;
303 }
304 /*
305 * No "else" here as errors from convert are OK at this
306 * point. If the error would have been fatal (e.g.
307 * filter is required), then we would have died already.
308 */
1a9d7e9b
JH
309 }
310
fd5db55d 311 fd = open_output_fd(path, ce, to_tempfile);
12dccc16
LT
312 if (fd < 0) {
313 free(new);
e1ebb3c2 314 return error_errno("unable to create file %s", path);
12dccc16 315 }
6c510bee 316
93822c22 317 wrote = write_in_full(fd, new, size);
fd5db55d
JH
318 if (!to_tempfile)
319 fstat_done = fstat_output(fd, state, &st);
12dccc16
LT
320 close(fd);
321 free(new);
322 if (wrote != size)
d43e9073 323 return error("unable to write file %s", path);
12dccc16 324 break;
302b9282 325 case S_IFGITLINK:
f0807e62 326 if (to_tempfile)
42063f95 327 return error("cannot create temporary submodule %s", path);
f0807e62 328 if (mkdir(path, 0777) < 0)
42063f95 329 return error("cannot create submodule directory %s", path);
6d14eac3
SB
330 sub = submodule_from_ce(ce);
331 if (sub)
332 return submodule_move_head(ce->name,
cd279e2e
SB
333 NULL, oid_to_hex(&ce->oid),
334 state->force ? SUBMODULE_MOVE_HEAD_FORCE : 0);
f0807e62 335 break;
12dccc16 336 default:
d43e9073 337 return error("unknown file mode for %s in index", path);
12dccc16
LT
338 }
339
dd8e9121 340finish:
6ee67f26 341 if (state->refresh_cache) {
d4a2024a 342 assert(state->istate);
e4c72923
KB
343 if (!fstat_done)
344 lstat(ce->name, &st);
12dccc16 345 fill_stat_cache_info(ce, &st);
078a58e8 346 ce->ce_flags |= CE_UPDATE_IN_BASE;
d4a2024a 347 state->istate->cache_changed |= CE_ENTRY_CHANGED;
12dccc16 348 }
03b95333 349delayed:
12dccc16
LT
350 return 0;
351}
352
b6986d8a
LT
353/*
354 * This is like 'lstat()', except it refuses to follow symlinks
da02ca50 355 * in the path, after skipping "skiplen".
b6986d8a 356 */
61b97df7 357static int check_path(const char *path, int len, struct stat *st, int skiplen)
b6986d8a 358{
da02ca50
JH
359 const char *slash = path + len;
360
361 while (path < slash && *slash != '/')
362 slash--;
363 if (!has_dirs_only_path(path, slash - path, skiplen)) {
b6986d8a
LT
364 errno = ENOENT;
365 return -1;
366 }
367 return lstat(path, st);
368}
369
af2a651d
JH
370/*
371 * Write the contents from ce out to the working tree.
372 *
373 * When topath[] is not NULL, instead of writing to the working tree
374 * file named by ce, a temporary file is created by this function and
375 * its name is returned in topath[], which must be able to hold at
376 * least TEMPORARY_FILENAME_LENGTH bytes long.
377 */
9c5e6c80
NTND
378int checkout_entry(struct cache_entry *ce,
379 const struct checkout *state, char *topath)
12dccc16 380{
f63272a3 381 static struct strbuf path = STRBUF_INIT;
de84f99c 382 struct stat st;
12dccc16 383
de84f99c
SP
384 if (topath)
385 return write_entry(ce, topath, state, 1);
386
f63272a3
MH
387 strbuf_reset(&path);
388 strbuf_add(&path, state->base_dir, state->base_dir_len);
389 strbuf_add(&path, ce->name, ce_namelen(ce));
12dccc16 390
f63272a3 391 if (!check_path(path.buf, path.len, &st, state->base_dir_len)) {
6d14eac3 392 const struct submodule *sub;
56cac48c 393 unsigned changed = ce_match_stat(ce, &st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE);
6d14eac3
SB
394 /*
395 * Needs to be checked before !changed returns early,
396 * as the possibly empty directory was not changed
397 */
398 sub = submodule_from_ce(ce);
399 if (sub) {
400 int err;
401 if (!is_submodule_populated_gently(ce->name, &err)) {
402 struct stat sb;
403 if (lstat(ce->name, &sb))
404 die(_("could not stat file '%s'"), ce->name);
405 if (!(st.st_mode & S_IFDIR))
406 unlink_or_warn(ce->name);
407
408 return submodule_move_head(ce->name,
cd279e2e 409 NULL, oid_to_hex(&ce->oid), 0);
6d14eac3
SB
410 } else
411 return submodule_move_head(ce->name,
412 "HEAD", oid_to_hex(&ce->oid),
cd279e2e 413 state->force ? SUBMODULE_MOVE_HEAD_FORCE : 0);
6d14eac3
SB
414 }
415
12dccc16
LT
416 if (!changed)
417 return 0;
418 if (!state->force) {
419 if (!state->quiet)
f63272a3
MH
420 fprintf(stderr,
421 "%s already exists, no checkout\n",
422 path.buf);
4b12dae6 423 return -1;
12dccc16
LT
424 }
425
426 /*
427 * We unlink the old file, to get the new one with the
428 * right permissions (including umask, which is nasty
429 * to emulate by hand - much easier to let the system
430 * just do the right thing)
431 */
d48a72f3 432 if (S_ISDIR(st.st_mode)) {
f0807e62 433 /* If it is a gitlink, leave it alone! */
7a51ed66 434 if (S_ISGITLINK(ce->ce_mode))
f0807e62 435 return 0;
d48a72f3 436 if (!state->force)
f63272a3 437 return error("%s is a directory", path.buf);
2f29e0c6 438 remove_subtree(&path);
f63272a3 439 } else if (unlink(path.buf))
e1ebb3c2 440 return error_errno("unable to unlink old '%s'", path.buf);
de84f99c 441 } else if (state->not_new)
12dccc16 442 return 0;
f63272a3
MH
443
444 create_directories(path.buf, path.len, state);
445 return write_entry(ce, path.buf, state, 0);
12dccc16 446}