Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 1 | #include "cache.h" |
Peter Eriksen | 8e44025 | 2006-04-02 14:44:09 +0200 | [diff] [blame] | 2 | #include "blob.h" |
Alexander Potashev | 8ca12c0 | 2009-01-10 15:07:50 +0300 | [diff] [blame] | 3 | #include "dir.h" |
Junio C Hamano | dd8e912 | 2011-05-12 14:31:08 -0700 | [diff] [blame] | 4 | #include "streaming.h" |
Stefan Beller | 6d14eac | 2017-03-14 14:46:40 -0700 | [diff] [blame] | 5 | #include "submodule.h" |
Lars Schneider | 52f1d62 | 2017-08-20 17:47:20 +0200 | [diff] [blame] | 6 | #include "progress.h" |
Ben Peart | 883e248 | 2017-09-22 12:35:40 -0400 | [diff] [blame] | 7 | #include "fsmonitor.h" |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 8 | |
Kjetil Barvik | 81a9aa6 | 2009-02-09 21:54:08 +0100 | [diff] [blame] | 9 | static void create_directories(const char *path, int path_len, |
| 10 | const struct checkout *state) |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 11 | { |
Jeff King | 3733e69 | 2016-02-22 17:44:28 -0500 | [diff] [blame] | 12 | char *buf = xmallocz(path_len); |
Kjetil Barvik | 81a9aa6 | 2009-02-09 21:54:08 +0100 | [diff] [blame] | 13 | int len = 0; |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 14 | |
Kjetil Barvik | 81a9aa6 | 2009-02-09 21:54:08 +0100 | [diff] [blame] | 15 | while (len < path_len) { |
| 16 | do { |
| 17 | buf[len] = path[len]; |
| 18 | len++; |
| 19 | } while (len < path_len && path[len] != '/'); |
| 20 | if (len >= path_len) |
| 21 | break; |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 22 | buf[len] = 0; |
Junio C Hamano | fa2e71c | 2007-07-17 22:58:28 -0700 | [diff] [blame] | 23 | |
Kjetil Barvik | bad4a54 | 2009-01-18 16:14:52 +0100 | [diff] [blame] | 24 | /* |
| 25 | * For 'checkout-index --prefix=<dir>', <dir> is |
| 26 | * allowed to be a symlink to an existing directory, |
| 27 | * and we set 'state->base_dir_len' below, such that |
| 28 | * we test the path components of the prefix with the |
| 29 | * stat() function instead of the lstat() function. |
| 30 | */ |
Kjetil Barvik | 5719989 | 2009-02-09 21:54:06 +0100 | [diff] [blame] | 31 | if (has_dirs_only_path(buf, len, state->base_dir_len)) |
Junio C Hamano | fa2e71c | 2007-07-17 22:58:28 -0700 | [diff] [blame] | 32 | continue; /* ok, it is already a directory. */ |
| 33 | |
| 34 | /* |
Kjetil Barvik | bad4a54 | 2009-01-18 16:14:52 +0100 | [diff] [blame] | 35 | * If this mkdir() would fail, it could be that there |
| 36 | * is already a symlink or something else exists |
| 37 | * there, therefore we then try to unlink it and try |
| 38 | * one more time to create the directory. |
Junio C Hamano | fa2e71c | 2007-07-17 22:58:28 -0700 | [diff] [blame] | 39 | */ |
Junio C Hamano | f312de0 | 2005-07-06 01:21:46 -0700 | [diff] [blame] | 40 | if (mkdir(buf, 0777)) { |
Junio C Hamano | fa2e71c | 2007-07-17 22:58:28 -0700 | [diff] [blame] | 41 | if (errno == EEXIST && state->force && |
Alex Riesen | 691f1a2 | 2009-04-29 23:22:56 +0200 | [diff] [blame] | 42 | !unlink_or_warn(buf) && !mkdir(buf, 0777)) |
Junio C Hamano | fa2e71c | 2007-07-17 22:58:28 -0700 | [diff] [blame] | 43 | continue; |
Thomas Rast | 0721c31 | 2009-06-27 17:58:47 +0200 | [diff] [blame] | 44 | die_errno("cannot create directory at '%s'", buf); |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 45 | } |
| 46 | } |
| 47 | free(buf); |
| 48 | } |
| 49 | |
Michael Haggerty | 2f29e0c | 2014-03-13 10:19:08 +0100 | [diff] [blame] | 50 | static void remove_subtree(struct strbuf *path) |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 51 | { |
Michael Haggerty | 2f29e0c | 2014-03-13 10:19:08 +0100 | [diff] [blame] | 52 | DIR *dir = opendir(path->buf); |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 53 | struct dirent *de; |
Michael Haggerty | 2f29e0c | 2014-03-13 10:19:08 +0100 | [diff] [blame] | 54 | int origlen = path->len; |
Junio C Hamano | a6080a0 | 2007-06-07 00:04:01 -0700 | [diff] [blame] | 55 | |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 56 | if (!dir) |
Michael Haggerty | 2f29e0c | 2014-03-13 10:19:08 +0100 | [diff] [blame] | 57 | die_errno("cannot opendir '%s'", path->buf); |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 58 | while ((de = readdir(dir)) != NULL) { |
| 59 | struct stat st; |
Michael Haggerty | 2f29e0c | 2014-03-13 10:19:08 +0100 | [diff] [blame] | 60 | |
Alexander Potashev | 8ca12c0 | 2009-01-10 15:07:50 +0300 | [diff] [blame] | 61 | if (is_dot_or_dotdot(de->d_name)) |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 62 | continue; |
Michael Haggerty | 2f29e0c | 2014-03-13 10:19:08 +0100 | [diff] [blame] | 63 | |
| 64 | strbuf_addch(path, '/'); |
| 65 | strbuf_addstr(path, de->d_name); |
| 66 | if (lstat(path->buf, &st)) |
| 67 | die_errno("cannot lstat '%s'", path->buf); |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 68 | if (S_ISDIR(st.st_mode)) |
Michael Haggerty | 2f29e0c | 2014-03-13 10:19:08 +0100 | [diff] [blame] | 69 | remove_subtree(path); |
| 70 | else if (unlink(path->buf)) |
| 71 | die_errno("cannot unlink '%s'", path->buf); |
| 72 | strbuf_setlen(path, origlen); |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 73 | } |
| 74 | closedir(dir); |
Michael Haggerty | 2f29e0c | 2014-03-13 10:19:08 +0100 | [diff] [blame] | 75 | if (rmdir(path->buf)) |
| 76 | die_errno("cannot rmdir '%s'", path->buf); |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Linus Torvalds | d48a72f | 2005-07-14 09:58:45 -0700 | [diff] [blame] | 79 | static int create_file(const char *path, unsigned int mode) |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 80 | { |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 81 | mode = (mode & 0100) ? 0777 : 0666; |
Alex Riesen | 781411e | 2006-01-05 09:58:06 +0100 | [diff] [blame] | 82 | return open(path, O_WRONLY | O_CREAT | O_EXCL, mode); |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Nguyễn Thái Ngọc Duy | 9c5e6c8 | 2013-07-09 22:29:00 +0700 | [diff] [blame] | 85 | static void *read_blob_entry(const struct cache_entry *ce, unsigned long *size) |
Linus Torvalds | f0807e6 | 2007-04-13 09:26:04 -0700 | [diff] [blame] | 86 | { |
| 87 | enum object_type type; |
brian m. carlson | b4f5aca | 2018-03-12 02:27:53 +0000 | [diff] [blame] | 88 | void *blob_data = read_object_file(&ce->oid, &type, size); |
Linus Torvalds | f0807e6 | 2007-04-13 09:26:04 -0700 | [diff] [blame] | 89 | |
Brandon Williams | d8f7180 | 2018-02-14 10:59:41 -0800 | [diff] [blame] | 90 | if (blob_data) { |
Linus Torvalds | f0807e6 | 2007-04-13 09:26:04 -0700 | [diff] [blame] | 91 | if (type == OBJ_BLOB) |
Brandon Williams | d8f7180 | 2018-02-14 10:59:41 -0800 | [diff] [blame] | 92 | return blob_data; |
| 93 | free(blob_data); |
Linus Torvalds | f0807e6 | 2007-04-13 09:26:04 -0700 | [diff] [blame] | 94 | } |
| 95 | return NULL; |
| 96 | } |
| 97 | |
Nguyễn Thái Ngọc Duy | 9c5e6c8 | 2013-07-09 22:29:00 +0700 | [diff] [blame] | 98 | static int open_output_fd(char *path, const struct cache_entry *ce, int to_tempfile) |
Junio C Hamano | fd5db55 | 2011-05-12 21:36:42 -0700 | [diff] [blame] | 99 | { |
| 100 | int symlink = (ce->ce_mode & S_IFMT) != S_IFREG; |
| 101 | if (to_tempfile) { |
Jeff King | 330c8e2 | 2015-09-24 17:06:53 -0400 | [diff] [blame] | 102 | xsnprintf(path, TEMPORARY_FILENAME_LENGTH, "%s", |
| 103 | symlink ? ".merge_link_XXXXXX" : ".merge_file_XXXXXX"); |
Junio C Hamano | fd5db55 | 2011-05-12 21:36:42 -0700 | [diff] [blame] | 104 | return mkstemp(path); |
| 105 | } else { |
| 106 | return create_file(path, !symlink ? ce->ce_mode : 0666); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | static int fstat_output(int fd, const struct checkout *state, struct stat *st) |
| 111 | { |
| 112 | /* use fstat() only when path == ce->name */ |
| 113 | if (fstat_is_reliable() && |
| 114 | state->refresh_cache && !state->base_dir_len) { |
| 115 | fstat(fd, st); |
| 116 | return 1; |
| 117 | } |
| 118 | return 0; |
| 119 | } |
| 120 | |
Nguyễn Thái Ngọc Duy | 9c5e6c8 | 2013-07-09 22:29:00 +0700 | [diff] [blame] | 121 | static int streaming_write_entry(const struct cache_entry *ce, char *path, |
Junio C Hamano | b669109 | 2011-05-20 14:33:31 -0700 | [diff] [blame] | 122 | struct stream_filter *filter, |
Junio C Hamano | dd8e912 | 2011-05-12 14:31:08 -0700 | [diff] [blame] | 123 | const struct checkout *state, int to_tempfile, |
| 124 | int *fstat_done, struct stat *statbuf) |
| 125 | { |
Jeff King | d9c31e1 | 2013-03-25 17:49:36 -0400 | [diff] [blame] | 126 | int result = 0; |
Junio C Hamano | 47a02ff | 2012-03-07 17:54:15 +0700 | [diff] [blame] | 127 | int fd; |
Junio C Hamano | dd8e912 | 2011-05-12 14:31:08 -0700 | [diff] [blame] | 128 | |
| 129 | fd = open_output_fd(path, ce, to_tempfile); |
Jeff King | d9c31e1 | 2013-03-25 17:49:36 -0400 | [diff] [blame] | 130 | if (fd < 0) |
| 131 | return -1; |
| 132 | |
brian m. carlson | 7eda0e4 | 2016-09-05 20:07:59 +0000 | [diff] [blame] | 133 | result |= stream_blob_to_fd(fd, &ce->oid, filter, 1); |
Jeff King | d9c31e1 | 2013-03-25 17:49:36 -0400 | [diff] [blame] | 134 | *fstat_done = fstat_output(fd, state, statbuf); |
| 135 | result |= close(fd); |
| 136 | |
| 137 | if (result) |
Junio C Hamano | dd8e912 | 2011-05-12 14:31:08 -0700 | [diff] [blame] | 138 | unlink(path); |
| 139 | return result; |
| 140 | } |
| 141 | |
Lars Schneider | 2841e8f | 2017-06-30 22:41:28 +0200 | [diff] [blame] | 142 | void enable_delayed_checkout(struct checkout *state) |
| 143 | { |
| 144 | if (!state->delayed_checkout) { |
| 145 | state->delayed_checkout = xmalloc(sizeof(*state->delayed_checkout)); |
| 146 | state->delayed_checkout->state = CE_CAN_DELAY; |
| 147 | string_list_init(&state->delayed_checkout->filters, 0); |
| 148 | string_list_init(&state->delayed_checkout->paths, 0); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | static int remove_available_paths(struct string_list_item *item, void *cb_data) |
| 153 | { |
| 154 | struct string_list *available_paths = cb_data; |
| 155 | struct string_list_item *available; |
| 156 | |
| 157 | available = string_list_lookup(available_paths, item->string); |
| 158 | if (available) |
| 159 | available->util = (void *)item->string; |
| 160 | return !available; |
| 161 | } |
| 162 | |
| 163 | int finish_delayed_checkout(struct checkout *state) |
| 164 | { |
| 165 | int errs = 0; |
Lars Schneider | 52f1d62 | 2017-08-20 17:47:20 +0200 | [diff] [blame] | 166 | unsigned delayed_object_count; |
| 167 | off_t filtered_bytes = 0; |
Lars Schneider | 2841e8f | 2017-06-30 22:41:28 +0200 | [diff] [blame] | 168 | struct string_list_item *filter, *path; |
Lars Schneider | 52f1d62 | 2017-08-20 17:47:20 +0200 | [diff] [blame] | 169 | struct progress *progress; |
Lars Schneider | 2841e8f | 2017-06-30 22:41:28 +0200 | [diff] [blame] | 170 | struct delayed_checkout *dco = state->delayed_checkout; |
| 171 | |
| 172 | if (!state->delayed_checkout) |
| 173 | return errs; |
| 174 | |
| 175 | dco->state = CE_RETRY; |
Lars Schneider | 52f1d62 | 2017-08-20 17:47:20 +0200 | [diff] [blame] | 176 | delayed_object_count = dco->paths.nr; |
Junio C Hamano | 7fbbd3e | 2017-09-10 17:08:22 +0900 | [diff] [blame] | 177 | progress = start_delayed_progress(_("Filtering content"), delayed_object_count); |
Lars Schneider | 2841e8f | 2017-06-30 22:41:28 +0200 | [diff] [blame] | 178 | while (dco->filters.nr > 0) { |
| 179 | for_each_string_list_item(filter, &dco->filters) { |
| 180 | struct string_list available_paths = STRING_LIST_INIT_NODUP; |
Lars Schneider | 52f1d62 | 2017-08-20 17:47:20 +0200 | [diff] [blame] | 181 | display_progress(progress, delayed_object_count - dco->paths.nr); |
Lars Schneider | 2841e8f | 2017-06-30 22:41:28 +0200 | [diff] [blame] | 182 | |
| 183 | if (!async_query_available_blobs(filter->string, &available_paths)) { |
| 184 | /* Filter reported an error */ |
| 185 | errs = 1; |
| 186 | filter->string = ""; |
| 187 | continue; |
| 188 | } |
| 189 | if (available_paths.nr <= 0) { |
| 190 | /* |
| 191 | * Filter responded with no entries. That means |
| 192 | * the filter is done and we can remove the |
| 193 | * filter from the list (see |
| 194 | * "string_list_remove_empty_items" call below). |
| 195 | */ |
| 196 | filter->string = ""; |
| 197 | continue; |
| 198 | } |
| 199 | |
| 200 | /* |
| 201 | * In dco->paths we store a list of all delayed paths. |
| 202 | * The filter just send us a list of available paths. |
| 203 | * Remove them from the list. |
| 204 | */ |
| 205 | filter_string_list(&dco->paths, 0, |
| 206 | &remove_available_paths, &available_paths); |
| 207 | |
| 208 | for_each_string_list_item(path, &available_paths) { |
| 209 | struct cache_entry* ce; |
| 210 | |
| 211 | if (!path->util) { |
| 212 | error("external filter '%s' signaled that '%s' " |
| 213 | "is now available although it has not been " |
| 214 | "delayed earlier", |
| 215 | filter->string, path->string); |
| 216 | errs |= 1; |
| 217 | |
| 218 | /* |
| 219 | * Do not ask the filter for available blobs, |
| 220 | * again, as the filter is likely buggy. |
| 221 | */ |
| 222 | filter->string = ""; |
| 223 | continue; |
| 224 | } |
| 225 | ce = index_file_exists(state->istate, path->string, |
| 226 | strlen(path->string), 0); |
Lars Schneider | 52f1d62 | 2017-08-20 17:47:20 +0200 | [diff] [blame] | 227 | if (ce) { |
| 228 | errs |= checkout_entry(ce, state, NULL); |
| 229 | filtered_bytes += ce->ce_stat_data.sd_size; |
| 230 | display_throughput(progress, filtered_bytes); |
| 231 | } else |
| 232 | errs = 1; |
Lars Schneider | 2841e8f | 2017-06-30 22:41:28 +0200 | [diff] [blame] | 233 | } |
| 234 | } |
| 235 | string_list_remove_empty_items(&dco->filters, 0); |
| 236 | } |
Lars Schneider | 52f1d62 | 2017-08-20 17:47:20 +0200 | [diff] [blame] | 237 | stop_progress(&progress); |
Lars Schneider | 2841e8f | 2017-06-30 22:41:28 +0200 | [diff] [blame] | 238 | string_list_clear(&dco->filters, 0); |
| 239 | |
| 240 | /* At this point we should not have any delayed paths anymore. */ |
| 241 | errs |= dco->paths.nr; |
| 242 | for_each_string_list_item(path, &dco->paths) { |
| 243 | error("'%s' was not filtered properly", path->string); |
| 244 | } |
| 245 | string_list_clear(&dco->paths, 0); |
| 246 | |
| 247 | free(dco); |
| 248 | state->delayed_checkout = NULL; |
| 249 | |
| 250 | return errs; |
| 251 | } |
| 252 | |
Nguyễn Thái Ngọc Duy | 9c5e6c8 | 2013-07-09 22:29:00 +0700 | [diff] [blame] | 253 | static int write_entry(struct cache_entry *ce, |
| 254 | char *path, const struct checkout *state, int to_tempfile) |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 255 | { |
Kjetil Barvik | 4857c76 | 2009-02-09 21:54:50 +0100 | [diff] [blame] | 256 | unsigned int ce_mode_s_ifmt = ce->ce_mode & S_IFMT; |
Jeff King | c602d3a | 2017-10-09 13:48:52 -0400 | [diff] [blame] | 257 | struct delayed_checkout *dco = state->delayed_checkout; |
Kjetil Barvik | e4c7292 | 2009-02-09 21:54:51 +0100 | [diff] [blame] | 258 | int fd, ret, fstat_done = 0; |
Brandon Williams | d8f7180 | 2018-02-14 10:59:41 -0800 | [diff] [blame] | 259 | char *new_blob; |
Kjetil Barvik | 4857c76 | 2009-02-09 21:54:50 +0100 | [diff] [blame] | 260 | struct strbuf buf = STRBUF_INIT; |
| 261 | unsigned long size; |
Jeff King | 564bde9 | 2017-09-13 13:16:28 -0400 | [diff] [blame] | 262 | ssize_t wrote; |
| 263 | size_t newsize = 0; |
Kjetil Barvik | e4c7292 | 2009-02-09 21:54:51 +0100 | [diff] [blame] | 264 | struct stat st; |
Stefan Beller | 6d14eac | 2017-03-14 14:46:40 -0700 | [diff] [blame] | 265 | const struct submodule *sub; |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 266 | |
Junio C Hamano | b669109 | 2011-05-20 14:33:31 -0700 | [diff] [blame] | 267 | if (ce_mode_s_ifmt == S_IFREG) { |
brian m. carlson | 99d1a98 | 2016-09-05 20:07:52 +0000 | [diff] [blame] | 268 | struct stream_filter *filter = get_stream_filter(ce->name, |
brian m. carlson | 1a75044 | 2018-03-12 02:27:56 +0000 | [diff] [blame] | 269 | &ce->oid); |
Junio C Hamano | b669109 | 2011-05-20 14:33:31 -0700 | [diff] [blame] | 270 | if (filter && |
| 271 | !streaming_write_entry(ce, path, filter, |
| 272 | state, to_tempfile, |
| 273 | &fstat_done, &st)) |
| 274 | goto finish; |
| 275 | } |
Junio C Hamano | dd8e912 | 2011-05-12 14:31:08 -0700 | [diff] [blame] | 276 | |
Kjetil Barvik | 4857c76 | 2009-02-09 21:54:50 +0100 | [diff] [blame] | 277 | switch (ce_mode_s_ifmt) { |
Kjetil Barvik | 4857c76 | 2009-02-09 21:54:50 +0100 | [diff] [blame] | 278 | case S_IFLNK: |
Brandon Williams | d8f7180 | 2018-02-14 10:59:41 -0800 | [diff] [blame] | 279 | new_blob = read_blob_entry(ce, &size); |
| 280 | if (!new_blob) |
Nguyễn Thái Ngọc Duy | d43e907 | 2010-11-28 11:36:38 +0700 | [diff] [blame] | 281 | return error("unable to read sha1 file of %s (%s)", |
Jeff King | 7cbbf9d | 2017-10-09 13:50:05 -0400 | [diff] [blame] | 282 | path, oid_to_hex(&ce->oid)); |
Junio C Hamano | 1a9d7e9 | 2007-08-14 01:41:02 -0700 | [diff] [blame] | 283 | |
Jeff King | 7cbbf9d | 2017-10-09 13:50:05 -0400 | [diff] [blame] | 284 | /* |
| 285 | * We can't make a real symlink; write out a regular file entry |
| 286 | * with the symlink destination as its contents. |
| 287 | */ |
| 288 | if (!has_symlinks || to_tempfile) |
| 289 | goto write_file_entry; |
| 290 | |
Brandon Williams | d8f7180 | 2018-02-14 10:59:41 -0800 | [diff] [blame] | 291 | ret = symlink(new_blob, path); |
| 292 | free(new_blob); |
Jeff King | 7cbbf9d | 2017-10-09 13:50:05 -0400 | [diff] [blame] | 293 | if (ret) |
| 294 | return error_errno("unable to create symlink %s", path); |
| 295 | break; |
| 296 | |
| 297 | case S_IFREG: |
Jeff King | c602d3a | 2017-10-09 13:48:52 -0400 | [diff] [blame] | 298 | /* |
| 299 | * We do not send the blob in case of a retry, so do not |
| 300 | * bother reading it at all. |
| 301 | */ |
Jeff King | 7cbbf9d | 2017-10-09 13:50:05 -0400 | [diff] [blame] | 302 | if (dco && dco->state == CE_RETRY) { |
Brandon Williams | d8f7180 | 2018-02-14 10:59:41 -0800 | [diff] [blame] | 303 | new_blob = NULL; |
Jeff King | c602d3a | 2017-10-09 13:48:52 -0400 | [diff] [blame] | 304 | size = 0; |
| 305 | } else { |
Brandon Williams | d8f7180 | 2018-02-14 10:59:41 -0800 | [diff] [blame] | 306 | new_blob = read_blob_entry(ce, &size); |
| 307 | if (!new_blob) |
Jeff King | c602d3a | 2017-10-09 13:48:52 -0400 | [diff] [blame] | 308 | return error("unable to read sha1 file of %s (%s)", |
| 309 | path, oid_to_hex(&ce->oid)); |
Kjetil Barvik | 4857c76 | 2009-02-09 21:54:50 +0100 | [diff] [blame] | 310 | } |
| 311 | |
Junio C Hamano | 1a9d7e9 | 2007-08-14 01:41:02 -0700 | [diff] [blame] | 312 | /* |
| 313 | * Convert from git internal format to working tree format |
| 314 | */ |
Jeff King | 7cbbf9d | 2017-10-09 13:50:05 -0400 | [diff] [blame] | 315 | if (dco && dco->state != CE_NO_DELAY) { |
Brandon Williams | d8f7180 | 2018-02-14 10:59:41 -0800 | [diff] [blame] | 316 | ret = async_convert_to_working_tree(ce->name, new_blob, |
Jeff King | 7cbbf9d | 2017-10-09 13:50:05 -0400 | [diff] [blame] | 317 | size, &buf, dco); |
| 318 | if (ret && string_list_has_string(&dco->paths, ce->name)) { |
Brandon Williams | d8f7180 | 2018-02-14 10:59:41 -0800 | [diff] [blame] | 319 | free(new_blob); |
Jeff King | 7cbbf9d | 2017-10-09 13:50:05 -0400 | [diff] [blame] | 320 | goto delayed; |
Lars Schneider | 2841e8f | 2017-06-30 22:41:28 +0200 | [diff] [blame] | 321 | } |
Jeff King | 7cbbf9d | 2017-10-09 13:50:05 -0400 | [diff] [blame] | 322 | } else |
Brandon Williams | d8f7180 | 2018-02-14 10:59:41 -0800 | [diff] [blame] | 323 | ret = convert_to_working_tree(ce->name, new_blob, size, &buf); |
Junio C Hamano | 1a9d7e9 | 2007-08-14 01:41:02 -0700 | [diff] [blame] | 324 | |
Jeff King | 7cbbf9d | 2017-10-09 13:50:05 -0400 | [diff] [blame] | 325 | if (ret) { |
Brandon Williams | d8f7180 | 2018-02-14 10:59:41 -0800 | [diff] [blame] | 326 | free(new_blob); |
| 327 | new_blob = strbuf_detach(&buf, &newsize); |
Jeff King | 7cbbf9d | 2017-10-09 13:50:05 -0400 | [diff] [blame] | 328 | size = newsize; |
| 329 | } |
| 330 | /* |
| 331 | * No "else" here as errors from convert are OK at this |
| 332 | * point. If the error would have been fatal (e.g. |
| 333 | * filter is required), then we would have died already. |
| 334 | */ |
| 335 | |
| 336 | write_file_entry: |
Junio C Hamano | fd5db55 | 2011-05-12 21:36:42 -0700 | [diff] [blame] | 337 | fd = open_output_fd(path, ce, to_tempfile); |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 338 | if (fd < 0) { |
Brandon Williams | d8f7180 | 2018-02-14 10:59:41 -0800 | [diff] [blame] | 339 | free(new_blob); |
Nguyễn Thái Ngọc Duy | e1ebb3c | 2016-05-08 16:47:44 +0700 | [diff] [blame] | 340 | return error_errno("unable to create file %s", path); |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 341 | } |
Linus Torvalds | 6c510be | 2007-02-13 11:07:23 -0800 | [diff] [blame] | 342 | |
Brandon Williams | d8f7180 | 2018-02-14 10:59:41 -0800 | [diff] [blame] | 343 | wrote = write_in_full(fd, new_blob, size); |
Junio C Hamano | fd5db55 | 2011-05-12 21:36:42 -0700 | [diff] [blame] | 344 | if (!to_tempfile) |
| 345 | fstat_done = fstat_output(fd, state, &st); |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 346 | close(fd); |
Brandon Williams | d8f7180 | 2018-02-14 10:59:41 -0800 | [diff] [blame] | 347 | free(new_blob); |
Jeff King | 564bde9 | 2017-09-13 13:16:28 -0400 | [diff] [blame] | 348 | if (wrote < 0) |
Nguyễn Thái Ngọc Duy | d43e907 | 2010-11-28 11:36:38 +0700 | [diff] [blame] | 349 | return error("unable to write file %s", path); |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 350 | break; |
Jeff King | 7cbbf9d | 2017-10-09 13:50:05 -0400 | [diff] [blame] | 351 | |
Martin Waitz | 302b928 | 2007-05-21 22:08:28 +0200 | [diff] [blame] | 352 | case S_IFGITLINK: |
Linus Torvalds | f0807e6 | 2007-04-13 09:26:04 -0700 | [diff] [blame] | 353 | if (to_tempfile) |
Thomas Rast | 42063f9 | 2013-07-18 14:26:55 +0200 | [diff] [blame] | 354 | return error("cannot create temporary submodule %s", path); |
Linus Torvalds | f0807e6 | 2007-04-13 09:26:04 -0700 | [diff] [blame] | 355 | if (mkdir(path, 0777) < 0) |
Thomas Rast | 42063f9 | 2013-07-18 14:26:55 +0200 | [diff] [blame] | 356 | return error("cannot create submodule directory %s", path); |
Stefan Beller | 6d14eac | 2017-03-14 14:46:40 -0700 | [diff] [blame] | 357 | sub = submodule_from_ce(ce); |
| 358 | if (sub) |
| 359 | return submodule_move_head(ce->name, |
Stefan Beller | cd279e2 | 2017-04-18 14:37:22 -0700 | [diff] [blame] | 360 | NULL, oid_to_hex(&ce->oid), |
| 361 | state->force ? SUBMODULE_MOVE_HEAD_FORCE : 0); |
Linus Torvalds | f0807e6 | 2007-04-13 09:26:04 -0700 | [diff] [blame] | 362 | break; |
Jeff King | 7cbbf9d | 2017-10-09 13:50:05 -0400 | [diff] [blame] | 363 | |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 364 | default: |
Nguyễn Thái Ngọc Duy | d43e907 | 2010-11-28 11:36:38 +0700 | [diff] [blame] | 365 | return error("unknown file mode for %s in index", path); |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 366 | } |
| 367 | |
Junio C Hamano | dd8e912 | 2011-05-12 14:31:08 -0700 | [diff] [blame] | 368 | finish: |
Linus Torvalds | 6ee67f2 | 2005-06-05 23:15:40 -0700 | [diff] [blame] | 369 | if (state->refresh_cache) { |
Nguyễn Thái Ngọc Duy | d4a2024 | 2014-06-13 19:19:34 +0700 | [diff] [blame] | 370 | assert(state->istate); |
Kjetil Barvik | e4c7292 | 2009-02-09 21:54:51 +0100 | [diff] [blame] | 371 | if (!fstat_done) |
Lars Schneider | 11179eb | 2017-10-05 12:44:07 +0200 | [diff] [blame] | 372 | if (lstat(ce->name, &st) < 0) |
| 373 | return error_errno("unable to stat just-written file %s", |
| 374 | ce->name); |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 375 | fill_stat_cache_info(ce, &st); |
Nguyễn Thái Ngọc Duy | 078a58e | 2014-06-13 19:19:39 +0700 | [diff] [blame] | 376 | ce->ce_flags |= CE_UPDATE_IN_BASE; |
Ben Peart | 883e248 | 2017-09-22 12:35:40 -0400 | [diff] [blame] | 377 | mark_fsmonitor_invalid(state->istate, ce); |
Nguyễn Thái Ngọc Duy | d4a2024 | 2014-06-13 19:19:34 +0700 | [diff] [blame] | 378 | state->istate->cache_changed |= CE_ENTRY_CHANGED; |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 379 | } |
Lars Schneider | 03b9533 | 2017-10-05 12:44:06 +0200 | [diff] [blame] | 380 | delayed: |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 381 | return 0; |
| 382 | } |
| 383 | |
Linus Torvalds | b6986d8 | 2009-07-29 20:22:25 -0700 | [diff] [blame] | 384 | /* |
| 385 | * This is like 'lstat()', except it refuses to follow symlinks |
Junio C Hamano | da02ca5 | 2009-08-16 23:53:12 -0700 | [diff] [blame] | 386 | * in the path, after skipping "skiplen". |
Linus Torvalds | b6986d8 | 2009-07-29 20:22:25 -0700 | [diff] [blame] | 387 | */ |
Junio C Hamano | 61b97df | 2010-01-11 22:27:31 -0800 | [diff] [blame] | 388 | static int check_path(const char *path, int len, struct stat *st, int skiplen) |
Linus Torvalds | b6986d8 | 2009-07-29 20:22:25 -0700 | [diff] [blame] | 389 | { |
Junio C Hamano | da02ca5 | 2009-08-16 23:53:12 -0700 | [diff] [blame] | 390 | const char *slash = path + len; |
| 391 | |
| 392 | while (path < slash && *slash != '/') |
| 393 | slash--; |
| 394 | if (!has_dirs_only_path(path, slash - path, skiplen)) { |
Linus Torvalds | b6986d8 | 2009-07-29 20:22:25 -0700 | [diff] [blame] | 395 | errno = ENOENT; |
| 396 | return -1; |
| 397 | } |
| 398 | return lstat(path, st); |
| 399 | } |
| 400 | |
Junio C Hamano | af2a651 | 2013-10-23 10:52:42 -0700 | [diff] [blame] | 401 | /* |
| 402 | * Write the contents from ce out to the working tree. |
| 403 | * |
| 404 | * When topath[] is not NULL, instead of writing to the working tree |
| 405 | * file named by ce, a temporary file is created by this function and |
| 406 | * its name is returned in topath[], which must be able to hold at |
| 407 | * least TEMPORARY_FILENAME_LENGTH bytes long. |
| 408 | */ |
Nguyễn Thái Ngọc Duy | 9c5e6c8 | 2013-07-09 22:29:00 +0700 | [diff] [blame] | 409 | int checkout_entry(struct cache_entry *ce, |
| 410 | const struct checkout *state, char *topath) |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 411 | { |
Michael Haggerty | f63272a | 2014-03-13 10:19:07 +0100 | [diff] [blame] | 412 | static struct strbuf path = STRBUF_INIT; |
Shawn Pearce | de84f99 | 2006-03-05 03:24:15 -0500 | [diff] [blame] | 413 | struct stat st; |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 414 | |
Shawn Pearce | de84f99 | 2006-03-05 03:24:15 -0500 | [diff] [blame] | 415 | if (topath) |
| 416 | return write_entry(ce, topath, state, 1); |
| 417 | |
Michael Haggerty | f63272a | 2014-03-13 10:19:07 +0100 | [diff] [blame] | 418 | strbuf_reset(&path); |
| 419 | strbuf_add(&path, state->base_dir, state->base_dir_len); |
| 420 | strbuf_add(&path, ce->name, ce_namelen(ce)); |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 421 | |
Michael Haggerty | f63272a | 2014-03-13 10:19:07 +0100 | [diff] [blame] | 422 | if (!check_path(path.buf, path.len, &st, state->base_dir_len)) { |
Stefan Beller | 6d14eac | 2017-03-14 14:46:40 -0700 | [diff] [blame] | 423 | const struct submodule *sub; |
Nguyễn Thái Ngọc Duy | 56cac48 | 2009-12-14 18:43:58 +0700 | [diff] [blame] | 424 | unsigned changed = ce_match_stat(ce, &st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE); |
Stefan Beller | 6d14eac | 2017-03-14 14:46:40 -0700 | [diff] [blame] | 425 | /* |
| 426 | * Needs to be checked before !changed returns early, |
| 427 | * as the possibly empty directory was not changed |
| 428 | */ |
| 429 | sub = submodule_from_ce(ce); |
| 430 | if (sub) { |
| 431 | int err; |
| 432 | if (!is_submodule_populated_gently(ce->name, &err)) { |
| 433 | struct stat sb; |
| 434 | if (lstat(ce->name, &sb)) |
| 435 | die(_("could not stat file '%s'"), ce->name); |
| 436 | if (!(st.st_mode & S_IFDIR)) |
| 437 | unlink_or_warn(ce->name); |
| 438 | |
| 439 | return submodule_move_head(ce->name, |
Stefan Beller | cd279e2 | 2017-04-18 14:37:22 -0700 | [diff] [blame] | 440 | NULL, oid_to_hex(&ce->oid), 0); |
Stefan Beller | 6d14eac | 2017-03-14 14:46:40 -0700 | [diff] [blame] | 441 | } else |
| 442 | return submodule_move_head(ce->name, |
| 443 | "HEAD", oid_to_hex(&ce->oid), |
Stefan Beller | cd279e2 | 2017-04-18 14:37:22 -0700 | [diff] [blame] | 444 | state->force ? SUBMODULE_MOVE_HEAD_FORCE : 0); |
Stefan Beller | 6d14eac | 2017-03-14 14:46:40 -0700 | [diff] [blame] | 445 | } |
| 446 | |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 447 | if (!changed) |
| 448 | return 0; |
| 449 | if (!state->force) { |
| 450 | if (!state->quiet) |
Michael Haggerty | f63272a | 2014-03-13 10:19:07 +0100 | [diff] [blame] | 451 | fprintf(stderr, |
| 452 | "%s already exists, no checkout\n", |
| 453 | path.buf); |
Junio C Hamano | 4b12dae | 2005-10-03 12:44:48 -0700 | [diff] [blame] | 454 | return -1; |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | /* |
| 458 | * We unlink the old file, to get the new one with the |
| 459 | * right permissions (including umask, which is nasty |
| 460 | * to emulate by hand - much easier to let the system |
| 461 | * just do the right thing) |
| 462 | */ |
Linus Torvalds | d48a72f | 2005-07-14 09:58:45 -0700 | [diff] [blame] | 463 | if (S_ISDIR(st.st_mode)) { |
Linus Torvalds | f0807e6 | 2007-04-13 09:26:04 -0700 | [diff] [blame] | 464 | /* If it is a gitlink, leave it alone! */ |
Linus Torvalds | 7a51ed6 | 2008-01-14 16:03:17 -0800 | [diff] [blame] | 465 | if (S_ISGITLINK(ce->ce_mode)) |
Linus Torvalds | f0807e6 | 2007-04-13 09:26:04 -0700 | [diff] [blame] | 466 | return 0; |
Linus Torvalds | d48a72f | 2005-07-14 09:58:45 -0700 | [diff] [blame] | 467 | if (!state->force) |
Michael Haggerty | f63272a | 2014-03-13 10:19:07 +0100 | [diff] [blame] | 468 | return error("%s is a directory", path.buf); |
Michael Haggerty | 2f29e0c | 2014-03-13 10:19:08 +0100 | [diff] [blame] | 469 | remove_subtree(&path); |
Michael Haggerty | f63272a | 2014-03-13 10:19:07 +0100 | [diff] [blame] | 470 | } else if (unlink(path.buf)) |
Nguyễn Thái Ngọc Duy | e1ebb3c | 2016-05-08 16:47:44 +0700 | [diff] [blame] | 471 | return error_errno("unable to unlink old '%s'", path.buf); |
Shawn Pearce | de84f99 | 2006-03-05 03:24:15 -0500 | [diff] [blame] | 472 | } else if (state->not_new) |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 473 | return 0; |
Michael Haggerty | f63272a | 2014-03-13 10:19:07 +0100 | [diff] [blame] | 474 | |
| 475 | create_directories(path.buf, path.len, state); |
| 476 | return write_entry(ce, path.buf, state, 0); |
Linus Torvalds | 12dccc1 | 2005-06-05 21:59:54 -0700 | [diff] [blame] | 477 | } |