]> git.ipfire.org Git - thirdparty/git.git/blame - entry.c
Merge branch 'maint'
[thirdparty/git.git] / entry.c
CommitLineData
12dccc16 1#include "cache.h"
8e440259 2#include "blob.h"
12dccc16 3
efbc5831 4static void create_directories(const char *path, const struct checkout *state)
12dccc16
LT
5{
6 int len = strlen(path);
7 char *buf = xmalloc(len + 1);
8 const char *slash = path;
9
10 while ((slash = strchr(slash+1, '/')) != NULL) {
11 len = slash - path;
12 memcpy(buf, path, len);
13 buf[len] = 0;
f312de01 14 if (mkdir(buf, 0777)) {
12dccc16
LT
15 if (errno == EEXIST) {
16 struct stat st;
f312de01 17 if (len > state->base_dir_len && state->force && !unlink(buf) && !mkdir(buf, 0777))
12dccc16
LT
18 continue;
19 if (!stat(buf, &st) && S_ISDIR(st.st_mode))
20 continue; /* ok */
21 }
22 die("cannot create directory at %s", buf);
23 }
24 }
25 free(buf);
26}
27
28static void remove_subtree(const char *path)
29{
30 DIR *dir = opendir(path);
31 struct dirent *de;
32 char pathbuf[PATH_MAX];
33 char *name;
34
35 if (!dir)
cc2903fc 36 die("cannot opendir %s (%s)", path, strerror(errno));
12dccc16
LT
37 strcpy(pathbuf, path);
38 name = pathbuf + strlen(path);
39 *name++ = '/';
40 while ((de = readdir(dir)) != NULL) {
41 struct stat st;
42 if ((de->d_name[0] == '.') &&
43 ((de->d_name[1] == 0) ||
44 ((de->d_name[1] == '.') && de->d_name[2] == 0)))
45 continue;
46 strcpy(name, de->d_name);
47 if (lstat(pathbuf, &st))
cc2903fc 48 die("cannot lstat %s (%s)", pathbuf, strerror(errno));
12dccc16
LT
49 if (S_ISDIR(st.st_mode))
50 remove_subtree(pathbuf);
51 else if (unlink(pathbuf))
cc2903fc 52 die("cannot unlink %s (%s)", pathbuf, strerror(errno));
12dccc16
LT
53 }
54 closedir(dir);
55 if (rmdir(path))
cc2903fc 56 die("cannot rmdir %s (%s)", path, strerror(errno));
12dccc16
LT
57}
58
d48a72f3 59static int create_file(const char *path, unsigned int mode)
12dccc16 60{
12dccc16 61 mode = (mode & 0100) ? 0777 : 0666;
781411ed 62 return open(path, O_WRONLY | O_CREAT | O_EXCL, mode);
12dccc16
LT
63}
64
f0807e62
LT
65static void *read_blob_entry(struct cache_entry *ce, const char *path, unsigned long *size)
66{
67 enum object_type type;
68 void *new = read_sha1_file(ce->sha1, &type, size);
69
70 if (new) {
71 if (type == OBJ_BLOB)
72 return new;
73 free(new);
74 }
75 return NULL;
76}
77
efbc5831 78static int write_entry(struct cache_entry *ce, char *path, const struct checkout *state, int to_tempfile)
12dccc16
LT
79{
80 int fd;
12dccc16 81 long wrote;
12dccc16 82
12dccc16 83 switch (ntohl(ce->ce_mode) & S_IFMT) {
f0807e62 84 char *buf, *new;
a2d7c6c6 85 unsigned long size;
6c510bee 86
12dccc16 87 case S_IFREG:
f0807e62
LT
88 new = read_blob_entry(ce, path, &size);
89 if (!new)
90 return error("git-checkout-index: unable to read sha1 file of %s (%s)",
91 path, sha1_to_hex(ce->sha1));
de84f99c
SP
92 if (to_tempfile) {
93 strcpy(path, ".merge_file_XXXXXX");
94 fd = mkstemp(path);
95 } else
96 fd = create_file(path, ntohl(ce->ce_mode));
12dccc16
LT
97 if (fd < 0) {
98 free(new);
215a7ad1 99 return error("git-checkout-index: unable to create file %s (%s)",
12dccc16
LT
100 path, strerror(errno));
101 }
6c510bee
LT
102
103 /*
104 * Convert from git internal format to working tree format
105 */
ac78e548
AR
106 buf = convert_to_working_tree(ce->name, new, &size);
107 if (buf) {
6c510bee
LT
108 free(new);
109 new = buf;
6c510bee
LT
110 }
111
93822c22 112 wrote = write_in_full(fd, new, size);
12dccc16
LT
113 close(fd);
114 free(new);
115 if (wrote != size)
215a7ad1 116 return error("git-checkout-index: unable to write file %s", path);
12dccc16
LT
117 break;
118 case S_IFLNK:
f0807e62
LT
119 new = read_blob_entry(ce, path, &size);
120 if (!new)
121 return error("git-checkout-index: unable to read sha1 file of %s (%s)",
122 path, sha1_to_hex(ce->sha1));
78a8d641
JS
123 if (to_tempfile || !has_symlinks) {
124 if (to_tempfile) {
125 strcpy(path, ".merge_link_XXXXXX");
126 fd = mkstemp(path);
127 } else
128 fd = create_file(path, 0666);
de84f99c
SP
129 if (fd < 0) {
130 free(new);
131 return error("git-checkout-index: unable to create "
132 "file %s (%s)", path, strerror(errno));
133 }
93822c22 134 wrote = write_in_full(fd, new, size);
de84f99c
SP
135 close(fd);
136 free(new);
137 if (wrote != size)
138 return error("git-checkout-index: unable to write file %s",
139 path);
140 } else {
141 wrote = symlink(new, path);
12dccc16 142 free(new);
de84f99c
SP
143 if (wrote)
144 return error("git-checkout-index: unable to create "
145 "symlink %s (%s)", path, strerror(errno));
12dccc16 146 }
12dccc16 147 break;
f0807e62
LT
148 case S_IFDIRLNK:
149 if (to_tempfile)
150 return error("git-checkout-index: cannot create temporary subproject %s", path);
151 if (mkdir(path, 0777) < 0)
152 return error("git-checkout-index: cannot create subproject directory %s", path);
153 break;
12dccc16 154 default:
215a7ad1 155 return error("git-checkout-index: unknown file mode for %s", path);
12dccc16
LT
156 }
157
6ee67f26 158 if (state->refresh_cache) {
12dccc16
LT
159 struct stat st;
160 lstat(ce->name, &st);
161 fill_stat_cache_info(ce, &st);
162 }
163 return 0;
164}
165
efbc5831 166int checkout_entry(struct cache_entry *ce, const struct checkout *state, char *topath)
12dccc16 167{
095c424d 168 static char path[PATH_MAX + 1];
de84f99c 169 struct stat st;
12dccc16
LT
170 int len = state->base_dir_len;
171
de84f99c
SP
172 if (topath)
173 return write_entry(ce, topath, state, 1);
174
12dccc16
LT
175 memcpy(path, state->base_dir, len);
176 strcpy(path + len, ce->name);
177
178 if (!lstat(path, &st)) {
5f73076c 179 unsigned changed = ce_match_stat(ce, &st, 1);
12dccc16
LT
180 if (!changed)
181 return 0;
182 if (!state->force) {
183 if (!state->quiet)
215a7ad1 184 fprintf(stderr, "git-checkout-index: %s already exists\n", path);
4b12dae6 185 return -1;
12dccc16
LT
186 }
187
188 /*
189 * We unlink the old file, to get the new one with the
190 * right permissions (including umask, which is nasty
191 * to emulate by hand - much easier to let the system
192 * just do the right thing)
193 */
194 unlink(path);
d48a72f3 195 if (S_ISDIR(st.st_mode)) {
f0807e62
LT
196 /* If it is a gitlink, leave it alone! */
197 if (S_ISDIRLNK(ntohl(ce->ce_mode)))
198 return 0;
d48a72f3
LT
199 if (!state->force)
200 return error("%s is a directory", path);
201 remove_subtree(path);
202 }
de84f99c 203 } else if (state->not_new)
12dccc16
LT
204 return 0;
205 create_directories(path, state);
de84f99c 206 return write_entry(ce, path, state, 0);
12dccc16 207}