blob: 17a6bccec64e0e523aacc124611c43bd818372e3 [file] [log] [blame]
Linus Torvalds12dccc12005-06-05 21:59:54 -07001#include "cache.h"
Peter Eriksen8e440252006-04-02 14:44:09 +02002#include "blob.h"
Alexander Potashev8ca12c02009-01-10 15:07:50 +03003#include "dir.h"
Junio C Hamanodd8e9122011-05-12 14:31:08 -07004#include "streaming.h"
Linus Torvalds12dccc12005-06-05 21:59:54 -07005
Kjetil Barvik81a9aa62009-02-09 21:54:08 +01006static void create_directories(const char *path, int path_len,
7 const struct checkout *state)
Linus Torvalds12dccc12005-06-05 21:59:54 -07008{
Kjetil Barvik81a9aa62009-02-09 21:54:08 +01009 char *buf = xmalloc(path_len + 1);
10 int len = 0;
Linus Torvalds12dccc12005-06-05 21:59:54 -070011
Kjetil Barvik81a9aa62009-02-09 21:54:08 +010012 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-05 21:59:54 -070019 buf[len] = 0;
Junio C Hamanofa2e71c2007-07-17 22:58:28 -070020
Kjetil Barvikbad4a542009-01-18 16:14:52 +010021 /*
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 21:54:06 +010028 if (has_dirs_only_path(buf, len, state->base_dir_len))
Junio C Hamanofa2e71c2007-07-17 22:58:28 -070029 continue; /* ok, it is already a directory. */
30
31 /*
Kjetil Barvikbad4a542009-01-18 16:14:52 +010032 * 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-17 22:58:28 -070036 */
Junio C Hamanof312de02005-07-06 01:21:46 -070037 if (mkdir(buf, 0777)) {
Junio C Hamanofa2e71c2007-07-17 22:58:28 -070038 if (errno == EEXIST && state->force &&
Alex Riesen691f1a22009-04-29 23:22:56 +020039 !unlink_or_warn(buf) && !mkdir(buf, 0777))
Junio C Hamanofa2e71c2007-07-17 22:58:28 -070040 continue;
Thomas Rast0721c312009-06-27 17:58:47 +020041 die_errno("cannot create directory at '%s'", buf);
Linus Torvalds12dccc12005-06-05 21:59:54 -070042 }
43 }
44 free(buf);
45}
46
47static void remove_subtree(const char *path)
48{
49 DIR *dir = opendir(path);
50 struct dirent *de;
51 char pathbuf[PATH_MAX];
52 char *name;
Junio C Hamanoa6080a02007-06-07 00:04:01 -070053
Linus Torvalds12dccc12005-06-05 21:59:54 -070054 if (!dir)
Thomas Rastd824cbb2009-06-27 17:58:46 +020055 die_errno("cannot opendir '%s'", path);
Linus Torvalds12dccc12005-06-05 21:59:54 -070056 strcpy(pathbuf, path);
57 name = pathbuf + strlen(path);
58 *name++ = '/';
59 while ((de = readdir(dir)) != NULL) {
60 struct stat st;
Alexander Potashev8ca12c02009-01-10 15:07:50 +030061 if (is_dot_or_dotdot(de->d_name))
Linus Torvalds12dccc12005-06-05 21:59:54 -070062 continue;
63 strcpy(name, de->d_name);
64 if (lstat(pathbuf, &st))
Thomas Rastd824cbb2009-06-27 17:58:46 +020065 die_errno("cannot lstat '%s'", pathbuf);
Linus Torvalds12dccc12005-06-05 21:59:54 -070066 if (S_ISDIR(st.st_mode))
67 remove_subtree(pathbuf);
68 else if (unlink(pathbuf))
Thomas Rastd824cbb2009-06-27 17:58:46 +020069 die_errno("cannot unlink '%s'", pathbuf);
Linus Torvalds12dccc12005-06-05 21:59:54 -070070 }
71 closedir(dir);
72 if (rmdir(path))
Thomas Rastd824cbb2009-06-27 17:58:46 +020073 die_errno("cannot rmdir '%s'", path);
Linus Torvalds12dccc12005-06-05 21:59:54 -070074}
75
Linus Torvaldsd48a72f2005-07-14 09:58:45 -070076static int create_file(const char *path, unsigned int mode)
Linus Torvalds12dccc12005-06-05 21:59:54 -070077{
Linus Torvalds12dccc12005-06-05 21:59:54 -070078 mode = (mode & 0100) ? 0777 : 0666;
Alex Riesen781411e2006-01-05 09:58:06 +010079 return open(path, O_WRONLY | O_CREAT | O_EXCL, mode);
Linus Torvalds12dccc12005-06-05 21:59:54 -070080}
81
Kjetil Barvik4857c762009-02-09 21:54:50 +010082static void *read_blob_entry(struct cache_entry *ce, unsigned long *size)
Linus Torvaldsf0807e62007-04-13 09:26:04 -070083{
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
Junio C Hamanofd5db552011-05-12 21:36:42 -070095static int open_output_fd(char *path, struct cache_entry *ce, int to_tempfile)
96{
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
Junio C Hamanodd8e9122011-05-12 14:31:08 -0700118static int streaming_write_entry(struct cache_entry *ce, char *path,
Junio C Hamanob6691092011-05-20 14:33:31 -0700119 struct stream_filter *filter,
Junio C Hamanodd8e9122011-05-12 14:31:08 -0700120 const struct checkout *state, int to_tempfile,
121 int *fstat_done, struct stat *statbuf)
122{
Junio C Hamanodd8e9122011-05-12 14:31:08 -0700123 int result = -1;
Junio C Hamano47a02ff2012-03-07 17:54:15 +0700124 int fd;
Junio C Hamanodd8e9122011-05-12 14:31:08 -0700125
126 fd = open_output_fd(path, ce, to_tempfile);
Junio C Hamano47a02ff2012-03-07 17:54:15 +0700127 if (0 <= fd) {
128 result = stream_blob_to_fd(fd, ce->sha1, filter, 1);
129 *fstat_done = fstat_output(fd, state, statbuf);
Junio C Hamanodd8e9122011-05-12 14:31:08 -0700130 result = close(fd);
Junio C Hamano47a02ff2012-03-07 17:54:15 +0700131 }
Junio C Hamanodd8e9122011-05-12 14:31:08 -0700132 if (result && 0 <= fd)
133 unlink(path);
134 return result;
135}
136
Luiz Fernando N. Capitulinoefbc5832007-04-25 11:18:08 -0300137static int write_entry(struct cache_entry *ce, char *path, const struct checkout *state, int to_tempfile)
Linus Torvalds12dccc12005-06-05 21:59:54 -0700138{
Kjetil Barvik4857c762009-02-09 21:54:50 +0100139 unsigned int ce_mode_s_ifmt = ce->ce_mode & S_IFMT;
Kjetil Barvike4c72922009-02-09 21:54:51 +0100140 int fd, ret, fstat_done = 0;
Kjetil Barvik4857c762009-02-09 21:54:50 +0100141 char *new;
142 struct strbuf buf = STRBUF_INIT;
143 unsigned long size;
144 size_t wrote, newsize = 0;
Kjetil Barvike4c72922009-02-09 21:54:51 +0100145 struct stat st;
Linus Torvalds12dccc12005-06-05 21:59:54 -0700146
Junio C Hamanob6691092011-05-20 14:33:31 -0700147 if (ce_mode_s_ifmt == S_IFREG) {
148 struct stream_filter *filter = get_stream_filter(path, ce->sha1);
149 if (filter &&
150 !streaming_write_entry(ce, path, filter,
151 state, to_tempfile,
152 &fstat_done, &st))
153 goto finish;
154 }
Junio C Hamanodd8e9122011-05-12 14:31:08 -0700155
Kjetil Barvik4857c762009-02-09 21:54:50 +0100156 switch (ce_mode_s_ifmt) {
Linus Torvalds12dccc12005-06-05 21:59:54 -0700157 case S_IFREG:
Kjetil Barvik4857c762009-02-09 21:54:50 +0100158 case S_IFLNK:
159 new = read_blob_entry(ce, &size);
Linus Torvaldsf0807e62007-04-13 09:26:04 -0700160 if (!new)
Nguyễn Thái Ngọc Duyd43e9072010-11-28 11:36:38 +0700161 return error("unable to read sha1 file of %s (%s)",
Linus Torvaldsf0807e62007-04-13 09:26:04 -0700162 path, sha1_to_hex(ce->sha1));
Junio C Hamano1a9d7e92007-08-14 01:41:02 -0700163
Kjetil Barvik4857c762009-02-09 21:54:50 +0100164 if (ce_mode_s_ifmt == S_IFLNK && has_symlinks && !to_tempfile) {
165 ret = symlink(new, path);
166 free(new);
167 if (ret)
Nguyễn Thái Ngọc Duyd43e9072010-11-28 11:36:38 +0700168 return error("unable to create symlink %s (%s)",
Kjetil Barvik4857c762009-02-09 21:54:50 +0100169 path, strerror(errno));
170 break;
171 }
172
Junio C Hamano1a9d7e92007-08-14 01:41:02 -0700173 /*
174 * Convert from git internal format to working tree format
175 */
Kjetil Barvik4857c762009-02-09 21:54:50 +0100176 if (ce_mode_s_ifmt == S_IFREG &&
177 convert_to_working_tree(ce->name, new, size, &buf)) {
Junio C Hamano1a9d7e92007-08-14 01:41:02 -0700178 free(new);
René Scharfec32f7492007-10-21 11:23:49 +0200179 new = strbuf_detach(&buf, &newsize);
180 size = newsize;
Junio C Hamano1a9d7e92007-08-14 01:41:02 -0700181 }
182
Junio C Hamanofd5db552011-05-12 21:36:42 -0700183 fd = open_output_fd(path, ce, to_tempfile);
Linus Torvalds12dccc12005-06-05 21:59:54 -0700184 if (fd < 0) {
185 free(new);
Nguyễn Thái Ngọc Duyd43e9072010-11-28 11:36:38 +0700186 return error("unable to create file %s (%s)",
Linus Torvalds12dccc12005-06-05 21:59:54 -0700187 path, strerror(errno));
188 }
Linus Torvalds6c510be2007-02-13 11:07:23 -0800189
Andy Whitcroft93822c22007-01-08 15:58:23 +0000190 wrote = write_in_full(fd, new, size);
Junio C Hamanofd5db552011-05-12 21:36:42 -0700191 if (!to_tempfile)
192 fstat_done = fstat_output(fd, state, &st);
Linus Torvalds12dccc12005-06-05 21:59:54 -0700193 close(fd);
194 free(new);
195 if (wrote != size)
Nguyễn Thái Ngọc Duyd43e9072010-11-28 11:36:38 +0700196 return error("unable to write file %s", path);
Linus Torvalds12dccc12005-06-05 21:59:54 -0700197 break;
Martin Waitz302b9282007-05-21 22:08:28 +0200198 case S_IFGITLINK:
Linus Torvaldsf0807e62007-04-13 09:26:04 -0700199 if (to_tempfile)
Nguyễn Thái Ngọc Duyd43e9072010-11-28 11:36:38 +0700200 return error("cannot create temporary subproject %s", path);
Linus Torvaldsf0807e62007-04-13 09:26:04 -0700201 if (mkdir(path, 0777) < 0)
Nguyễn Thái Ngọc Duyd43e9072010-11-28 11:36:38 +0700202 return error("cannot create subproject directory %s", path);
Linus Torvaldsf0807e62007-04-13 09:26:04 -0700203 break;
Linus Torvalds12dccc12005-06-05 21:59:54 -0700204 default:
Nguyễn Thái Ngọc Duyd43e9072010-11-28 11:36:38 +0700205 return error("unknown file mode for %s in index", path);
Linus Torvalds12dccc12005-06-05 21:59:54 -0700206 }
207
Junio C Hamanodd8e9122011-05-12 14:31:08 -0700208finish:
Linus Torvalds6ee67f22005-06-05 23:15:40 -0700209 if (state->refresh_cache) {
Kjetil Barvike4c72922009-02-09 21:54:51 +0100210 if (!fstat_done)
211 lstat(ce->name, &st);
Linus Torvalds12dccc12005-06-05 21:59:54 -0700212 fill_stat_cache_info(ce, &st);
213 }
214 return 0;
215}
216
Linus Torvaldsb6986d82009-07-29 20:22:25 -0700217/*
218 * This is like 'lstat()', except it refuses to follow symlinks
Junio C Hamanoda02ca52009-08-16 23:53:12 -0700219 * in the path, after skipping "skiplen".
Linus Torvaldsb6986d82009-07-29 20:22:25 -0700220 */
Junio C Hamano61b97df2010-01-11 22:27:31 -0800221static int check_path(const char *path, int len, struct stat *st, int skiplen)
Linus Torvaldsb6986d82009-07-29 20:22:25 -0700222{
Junio C Hamanoda02ca52009-08-16 23:53:12 -0700223 const char *slash = path + len;
224
225 while (path < slash && *slash != '/')
226 slash--;
227 if (!has_dirs_only_path(path, slash - path, skiplen)) {
Linus Torvaldsb6986d82009-07-29 20:22:25 -0700228 errno = ENOENT;
229 return -1;
230 }
231 return lstat(path, st);
232}
233
Luiz Fernando N. Capitulinoefbc5832007-04-25 11:18:08 -0300234int checkout_entry(struct cache_entry *ce, const struct checkout *state, char *topath)
Linus Torvalds12dccc12005-06-05 21:59:54 -0700235{
Jonas Fonseca095c4242006-08-26 16:09:17 +0200236 static char path[PATH_MAX + 1];
Shawn Pearcede84f992006-03-05 03:24:15 -0500237 struct stat st;
Linus Torvalds12dccc12005-06-05 21:59:54 -0700238 int len = state->base_dir_len;
239
Shawn Pearcede84f992006-03-05 03:24:15 -0500240 if (topath)
241 return write_entry(ce, topath, state, 1);
242
Linus Torvalds12dccc12005-06-05 21:59:54 -0700243 memcpy(path, state->base_dir, len);
244 strcpy(path + len, ce->name);
Kjetil Barvik81a9aa62009-02-09 21:54:08 +0100245 len += ce_namelen(ce);
Linus Torvalds12dccc12005-06-05 21:59:54 -0700246
Junio C Hamanoda02ca52009-08-16 23:53:12 -0700247 if (!check_path(path, len, &st, state->base_dir_len)) {
Nguyễn Thái Ngọc Duy56cac482009-12-14 18:43:58 +0700248 unsigned changed = ce_match_stat(ce, &st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE);
Linus Torvalds12dccc12005-06-05 21:59:54 -0700249 if (!changed)
250 return 0;
251 if (!state->force) {
252 if (!state->quiet)
Nguyễn Thái Ngọc Duyd43e9072010-11-28 11:36:38 +0700253 fprintf(stderr, "%s already exists, no checkout\n", path);
Junio C Hamano4b12dae2005-10-03 12:44:48 -0700254 return -1;
Linus Torvalds12dccc12005-06-05 21:59:54 -0700255 }
256
257 /*
258 * We unlink the old file, to get the new one with the
259 * right permissions (including umask, which is nasty
260 * to emulate by hand - much easier to let the system
261 * just do the right thing)
262 */
Linus Torvaldsd48a72f2005-07-14 09:58:45 -0700263 if (S_ISDIR(st.st_mode)) {
Linus Torvaldsf0807e62007-04-13 09:26:04 -0700264 /* If it is a gitlink, leave it alone! */
Linus Torvalds7a51ed62008-01-14 16:03:17 -0800265 if (S_ISGITLINK(ce->ce_mode))
Linus Torvaldsf0807e62007-04-13 09:26:04 -0700266 return 0;
Linus Torvaldsd48a72f2005-07-14 09:58:45 -0700267 if (!state->force)
268 return error("%s is a directory", path);
269 remove_subtree(path);
Linus Torvalds971f2292008-03-17 08:56:27 -0700270 } else if (unlink(path))
271 return error("unable to unlink old '%s' (%s)", path, strerror(errno));
Shawn Pearcede84f992006-03-05 03:24:15 -0500272 } else if (state->not_new)
Linus Torvalds12dccc12005-06-05 21:59:54 -0700273 return 0;
Kjetil Barvik81a9aa62009-02-09 21:54:08 +0100274 create_directories(path, len, state);
Shawn Pearcede84f992006-03-05 03:24:15 -0500275 return write_entry(ce, path, state, 0);
Linus Torvalds12dccc12005-06-05 21:59:54 -0700276}