lstat_cache(): introduce has_dirs_only_path() function
[git/git.git] / entry.c
CommitLineData
12dccc16 1#include "cache.h"
8e440259 2#include "blob.h"
12dccc16 3
efbc5831 4static void create_directories(const char *path, const struct checkout *state)
12dccc16
LT
5{
6 int len = strlen(path);
7 char *buf = xmalloc(len + 1);
8 const char *slash = path;
9
10 while ((slash = strchr(slash+1, '/')) != NULL) {
11 len = slash - path;
12 memcpy(buf, path, len);
13 buf[len] = 0;
fa2e71c9 14
bad4a54f
KB
15 /*
16 * For 'checkout-index --prefix=<dir>', <dir> is
17 * allowed to be a symlink to an existing directory,
18 * and we set 'state->base_dir_len' below, such that
19 * we test the path components of the prefix with the
20 * stat() function instead of the lstat() function.
21 */
22 if (has_dirs_only_path(len, buf, state->base_dir_len))
fa2e71c9
JH
23 continue; /* ok, it is already a directory. */
24
25 /*
bad4a54f
KB
26 * If this mkdir() would fail, it could be that there
27 * is already a symlink or something else exists
28 * there, therefore we then try to unlink it and try
29 * one more time to create the directory.
fa2e71c9 30 */
f312de01 31 if (mkdir(buf, 0777)) {
fa2e71c9
JH
32 if (errno == EEXIST && state->force &&
33 !unlink(buf) && !mkdir(buf, 0777))
34 continue;
12dccc16
LT
35 die("cannot create directory at %s", buf);
36 }
37 }
38 free(buf);
39}
40
41static void remove_subtree(const char *path)
42{
43 DIR *dir = opendir(path);
44 struct dirent *de;
45 char pathbuf[PATH_MAX];
46 char *name;
a6080a0a 47
12dccc16 48 if (!dir)
cc2903fc 49 die("cannot opendir %s (%s)", path, strerror(errno));
12dccc16
LT
50 strcpy(pathbuf, path);
51 name = pathbuf + strlen(path);
52 *name++ = '/';
53 while ((de = readdir(dir)) != NULL) {
54 struct stat st;
55 if ((de->d_name[0] == '.') &&
56 ((de->d_name[1] == 0) ||
57 ((de->d_name[1] == '.') && de->d_name[2] == 0)))
58 continue;
59 strcpy(name, de->d_name);
60 if (lstat(pathbuf, &st))
cc2903fc 61 die("cannot lstat %s (%s)", pathbuf, strerror(errno));
12dccc16
LT
62 if (S_ISDIR(st.st_mode))
63 remove_subtree(pathbuf);
64 else if (unlink(pathbuf))
cc2903fc 65 die("cannot unlink %s (%s)", pathbuf, strerror(errno));
12dccc16
LT
66 }
67 closedir(dir);
68 if (rmdir(path))
cc2903fc 69 die("cannot rmdir %s (%s)", path, strerror(errno));
12dccc16
LT
70}
71
d48a72f3 72static int create_file(const char *path, unsigned int mode)
12dccc16 73{
12dccc16 74 mode = (mode & 0100) ? 0777 : 0666;
781411ed 75 return open(path, O_WRONLY | O_CREAT | O_EXCL, mode);
12dccc16
LT
76}
77
f0807e62
LT
78static void *read_blob_entry(struct cache_entry *ce, const char *path, unsigned long *size)
79{
80 enum object_type type;
81 void *new = read_sha1_file(ce->sha1, &type, size);
82
83 if (new) {
84 if (type == OBJ_BLOB)
85 return new;
86 free(new);
87 }
88 return NULL;
89}
90
efbc5831 91static int write_entry(struct cache_entry *ce, char *path, const struct checkout *state, int to_tempfile)
12dccc16
LT
92{
93 int fd;
12dccc16 94 long wrote;
12dccc16 95
7a51ed66 96 switch (ce->ce_mode & S_IFMT) {
5ecd293d
PH
97 char *new;
98 struct strbuf buf;
a2d7c6c6 99 unsigned long size;
6c510bee 100
12dccc16 101 case S_IFREG:
f0807e62
LT
102 new = read_blob_entry(ce, path, &size);
103 if (!new)
7e44c935 104 return error("git checkout-index: unable to read sha1 file of %s (%s)",
f0807e62 105 path, sha1_to_hex(ce->sha1));
1a9d7e9b
JH
106
107 /*
108 * Convert from git internal format to working tree format
109 */
5ecd293d
PH
110 strbuf_init(&buf, 0);
111 if (convert_to_working_tree(ce->name, new, size, &buf)) {
c32f749f 112 size_t newsize = 0;
1a9d7e9b 113 free(new);
c32f749f
RS
114 new = strbuf_detach(&buf, &newsize);
115 size = newsize;
1a9d7e9b
JH
116 }
117
de84f99c
SP
118 if (to_tempfile) {
119 strcpy(path, ".merge_file_XXXXXX");
120 fd = mkstemp(path);
121 } else
7a51ed66 122 fd = create_file(path, ce->ce_mode);
12dccc16
LT
123 if (fd < 0) {
124 free(new);
7e44c935 125 return error("git checkout-index: unable to create file %s (%s)",
12dccc16
LT
126 path, strerror(errno));
127 }
6c510bee 128
93822c22 129 wrote = write_in_full(fd, new, size);
12dccc16
LT
130 close(fd);
131 free(new);
132 if (wrote != size)
7e44c935 133 return error("git checkout-index: unable to write file %s", path);
12dccc16
LT
134 break;
135 case S_IFLNK:
f0807e62
LT
136 new = read_blob_entry(ce, path, &size);
137 if (!new)
7e44c935 138 return error("git checkout-index: unable to read sha1 file of %s (%s)",
f0807e62 139 path, sha1_to_hex(ce->sha1));
78a8d641
JS
140 if (to_tempfile || !has_symlinks) {
141 if (to_tempfile) {
142 strcpy(path, ".merge_link_XXXXXX");
143 fd = mkstemp(path);
144 } else
145 fd = create_file(path, 0666);
de84f99c
SP
146 if (fd < 0) {
147 free(new);
7e44c935 148 return error("git checkout-index: unable to create "
de84f99c
SP
149 "file %s (%s)", path, strerror(errno));
150 }
93822c22 151 wrote = write_in_full(fd, new, size);
de84f99c
SP
152 close(fd);
153 free(new);
154 if (wrote != size)
7e44c935 155 return error("git checkout-index: unable to write file %s",
de84f99c
SP
156 path);
157 } else {
158 wrote = symlink(new, path);
12dccc16 159 free(new);
de84f99c 160 if (wrote)
7e44c935 161 return error("git checkout-index: unable to create "
de84f99c 162 "symlink %s (%s)", path, strerror(errno));
12dccc16 163 }
12dccc16 164 break;
302b9282 165 case S_IFGITLINK:
f0807e62 166 if (to_tempfile)
7e44c935 167 return error("git checkout-index: cannot create temporary subproject %s", path);
f0807e62 168 if (mkdir(path, 0777) < 0)
7e44c935 169 return error("git checkout-index: cannot create subproject directory %s", path);
f0807e62 170 break;
12dccc16 171 default:
7e44c935 172 return error("git checkout-index: unknown file mode for %s", path);
12dccc16
LT
173 }
174
6ee67f26 175 if (state->refresh_cache) {
12dccc16
LT
176 struct stat st;
177 lstat(ce->name, &st);
178 fill_stat_cache_info(ce, &st);
179 }
180 return 0;
181}
182
efbc5831 183int checkout_entry(struct cache_entry *ce, const struct checkout *state, char *topath)
12dccc16 184{
095c424d 185 static char path[PATH_MAX + 1];
de84f99c 186 struct stat st;
12dccc16
LT
187 int len = state->base_dir_len;
188
de84f99c
SP
189 if (topath)
190 return write_entry(ce, topath, state, 1);
191
12dccc16
LT
192 memcpy(path, state->base_dir, len);
193 strcpy(path + len, ce->name);
194
195 if (!lstat(path, &st)) {
4bd5b7da 196 unsigned changed = ce_match_stat(ce, &st, CE_MATCH_IGNORE_VALID);
12dccc16
LT
197 if (!changed)
198 return 0;
199 if (!state->force) {
200 if (!state->quiet)
215a7ad1 201 fprintf(stderr, "git-checkout-index: %s already exists\n", path);
4b12dae6 202 return -1;
12dccc16
LT
203 }
204
205 /*
206 * We unlink the old file, to get the new one with the
207 * right permissions (including umask, which is nasty
208 * to emulate by hand - much easier to let the system
209 * just do the right thing)
210 */
d48a72f3 211 if (S_ISDIR(st.st_mode)) {
f0807e62 212 /* If it is a gitlink, leave it alone! */
7a51ed66 213 if (S_ISGITLINK(ce->ce_mode))
f0807e62 214 return 0;
d48a72f3
LT
215 if (!state->force)
216 return error("%s is a directory", path);
217 remove_subtree(path);
971f229c
LT
218 } else if (unlink(path))
219 return error("unable to unlink old '%s' (%s)", path, strerror(errno));
de84f99c 220 } else if (state->not_new)
12dccc16
LT
221 return 0;
222 create_directories(path, state);
de84f99c 223 return write_entry(ce, path, state, 0);
12dccc16 224}