bash completion: add 'rename' subcommand to git-remote
[git/jrn.git] / entry.c
blob5f24816eb9fe9fa934889336f2dbcdc7bf006635
1 #include "cache.h"
2 #include "blob.h"
3 #include "dir.h"
5 static void create_directories(const char *path, const 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) {
12 struct stat st;
13 int stat_status;
15 len = slash - path;
16 memcpy(buf, path, len);
17 buf[len] = 0;
19 if (len <= state->base_dir_len)
21 * checkout-index --prefix=<dir>; <dir> is
22 * allowed to be a symlink to an existing
23 * directory.
25 stat_status = stat(buf, &st);
26 else
28 * if there currently is a symlink, we would
29 * want to replace it with a real directory.
31 stat_status = lstat(buf, &st);
33 if (!stat_status && S_ISDIR(st.st_mode))
34 continue; /* ok, it is already a directory. */
37 * We know stat_status == 0 means something exists
38 * there and this mkdir would fail, but that is an
39 * error codepath; we do not care, as we unlink and
40 * mkdir again in such a case.
42 if (mkdir(buf, 0777)) {
43 if (errno == EEXIST && state->force &&
44 !unlink(buf) && !mkdir(buf, 0777))
45 continue;
46 die("cannot create directory at %s", buf);
49 free(buf);
52 static void remove_subtree(const char *path)
54 DIR *dir = opendir(path);
55 struct dirent *de;
56 char pathbuf[PATH_MAX];
57 char *name;
59 if (!dir)
60 die("cannot opendir %s (%s)", path, strerror(errno));
61 strcpy(pathbuf, path);
62 name = pathbuf + strlen(path);
63 *name++ = '/';
64 while ((de = readdir(dir)) != NULL) {
65 struct stat st;
66 if (is_dot_or_dotdot(de->d_name))
67 continue;
68 strcpy(name, de->d_name);
69 if (lstat(pathbuf, &st))
70 die("cannot lstat %s (%s)", pathbuf, strerror(errno));
71 if (S_ISDIR(st.st_mode))
72 remove_subtree(pathbuf);
73 else if (unlink(pathbuf))
74 die("cannot unlink %s (%s)", pathbuf, strerror(errno));
76 closedir(dir);
77 if (rmdir(path))
78 die("cannot rmdir %s (%s)", path, strerror(errno));
81 static int create_file(const char *path, unsigned int mode)
83 mode = (mode & 0100) ? 0777 : 0666;
84 return open(path, O_WRONLY | O_CREAT | O_EXCL, mode);
87 static void *read_blob_entry(struct cache_entry *ce, const char *path, unsigned long *size)
89 enum object_type type;
90 void *new = read_sha1_file(ce->sha1, &type, size);
92 if (new) {
93 if (type == OBJ_BLOB)
94 return new;
95 free(new);
97 return NULL;
100 static int write_entry(struct cache_entry *ce, char *path, const struct checkout *state, int to_tempfile)
102 int fd;
103 long wrote;
105 switch (ce->ce_mode & S_IFMT) {
106 char *new;
107 struct strbuf buf;
108 unsigned long size;
110 case S_IFREG:
111 new = read_blob_entry(ce, path, &size);
112 if (!new)
113 return error("git checkout-index: unable to read sha1 file of %s (%s)",
114 path, sha1_to_hex(ce->sha1));
117 * Convert from git internal format to working tree format
119 strbuf_init(&buf, 0);
120 if (convert_to_working_tree(ce->name, new, size, &buf)) {
121 size_t newsize = 0;
122 free(new);
123 new = strbuf_detach(&buf, &newsize);
124 size = newsize;
127 if (to_tempfile) {
128 strcpy(path, ".merge_file_XXXXXX");
129 fd = mkstemp(path);
130 } else
131 fd = create_file(path, ce->ce_mode);
132 if (fd < 0) {
133 free(new);
134 return error("git checkout-index: unable to create file %s (%s)",
135 path, strerror(errno));
138 wrote = write_in_full(fd, new, size);
139 close(fd);
140 free(new);
141 if (wrote != size)
142 return error("git checkout-index: unable to write file %s", path);
143 break;
144 case S_IFLNK:
145 new = read_blob_entry(ce, path, &size);
146 if (!new)
147 return error("git checkout-index: unable to read sha1 file of %s (%s)",
148 path, sha1_to_hex(ce->sha1));
149 if (to_tempfile || !has_symlinks) {
150 if (to_tempfile) {
151 strcpy(path, ".merge_link_XXXXXX");
152 fd = mkstemp(path);
153 } else
154 fd = create_file(path, 0666);
155 if (fd < 0) {
156 free(new);
157 return error("git checkout-index: unable to create "
158 "file %s (%s)", path, strerror(errno));
160 wrote = write_in_full(fd, new, size);
161 close(fd);
162 free(new);
163 if (wrote != size)
164 return error("git checkout-index: unable to write file %s",
165 path);
166 } else {
167 wrote = symlink(new, path);
168 free(new);
169 if (wrote)
170 return error("git checkout-index: unable to create "
171 "symlink %s (%s)", path, strerror(errno));
173 break;
174 case S_IFGITLINK:
175 if (to_tempfile)
176 return error("git checkout-index: cannot create temporary subproject %s", path);
177 if (mkdir(path, 0777) < 0)
178 return error("git checkout-index: cannot create subproject directory %s", path);
179 break;
180 default:
181 return error("git checkout-index: unknown file mode for %s", path);
184 if (state->refresh_cache) {
185 struct stat st;
186 lstat(ce->name, &st);
187 fill_stat_cache_info(ce, &st);
189 return 0;
192 int checkout_entry(struct cache_entry *ce, const struct checkout *state, char *topath)
194 static char path[PATH_MAX + 1];
195 struct stat st;
196 int len = state->base_dir_len;
198 if (topath)
199 return write_entry(ce, topath, state, 1);
201 memcpy(path, state->base_dir, len);
202 strcpy(path + len, ce->name);
204 if (!lstat(path, &st)) {
205 unsigned changed = ce_match_stat(ce, &st, CE_MATCH_IGNORE_VALID);
206 if (!changed)
207 return 0;
208 if (!state->force) {
209 if (!state->quiet)
210 fprintf(stderr, "git-checkout-index: %s already exists\n", path);
211 return -1;
215 * We unlink the old file, to get the new one with the
216 * right permissions (including umask, which is nasty
217 * to emulate by hand - much easier to let the system
218 * just do the right thing)
220 if (S_ISDIR(st.st_mode)) {
221 /* If it is a gitlink, leave it alone! */
222 if (S_ISGITLINK(ce->ce_mode))
223 return 0;
224 if (!state->force)
225 return error("%s is a directory", path);
226 remove_subtree(path);
227 } else if (unlink(path))
228 return error("unable to unlink old '%s' (%s)", path, strerror(errno));
229 } else if (state->not_new)
230 return 0;
231 create_directories(path, state);
232 return write_entry(ce, path, state, 0);