1#include "cache.h"
2#include "blob.h"
3#include "dir.h"
45
static void create_directories(const char *path, int path_len,
6const struct checkout *state)
7{
8char *buf = xmalloc(path_len + 1);
9int len = 0;
1011
while (len < path_len) {
12do {
13buf[len] = path[len];
14len++;
15} while (len < path_len && path[len] != '/');
16if (len >= path_len)
17break;
18buf[len] = 0;
1920
/*
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.
26*/
27if (has_dirs_only_path(buf, len, state->base_dir_len))
28continue; /* ok, it is already a directory. */
2930
/*
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.
35*/
36if (mkdir(buf, 0777)) {
37if (errno == EEXIST && state->force &&
38!unlink(buf) && !mkdir(buf, 0777))
39continue;
40die("cannot create directory at %s", buf);
41}
42}
43free(buf);
44}
4546
static void remove_subtree(const char *path)
47{
48DIR *dir = opendir(path);
49struct dirent *de;
50char pathbuf[PATH_MAX];
51char *name;
5253
if (!dir)
54die("cannot opendir %s (%s)", path, strerror(errno));
55strcpy(pathbuf, path);
56name = pathbuf + strlen(path);
57*name++ = '/';
58while ((de = readdir(dir)) != NULL) {
59struct stat st;
60if (is_dot_or_dotdot(de->d_name))
61continue;
62strcpy(name, de->d_name);
63if (lstat(pathbuf, &st))
64die("cannot lstat %s (%s)", pathbuf, strerror(errno));
65if (S_ISDIR(st.st_mode))
66remove_subtree(pathbuf);
67else if (unlink(pathbuf))
68die("cannot unlink %s (%s)", pathbuf, strerror(errno));
69}
70closedir(dir);
71if (rmdir(path))
72die("cannot rmdir %s (%s)", path, strerror(errno));
73}
7475
static int create_file(const char *path, unsigned int mode)
76{
77mode = (mode & 0100) ? 0777 : 0666;
78return open(path, O_WRONLY | O_CREAT | O_EXCL, mode);
79}
8081
static void *read_blob_entry(struct cache_entry *ce, const char *path, unsigned long *size)
82{
83enum object_type type;
84void *new = read_sha1_file(ce->sha1, &type, size);
8586
if (new) {
87if (type == OBJ_BLOB)
88return new;
89free(new);
90}
91return NULL;
92}
9394
static int write_entry(struct cache_entry *ce, char *path, const struct checkout *state, int to_tempfile)
95{
96int fd;
97long wrote;
9899
switch (ce->ce_mode & S_IFMT) {
100char *new;
101struct strbuf buf;
102unsigned long size;
103104
case S_IFREG:
105new = read_blob_entry(ce, path, &size);
106if (!new)
107return error("git checkout-index: unable to read sha1 file of %s (%s)",
108path, sha1_to_hex(ce->sha1));
109110
/*
111* Convert from git internal format to working tree format
112*/
113strbuf_init(&buf, 0);
114if (convert_to_working_tree(ce->name, new, size, &buf)) {
115size_t newsize = 0;
116free(new);
117new = strbuf_detach(&buf, &newsize);
118size = newsize;
119}
120121
if (to_tempfile) {
122strcpy(path, ".merge_file_XXXXXX");
123fd = mkstemp(path);
124} else
125fd = create_file(path, ce->ce_mode);
126if (fd < 0) {
127free(new);
128return error("git checkout-index: unable to create file %s (%s)",
129path, strerror(errno));
130}
131132
wrote = write_in_full(fd, new, size);
133close(fd);
134free(new);
135if (wrote != size)
136return error("git checkout-index: unable to write file %s", path);
137break;
138case S_IFLNK:
139new = read_blob_entry(ce, path, &size);
140if (!new)
141return error("git checkout-index: unable to read sha1 file of %s (%s)",
142path, sha1_to_hex(ce->sha1));
143if (to_tempfile || !has_symlinks) {
144if (to_tempfile) {
145strcpy(path, ".merge_link_XXXXXX");
146fd = mkstemp(path);
147} else
148fd = create_file(path, 0666);
149if (fd < 0) {
150free(new);
151return error("git checkout-index: unable to create "
152"file %s (%s)", path, strerror(errno));
153}
154wrote = write_in_full(fd, new, size);
155close(fd);
156free(new);
157if (wrote != size)
158return error("git checkout-index: unable to write file %s",
159path);
160} else {
161wrote = symlink(new, path);
162free(new);
163if (wrote)
164return error("git checkout-index: unable to create "
165"symlink %s (%s)", path, strerror(errno));
166}
167break;
168case S_IFGITLINK:
169if (to_tempfile)
170return error("git checkout-index: cannot create temporary subproject %s", path);
171if (mkdir(path, 0777) < 0)
172return error("git checkout-index: cannot create subproject directory %s", path);
173break;
174default:
175return error("git checkout-index: unknown file mode for %s", path);
176}
177178
if (state->refresh_cache) {
179struct stat st;
180lstat(ce->name, &st);
181fill_stat_cache_info(ce, &st);
182}
183return 0;
184}
185186
int checkout_entry(struct cache_entry *ce, const struct checkout *state, char *topath)
187{
188static char path[PATH_MAX + 1];
189struct stat st;
190int len = state->base_dir_len;
191192
if (topath)
193return write_entry(ce, topath, state, 1);
194195
memcpy(path, state->base_dir, len);
196strcpy(path + len, ce->name);
197len += ce_namelen(ce);
198199
if (!lstat(path, &st)) {
200unsigned changed = ce_match_stat(ce, &st, CE_MATCH_IGNORE_VALID);
201if (!changed)
202return 0;
203if (!state->force) {
204if (!state->quiet)
205fprintf(stderr, "git-checkout-index: %s already exists\n", path);
206return -1;
207}
208209
/*
210* We unlink the old file, to get the new one with the
211* right permissions (including umask, which is nasty
212* to emulate by hand - much easier to let the system
213* just do the right thing)
214*/
215if (S_ISDIR(st.st_mode)) {
216/* If it is a gitlink, leave it alone! */
217if (S_ISGITLINK(ce->ce_mode))
218return 0;
219if (!state->force)
220return error("%s is a directory", path);
221remove_subtree(path);
222} else if (unlink(path))
223return error("unable to unlink old '%s' (%s)", path, strerror(errno));
224} else if (state->not_new)
225return 0;
226create_directories(path, len, state);
227return write_entry(ce, path, state, 0);
228}