5 static void create_directories(const char *path, struct checkout *state)
7 int len = strlen(path);
8 char *buf = xmalloc(len + 1);
9 const char *slash = path;
11 while ((slash = strchr(slash+1, '/')) != NULL) {
13 memcpy(buf, path, len);
15 if (mkdir(buf, 0777)) {
16 if (errno == EEXIST) {
18 if (len > state->base_dir_len && state->force && !unlink(buf) && !mkdir(buf, 0777))
20 if (!stat(buf, &st) && S_ISDIR(st.st_mode))
23 die("cannot create directory at %s", buf);
29 static void remove_subtree(const char *path)
31 DIR *dir = opendir(path);
33 char pathbuf[PATH_MAX];
37 die("cannot opendir %s", path);
38 strcpy(pathbuf, path);
39 name = pathbuf + strlen(path);
41 while ((de = readdir(dir)) != NULL) {
43 if ((de->d_name[0] == '.') &&
44 ((de->d_name[1] == 0) ||
45 ((de->d_name[1] == '.') && de->d_name[2] == 0)))
47 strcpy(name, de->d_name);
48 if (lstat(pathbuf, &st))
49 die("cannot lstat %s", pathbuf);
50 if (S_ISDIR(st.st_mode))
51 remove_subtree(pathbuf);
52 else if (unlink(pathbuf))
53 die("cannot unlink %s", pathbuf);
57 die("cannot rmdir %s", path);
60 static int create_file(const char *path, unsigned int mode)
62 mode = (mode & 0100) ? 0777 : 0666;
63 return open(path, O_WRONLY | O_CREAT | O_EXCL, mode);
66 static int write_entry(struct cache_entry *ce, char *path, struct checkout *state, int to_tempfile)
74 new = read_sha1_file(ce->sha1, type, &size);
75 if (!new || strcmp(type, "blob")) {
78 return error("git-checkout-index: unable to read sha1 file of %s (%s)",
79 path, sha1_to_hex(ce->sha1));
81 switch (ntohl(ce->ce_mode) & S_IFMT) {
84 strcpy(path, ".merge_file_XXXXXX");
87 fd = create_file(path, ntohl(ce->ce_mode));
90 return error("git-checkout-index: unable to create file %s (%s)",
91 path, strerror(errno));
93 wrote = write(fd, new, size);
97 return error("git-checkout-index: unable to write file %s", path);
101 strcpy(path, ".merge_link_XXXXXX");
105 return error("git-checkout-index: unable to create "
106 "file %s (%s)", path, strerror(errno));
108 wrote = write(fd, new, size);
112 return error("git-checkout-index: unable to write file %s",
115 wrote = symlink(new, path);
118 return error("git-checkout-index: unable to create "
119 "symlink %s (%s)", path, strerror(errno));
124 return error("git-checkout-index: unknown file mode for %s", path);
127 if (state->refresh_cache) {
129 lstat(ce->name, &st);
130 fill_stat_cache_info(ce, &st);
135 int checkout_entry(struct cache_entry *ce, struct checkout *state, char *topath)
137 static char path[MAXPATHLEN+1];
139 int len = state->base_dir_len;
142 return write_entry(ce, topath, state, 1);
144 memcpy(path, state->base_dir, len);
145 strcpy(path + len, ce->name);
147 if (!lstat(path, &st)) {
148 unsigned changed = ce_match_stat(ce, &st, 1);
153 fprintf(stderr, "git-checkout-index: %s already exists\n", path);
158 * We unlink the old file, to get the new one with the
159 * right permissions (including umask, which is nasty
160 * to emulate by hand - much easier to let the system
161 * just do the right thing)
164 if (S_ISDIR(st.st_mode)) {
166 return error("%s is a directory", path);
167 remove_subtree(path);
169 } else if (state->not_new)
171 create_directories(path, state);
172 return write_entry(ce, path, state, 0);