convert.h: move declarations for conversion from cache.h
[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{
81a9aa60
KB
9 char *buf = xmalloc(path_len + 1);
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
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;
a6080a0a 53
12dccc16 54 if (!dir)
d824cbba 55 die_errno("cannot opendir '%s'", path);
12dccc16
LT
56 strcpy(pathbuf, path);
57 name = pathbuf + strlen(path);
58 *name++ = '/';
59 while ((de = readdir(dir)) != NULL) {
60 struct stat st;
8ca12c0d 61 if (is_dot_or_dotdot(de->d_name))
12dccc16
LT
62 continue;
63 strcpy(name, de->d_name);
64 if (lstat(pathbuf, &st))
d824cbba 65 die_errno("cannot lstat '%s'", pathbuf);
12dccc16
LT
66 if (S_ISDIR(st.st_mode))
67 remove_subtree(pathbuf);
68 else if (unlink(pathbuf))
d824cbba 69 die_errno("cannot unlink '%s'", pathbuf);
12dccc16
LT
70 }
71 closedir(dir);
72 if (rmdir(path))
d824cbba 73 die_errno("cannot rmdir '%s'", path);
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
4857c761 82static void *read_blob_entry(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
fd5db55d
JH
95static 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
dd8e9121
JH
118static int streaming_write_entry(struct cache_entry *ce, char *path,
119 const struct checkout *state, int to_tempfile,
120 int *fstat_done, struct stat *statbuf)
121{
122 struct git_istream *st;
123 enum object_type type;
124 unsigned long sz;
125 int result = -1;
de6182db 126 ssize_t kept = 0;
dd8e9121
JH
127 int fd = -1;
128
129 st = open_istream(ce->sha1, &type, &sz);
130 if (!st)
131 return -1;
132 if (type != OBJ_BLOB)
133 goto close_and_exit;
134
135 fd = open_output_fd(path, ce, to_tempfile);
136 if (fd < 0)
137 goto close_and_exit;
138
139 for (;;) {
de6182db
JH
140 char buf[1024 * 16];
141 ssize_t wrote, holeto;
dd8e9121
JH
142 ssize_t readlen = read_istream(st, buf, sizeof(buf));
143
144 if (!readlen)
145 break;
de6182db
JH
146 if (sizeof(buf) == readlen) {
147 for (holeto = 0; holeto < readlen; holeto++)
148 if (buf[holeto])
149 break;
150 if (readlen == holeto) {
151 kept += holeto;
152 continue;
153 }
154 }
dd8e9121 155
de6182db
JH
156 if (kept && lseek(fd, kept, SEEK_CUR) == (off_t) -1)
157 goto close_and_exit;
158 else
159 kept = 0;
dd8e9121
JH
160 wrote = write_in_full(fd, buf, readlen);
161
162 if (wrote != readlen)
163 goto close_and_exit;
164 }
de6182db
JH
165 if (kept && (lseek(fd, kept - 1, SEEK_CUR) == (off_t) -1 ||
166 write(fd, "", 1) != 1))
167 goto close_and_exit;
dd8e9121
JH
168 *fstat_done = fstat_output(fd, state, statbuf);
169
170close_and_exit:
171 close_istream(st);
172 if (0 <= fd)
173 result = close(fd);
174 if (result && 0 <= fd)
175 unlink(path);
176 return result;
177}
178
efbc5831 179static int write_entry(struct cache_entry *ce, char *path, const struct checkout *state, int to_tempfile)
12dccc16 180{
4857c761 181 unsigned int ce_mode_s_ifmt = ce->ce_mode & S_IFMT;
e4c72923 182 int fd, ret, fstat_done = 0;
4857c761
KB
183 char *new;
184 struct strbuf buf = STRBUF_INIT;
185 unsigned long size;
186 size_t wrote, newsize = 0;
e4c72923 187 struct stat st;
4857c761 188
dd8e9121
JH
189 if ((ce_mode_s_ifmt == S_IFREG) &&
190 can_bypass_conversion(path) &&
191 !streaming_write_entry(ce, path, state, to_tempfile,
192 &fstat_done, &st))
193 goto finish;
194
4857c761 195 switch (ce_mode_s_ifmt) {
12dccc16 196 case S_IFREG:
4857c761
KB
197 case S_IFLNK:
198 new = read_blob_entry(ce, &size);
f0807e62 199 if (!new)
d43e9073 200 return error("unable to read sha1 file of %s (%s)",
f0807e62 201 path, sha1_to_hex(ce->sha1));
1a9d7e9b 202
4857c761
KB
203 if (ce_mode_s_ifmt == S_IFLNK && has_symlinks && !to_tempfile) {
204 ret = symlink(new, path);
205 free(new);
206 if (ret)
d43e9073 207 return error("unable to create symlink %s (%s)",
4857c761
KB
208 path, strerror(errno));
209 break;
210 }
211
1a9d7e9b
JH
212 /*
213 * Convert from git internal format to working tree format
214 */
4857c761
KB
215 if (ce_mode_s_ifmt == S_IFREG &&
216 convert_to_working_tree(ce->name, new, size, &buf)) {
1a9d7e9b 217 free(new);
c32f749f
RS
218 new = strbuf_detach(&buf, &newsize);
219 size = newsize;
1a9d7e9b
JH
220 }
221
fd5db55d 222 fd = open_output_fd(path, ce, to_tempfile);
12dccc16
LT
223 if (fd < 0) {
224 free(new);
d43e9073 225 return error("unable to create file %s (%s)",
12dccc16
LT
226 path, strerror(errno));
227 }
6c510bee 228
93822c22 229 wrote = write_in_full(fd, new, size);
fd5db55d
JH
230 if (!to_tempfile)
231 fstat_done = fstat_output(fd, state, &st);
12dccc16
LT
232 close(fd);
233 free(new);
234 if (wrote != size)
d43e9073 235 return error("unable to write file %s", path);
12dccc16 236 break;
302b9282 237 case S_IFGITLINK:
f0807e62 238 if (to_tempfile)
d43e9073 239 return error("cannot create temporary subproject %s", path);
f0807e62 240 if (mkdir(path, 0777) < 0)
d43e9073 241 return error("cannot create subproject directory %s", path);
f0807e62 242 break;
12dccc16 243 default:
d43e9073 244 return error("unknown file mode for %s in index", path);
12dccc16
LT
245 }
246
dd8e9121 247finish:
6ee67f26 248 if (state->refresh_cache) {
e4c72923
KB
249 if (!fstat_done)
250 lstat(ce->name, &st);
12dccc16
LT
251 fill_stat_cache_info(ce, &st);
252 }
253 return 0;
254}
255
b6986d8a
LT
256/*
257 * This is like 'lstat()', except it refuses to follow symlinks
da02ca50 258 * in the path, after skipping "skiplen".
b6986d8a 259 */
61b97df7 260static int check_path(const char *path, int len, struct stat *st, int skiplen)
b6986d8a 261{
da02ca50
JH
262 const char *slash = path + len;
263
264 while (path < slash && *slash != '/')
265 slash--;
266 if (!has_dirs_only_path(path, slash - path, skiplen)) {
b6986d8a
LT
267 errno = ENOENT;
268 return -1;
269 }
270 return lstat(path, st);
271}
272
efbc5831 273int checkout_entry(struct cache_entry *ce, const struct checkout *state, char *topath)
12dccc16 274{
095c424d 275 static char path[PATH_MAX + 1];
de84f99c 276 struct stat st;
12dccc16
LT
277 int len = state->base_dir_len;
278
de84f99c
SP
279 if (topath)
280 return write_entry(ce, topath, state, 1);
281
12dccc16
LT
282 memcpy(path, state->base_dir, len);
283 strcpy(path + len, ce->name);
81a9aa60 284 len += ce_namelen(ce);
12dccc16 285
da02ca50 286 if (!check_path(path, len, &st, state->base_dir_len)) {
56cac48c 287 unsigned changed = ce_match_stat(ce, &st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE);
12dccc16
LT
288 if (!changed)
289 return 0;
290 if (!state->force) {
291 if (!state->quiet)
d43e9073 292 fprintf(stderr, "%s already exists, no checkout\n", path);
4b12dae6 293 return -1;
12dccc16
LT
294 }
295
296 /*
297 * We unlink the old file, to get the new one with the
298 * right permissions (including umask, which is nasty
299 * to emulate by hand - much easier to let the system
300 * just do the right thing)
301 */
d48a72f3 302 if (S_ISDIR(st.st_mode)) {
f0807e62 303 /* If it is a gitlink, leave it alone! */
7a51ed66 304 if (S_ISGITLINK(ce->ce_mode))
f0807e62 305 return 0;
d48a72f3
LT
306 if (!state->force)
307 return error("%s is a directory", path);
308 remove_subtree(path);
971f229c
LT
309 } else if (unlink(path))
310 return error("unable to unlink old '%s' (%s)", path, strerror(errno));
de84f99c 311 } else if (state->not_new)
12dccc16 312 return 0;
81a9aa60 313 create_directories(path, len, state);
de84f99c 314 return write_entry(ce, path, state, 0);
12dccc16 315}