6 static void create_directories(const char *path, int path_len,
7 const struct checkout *state)
9 char *buf = xmallocz(path_len);
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(struct strbuf *path)
49 DIR *dir = opendir(path->buf);
51 int origlen = path->len;
54 die_errno("cannot opendir '%s'", path->buf);
55 while ((de = readdir(dir)) != NULL) {
58 if (is_dot_or_dotdot(de->d_name))
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))
67 else if (unlink(path->buf))
68 die_errno("cannot unlink '%s'", path->buf);
69 strbuf_setlen(path, origlen);
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);
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;
99 xsnprintf(path, TEMPORARY_FILENAME_LENGTH, "%s",
100 symlink ? ".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(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)
126 fd = open_output_fd(path, ce, to_tempfile);
130 result |= stream_blob_to_fd(fd, ce->sha1, filter, 1);
131 *fstat_done = fstat_output(fd, state, statbuf);
139 static int write_entry(struct cache_entry *ce,
140 char *path, const struct checkout *state, int to_tempfile)
142 unsigned int ce_mode_s_ifmt = ce->ce_mode & S_IFMT;
143 int fd, ret, fstat_done = 0;
145 struct strbuf buf = STRBUF_INIT;
147 size_t wrote, newsize = 0;
150 if (ce_mode_s_ifmt == S_IFREG) {
151 struct stream_filter *filter = get_stream_filter(ce->name, ce->sha1);
153 !streaming_write_entry(ce, path, filter,
159 switch (ce_mode_s_ifmt) {
162 new = read_blob_entry(ce, &size);
164 return error("unable to read sha1 file of %s (%s)",
165 path, sha1_to_hex(ce->sha1));
167 if (ce_mode_s_ifmt == S_IFLNK && has_symlinks && !to_tempfile) {
168 ret = symlink(new, path);
171 return error_errno("unable to create symlink %s",
177 * Convert from git internal format to working tree format
179 if (ce_mode_s_ifmt == S_IFREG &&
180 convert_to_working_tree(ce->name, new, size, &buf)) {
182 new = strbuf_detach(&buf, &newsize);
186 fd = open_output_fd(path, ce, to_tempfile);
189 return error_errno("unable to create file %s", path);
192 wrote = write_in_full(fd, new, size);
194 fstat_done = fstat_output(fd, state, &st);
198 return error("unable to write file %s", path);
202 return error("cannot create temporary submodule %s", path);
203 if (mkdir(path, 0777) < 0)
204 return error("cannot create submodule directory %s", path);
207 return error("unknown file mode for %s in index", path);
211 if (state->refresh_cache) {
212 assert(state->istate);
214 lstat(ce->name, &st);
215 fill_stat_cache_info(ce, &st);
216 ce->ce_flags |= CE_UPDATE_IN_BASE;
217 state->istate->cache_changed |= CE_ENTRY_CHANGED;
223 * This is like 'lstat()', except it refuses to follow symlinks
224 * in the path, after skipping "skiplen".
226 static int check_path(const char *path, int len, struct stat *st, int skiplen)
228 const char *slash = path + len;
230 while (path < slash && *slash != '/')
232 if (!has_dirs_only_path(path, slash - path, skiplen)) {
236 return lstat(path, st);
240 * Write the contents from ce out to the working tree.
242 * When topath[] is not NULL, instead of writing to the working tree
243 * file named by ce, a temporary file is created by this function and
244 * its name is returned in topath[], which must be able to hold at
245 * least TEMPORARY_FILENAME_LENGTH bytes long.
247 int checkout_entry(struct cache_entry *ce,
248 const struct checkout *state, char *topath)
250 static struct strbuf path = STRBUF_INIT;
254 return write_entry(ce, topath, state, 1);
257 strbuf_add(&path, state->base_dir, state->base_dir_len);
258 strbuf_add(&path, ce->name, ce_namelen(ce));
260 if (!check_path(path.buf, path.len, &st, state->base_dir_len)) {
261 unsigned changed = ce_match_stat(ce, &st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE);
267 "%s already exists, no checkout\n",
273 * We unlink the old file, to get the new one with the
274 * right permissions (including umask, which is nasty
275 * to emulate by hand - much easier to let the system
276 * just do the right thing)
278 if (S_ISDIR(st.st_mode)) {
279 /* If it is a gitlink, leave it alone! */
280 if (S_ISGITLINK(ce->ce_mode))
283 return error("%s is a directory", path.buf);
284 remove_subtree(&path);
285 } else if (unlink(path.buf))
286 return error_errno("unable to unlink old '%s'", path.buf);
287 } else if (state->not_new)
290 create_directories(path.buf, path.len, state);
291 return write_entry(ce, path.buf, state, 0);