lstat_cache(): introduce has_dirs_only_path() function
[git/dscho.git] / entry.c
blob01a683ee5dedb6c191faa5c42d694ea8cbcf08a3
1 #include "cache.h"
2 #include "blob.h"
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) {
11 len = slash - path;
12 memcpy(buf, path, len);
13 buf[len] = 0;
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))
34 continue;
35 die("cannot create directory at %s", buf);
38 free(buf);
41 static void remove_subtree(const char *path)
43 DIR *dir = opendir(path);
44 struct dirent *de;
45 char pathbuf[PATH_MAX];
46 char *name;
48 if (!dir)
49 die("cannot opendir %s (%s)", path, strerror(errno));
50 strcpy(pathbuf, path);
51 name = pathbuf + strlen(path);
52 *name++ = '/';
53 while ((de = readdir(dir)) != NULL) {
54 struct stat st;
55 if ((de->d_name[0] == '.') &&
56 ((de->d_name[1] == 0) ||
57 ((de->d_name[1] == '.') && de->d_name[2] == 0)))
58 continue;
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));
67 closedir(dir);
68 if (rmdir(path))
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);
83 if (new) {
84 if (type == OBJ_BLOB)
85 return new;
86 free(new);
88 return NULL;
91 static int write_entry(struct cache_entry *ce, char *path, const struct checkout *state, int to_tempfile)
93 int fd;
94 long wrote;
96 switch (ce->ce_mode & S_IFMT) {
97 char *new;
98 struct strbuf buf;
99 unsigned long size;
101 case S_IFREG:
102 new = read_blob_entry(ce, path, &size);
103 if (!new)
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)) {
112 size_t newsize = 0;
113 free(new);
114 new = strbuf_detach(&buf, &newsize);
115 size = newsize;
118 if (to_tempfile) {
119 strcpy(path, ".merge_file_XXXXXX");
120 fd = mkstemp(path);
121 } else
122 fd = create_file(path, ce->ce_mode);
123 if (fd < 0) {
124 free(new);
125 return error("git checkout-index: unable to create file %s (%s)",
126 path, strerror(errno));
129 wrote = write_in_full(fd, new, size);
130 close(fd);
131 free(new);
132 if (wrote != size)
133 return error("git checkout-index: unable to write file %s", path);
134 break;
135 case S_IFLNK:
136 new = read_blob_entry(ce, path, &size);
137 if (!new)
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) {
141 if (to_tempfile) {
142 strcpy(path, ".merge_link_XXXXXX");
143 fd = mkstemp(path);
144 } else
145 fd = create_file(path, 0666);
146 if (fd < 0) {
147 free(new);
148 return error("git checkout-index: unable to create "
149 "file %s (%s)", path, strerror(errno));
151 wrote = write_in_full(fd, new, size);
152 close(fd);
153 free(new);
154 if (wrote != size)
155 return error("git checkout-index: unable to write file %s",
156 path);
157 } else {
158 wrote = symlink(new, path);
159 free(new);
160 if (wrote)
161 return error("git checkout-index: unable to create "
162 "symlink %s (%s)", path, strerror(errno));
164 break;
165 case S_IFGITLINK:
166 if (to_tempfile)
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);
170 break;
171 default:
172 return error("git checkout-index: unknown file mode for %s", path);
175 if (state->refresh_cache) {
176 struct stat st;
177 lstat(ce->name, &st);
178 fill_stat_cache_info(ce, &st);
180 return 0;
183 int checkout_entry(struct cache_entry *ce, const struct checkout *state, char *topath)
185 static char path[PATH_MAX + 1];
186 struct stat st;
187 int len = state->base_dir_len;
189 if (topath)
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);
197 if (!changed)
198 return 0;
199 if (!state->force) {
200 if (!state->quiet)
201 fprintf(stderr, "git-checkout-index: %s already exists\n", path);
202 return -1;
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))
214 return 0;
215 if (!state->force)
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)
221 return 0;
222 create_directories(path, state);
223 return write_entry(ce, path, state, 0);