blob: b017167f2015623fb9c721e91d0a940abd1d5196 [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"
Linus Torvalds12dccc12005-06-05 21:59:54 -07004
Kjetil Barvik81a9aa62009-02-09 21:54:08 +01005static void create_directories(const char *path, int path_len,
6 const struct checkout *state)
Linus Torvalds12dccc12005-06-05 21:59:54 -07007{
Kjetil Barvik81a9aa62009-02-09 21:54:08 +01008 char *buf = xmalloc(path_len + 1);
9 int len = 0;
Linus Torvalds12dccc12005-06-05 21:59:54 -070010
Kjetil Barvik81a9aa62009-02-09 21:54:08 +010011 while (len < path_len) {
12 do {
13 buf[len] = path[len];
14 len++;
15 } while (len < path_len && path[len] != '/');
16 if (len >= path_len)
17 break;
Linus Torvalds12dccc12005-06-05 21:59:54 -070018 buf[len] = 0;
Junio C Hamanofa2e71c2007-07-17 22:58:28 -070019
Kjetil Barvikbad4a542009-01-18 16:14:52 +010020 /*
21 * For 'checkout-index --prefix=<dir>', <dir> is
22 * allowed to be a symlink to an existing directory,
23 * and we set 'state->base_dir_len' below, such that
24 * we test the path components of the prefix with the
25 * stat() function instead of the lstat() function.
26 */
Kjetil Barvik57199892009-02-09 21:54:06 +010027 if (has_dirs_only_path(buf, len, state->base_dir_len))
Junio C Hamanofa2e71c2007-07-17 22:58:28 -070028 continue; /* ok, it is already a directory. */
29
30 /*
Kjetil Barvikbad4a542009-01-18 16:14:52 +010031 * If this mkdir() would fail, it could be that there
32 * is already a symlink or something else exists
33 * there, therefore we then try to unlink it and try
34 * one more time to create the directory.
Junio C Hamanofa2e71c2007-07-17 22:58:28 -070035 */
Junio C Hamanof312de02005-07-06 01:21:46 -070036 if (mkdir(buf, 0777)) {
Junio C Hamanofa2e71c2007-07-17 22:58:28 -070037 if (errno == EEXIST && state->force &&
Alex Riesen691f1a22009-04-29 23:22:56 +020038 !unlink_or_warn(buf) && !mkdir(buf, 0777))
Junio C Hamanofa2e71c2007-07-17 22:58:28 -070039 continue;
Thomas Rast0721c312009-06-27 17:58:47 +020040 die_errno("cannot create directory at '%s'", buf);
Linus Torvalds12dccc12005-06-05 21:59:54 -070041 }
42 }
43 free(buf);
44}
45
46static void remove_subtree(const char *path)
47{
48 DIR *dir = opendir(path);
49 struct dirent *de;
50 char pathbuf[PATH_MAX];
51 char *name;
Junio C Hamanoa6080a02007-06-07 00:04:01 -070052
Linus Torvalds12dccc12005-06-05 21:59:54 -070053 if (!dir)
Thomas Rastd824cbb2009-06-27 17:58:46 +020054 die_errno("cannot opendir '%s'", path);
Linus Torvalds12dccc12005-06-05 21:59:54 -070055 strcpy(pathbuf, path);
56 name = pathbuf + strlen(path);
57 *name++ = '/';
58 while ((de = readdir(dir)) != NULL) {
59 struct stat st;
Alexander Potashev8ca12c02009-01-10 15:07:50 +030060 if (is_dot_or_dotdot(de->d_name))
Linus Torvalds12dccc12005-06-05 21:59:54 -070061 continue;
62 strcpy(name, de->d_name);
63 if (lstat(pathbuf, &st))
Thomas Rastd824cbb2009-06-27 17:58:46 +020064 die_errno("cannot lstat '%s'", pathbuf);
Linus Torvalds12dccc12005-06-05 21:59:54 -070065 if (S_ISDIR(st.st_mode))
66 remove_subtree(pathbuf);
67 else if (unlink(pathbuf))
Thomas Rastd824cbb2009-06-27 17:58:46 +020068 die_errno("cannot unlink '%s'", pathbuf);
Linus Torvalds12dccc12005-06-05 21:59:54 -070069 }
70 closedir(dir);
71 if (rmdir(path))
Thomas Rastd824cbb2009-06-27 17:58:46 +020072 die_errno("cannot rmdir '%s'", path);
Linus Torvalds12dccc12005-06-05 21:59:54 -070073}
74
Linus Torvaldsd48a72f2005-07-14 09:58:45 -070075static int create_file(const char *path, unsigned int mode)
Linus Torvalds12dccc12005-06-05 21:59:54 -070076{
Linus Torvalds12dccc12005-06-05 21:59:54 -070077 mode = (mode & 0100) ? 0777 : 0666;
Alex Riesen781411e2006-01-05 09:58:06 +010078 return open(path, O_WRONLY | O_CREAT | O_EXCL, mode);
Linus Torvalds12dccc12005-06-05 21:59:54 -070079}
80
Kjetil Barvik4857c762009-02-09 21:54:50 +010081static void *read_blob_entry(struct cache_entry *ce, unsigned long *size)
Linus Torvaldsf0807e62007-04-13 09:26:04 -070082{
83 enum object_type type;
84 void *new = read_sha1_file(ce->sha1, &type, size);
85
86 if (new) {
87 if (type == OBJ_BLOB)
88 return new;
89 free(new);
90 }
91 return NULL;
92}
93
Luiz Fernando N. Capitulinoefbc5832007-04-25 11:18:08 -030094static int write_entry(struct cache_entry *ce, char *path, const struct checkout *state, int to_tempfile)
Linus Torvalds12dccc12005-06-05 21:59:54 -070095{
Kjetil Barvik4857c762009-02-09 21:54:50 +010096 unsigned int ce_mode_s_ifmt = ce->ce_mode & S_IFMT;
Kjetil Barvike4c72922009-02-09 21:54:51 +010097 int fd, ret, fstat_done = 0;
Kjetil Barvik4857c762009-02-09 21:54:50 +010098 char *new;
99 struct strbuf buf = STRBUF_INIT;
100 unsigned long size;
101 size_t wrote, newsize = 0;
Kjetil Barvike4c72922009-02-09 21:54:51 +0100102 struct stat st;
Linus Torvalds12dccc12005-06-05 21:59:54 -0700103
Kjetil Barvik4857c762009-02-09 21:54:50 +0100104 switch (ce_mode_s_ifmt) {
Linus Torvalds12dccc12005-06-05 21:59:54 -0700105 case S_IFREG:
Kjetil Barvik4857c762009-02-09 21:54:50 +0100106 case S_IFLNK:
107 new = read_blob_entry(ce, &size);
Linus Torvaldsf0807e62007-04-13 09:26:04 -0700108 if (!new)
Nguyễn Thái Ngọc Duyd43e9072010-11-28 11:36:38 +0700109 return error("unable to read sha1 file of %s (%s)",
Linus Torvaldsf0807e62007-04-13 09:26:04 -0700110 path, sha1_to_hex(ce->sha1));
Junio C Hamano1a9d7e92007-08-14 01:41:02 -0700111
Kjetil Barvik4857c762009-02-09 21:54:50 +0100112 if (ce_mode_s_ifmt == S_IFLNK && has_symlinks && !to_tempfile) {
113 ret = symlink(new, path);
114 free(new);
115 if (ret)
Nguyễn Thái Ngọc Duyd43e9072010-11-28 11:36:38 +0700116 return error("unable to create symlink %s (%s)",
Kjetil Barvik4857c762009-02-09 21:54:50 +0100117 path, strerror(errno));
118 break;
119 }
120
Junio C Hamano1a9d7e92007-08-14 01:41:02 -0700121 /*
122 * Convert from git internal format to working tree format
123 */
Kjetil Barvik4857c762009-02-09 21:54:50 +0100124 if (ce_mode_s_ifmt == S_IFREG &&
125 convert_to_working_tree(ce->name, new, size, &buf)) {
Junio C Hamano1a9d7e92007-08-14 01:41:02 -0700126 free(new);
René Scharfec32f7492007-10-21 11:23:49 +0200127 new = strbuf_detach(&buf, &newsize);
128 size = newsize;
Junio C Hamano1a9d7e92007-08-14 01:41:02 -0700129 }
130
Shawn Pearcede84f992006-03-05 03:24:15 -0500131 if (to_tempfile) {
Kjetil Barvik4857c762009-02-09 21:54:50 +0100132 if (ce_mode_s_ifmt == S_IFREG)
133 strcpy(path, ".merge_file_XXXXXX");
134 else
135 strcpy(path, ".merge_link_XXXXXX");
Shawn Pearcede84f992006-03-05 03:24:15 -0500136 fd = mkstemp(path);
Kjetil Barvik4857c762009-02-09 21:54:50 +0100137 } else if (ce_mode_s_ifmt == S_IFREG) {
Linus Torvalds7a51ed62008-01-14 16:03:17 -0800138 fd = create_file(path, ce->ce_mode);
Kjetil Barvik4857c762009-02-09 21:54:50 +0100139 } else {
140 fd = create_file(path, 0666);
141 }
Linus Torvalds12dccc12005-06-05 21:59:54 -0700142 if (fd < 0) {
143 free(new);
Nguyễn Thái Ngọc Duyd43e9072010-11-28 11:36:38 +0700144 return error("unable to create file %s (%s)",
Linus Torvalds12dccc12005-06-05 21:59:54 -0700145 path, strerror(errno));
146 }
Linus Torvalds6c510be2007-02-13 11:07:23 -0800147
Andy Whitcroft93822c22007-01-08 15:58:23 +0000148 wrote = write_in_full(fd, new, size);
Kjetil Barvike4c72922009-02-09 21:54:51 +0100149 /* use fstat() only when path == ce->name */
Johannes Sixt34779c52009-04-20 10:17:00 +0200150 if (fstat_is_reliable() &&
151 state->refresh_cache && !to_tempfile && !state->base_dir_len) {
Kjetil Barvike4c72922009-02-09 21:54:51 +0100152 fstat(fd, &st);
153 fstat_done = 1;
154 }
Linus Torvalds12dccc12005-06-05 21:59:54 -0700155 close(fd);
156 free(new);
157 if (wrote != size)
Nguyễn Thái Ngọc Duyd43e9072010-11-28 11:36:38 +0700158 return error("unable to write file %s", path);
Linus Torvalds12dccc12005-06-05 21:59:54 -0700159 break;
Martin Waitz302b9282007-05-21 22:08:28 +0200160 case S_IFGITLINK:
Linus Torvaldsf0807e62007-04-13 09:26:04 -0700161 if (to_tempfile)
Nguyễn Thái Ngọc Duyd43e9072010-11-28 11:36:38 +0700162 return error("cannot create temporary subproject %s", path);
Linus Torvaldsf0807e62007-04-13 09:26:04 -0700163 if (mkdir(path, 0777) < 0)
Nguyễn Thái Ngọc Duyd43e9072010-11-28 11:36:38 +0700164 return error("cannot create subproject directory %s", path);
Linus Torvaldsf0807e62007-04-13 09:26:04 -0700165 break;
Linus Torvalds12dccc12005-06-05 21:59:54 -0700166 default:
Nguyễn Thái Ngọc Duyd43e9072010-11-28 11:36:38 +0700167 return error("unknown file mode for %s in index", path);
Linus Torvalds12dccc12005-06-05 21:59:54 -0700168 }
169
Linus Torvalds6ee67f22005-06-05 23:15:40 -0700170 if (state->refresh_cache) {
Kjetil Barvike4c72922009-02-09 21:54:51 +0100171 if (!fstat_done)
172 lstat(ce->name, &st);
Linus Torvalds12dccc12005-06-05 21:59:54 -0700173 fill_stat_cache_info(ce, &st);
174 }
175 return 0;
176}
177
Linus Torvaldsb6986d82009-07-29 20:22:25 -0700178/*
179 * This is like 'lstat()', except it refuses to follow symlinks
Junio C Hamanoda02ca52009-08-16 23:53:12 -0700180 * in the path, after skipping "skiplen".
Linus Torvaldsb6986d82009-07-29 20:22:25 -0700181 */
Junio C Hamano61b97df2010-01-11 22:27:31 -0800182static int check_path(const char *path, int len, struct stat *st, int skiplen)
Linus Torvaldsb6986d82009-07-29 20:22:25 -0700183{
Junio C Hamanoda02ca52009-08-16 23:53:12 -0700184 const char *slash = path + len;
185
186 while (path < slash && *slash != '/')
187 slash--;
188 if (!has_dirs_only_path(path, slash - path, skiplen)) {
Linus Torvaldsb6986d82009-07-29 20:22:25 -0700189 errno = ENOENT;
190 return -1;
191 }
192 return lstat(path, st);
193}
194
Luiz Fernando N. Capitulinoefbc5832007-04-25 11:18:08 -0300195int checkout_entry(struct cache_entry *ce, const struct checkout *state, char *topath)
Linus Torvalds12dccc12005-06-05 21:59:54 -0700196{
Jonas Fonseca095c4242006-08-26 16:09:17 +0200197 static char path[PATH_MAX + 1];
Shawn Pearcede84f992006-03-05 03:24:15 -0500198 struct stat st;
Linus Torvalds12dccc12005-06-05 21:59:54 -0700199 int len = state->base_dir_len;
200
Shawn Pearcede84f992006-03-05 03:24:15 -0500201 if (topath)
202 return write_entry(ce, topath, state, 1);
203
Linus Torvalds12dccc12005-06-05 21:59:54 -0700204 memcpy(path, state->base_dir, len);
205 strcpy(path + len, ce->name);
Kjetil Barvik81a9aa62009-02-09 21:54:08 +0100206 len += ce_namelen(ce);
Linus Torvalds12dccc12005-06-05 21:59:54 -0700207
Junio C Hamanoda02ca52009-08-16 23:53:12 -0700208 if (!check_path(path, len, &st, state->base_dir_len)) {
Nguyễn Thái Ngọc Duy56cac482009-12-14 18:43:58 +0700209 unsigned changed = ce_match_stat(ce, &st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE);
Linus Torvalds12dccc12005-06-05 21:59:54 -0700210 if (!changed)
211 return 0;
212 if (!state->force) {
213 if (!state->quiet)
Nguyễn Thái Ngọc Duyd43e9072010-11-28 11:36:38 +0700214 fprintf(stderr, "%s already exists, no checkout\n", path);
Junio C Hamano4b12dae2005-10-03 12:44:48 -0700215 return -1;
Linus Torvalds12dccc12005-06-05 21:59:54 -0700216 }
217
218 /*
219 * We unlink the old file, to get the new one with the
220 * right permissions (including umask, which is nasty
221 * to emulate by hand - much easier to let the system
222 * just do the right thing)
223 */
Linus Torvaldsd48a72f2005-07-14 09:58:45 -0700224 if (S_ISDIR(st.st_mode)) {
Linus Torvaldsf0807e62007-04-13 09:26:04 -0700225 /* If it is a gitlink, leave it alone! */
Linus Torvalds7a51ed62008-01-14 16:03:17 -0800226 if (S_ISGITLINK(ce->ce_mode))
Linus Torvaldsf0807e62007-04-13 09:26:04 -0700227 return 0;
Linus Torvaldsd48a72f2005-07-14 09:58:45 -0700228 if (!state->force)
229 return error("%s is a directory", path);
230 remove_subtree(path);
Linus Torvalds971f2292008-03-17 08:56:27 -0700231 } else if (unlink(path))
232 return error("unable to unlink old '%s' (%s)", path, strerror(errno));
Shawn Pearcede84f992006-03-05 03:24:15 -0500233 } else if (state->not_new)
Linus Torvalds12dccc12005-06-05 21:59:54 -0700234 return 0;
Kjetil Barvik81a9aa62009-02-09 21:54:08 +0100235 create_directories(path, len, state);
Shawn Pearcede84f992006-03-05 03:24:15 -0500236 return write_entry(ce, path, state, 0);
Linus Torvalds12dccc12005-06-05 21:59:54 -0700237}