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