6 static void create_directories(const char *path, struct checkout *state)
8 int len = strlen(path);
9 char *buf = xmalloc(len + 1);
10 const char *slash = path;
12 while ((slash = strchr(slash+1, '/')) != NULL) {
14 memcpy(buf, path, len);
16 if (mkdir(buf, 0777)) {
17 if (errno == EEXIST) {
19 if (len > state->base_dir_len && state->force && !unlink(buf) && !mkdir(buf, 0777))
21 if (!stat(buf, &st) && S_ISDIR(st.st_mode))
24 die("cannot create directory at %s", buf);
30 static void remove_subtree(const char *path)
32 DIR *dir = opendir(path);
34 char pathbuf[PATH_MAX];
38 die("cannot opendir %s", path);
39 strcpy(pathbuf, path);
40 name = pathbuf + strlen(path);
42 while ((de = readdir(dir)) != NULL) {
44 if ((de->d_name[0] == '.') &&
45 ((de->d_name[1] == 0) ||
46 ((de->d_name[1] == '.') && de->d_name[2] == 0)))
48 strcpy(name, de->d_name);
49 if (lstat(pathbuf, &st))
50 die("cannot lstat %s", pathbuf);
51 if (S_ISDIR(st.st_mode))
52 remove_subtree(pathbuf);
53 else if (unlink(pathbuf))
54 die("cannot unlink %s", pathbuf);
58 die("cannot rmdir %s", path);
61 static int create_file(const char *path, unsigned int mode)
63 mode = (mode & 0100) ? 0777 : 0666;
64 return open(path, O_WRONLY | O_CREAT | O_EXCL, mode);
67 static int write_entry(struct cache_entry *ce, char *path, struct checkout *state, int to_tempfile)
75 new = read_sha1_file(ce->sha1, type, &size);
76 if (!new || strcmp(type, blob_type)) {
79 return error("git-checkout-index: unable to read sha1 file of %s (%s)",
80 path, sha1_to_hex(ce->sha1));
82 switch (ntohl(ce->ce_mode) & S_IFMT) {
85 strcpy(path, ".merge_file_XXXXXX");
88 fd = create_file(path, ntohl(ce->ce_mode));
91 return error("git-checkout-index: unable to create file %s (%s)",
92 path, strerror(errno));
94 wrote = write(fd, new, size);
98 return error("git-checkout-index: unable to write file %s", path);
102 strcpy(path, ".merge_link_XXXXXX");
106 return error("git-checkout-index: unable to create "
107 "file %s (%s)", path, strerror(errno));
109 wrote = write(fd, new, size);
113 return error("git-checkout-index: unable to write file %s",
116 wrote = symlink(new, path);
119 return error("git-checkout-index: unable to create "
120 "symlink %s (%s)", path, strerror(errno));
125 return error("git-checkout-index: unknown file mode for %s", path);
128 if (state->refresh_cache) {
130 lstat(ce->name, &st);
131 fill_stat_cache_info(ce, &st);
136 int checkout_entry(struct cache_entry *ce, struct checkout *state, char *topath)
138 static char path[MAXPATHLEN+1];
140 int len = state->base_dir_len;
143 return write_entry(ce, topath, state, 1);
145 memcpy(path, state->base_dir, len);
146 strcpy(path + len, ce->name);
148 if (!lstat(path, &st)) {
149 unsigned changed = ce_match_stat(ce, &st, 1);
154 fprintf(stderr, "git-checkout-index: %s already exists\n", path);
159 * We unlink the old file, to get the new one with the
160 * right permissions (including umask, which is nasty
161 * to emulate by hand - much easier to let the system
162 * just do the right thing)
165 if (S_ISDIR(st.st_mode)) {
167 return error("%s is a directory", path);
168 remove_subtree(path);
170 } else if (state->not_new)
172 create_directories(path, state);
173 return write_entry(ce, path, state, 0);