fixup.cc5711424b7ae36276a40c06ede5d95f87ca20f0
[git/dscho.git] / entry.c
bloba38811f1c0863d0f667023db7dd1da862c6d3a4c
1 #include "cache.h"
2 #include "blob.h"
3 #include "dir.h"
5 static void create_directories(const char *path, int path_len,
6 const struct checkout *state)
8 char *buf = xmalloc(path_len + 1);
9 int len = 0;
11 while (len < path_len) {
12 do {
13 buf[len] = path[len];
14 len++;
15 } while (len < path_len && path[len] != '/');
16 if (len >= path_len)
17 break;
18 buf[len] = 0;
21 * For 'checkout-index --prefix=<dir>', <dir> is
22 * allowed to be a symlink to an existing directory,
23 * and we set 'state->base_dir_len' below, such that
24 * we test the path components of the prefix with the
25 * stat() function instead of the lstat() function.
27 if (has_dirs_only_path(buf, len, state->base_dir_len))
28 continue; /* ok, it is already a directory. */
31 * If this mkdir() would fail, it could be that there
32 * is already a symlink or something else exists
33 * there, therefore we then try to unlink it and try
34 * one more time to create the directory.
36 if (mkdir(buf, 0777)) {
37 if (errno == EEXIST && state->force &&
38 !unlink_or_warn(buf) && !mkdir(buf, 0777))
39 continue;
40 die_errno("cannot create directory at '%s'", buf);
43 free(buf);
46 static void remove_subtree(const char *path)
48 DIR *dir = opendir(path);
49 struct dirent *de;
50 char pathbuf[PATH_MAX];
51 char *name;
53 if (!dir)
54 die_errno("cannot opendir '%s'", path);
55 strcpy(pathbuf, path);
56 name = pathbuf + strlen(path);
57 *name++ = '/';
58 while ((de = readdir(dir)) != NULL) {
59 struct stat st;
60 if (is_dot_or_dotdot(de->d_name))
61 continue;
62 strcpy(name, de->d_name);
63 if (lstat(pathbuf, &st))
64 die_errno("cannot lstat '%s'", pathbuf);
65 if (S_ISDIR(st.st_mode))
66 remove_subtree(pathbuf);
67 else if (unlink(pathbuf))
68 die_errno("cannot unlink '%s'", pathbuf);
70 closedir(dir);
71 if (rmdir(path))
72 die_errno("cannot rmdir '%s'", path);
75 static int create_file(const char *path, unsigned int mode)
77 mode = (mode & 0100) ? 0777 : 0666;
78 return open(path, O_WRONLY | O_CREAT |
79 (keep_hard_links ? O_TRUNC : O_EXCL), mode);
82 static void *read_blob_entry(struct cache_entry *ce, unsigned long *size)
84 enum object_type type;
85 void *new = read_sha1_file(ce->sha1, &type, size);
87 if (new) {
88 if (type == OBJ_BLOB)
89 return new;
90 free(new);
92 return NULL;
95 static int write_entry(struct cache_entry *ce, char *path, const struct checkout *state, int to_tempfile)
97 unsigned int ce_mode_s_ifmt = ce->ce_mode & S_IFMT;
98 int fd, ret, fstat_done = 0;
99 char *new;
100 struct strbuf buf = STRBUF_INIT;
101 unsigned long size;
102 size_t wrote, newsize = 0;
103 struct stat st;
105 switch (ce_mode_s_ifmt) {
106 case S_IFREG:
107 case S_IFLNK:
108 new = read_blob_entry(ce, &size);
109 if (!new)
110 return error("git checkout-index: unable to read sha1 file of %s (%s)",
111 path, sha1_to_hex(ce->sha1));
113 if (ce_mode_s_ifmt == S_IFLNK && has_symlinks && !to_tempfile) {
114 ret = symlink(new, path);
115 free(new);
116 if (ret)
117 return error("git checkout-index: unable to create symlink %s (%s)",
118 path, strerror(errno));
119 break;
123 * Convert from git internal format to working tree format
125 if (ce_mode_s_ifmt == S_IFREG &&
126 convert_to_working_tree(ce->name, new, size, &buf)) {
127 free(new);
128 new = strbuf_detach(&buf, &newsize);
129 size = newsize;
132 if (to_tempfile) {
133 if (ce_mode_s_ifmt == S_IFREG)
134 strcpy(path, ".merge_file_XXXXXX");
135 else
136 strcpy(path, ".merge_link_XXXXXX");
137 fd = mkstemp(path);
138 } else if (ce_mode_s_ifmt == S_IFREG) {
139 fd = create_file(path, ce->ce_mode);
140 } else {
141 fd = create_file(path, 0666);
143 if (fd < 0) {
144 free(new);
145 return error("git checkout-index: unable to create file %s (%s)",
146 path, strerror(errno));
149 wrote = write_in_full(fd, new, size);
150 /* use fstat() only when path == ce->name */
151 if (fstat_is_reliable() &&
152 state->refresh_cache && !to_tempfile && !state->base_dir_len) {
153 fstat(fd, &st);
154 fstat_done = 1;
156 close(fd);
157 free(new);
158 if (wrote != size)
159 return error("git checkout-index: unable to write file %s", path);
160 break;
161 case S_IFGITLINK:
162 if (to_tempfile)
163 return error("git checkout-index: cannot create temporary subproject %s", path);
164 if (mkdir(path, 0777) < 0)
165 return error("git checkout-index: cannot create subproject directory %s", path);
166 break;
167 default:
168 return error("git checkout-index: unknown file mode for %s", path);
171 if (state->refresh_cache) {
172 if (!fstat_done)
173 lstat(ce->name, &st);
174 fill_stat_cache_info(ce, &st);
176 return 0;
180 * This is like 'lstat()', except it refuses to follow symlinks
181 * in the path, after skipping "skiplen".
183 static int check_path(const char *path, int len, struct stat *st, int skiplen)
185 const char *slash = path + len;
187 while (path < slash && *slash != '/')
188 slash--;
189 if (!has_dirs_only_path(path, slash - path, skiplen)) {
190 errno = ENOENT;
191 return -1;
193 return lstat(path, st);
196 int checkout_entry(struct cache_entry *ce, const struct checkout *state, char *topath)
198 static char path[PATH_MAX + 1];
199 struct stat st;
200 int len = state->base_dir_len;
202 if (topath)
203 return write_entry(ce, topath, state, 1);
205 memcpy(path, state->base_dir, len);
206 strcpy(path + len, ce->name);
207 len += ce_namelen(ce);
209 if (!check_path(path, len, &st, state->base_dir_len)) {
210 unsigned changed = ce_match_stat(ce, &st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE);
211 if (!changed)
212 return 0;
213 if (!state->force) {
214 if (!state->quiet)
215 fprintf(stderr, "git-checkout-index: %s already exists\n", path);
216 return -1;
220 * We unlink the old file, to get the new one with the
221 * right permissions (including umask, which is nasty
222 * to emulate by hand - much easier to let the system
223 * just do the right thing)
225 if (S_ISDIR(st.st_mode)) {
226 /* If it is a gitlink, leave it alone! */
227 if (S_ISGITLINK(ce->ce_mode))
228 return 0;
229 if (!state->force)
230 return error("%s is a directory", path);
231 remove_subtree(path);
232 } else if ((!keep_hard_links || !S_ISREG(st.st_mode) ||
233 st.st_mode != ce->ce_mode) &&
234 unlink(path))
235 return error("unable to unlink old '%s' (%s)", path, strerror(errno));
236 } else if (state->not_new)
237 return 0;
238 create_directories(path, len, state);
239 return write_entry(ce, path, state, 0);