6 static void create_directories(const char *path, int path_len,
7 const struct checkout *state)
9 char *buf = xmalloc(path_len + 1);
12 while (len < path_len) {
16 } while (len < path_len && path[len] != '/');
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))
41 die_errno("cannot create directory at '%s'", buf);
47 static void remove_subtree(const char *path)
49 DIR *dir = opendir(path);
51 char pathbuf[PATH_MAX];
55 die_errno("cannot opendir '%s'", path);
56 strcpy(pathbuf, path);
57 name = pathbuf + strlen(path);
59 while ((de = readdir(dir)) != NULL) {
61 if (is_dot_or_dotdot(de->d_name))
63 strcpy(name, de->d_name);
64 if (lstat(pathbuf, &st))
65 die_errno("cannot lstat '%s'", pathbuf);
66 if (S_ISDIR(st.st_mode))
67 remove_subtree(pathbuf);
68 else if (unlink(pathbuf))
69 die_errno("cannot unlink '%s'", pathbuf);
73 die_errno("cannot rmdir '%s'", path);
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(struct cache_entry *ce, unsigned long *size)
84 enum object_type type;
85 void *new = read_sha1_file(ce->sha1, &type, size);
95 static int open_output_fd(char *path, struct cache_entry *ce, int to_tempfile)
97 int symlink = (ce->ce_mode & S_IFMT) != S_IFREG;
100 ? ".merge_link_XXXXXX" : ".merge_file_XXXXXX");
101 return mkstemp(path);
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) {
118 static int streaming_write_entry(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 struct git_istream *st;
124 enum object_type type;
130 st = open_istream(ce->sha1, &type, &sz, filter);
133 if (type != OBJ_BLOB)
136 fd = open_output_fd(path, ce, to_tempfile);
142 ssize_t wrote, holeto;
143 ssize_t readlen = read_istream(st, buf, sizeof(buf));
147 if (sizeof(buf) == readlen) {
148 for (holeto = 0; holeto < readlen; holeto++)
151 if (readlen == holeto) {
157 if (kept && lseek(fd, kept, SEEK_CUR) == (off_t) -1)
161 wrote = write_in_full(fd, buf, readlen);
163 if (wrote != readlen)
166 if (kept && (lseek(fd, kept - 1, SEEK_CUR) == (off_t) -1 ||
167 write(fd, "", 1) != 1))
169 *fstat_done = fstat_output(fd, state, statbuf);
175 if (result && 0 <= fd)
180 static int write_entry(struct cache_entry *ce, char *path, const struct checkout *state, int to_tempfile)
182 unsigned int ce_mode_s_ifmt = ce->ce_mode & S_IFMT;
183 int fd, ret, fstat_done = 0;
185 struct strbuf buf = STRBUF_INIT;
187 size_t wrote, newsize = 0;
190 if (ce_mode_s_ifmt == S_IFREG) {
191 struct stream_filter *filter = get_stream_filter(path, ce->sha1);
193 !streaming_write_entry(ce, path, filter,
199 switch (ce_mode_s_ifmt) {
202 new = read_blob_entry(ce, &size);
204 return error("unable to read sha1 file of %s (%s)",
205 path, sha1_to_hex(ce->sha1));
207 if (ce_mode_s_ifmt == S_IFLNK && has_symlinks && !to_tempfile) {
208 ret = symlink(new, path);
211 return error("unable to create symlink %s (%s)",
212 path, strerror(errno));
217 * Convert from git internal format to working tree format
219 if (ce_mode_s_ifmt == S_IFREG &&
220 convert_to_working_tree(ce->name, new, size, &buf)) {
222 new = strbuf_detach(&buf, &newsize);
226 fd = open_output_fd(path, ce, to_tempfile);
229 return error("unable to create file %s (%s)",
230 path, strerror(errno));
233 wrote = write_in_full(fd, new, size);
235 fstat_done = fstat_output(fd, state, &st);
239 return error("unable to write file %s", path);
243 return error("cannot create temporary subproject %s", path);
244 if (mkdir(path, 0777) < 0)
245 return error("cannot create subproject directory %s", path);
248 return error("unknown file mode for %s in index", path);
252 if (state->refresh_cache) {
254 lstat(ce->name, &st);
255 fill_stat_cache_info(ce, &st);
261 * This is like 'lstat()', except it refuses to follow symlinks
262 * in the path, after skipping "skiplen".
264 static int check_path(const char *path, int len, struct stat *st, int skiplen)
266 const char *slash = path + len;
268 while (path < slash && *slash != '/')
270 if (!has_dirs_only_path(path, slash - path, skiplen)) {
274 return lstat(path, st);
277 int checkout_entry(struct cache_entry *ce, const struct checkout *state, char *topath)
279 static char path[PATH_MAX + 1];
281 int len = state->base_dir_len;
284 return write_entry(ce, topath, state, 1);
286 memcpy(path, state->base_dir, len);
287 strcpy(path + len, ce->name);
288 len += ce_namelen(ce);
290 if (!check_path(path, len, &st, state->base_dir_len)) {
291 unsigned changed = ce_match_stat(ce, &st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE);
296 fprintf(stderr, "%s already exists, no checkout\n", path);
301 * We unlink the old file, to get the new one with the
302 * right permissions (including umask, which is nasty
303 * to emulate by hand - much easier to let the system
304 * just do the right thing)
306 if (S_ISDIR(st.st_mode)) {
307 /* If it is a gitlink, leave it alone! */
308 if (S_ISGITLINK(ce->ce_mode))
311 return error("%s is a directory", path);
312 remove_subtree(path);
313 } else if (unlink(path))
314 return error("unable to unlink old '%s' (%s)", path, strerror(errno));
315 } else if (state->not_new)
317 create_directories(path, len, state);
318 return write_entry(ce, path, state, 0);