Git 2.10
[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"
12dccc16 5
81a9aa60
KB
6static void create_directories(const char *path, int path_len,
7 const struct checkout *state)
12dccc16 8{
3733e694 9 char *buf = xmallocz(path_len);
81a9aa60
KB
10 int len = 0;
11
12 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;
12dccc16 19 buf[len] = 0;
fa2e71c9 20
bad4a54f
KB
21 /*
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 */
57199892 28 if (has_dirs_only_path(buf, len, state->base_dir_len))
fa2e71c9
JH
29 continue; /* ok, it is already a directory. */
30
31 /*
bad4a54f
KB
32 * 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.
fa2e71c9 36 */
f312de01 37 if (mkdir(buf, 0777)) {
fa2e71c9 38 if (errno == EEXIST && state->force &&
691f1a28 39 !unlink_or_warn(buf) && !mkdir(buf, 0777))
fa2e71c9 40 continue;
0721c314 41 die_errno("cannot create directory at '%s'", buf);
12dccc16
LT
42 }
43 }
44 free(buf);
45}
46
2f29e0c6 47static void remove_subtree(struct strbuf *path)
12dccc16 48{
2f29e0c6 49 DIR *dir = opendir(path->buf);
12dccc16 50 struct dirent *de;
2f29e0c6 51 int origlen = path->len;
a6080a0a 52
12dccc16 53 if (!dir)
2f29e0c6 54 die_errno("cannot opendir '%s'", path->buf);
12dccc16
LT
55 while ((de = readdir(dir)) != NULL) {
56 struct stat st;
2f29e0c6 57
8ca12c0d 58 if (is_dot_or_dotdot(de->d_name))
12dccc16 59 continue;
2f29e0c6
MH
60
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);
12dccc16 65 if (S_ISDIR(st.st_mode))
2f29e0c6
MH
66 remove_subtree(path);
67 else if (unlink(path->buf))
68 die_errno("cannot unlink '%s'", path->buf);
69 strbuf_setlen(path, origlen);
12dccc16
LT
70 }
71 closedir(dir);
2f29e0c6
MH
72 if (rmdir(path->buf))
73 die_errno("cannot rmdir '%s'", path->buf);
12dccc16
LT
74}
75
d48a72f3 76static int create_file(const char *path, unsigned int mode)
12dccc16 77{
12dccc16 78 mode = (mode & 0100) ? 0777 : 0666;
781411ed 79 return open(path, O_WRONLY | O_CREAT | O_EXCL, mode);
12dccc16
LT
80}
81
9c5e6c80 82static void *read_blob_entry(const struct cache_entry *ce, unsigned long *size)
f0807e62
LT
83{
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
9c5e6c80 95static int open_output_fd(char *path, const struct cache_entry *ce, int to_tempfile)
fd5db55d
JH
96{
97 int symlink = (ce->ce_mode & S_IFMT) != S_IFREG;
98 if (to_tempfile) {
330c8e26
JK
99 xsnprintf(path, TEMPORARY_FILENAME_LENGTH, "%s",
100 symlink ? ".merge_link_XXXXXX" : ".merge_file_XXXXXX");
fd5db55d
JH
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
9c5e6c80 118static int streaming_write_entry(const struct cache_entry *ce, char *path,
b6691092 119 struct stream_filter *filter,
dd8e9121
JH
120 const struct checkout *state, int to_tempfile,
121 int *fstat_done, struct stat *statbuf)
122{
d9c31e14 123 int result = 0;
47a02ff2 124 int fd;
dd8e9121
JH
125
126 fd = open_output_fd(path, ce, to_tempfile);
d9c31e14
JK
127 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)
dd8e9121
JH
135 unlink(path);
136 return result;
137}
138
9c5e6c80
NTND
139static int write_entry(struct cache_entry *ce,
140 char *path, const struct checkout *state, int to_tempfile)
12dccc16 141{
4857c761 142 unsigned int ce_mode_s_ifmt = ce->ce_mode & S_IFMT;
e4c72923 143 int fd, ret, fstat_done = 0;
4857c761
KB
144 char *new;
145 struct strbuf buf = STRBUF_INIT;
146 unsigned long size;
147 size_t wrote, newsize = 0;
e4c72923 148 struct stat st;
4857c761 149
b6691092 150 if (ce_mode_s_ifmt == S_IFREG) {
7297a440 151 struct stream_filter *filter = get_stream_filter(ce->name, ce->sha1);
b6691092
JH
152 if (filter &&
153 !streaming_write_entry(ce, path, filter,
154 state, to_tempfile,
155 &fstat_done, &st))
156 goto finish;
157 }
dd8e9121 158
4857c761 159 switch (ce_mode_s_ifmt) {
12dccc16 160 case S_IFREG:
4857c761
KB
161 case S_IFLNK:
162 new = read_blob_entry(ce, &size);
f0807e62 163 if (!new)
d43e9073 164 return error("unable to read sha1 file of %s (%s)",
f0807e62 165 path, sha1_to_hex(ce->sha1));
1a9d7e9b 166
4857c761
KB
167 if (ce_mode_s_ifmt == S_IFLNK && has_symlinks && !to_tempfile) {
168 ret = symlink(new, path);
169 free(new);
170 if (ret)
e1ebb3c2
NTND
171 return error_errno("unable to create symlink %s",
172 path);
4857c761
KB
173 break;
174 }
175
1a9d7e9b
JH
176 /*
177 * Convert from git internal format to working tree format
178 */
4857c761
KB
179 if (ce_mode_s_ifmt == S_IFREG &&
180 convert_to_working_tree(ce->name, new, size, &buf)) {
1a9d7e9b 181 free(new);
c32f749f
RS
182 new = strbuf_detach(&buf, &newsize);
183 size = newsize;
1a9d7e9b
JH
184 }
185
fd5db55d 186 fd = open_output_fd(path, ce, to_tempfile);
12dccc16
LT
187 if (fd < 0) {
188 free(new);
e1ebb3c2 189 return error_errno("unable to create file %s", path);
12dccc16 190 }
6c510bee 191
93822c22 192 wrote = write_in_full(fd, new, size);
fd5db55d
JH
193 if (!to_tempfile)
194 fstat_done = fstat_output(fd, state, &st);
12dccc16
LT
195 close(fd);
196 free(new);
197 if (wrote != size)
d43e9073 198 return error("unable to write file %s", path);
12dccc16 199 break;
302b9282 200 case S_IFGITLINK:
f0807e62 201 if (to_tempfile)
42063f95 202 return error("cannot create temporary submodule %s", path);
f0807e62 203 if (mkdir(path, 0777) < 0)
42063f95 204 return error("cannot create submodule directory %s", path);
f0807e62 205 break;
12dccc16 206 default:
d43e9073 207 return error("unknown file mode for %s in index", path);
12dccc16
LT
208 }
209
dd8e9121 210finish:
6ee67f26 211 if (state->refresh_cache) {
d4a2024a 212 assert(state->istate);
e4c72923
KB
213 if (!fstat_done)
214 lstat(ce->name, &st);
12dccc16 215 fill_stat_cache_info(ce, &st);
078a58e8 216 ce->ce_flags |= CE_UPDATE_IN_BASE;
d4a2024a 217 state->istate->cache_changed |= CE_ENTRY_CHANGED;
12dccc16
LT
218 }
219 return 0;
220}
221
b6986d8a
LT
222/*
223 * This is like 'lstat()', except it refuses to follow symlinks
da02ca50 224 * in the path, after skipping "skiplen".
b6986d8a 225 */
61b97df7 226static int check_path(const char *path, int len, struct stat *st, int skiplen)
b6986d8a 227{
da02ca50
JH
228 const char *slash = path + len;
229
230 while (path < slash && *slash != '/')
231 slash--;
232 if (!has_dirs_only_path(path, slash - path, skiplen)) {
b6986d8a
LT
233 errno = ENOENT;
234 return -1;
235 }
236 return lstat(path, st);
237}
238
af2a651d
JH
239/*
240 * Write the contents from ce out to the working tree.
241 *
242 * When topath[] is not NULL, instead of writing to the working tree
243 * file named by ce, a temporary file is created by this function and
244 * its name is returned in topath[], which must be able to hold at
245 * least TEMPORARY_FILENAME_LENGTH bytes long.
246 */
9c5e6c80
NTND
247int checkout_entry(struct cache_entry *ce,
248 const struct checkout *state, char *topath)
12dccc16 249{
f63272a3 250 static struct strbuf path = STRBUF_INIT;
de84f99c 251 struct stat st;
12dccc16 252
de84f99c
SP
253 if (topath)
254 return write_entry(ce, topath, state, 1);
255
f63272a3
MH
256 strbuf_reset(&path);
257 strbuf_add(&path, state->base_dir, state->base_dir_len);
258 strbuf_add(&path, ce->name, ce_namelen(ce));
12dccc16 259
f63272a3 260 if (!check_path(path.buf, path.len, &st, state->base_dir_len)) {
56cac48c 261 unsigned changed = ce_match_stat(ce, &st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE);
12dccc16
LT
262 if (!changed)
263 return 0;
264 if (!state->force) {
265 if (!state->quiet)
f63272a3
MH
266 fprintf(stderr,
267 "%s already exists, no checkout\n",
268 path.buf);
4b12dae6 269 return -1;
12dccc16
LT
270 }
271
272 /*
273 * We unlink the old file, to get the new one with the
274 * right permissions (including umask, which is nasty
275 * to emulate by hand - much easier to let the system
276 * just do the right thing)
277 */
d48a72f3 278 if (S_ISDIR(st.st_mode)) {
f0807e62 279 /* If it is a gitlink, leave it alone! */
7a51ed66 280 if (S_ISGITLINK(ce->ce_mode))
f0807e62 281 return 0;
d48a72f3 282 if (!state->force)
f63272a3 283 return error("%s is a directory", path.buf);
2f29e0c6 284 remove_subtree(&path);
f63272a3 285 } else if (unlink(path.buf))
e1ebb3c2 286 return error_errno("unable to unlink old '%s'", path.buf);
de84f99c 287 } else if (state->not_new)
12dccc16 288 return 0;
f63272a3
MH
289
290 create_directories(path.buf, path.len, state);
291 return write_entry(ce, path.buf, state, 0);
12dccc16 292}