]> git.ipfire.org Git - thirdparty/git.git/blame - entry.c
streaming: a new API to read from the object store
[thirdparty/git.git] / entry.c
CommitLineData
12dccc16 1#include "cache.h"
8e440259 2#include "blob.h"
8ca12c0d 3#include "dir.h"
12dccc16 4
81a9aa60
KB
5static void create_directories(const char *path, int path_len,
6 const struct checkout *state)
12dccc16 7{
81a9aa60
KB
8 char *buf = xmalloc(path_len + 1);
9 int len = 0;
10
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;
12dccc16 18 buf[len] = 0;
fa2e71c9 19
bad4a54f
KB
20 /*
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 */
57199892 27 if (has_dirs_only_path(buf, len, state->base_dir_len))
fa2e71c9
JH
28 continue; /* ok, it is already a directory. */
29
30 /*
bad4a54f
KB
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.
fa2e71c9 35 */
f312de01 36 if (mkdir(buf, 0777)) {
fa2e71c9 37 if (errno == EEXIST && state->force &&
691f1a28 38 !unlink_or_warn(buf) && !mkdir(buf, 0777))
fa2e71c9 39 continue;
0721c314 40 die_errno("cannot create directory at '%s'", buf);
12dccc16
LT
41 }
42 }
43 free(buf);
44}
45
46static void remove_subtree(const char *path)
47{
48 DIR *dir = opendir(path);
49 struct dirent *de;
50 char pathbuf[PATH_MAX];
51 char *name;
a6080a0a 52
12dccc16 53 if (!dir)
d824cbba 54 die_errno("cannot opendir '%s'", path);
12dccc16
LT
55 strcpy(pathbuf, path);
56 name = pathbuf + strlen(path);
57 *name++ = '/';
58 while ((de = readdir(dir)) != NULL) {
59 struct stat st;
8ca12c0d 60 if (is_dot_or_dotdot(de->d_name))
12dccc16
LT
61 continue;
62 strcpy(name, de->d_name);
63 if (lstat(pathbuf, &st))
d824cbba 64 die_errno("cannot lstat '%s'", pathbuf);
12dccc16
LT
65 if (S_ISDIR(st.st_mode))
66 remove_subtree(pathbuf);
67 else if (unlink(pathbuf))
d824cbba 68 die_errno("cannot unlink '%s'", pathbuf);
12dccc16
LT
69 }
70 closedir(dir);
71 if (rmdir(path))
d824cbba 72 die_errno("cannot rmdir '%s'", path);
12dccc16
LT
73}
74
d48a72f3 75static int create_file(const char *path, unsigned int mode)
12dccc16 76{
12dccc16 77 mode = (mode & 0100) ? 0777 : 0666;
781411ed 78 return open(path, O_WRONLY | O_CREAT | O_EXCL, mode);
12dccc16
LT
79}
80
4857c761 81static void *read_blob_entry(struct cache_entry *ce, unsigned long *size)
f0807e62
LT
82{
83 enum object_type type;
84 void *new = read_sha1_file(ce->sha1, &type, size);
85
86 if (new) {
87 if (type == OBJ_BLOB)
88 return new;
89 free(new);
90 }
91 return NULL;
92}
93
fd5db55d
JH
94static int open_output_fd(char *path, struct cache_entry *ce, int to_tempfile)
95{
96 int symlink = (ce->ce_mode & S_IFMT) != S_IFREG;
97 if (to_tempfile) {
98 strcpy(path, symlink
99 ? ".merge_link_XXXXXX" : ".merge_file_XXXXXX");
100 return mkstemp(path);
101 } else {
102 return create_file(path, !symlink ? ce->ce_mode : 0666);
103 }
104}
105
106static int fstat_output(int fd, const struct checkout *state, struct stat *st)
107{
108 /* use fstat() only when path == ce->name */
109 if (fstat_is_reliable() &&
110 state->refresh_cache && !state->base_dir_len) {
111 fstat(fd, st);
112 return 1;
113 }
114 return 0;
115}
116
efbc5831 117static int write_entry(struct cache_entry *ce, char *path, const struct checkout *state, int to_tempfile)
12dccc16 118{
4857c761 119 unsigned int ce_mode_s_ifmt = ce->ce_mode & S_IFMT;
e4c72923 120 int fd, ret, fstat_done = 0;
4857c761
KB
121 char *new;
122 struct strbuf buf = STRBUF_INIT;
123 unsigned long size;
124 size_t wrote, newsize = 0;
e4c72923 125 struct stat st;
4857c761
KB
126
127 switch (ce_mode_s_ifmt) {
12dccc16 128 case S_IFREG:
4857c761
KB
129 case S_IFLNK:
130 new = read_blob_entry(ce, &size);
f0807e62 131 if (!new)
d43e9073 132 return error("unable to read sha1 file of %s (%s)",
f0807e62 133 path, sha1_to_hex(ce->sha1));
1a9d7e9b 134
4857c761
KB
135 if (ce_mode_s_ifmt == S_IFLNK && has_symlinks && !to_tempfile) {
136 ret = symlink(new, path);
137 free(new);
138 if (ret)
d43e9073 139 return error("unable to create symlink %s (%s)",
4857c761
KB
140 path, strerror(errno));
141 break;
142 }
143
1a9d7e9b
JH
144 /*
145 * Convert from git internal format to working tree format
146 */
4857c761
KB
147 if (ce_mode_s_ifmt == S_IFREG &&
148 convert_to_working_tree(ce->name, new, size, &buf)) {
1a9d7e9b 149 free(new);
c32f749f
RS
150 new = strbuf_detach(&buf, &newsize);
151 size = newsize;
1a9d7e9b
JH
152 }
153
fd5db55d 154 fd = open_output_fd(path, ce, to_tempfile);
12dccc16
LT
155 if (fd < 0) {
156 free(new);
d43e9073 157 return error("unable to create file %s (%s)",
12dccc16
LT
158 path, strerror(errno));
159 }
6c510bee 160
93822c22 161 wrote = write_in_full(fd, new, size);
fd5db55d
JH
162 if (!to_tempfile)
163 fstat_done = fstat_output(fd, state, &st);
12dccc16
LT
164 close(fd);
165 free(new);
166 if (wrote != size)
d43e9073 167 return error("unable to write file %s", path);
12dccc16 168 break;
302b9282 169 case S_IFGITLINK:
f0807e62 170 if (to_tempfile)
d43e9073 171 return error("cannot create temporary subproject %s", path);
f0807e62 172 if (mkdir(path, 0777) < 0)
d43e9073 173 return error("cannot create subproject directory %s", path);
f0807e62 174 break;
12dccc16 175 default:
d43e9073 176 return error("unknown file mode for %s in index", path);
12dccc16
LT
177 }
178
6ee67f26 179 if (state->refresh_cache) {
e4c72923
KB
180 if (!fstat_done)
181 lstat(ce->name, &st);
12dccc16
LT
182 fill_stat_cache_info(ce, &st);
183 }
184 return 0;
185}
186
b6986d8a
LT
187/*
188 * This is like 'lstat()', except it refuses to follow symlinks
da02ca50 189 * in the path, after skipping "skiplen".
b6986d8a 190 */
61b97df7 191static int check_path(const char *path, int len, struct stat *st, int skiplen)
b6986d8a 192{
da02ca50
JH
193 const char *slash = path + len;
194
195 while (path < slash && *slash != '/')
196 slash--;
197 if (!has_dirs_only_path(path, slash - path, skiplen)) {
b6986d8a
LT
198 errno = ENOENT;
199 return -1;
200 }
201 return lstat(path, st);
202}
203
efbc5831 204int checkout_entry(struct cache_entry *ce, const struct checkout *state, char *topath)
12dccc16 205{
095c424d 206 static char path[PATH_MAX + 1];
de84f99c 207 struct stat st;
12dccc16
LT
208 int len = state->base_dir_len;
209
de84f99c
SP
210 if (topath)
211 return write_entry(ce, topath, state, 1);
212
12dccc16
LT
213 memcpy(path, state->base_dir, len);
214 strcpy(path + len, ce->name);
81a9aa60 215 len += ce_namelen(ce);
12dccc16 216
da02ca50 217 if (!check_path(path, len, &st, state->base_dir_len)) {
56cac48c 218 unsigned changed = ce_match_stat(ce, &st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE);
12dccc16
LT
219 if (!changed)
220 return 0;
221 if (!state->force) {
222 if (!state->quiet)
d43e9073 223 fprintf(stderr, "%s already exists, no checkout\n", path);
4b12dae6 224 return -1;
12dccc16
LT
225 }
226
227 /*
228 * We unlink the old file, to get the new one with the
229 * right permissions (including umask, which is nasty
230 * to emulate by hand - much easier to let the system
231 * just do the right thing)
232 */
d48a72f3 233 if (S_ISDIR(st.st_mode)) {
f0807e62 234 /* If it is a gitlink, leave it alone! */
7a51ed66 235 if (S_ISGITLINK(ce->ce_mode))
f0807e62 236 return 0;
d48a72f3
LT
237 if (!state->force)
238 return error("%s is a directory", path);
239 remove_subtree(path);
971f229c
LT
240 } else if (unlink(path))
241 return error("unable to unlink old '%s' (%s)", path, strerror(errno));
de84f99c 242 } else if (state->not_new)
12dccc16 243 return 0;
81a9aa60 244 create_directories(path, len, state);
de84f99c 245 return write_entry(ce, path, state, 0);
12dccc16 246}