Teach prune-packed to use the standard progress meter
[git/git-svn.git] / entry.c
blob98f5f6d4ecfc0dabae9a920bdf3beadbf20abcaf
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 struct stat st;
12 int stat_status;
14 len = slash - path;
15 memcpy(buf, path, len);
16 buf[len] = 0;
18 if (len <= state->base_dir_len)
20 * checkout-index --prefix=<dir>; <dir> is
21 * allowed to be a symlink to an existing
22 * directory.
24 stat_status = stat(buf, &st);
25 else
27 * if there currently is a symlink, we would
28 * want to replace it with a real directory.
30 stat_status = lstat(buf, &st);
32 if (!stat_status && S_ISDIR(st.st_mode))
33 continue; /* ok, it is already a directory. */
36 * We know stat_status == 0 means something exists
37 * there and this mkdir would fail, but that is an
38 * error codepath; we do not care, as we unlink and
39 * mkdir again in such a case.
41 if (mkdir(buf, 0777)) {
42 if (errno == EEXIST && state->force &&
43 !unlink(buf) && !mkdir(buf, 0777))
44 continue;
45 die("cannot create directory at %s", buf);
48 free(buf);
51 static void remove_subtree(const char *path)
53 DIR *dir = opendir(path);
54 struct dirent *de;
55 char pathbuf[PATH_MAX];
56 char *name;
58 if (!dir)
59 die("cannot opendir %s (%s)", path, strerror(errno));
60 strcpy(pathbuf, path);
61 name = pathbuf + strlen(path);
62 *name++ = '/';
63 while ((de = readdir(dir)) != NULL) {
64 struct stat st;
65 if ((de->d_name[0] == '.') &&
66 ((de->d_name[1] == 0) ||
67 ((de->d_name[1] == '.') && de->d_name[2] == 0)))
68 continue;
69 strcpy(name, de->d_name);
70 if (lstat(pathbuf, &st))
71 die("cannot lstat %s (%s)", pathbuf, strerror(errno));
72 if (S_ISDIR(st.st_mode))
73 remove_subtree(pathbuf);
74 else if (unlink(pathbuf))
75 die("cannot unlink %s (%s)", pathbuf, strerror(errno));
77 closedir(dir);
78 if (rmdir(path))
79 die("cannot rmdir %s (%s)", path, strerror(errno));
82 static int create_file(const char *path, unsigned int mode)
84 mode = (mode & 0100) ? 0777 : 0666;
85 return open(path, O_WRONLY | O_CREAT | O_EXCL, mode);
88 static void *read_blob_entry(struct cache_entry *ce, const char *path, unsigned long *size)
90 enum object_type type;
91 void *new = read_sha1_file(ce->sha1, &type, size);
93 if (new) {
94 if (type == OBJ_BLOB)
95 return new;
96 free(new);
98 return NULL;
101 static int write_entry(struct cache_entry *ce, char *path, const struct checkout *state, int to_tempfile)
103 int fd;
104 long wrote;
106 switch (ntohl(ce->ce_mode) & S_IFMT) {
107 char *new;
108 struct strbuf buf;
109 unsigned long size;
111 case S_IFREG:
112 new = read_blob_entry(ce, path, &size);
113 if (!new)
114 return error("git-checkout-index: unable to read sha1 file of %s (%s)",
115 path, sha1_to_hex(ce->sha1));
118 * Convert from git internal format to working tree format
120 strbuf_init(&buf, 0);
121 if (convert_to_working_tree(ce->name, new, size, &buf)) {
122 free(new);
123 new = strbuf_detach(&buf, &size);
126 if (to_tempfile) {
127 strcpy(path, ".merge_file_XXXXXX");
128 fd = mkstemp(path);
129 } else
130 fd = create_file(path, ntohl(ce->ce_mode));
131 if (fd < 0) {
132 free(new);
133 return error("git-checkout-index: unable to create file %s (%s)",
134 path, strerror(errno));
137 wrote = write_in_full(fd, new, size);
138 close(fd);
139 free(new);
140 if (wrote != size)
141 return error("git-checkout-index: unable to write file %s", path);
142 break;
143 case S_IFLNK:
144 new = read_blob_entry(ce, path, &size);
145 if (!new)
146 return error("git-checkout-index: unable to read sha1 file of %s (%s)",
147 path, sha1_to_hex(ce->sha1));
148 if (to_tempfile || !has_symlinks) {
149 if (to_tempfile) {
150 strcpy(path, ".merge_link_XXXXXX");
151 fd = mkstemp(path);
152 } else
153 fd = create_file(path, 0666);
154 if (fd < 0) {
155 free(new);
156 return error("git-checkout-index: unable to create "
157 "file %s (%s)", path, strerror(errno));
159 wrote = write_in_full(fd, new, size);
160 close(fd);
161 free(new);
162 if (wrote != size)
163 return error("git-checkout-index: unable to write file %s",
164 path);
165 } else {
166 wrote = symlink(new, path);
167 free(new);
168 if (wrote)
169 return error("git-checkout-index: unable to create "
170 "symlink %s (%s)", path, strerror(errno));
172 break;
173 case S_IFGITLINK:
174 if (to_tempfile)
175 return error("git-checkout-index: cannot create temporary subproject %s", path);
176 if (mkdir(path, 0777) < 0)
177 return error("git-checkout-index: cannot create subproject directory %s", path);
178 break;
179 default:
180 return error("git-checkout-index: unknown file mode for %s", path);
183 if (state->refresh_cache) {
184 struct stat st;
185 lstat(ce->name, &st);
186 fill_stat_cache_info(ce, &st);
188 return 0;
191 int checkout_entry(struct cache_entry *ce, const struct checkout *state, char *topath)
193 static char path[PATH_MAX + 1];
194 struct stat st;
195 int len = state->base_dir_len;
197 if (topath)
198 return write_entry(ce, topath, state, 1);
200 memcpy(path, state->base_dir, len);
201 strcpy(path + len, ce->name);
203 if (!lstat(path, &st)) {
204 unsigned changed = ce_match_stat(ce, &st, 1);
205 if (!changed)
206 return 0;
207 if (!state->force) {
208 if (!state->quiet)
209 fprintf(stderr, "git-checkout-index: %s already exists\n", path);
210 return -1;
214 * We unlink the old file, to get the new one with the
215 * right permissions (including umask, which is nasty
216 * to emulate by hand - much easier to let the system
217 * just do the right thing)
219 unlink(path);
220 if (S_ISDIR(st.st_mode)) {
221 /* If it is a gitlink, leave it alone! */
222 if (S_ISGITLINK(ntohl(ce->ce_mode)))
223 return 0;
224 if (!state->force)
225 return error("%s is a directory", path);
226 remove_subtree(path);
228 } else if (state->not_new)
229 return 0;
230 create_directories(path, state);
231 return write_entry(ce, path, state, 0);