🌐 AI搜索 & 代理 主页
blob: 1eda8e94714bbd6020d61b4b627f721b1042673c [file] [log] [blame]
Linus Torvalds12dccc12005-06-06 04:59:541#include "cache.h"
Peter Eriksen8e440252006-04-02 12:44:092#include "blob.h"
Alexander Potashev8ca12c02009-01-10 12:07:503#include "dir.h"
Junio C Hamanodd8e9122011-05-12 21:31:084#include "streaming.h"
Linus Torvalds12dccc12005-06-06 04:59:545
Kjetil Barvik81a9aa62009-02-09 20:54:086static void create_directories(const char *path, int path_len,
7 const struct checkout *state)
Linus Torvalds12dccc12005-06-06 04:59:548{
Kjetil Barvik81a9aa62009-02-09 20:54:089 char *buf = xmalloc(path_len + 1);
10 int len = 0;
Linus Torvalds12dccc12005-06-06 04:59:5411
Kjetil Barvik81a9aa62009-02-09 20:54:0812 while (len < path_len) {
13 do {
14 buf[len] = path[len];
15 len++;
16 } while (len < path_len && path[len] != '/');
17 if (len >= path_len)
18 break;
Linus Torvalds12dccc12005-06-06 04:59:5419 buf[len] = 0;
Junio C Hamanofa2e71c2007-07-18 05:58:2820
Kjetil Barvikbad4a542009-01-18 15:14:5221 /*
22 * For 'checkout-index --prefix=<dir>', <dir> is
23 * allowed to be a symlink to an existing directory,
24 * and we set 'state->base_dir_len' below, such that
25 * we test the path components of the prefix with the
26 * stat() function instead of the lstat() function.
27 */
Kjetil Barvik57199892009-02-09 20:54:0628 if (has_dirs_only_path(buf, len, state->base_dir_len))
Junio C Hamanofa2e71c2007-07-18 05:58:2829 continue; /* ok, it is already a directory. */
30
31 /*
Kjetil Barvikbad4a542009-01-18 15:14:5232 * If this mkdir() would fail, it could be that there
33 * is already a symlink or something else exists
34 * there, therefore we then try to unlink it and try
35 * one more time to create the directory.
Junio C Hamanofa2e71c2007-07-18 05:58:2836 */
Junio C Hamanof312de02005-07-06 08:21:4637 if (mkdir(buf, 0777)) {
Junio C Hamanofa2e71c2007-07-18 05:58:2838 if (errno == EEXIST && state->force &&
Alex Riesen691f1a22009-04-29 21:22:5639 !unlink_or_warn(buf) && !mkdir(buf, 0777))
Junio C Hamanofa2e71c2007-07-18 05:58:2840 continue;
Thomas Rast0721c312009-06-27 15:58:4741 die_errno("cannot create directory at '%s'", buf);
Linus Torvalds12dccc12005-06-06 04:59:5442 }
43 }
44 free(buf);
45}
46
Michael Haggerty2f29e0c2014-03-13 09:19:0847static void remove_subtree(struct strbuf *path)
Linus Torvalds12dccc12005-06-06 04:59:5448{
Michael Haggerty2f29e0c2014-03-13 09:19:0849 DIR *dir = opendir(path->buf);
Linus Torvalds12dccc12005-06-06 04:59:5450 struct dirent *de;
Michael Haggerty2f29e0c2014-03-13 09:19:0851 int origlen = path->len;
Junio C Hamanoa6080a02007-06-07 07:04:0152
Linus Torvalds12dccc12005-06-06 04:59:5453 if (!dir)
Michael Haggerty2f29e0c2014-03-13 09:19:0854 die_errno("cannot opendir '%s'", path->buf);
Linus Torvalds12dccc12005-06-06 04:59:5455 while ((de = readdir(dir)) != NULL) {
56 struct stat st;
Michael Haggerty2f29e0c2014-03-13 09:19:0857
Alexander Potashev8ca12c02009-01-10 12:07:5058 if (is_dot_or_dotdot(de->d_name))
Linus Torvalds12dccc12005-06-06 04:59:5459 continue;
Michael Haggerty2f29e0c2014-03-13 09:19:0860
61 strbuf_addch(path, '/');
62 strbuf_addstr(path, de->d_name);
63 if (lstat(path->buf, &st))
64 die_errno("cannot lstat '%s'", path->buf);
Linus Torvalds12dccc12005-06-06 04:59:5465 if (S_ISDIR(st.st_mode))
Michael Haggerty2f29e0c2014-03-13 09:19:0866 remove_subtree(path);
67 else if (unlink(path->buf))
68 die_errno("cannot unlink '%s'", path->buf);
69 strbuf_setlen(path, origlen);
Linus Torvalds12dccc12005-06-06 04:59:5470 }
71 closedir(dir);
Michael Haggerty2f29e0c2014-03-13 09:19:0872 if (rmdir(path->buf))
73 die_errno("cannot rmdir '%s'", path->buf);
Linus Torvalds12dccc12005-06-06 04:59:5474}
75
Linus Torvaldsd48a72f2005-07-14 16:58:4576static int create_file(const char *path, unsigned int mode)
Linus Torvalds12dccc12005-06-06 04:59:5477{
Linus Torvalds12dccc12005-06-06 04:59:5478 mode = (mode & 0100) ? 0777 : 0666;
Alex Riesen781411e2006-01-05 08:58:0679 return open(path, O_WRONLY | O_CREAT | O_EXCL, mode);
Linus Torvalds12dccc12005-06-06 04:59:5480}
81
Nguyễn Thái Ngọc Duy9c5e6c82013-07-09 15:29:0082static void *read_blob_entry(const struct cache_entry *ce, unsigned long *size)
Linus Torvaldsf0807e62007-04-13 16:26:0483{
84 enum object_type type;
85 void *new = read_sha1_file(ce->sha1, &type, size);
86
87 if (new) {
88 if (type == OBJ_BLOB)
89 return new;
90 free(new);
91 }
92 return NULL;
93}
94
Nguyễn Thái Ngọc Duy9c5e6c82013-07-09 15:29:0095static int open_output_fd(char *path, const struct cache_entry *ce, int to_tempfile)
Junio C Hamanofd5db552011-05-13 04:36:4296{
97 int symlink = (ce->ce_mode & S_IFMT) != S_IFREG;
98 if (to_tempfile) {
99 strcpy(path, symlink
100 ? ".merge_link_XXXXXX" : ".merge_file_XXXXXX");
101 return mkstemp(path);
102 } else {
103 return create_file(path, !symlink ? ce->ce_mode : 0666);
104 }
105}
106
107static int fstat_output(int fd, const struct checkout *state, struct stat *st)
108{
109 /* use fstat() only when path == ce->name */
110 if (fstat_is_reliable() &&
111 state->refresh_cache && !state->base_dir_len) {
112 fstat(fd, st);
113 return 1;
114 }
115 return 0;
116}
117
Nguyễn Thái Ngọc Duy9c5e6c82013-07-09 15:29:00118static int streaming_write_entry(const struct cache_entry *ce, char *path,
Junio C Hamanob6691092011-05-20 21:33:31119 struct stream_filter *filter,
Junio C Hamanodd8e9122011-05-12 21:31:08120 const struct checkout *state, int to_tempfile,
121 int *fstat_done, struct stat *statbuf)
122{
Jeff Kingd9c31e12013-03-25 21:49:36123 int result = 0;
Junio C Hamano47a02ff2012-03-07 10:54:15124 int fd;
Junio C Hamanodd8e9122011-05-12 21:31:08125
126 fd = open_output_fd(path, ce, to_tempfile);
Jeff Kingd9c31e12013-03-25 21:49:36127 if (fd < 0)
128 return -1;
129
130 result |= stream_blob_to_fd(fd, ce->sha1, filter, 1);
131 *fstat_done = fstat_output(fd, state, statbuf);
132 result |= close(fd);
133
134 if (result)
Junio C Hamanodd8e9122011-05-12 21:31:08135 unlink(path);
136 return result;
137}
138
Nguyễn Thái Ngọc Duy9c5e6c82013-07-09 15:29:00139static int write_entry(struct cache_entry *ce,
140 char *path, const struct checkout *state, int to_tempfile)
Linus Torvalds12dccc12005-06-06 04:59:54141{
Kjetil Barvik4857c762009-02-09 20:54:50142 unsigned int ce_mode_s_ifmt = ce->ce_mode & S_IFMT;
Kjetil Barvike4c72922009-02-09 20:54:51143 int fd, ret, fstat_done = 0;
Kjetil Barvik4857c762009-02-09 20:54:50144 char *new;
145 struct strbuf buf = STRBUF_INIT;
146 unsigned long size;
147 size_t wrote, newsize = 0;
Kjetil Barvike4c72922009-02-09 20:54:51148 struct stat st;
Linus Torvalds12dccc12005-06-06 04:59:54149
Junio C Hamanob6691092011-05-20 21:33:31150 if (ce_mode_s_ifmt == S_IFREG) {
John Keeping7297a442013-03-14 20:00:51151 struct stream_filter *filter = get_stream_filter(ce->name, ce->sha1);
Junio C Hamanob6691092011-05-20 21:33:31152 if (filter &&
153 !streaming_write_entry(ce, path, filter,
154 state, to_tempfile,
155 &fstat_done, &st))
156 goto finish;
157 }
Junio C Hamanodd8e9122011-05-12 21:31:08158
Kjetil Barvik4857c762009-02-09 20:54:50159 switch (ce_mode_s_ifmt) {
Linus Torvalds12dccc12005-06-06 04:59:54160 case S_IFREG:
Kjetil Barvik4857c762009-02-09 20:54:50161 case S_IFLNK:
162 new = read_blob_entry(ce, &size);
Linus Torvaldsf0807e62007-04-13 16:26:04163 if (!new)
Nguyễn Thái Ngọc Duyd43e9072010-11-28 04:36:38164 return error("unable to read sha1 file of %s (%s)",
Linus Torvaldsf0807e62007-04-13 16:26:04165 path, sha1_to_hex(ce->sha1));
Junio C Hamano1a9d7e92007-08-14 08:41:02166
Kjetil Barvik4857c762009-02-09 20:54:50167 if (ce_mode_s_ifmt == S_IFLNK && has_symlinks && !to_tempfile) {
168 ret = symlink(new, path);
169 free(new);
170 if (ret)
Nguyễn Thái Ngọc Duyd43e9072010-11-28 04:36:38171 return error("unable to create symlink %s (%s)",
Kjetil Barvik4857c762009-02-09 20:54:50172 path, strerror(errno));
173 break;
174 }
175
Junio C Hamano1a9d7e92007-08-14 08:41:02176 /*
177 * Convert from git internal format to working tree format
178 */
Kjetil Barvik4857c762009-02-09 20:54:50179 if (ce_mode_s_ifmt == S_IFREG &&
180 convert_to_working_tree(ce->name, new, size, &buf)) {
Junio C Hamano1a9d7e92007-08-14 08:41:02181 free(new);
René Scharfec32f7492007-10-21 09:23:49182 new = strbuf_detach(&buf, &newsize);
183 size = newsize;
Junio C Hamano1a9d7e92007-08-14 08:41:02184 }
185
Junio C Hamanofd5db552011-05-13 04:36:42186 fd = open_output_fd(path, ce, to_tempfile);
Linus Torvalds12dccc12005-06-06 04:59:54187 if (fd < 0) {
188 free(new);
Nguyễn Thái Ngọc Duyd43e9072010-11-28 04:36:38189 return error("unable to create file %s (%s)",
Linus Torvalds12dccc12005-06-06 04:59:54190 path, strerror(errno));
191 }
Linus Torvalds6c510be2007-02-13 19:07:23192
Andy Whitcroft93822c22007-01-08 15:58:23193 wrote = write_in_full(fd, new, size);
Junio C Hamanofd5db552011-05-13 04:36:42194 if (!to_tempfile)
195 fstat_done = fstat_output(fd, state, &st);
Linus Torvalds12dccc12005-06-06 04:59:54196 close(fd);
197 free(new);
198 if (wrote != size)
Nguyễn Thái Ngọc Duyd43e9072010-11-28 04:36:38199 return error("unable to write file %s", path);
Linus Torvalds12dccc12005-06-06 04:59:54200 break;
Martin Waitz302b9282007-05-21 20:08:28201 case S_IFGITLINK:
Linus Torvaldsf0807e62007-04-13 16:26:04202 if (to_tempfile)
Thomas Rast42063f92013-07-18 12:26:55203 return error("cannot create temporary submodule %s", path);
Linus Torvaldsf0807e62007-04-13 16:26:04204 if (mkdir(path, 0777) < 0)
Thomas Rast42063f92013-07-18 12:26:55205 return error("cannot create submodule directory %s", path);
Linus Torvaldsf0807e62007-04-13 16:26:04206 break;
Linus Torvalds12dccc12005-06-06 04:59:54207 default:
Nguyễn Thái Ngọc Duyd43e9072010-11-28 04:36:38208 return error("unknown file mode for %s in index", path);
Linus Torvalds12dccc12005-06-06 04:59:54209 }
210
Junio C Hamanodd8e9122011-05-12 21:31:08211finish:
Linus Torvalds6ee67f22005-06-06 06:15:40212 if (state->refresh_cache) {
Nguyễn Thái Ngọc Duyd4a20242014-06-13 12:19:34213 assert(state->istate);
Kjetil Barvike4c72922009-02-09 20:54:51214 if (!fstat_done)
215 lstat(ce->name, &st);
Linus Torvalds12dccc12005-06-06 04:59:54216 fill_stat_cache_info(ce, &st);
Nguyễn Thái Ngọc Duy078a58e2014-06-13 12:19:39217 ce->ce_flags |= CE_UPDATE_IN_BASE;
Nguyễn Thái Ngọc Duyd4a20242014-06-13 12:19:34218 state->istate->cache_changed |= CE_ENTRY_CHANGED;
Linus Torvalds12dccc12005-06-06 04:59:54219 }
220 return 0;
221}
222
Linus Torvaldsb6986d82009-07-30 03:22:25223/*
224 * This is like 'lstat()', except it refuses to follow symlinks
Junio C Hamanoda02ca52009-08-17 06:53:12225 * in the path, after skipping "skiplen".
Linus Torvaldsb6986d82009-07-30 03:22:25226 */
Junio C Hamano61b97df2010-01-12 06:27:31227static int check_path(const char *path, int len, struct stat *st, int skiplen)
Linus Torvaldsb6986d82009-07-30 03:22:25228{
Junio C Hamanoda02ca52009-08-17 06:53:12229 const char *slash = path + len;
230
231 while (path < slash && *slash != '/')
232 slash--;
233 if (!has_dirs_only_path(path, slash - path, skiplen)) {
Linus Torvaldsb6986d82009-07-30 03:22:25234 errno = ENOENT;
235 return -1;
236 }
237 return lstat(path, st);
238}
239
Junio C Hamanoaf2a6512013-10-23 17:52:42240/*
241 * Write the contents from ce out to the working tree.
242 *
243 * When topath[] is not NULL, instead of writing to the working tree
244 * file named by ce, a temporary file is created by this function and
245 * its name is returned in topath[], which must be able to hold at
246 * least TEMPORARY_FILENAME_LENGTH bytes long.
247 */
Nguyễn Thái Ngọc Duy9c5e6c82013-07-09 15:29:00248int checkout_entry(struct cache_entry *ce,
249 const struct checkout *state, char *topath)
Linus Torvalds12dccc12005-06-06 04:59:54250{
Michael Haggertyf63272a2014-03-13 09:19:07251 static struct strbuf path = STRBUF_INIT;
Shawn Pearcede84f992006-03-05 08:24:15252 struct stat st;
Linus Torvalds12dccc12005-06-06 04:59:54253
Shawn Pearcede84f992006-03-05 08:24:15254 if (topath)
255 return write_entry(ce, topath, state, 1);
256
Michael Haggertyf63272a2014-03-13 09:19:07257 strbuf_reset(&path);
258 strbuf_add(&path, state->base_dir, state->base_dir_len);
259 strbuf_add(&path, ce->name, ce_namelen(ce));
Linus Torvalds12dccc12005-06-06 04:59:54260
Michael Haggertyf63272a2014-03-13 09:19:07261 if (!check_path(path.buf, path.len, &st, state->base_dir_len)) {
Nguyễn Thái Ngọc Duy56cac482009-12-14 11:43:58262 unsigned changed = ce_match_stat(ce, &st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE);
Linus Torvalds12dccc12005-06-06 04:59:54263 if (!changed)
264 return 0;
265 if (!state->force) {
266 if (!state->quiet)
Michael Haggertyf63272a2014-03-13 09:19:07267 fprintf(stderr,
268 "%s already exists, no checkout\n",
269 path.buf);
Junio C Hamano4b12dae2005-10-03 19:44:48270 return -1;
Linus Torvalds12dccc12005-06-06 04:59:54271 }
272
273 /*
274 * We unlink the old file, to get the new one with the
275 * right permissions (including umask, which is nasty
276 * to emulate by hand - much easier to let the system
277 * just do the right thing)
278 */
Linus Torvaldsd48a72f2005-07-14 16:58:45279 if (S_ISDIR(st.st_mode)) {
Linus Torvaldsf0807e62007-04-13 16:26:04280 /* If it is a gitlink, leave it alone! */
Linus Torvalds7a51ed62008-01-15 00:03:17281 if (S_ISGITLINK(ce->ce_mode))
Linus Torvaldsf0807e62007-04-13 16:26:04282 return 0;
Linus Torvaldsd48a72f2005-07-14 16:58:45283 if (!state->force)
Michael Haggertyf63272a2014-03-13 09:19:07284 return error("%s is a directory", path.buf);
Michael Haggerty2f29e0c2014-03-13 09:19:08285 remove_subtree(&path);
Michael Haggertyf63272a2014-03-13 09:19:07286 } else if (unlink(path.buf))
287 return error("unable to unlink old '%s' (%s)",
288 path.buf, strerror(errno));
Shawn Pearcede84f992006-03-05 08:24:15289 } else if (state->not_new)
Linus Torvalds12dccc12005-06-06 04:59:54290 return 0;
Michael Haggertyf63272a2014-03-13 09:19:07291
292 create_directories(path.buf, path.len, state);
293 return write_entry(ce, path.buf, state, 0);
Linus Torvalds12dccc12005-06-06 04:59:54294}