4 static void create_directories(const char *path, const struct checkout *state)
6 int len = strlen(path);
7 char *buf = xmalloc(len + 1);
8 const char *slash = path;
10 while ((slash = strchr(slash+1, '/')) != NULL) {
12 memcpy(buf, path, len);
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.
22 if (has_dirs_only_path(len, buf, state->base_dir_len))
23 continue; /* ok, it is already a directory. */
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.
31 if (mkdir(buf, 0777)) {
32 if (errno == EEXIST && state->force &&
33 !unlink(buf) && !mkdir(buf, 0777))
35 die("cannot create directory at %s", buf);
41 static void remove_subtree(const char *path)
43 DIR *dir = opendir(path);
45 char pathbuf[PATH_MAX];
49 die("cannot opendir %s (%s)", path, strerror(errno));
50 strcpy(pathbuf, path);
51 name = pathbuf + strlen(path);
53 while ((de = readdir(dir)) != NULL) {
55 if ((de->d_name[0] == '.') &&
56 ((de->d_name[1] == 0) ||
57 ((de->d_name[1] == '.') && de->d_name[2] == 0)))
59 strcpy(name, de->d_name);
60 if (lstat(pathbuf, &st))
61 die("cannot lstat %s (%s)", pathbuf, strerror(errno));
62 if (S_ISDIR(st.st_mode))
63 remove_subtree(pathbuf);
64 else if (unlink(pathbuf))
65 die("cannot unlink %s (%s)", pathbuf, strerror(errno));
69 die("cannot rmdir %s (%s)", path, strerror(errno));
72 static int create_file(const char *path, unsigned int mode)
74 mode = (mode & 0100) ? 0777 : 0666;
75 return open(path, O_WRONLY | O_CREAT | O_EXCL, mode);
78 static void *read_blob_entry(struct cache_entry *ce, const char *path, unsigned long *size)
80 enum object_type type;
81 void *new = read_sha1_file(ce->sha1, &type, size);
91 static int write_entry(struct cache_entry *ce, char *path, const struct checkout *state, int to_tempfile)
96 switch (ce->ce_mode & S_IFMT) {
102 new = read_blob_entry(ce, path, &size);
104 return error("git checkout-index: unable to read sha1 file of %s (%s)",
105 path, sha1_to_hex(ce->sha1));
108 * Convert from git internal format to working tree format
110 strbuf_init(&buf, 0);
111 if (convert_to_working_tree(ce->name, new, size, &buf)) {
114 new = strbuf_detach(&buf, &newsize);
119 strcpy(path, ".merge_file_XXXXXX");
122 fd = create_file(path, ce->ce_mode);
125 return error("git checkout-index: unable to create file %s (%s)",
126 path, strerror(errno));
129 wrote = write_in_full(fd, new, size);
133 return error("git checkout-index: unable to write file %s", path);
136 new = read_blob_entry(ce, path, &size);
138 return error("git checkout-index: unable to read sha1 file of %s (%s)",
139 path, sha1_to_hex(ce->sha1));
140 if (to_tempfile || !has_symlinks) {
142 strcpy(path, ".merge_link_XXXXXX");
145 fd = create_file(path, 0666);
148 return error("git checkout-index: unable to create "
149 "file %s (%s)", path, strerror(errno));
151 wrote = write_in_full(fd, new, size);
155 return error("git checkout-index: unable to write file %s",
158 wrote = symlink(new, path);
161 return error("git checkout-index: unable to create "
162 "symlink %s (%s)", path, strerror(errno));
167 return error("git checkout-index: cannot create temporary subproject %s", path);
168 if (mkdir(path, 0777) < 0)
169 return error("git checkout-index: cannot create subproject directory %s", path);
172 return error("git checkout-index: unknown file mode for %s", path);
175 if (state->refresh_cache) {
177 lstat(ce->name, &st);
178 fill_stat_cache_info(ce, &st);
183 int checkout_entry(struct cache_entry *ce, const struct checkout *state, char *topath)
185 static char path[PATH_MAX + 1];
187 int len = state->base_dir_len;
190 return write_entry(ce, topath, state, 1);
192 memcpy(path, state->base_dir, len);
193 strcpy(path + len, ce->name);
195 if (!lstat(path, &st)) {
196 unsigned changed = ce_match_stat(ce, &st, CE_MATCH_IGNORE_VALID);
201 fprintf(stderr, "git-checkout-index: %s already exists\n", path);
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)
211 if (S_ISDIR(st.st_mode)) {
212 /* If it is a gitlink, leave it alone! */
213 if (S_ISGITLINK(ce->ce_mode))
216 return error("%s is a directory", path);
217 remove_subtree(path);
218 } else if (unlink(path))
219 return error("unable to unlink old '%s' (%s)", path, strerror(errno));
220 } else if (state->not_new)
222 create_directories(path, state);
223 return write_entry(ce, path, state, 0);