Use a strbuf in symlink
[git/mingw/4msysgit.git] / entry.c
blob9c39003c0ac1401538553adaacfb102ce599323c
1 #include "cache.h"
2 #include "blob.h"
3 #include "dir.h"
4 #include "streaming.h"
6 static void create_directories(const char *path, int path_len,
7 const struct checkout *state)
9 char *buf = xmalloc(path_len + 1);
10 int len = 0;
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;
19 buf[len] = 0;
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.
28 if (has_dirs_only_path(buf, len, state->base_dir_len))
29 continue; /* ok, it is already a directory. */
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.
37 if (mkdir(buf, 0777)) {
38 if (errno == EEXIST && state->force &&
39 !unlink_or_warn(buf) && !mkdir(buf, 0777))
40 continue;
41 die_errno("cannot create directory at '%s'", buf);
44 free(buf);
47 static void remove_subtree(struct strbuf *path)
49 DIR *dir = opendir(path->buf);
50 struct dirent *de;
51 int origlen = path->len;
53 if (!dir)
54 die_errno("cannot opendir '%s'", path->buf);
55 while ((de = readdir(dir)) != NULL) {
56 struct stat st;
58 if (is_dot_or_dotdot(de->d_name))
59 continue;
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);
65 if (S_ISDIR(st.st_mode))
66 remove_subtree(path);
67 else if (unlink(path->buf))
68 die_errno("cannot unlink '%s'", path->buf);
69 strbuf_setlen(path, origlen);
71 closedir(dir);
72 if (rmdir(path->buf))
73 die_errno("cannot rmdir '%s'", path->buf);
76 static int create_file(const char *path, unsigned int mode)
78 mode = (mode & 0100) ? 0777 : 0666;
79 return open(path, O_WRONLY | O_CREAT | O_EXCL, mode);
82 static void *read_blob_entry(const struct cache_entry *ce, unsigned long *size)
84 enum object_type type;
85 void *new = read_sha1_file(ce->sha1, &type, size);
87 if (new) {
88 if (type == OBJ_BLOB)
89 return new;
90 free(new);
92 return NULL;
95 static int open_output_fd(char *path, const struct cache_entry *ce, int to_tempfile)
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);
107 static int fstat_output(int fd, const struct checkout *state, struct stat *st)
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;
115 return 0;
118 static int streaming_write_entry(const struct cache_entry *ce, char *path,
119 struct stream_filter *filter,
120 const struct checkout *state, int to_tempfile,
121 int *fstat_done, struct stat *statbuf)
123 int result = 0;
124 int fd;
126 fd = open_output_fd(path, ce, to_tempfile);
127 if (fd < 0)
128 return -1;
130 result |= stream_blob_to_fd(fd, ce->sha1, filter, 1);
131 *fstat_done = fstat_output(fd, state, statbuf);
132 result |= close(fd);
134 if (result)
135 unlink(path);
136 return result;
140 * Does 'match' match the given name?
141 * A match is found if
143 * (1) the 'match' string is leading directory of 'name', or
144 * (2) the 'match' string is exactly the same as 'name'.
146 * and the return value tells which case it was.
148 * It returns 0 when there is no match.
150 * Preserved and simplified from dir.c for use here (without glob special matching)
152 static int match_one(const char *match, const char *name, int namelen)
154 int matchlen;
156 /* If the match was just the prefix, we matched */
157 if (!*match)
158 return MATCHED_RECURSIVELY;
160 if (ignore_case) {
161 for (;;) {
162 unsigned char c1 = tolower(*match);
163 unsigned char c2 = tolower(*name);
164 if (c1 == '\0' )
165 break;
166 if (c1 != c2)
167 return 0;
168 match++;
169 name++;
170 namelen--;
172 /* We don't match the matchstring exactly, */
173 matchlen = strlen(match);
174 if (strncmp_icase(match, name, matchlen))
175 return 0;
176 } else {
177 for (;;) {
178 unsigned char c1 = *match;
179 unsigned char c2 = *name;
180 if (c1 == '\0' )
181 break;
182 if (c1 != c2)
183 return 0;
184 match++;
185 name++;
186 namelen--;
188 /* We don't match the matchstring exactly, */
189 matchlen = strlen(match);
190 if (strncmp(match, name, matchlen))
191 return 0;
194 if (namelen == matchlen)
195 return MATCHED_EXACTLY;
196 if (match[matchlen-1] == '/' || name[matchlen] == '/')
197 return MATCHED_RECURSIVELY;
198 return 0;
201 static enum git_target_type get_symlink_type(const char *filepath, const char *symlinkpath)
203 /* For certain O/S and file-systems, symlinks need to know before-hand whether it
204 * is a directory or a file being pointed to.
206 * This allows us to use index information for relative paths that lie
207 * within the working directory.
209 * This function is not interested in interrogating the file-system.
211 char *sanitized;
212 const char *fpos, *last;
213 enum git_target_type ret;
214 int len, pos;
216 /* This is an absolute path, so git doesn't know.
218 if (is_absolute_path(symlinkpath))
219 return GIT_TARGET_UNKNOWN;
221 /* Work on a sanitized version of the path that can be
222 * matched against the index.
224 last = NULL;
225 for (fpos = filepath; *fpos; ++fpos)
226 if (is_dir_sep(*fpos))
227 last = fpos;
229 if (last) {
230 len = (1+last-filepath);
231 sanitized = xmalloc(len + strlen(symlinkpath)+1);
232 memcpy(sanitized, filepath, 1+last-filepath);
233 } else {
234 len = 0;
235 sanitized = xmalloc(strlen(symlinkpath)+1);
237 strcpy(sanitized+len, symlinkpath);
239 ret = GIT_TARGET_UNKNOWN;
240 if (!normalize_path_copy(sanitized, sanitized)) {
241 for (pos = 0; pos < active_nr; pos++) {
242 struct cache_entry *ce = active_cache[pos];
243 switch (match_one(sanitized, ce->name, ce_namelen(ce))) {
244 case MATCHED_EXACTLY:
245 case MATCHED_FNMATCH:
246 ret = GIT_TARGET_ISFILE;
247 break;
248 case MATCHED_RECURSIVELY:
249 ret = GIT_TARGET_ISDIR;
250 break;
255 free(sanitized);
256 return ret;
259 static int write_entry(struct cache_entry *ce,
260 char *path, const struct checkout *state, int to_tempfile)
262 unsigned int ce_mode_s_ifmt = ce->ce_mode & S_IFMT;
263 int fd, ret, fstat_done = 0;
264 char *new;
265 struct strbuf buf = STRBUF_INIT;
266 unsigned long size;
267 size_t wrote, newsize = 0;
268 struct stat st;
270 if (ce_mode_s_ifmt == S_IFREG) {
271 struct stream_filter *filter = get_stream_filter(ce->name, ce->sha1);
272 if (filter &&
273 !streaming_write_entry(ce, path, filter,
274 state, to_tempfile,
275 &fstat_done, &st))
276 goto finish;
279 switch (ce_mode_s_ifmt) {
280 case S_IFREG:
281 case S_IFLNK:
282 new = read_blob_entry(ce, &size);
283 if (!new)
284 return error("unable to read sha1 file of %s (%s)",
285 path, sha1_to_hex(ce->sha1));
287 if (ce_mode_s_ifmt == S_IFLNK && has_symlinks && !to_tempfile) {
288 /* Note that symlink_with_type is a macro, and that for filesystems that
289 * don't care, get_symlink_type will not be called.
291 ret = symlink_with_type(new, path, get_symlink_type(path, new));
292 free(new);
293 if (ret)
294 return error("unable to create symlink %s (%s)",
295 path, strerror(errno));
296 break;
300 * Convert from git internal format to working tree format
302 if (ce_mode_s_ifmt == S_IFREG &&
303 convert_to_working_tree(ce->name, new, size, &buf)) {
304 free(new);
305 new = strbuf_detach(&buf, &newsize);
306 size = newsize;
309 fd = open_output_fd(path, ce, to_tempfile);
310 if (fd < 0) {
311 free(new);
312 return error("unable to create file %s (%s)",
313 path, strerror(errno));
316 wrote = write_in_full(fd, new, size);
317 if (!to_tempfile)
318 fstat_done = fstat_output(fd, state, &st);
319 close(fd);
320 free(new);
321 if (wrote != size)
322 return error("unable to write file %s", path);
323 break;
324 case S_IFGITLINK:
325 if (to_tempfile)
326 return error("cannot create temporary submodule %s", path);
327 if (mkdir(path, 0777) < 0)
328 return error("cannot create submodule directory %s", path);
329 break;
330 default:
331 return error("unknown file mode for %s in index", path);
334 finish:
335 if (state->refresh_cache) {
336 if (!fstat_done)
337 lstat(ce->name, &st);
338 fill_stat_cache_info(ce, &st);
340 return 0;
344 * This is like 'lstat()', except it refuses to follow symlinks
345 * in the path, after skipping "skiplen".
347 static int check_path(const char *path, int len, struct stat *st, int skiplen)
349 const char *slash = path + len;
351 while (path < slash && *slash != '/')
352 slash--;
353 if (!has_dirs_only_path(path, slash - path, skiplen)) {
354 errno = ENOENT;
355 return -1;
357 return lstat(path, st);
361 * Write the contents from ce out to the working tree.
363 * When topath[] is not NULL, instead of writing to the working tree
364 * file named by ce, a temporary file is created by this function and
365 * its name is returned in topath[], which must be able to hold at
366 * least TEMPORARY_FILENAME_LENGTH bytes long.
368 int checkout_entry(struct cache_entry *ce,
369 const struct checkout *state, char *topath)
371 static struct strbuf path = STRBUF_INIT;
372 struct stat st;
374 if (topath)
375 return write_entry(ce, topath, state, 1);
377 strbuf_reset(&path);
378 strbuf_add(&path, state->base_dir, state->base_dir_len);
379 strbuf_add(&path, ce->name, ce_namelen(ce));
381 if (!check_path(path.buf, path.len, &st, state->base_dir_len)) {
382 unsigned changed = ce_match_stat(ce, &st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE);
383 if (!changed)
384 return 0;
385 if (!state->force) {
386 if (!state->quiet)
387 fprintf(stderr,
388 "%s already exists, no checkout\n",
389 path.buf);
390 return -1;
394 * We unlink the old file, to get the new one with the
395 * right permissions (including umask, which is nasty
396 * to emulate by hand - much easier to let the system
397 * just do the right thing)
399 if (S_ISDIR(st.st_mode)) {
400 /* If it is a gitlink, leave it alone! */
401 if (S_ISGITLINK(ce->ce_mode))
402 return 0;
403 if (!state->force)
404 return error("%s is a directory", path.buf);
405 remove_subtree(&path);
406 } else if (unlink(path.buf))
407 return error("unable to unlink old '%s' (%s)",
408 path.buf, strerror(errno));
409 } else if (state->not_new)
410 return 0;
412 create_directories(path.buf, path.len, state);
413 return write_entry(ce, path.buf, state, 0);