Blame


1 86c3caaf 2018-03-09 stsp /*
2 86c3caaf 2018-03-09 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 86c3caaf 2018-03-09 stsp *
4 86c3caaf 2018-03-09 stsp * Permission to use, copy, modify, and distribute this software for any
5 86c3caaf 2018-03-09 stsp * purpose with or without fee is hereby granted, provided that the above
6 86c3caaf 2018-03-09 stsp * copyright notice and this permission notice appear in all copies.
7 86c3caaf 2018-03-09 stsp *
8 86c3caaf 2018-03-09 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 86c3caaf 2018-03-09 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 86c3caaf 2018-03-09 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 86c3caaf 2018-03-09 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 86c3caaf 2018-03-09 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 86c3caaf 2018-03-09 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 86c3caaf 2018-03-09 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 86c3caaf 2018-03-09 stsp */
16 86c3caaf 2018-03-09 stsp
17 86c3caaf 2018-03-09 stsp #include <sys/stat.h>
18 6d9d28c3 2018-03-11 stsp #include <sys/limits.h>
19 9d31a1d8 2018-03-11 stsp #include <sys/queue.h>
20 86c3caaf 2018-03-09 stsp
21 f5c49f82 2019-01-06 stsp #include <stddef.h>
22 86c3caaf 2018-03-09 stsp #include <string.h>
23 86c3caaf 2018-03-09 stsp #include <stdio.h>
24 86c3caaf 2018-03-09 stsp #include <stdlib.h>
25 86c3caaf 2018-03-09 stsp #include <fcntl.h>
26 86c3caaf 2018-03-09 stsp #include <errno.h>
27 86c3caaf 2018-03-09 stsp #include <unistd.h>
28 9d31a1d8 2018-03-11 stsp #include <sha1.h>
29 9d31a1d8 2018-03-11 stsp #include <zlib.h>
30 9d31a1d8 2018-03-11 stsp #include <fnmatch.h>
31 512f0d0e 2019-01-02 stsp #include <libgen.h>
32 86c3caaf 2018-03-09 stsp
33 86c3caaf 2018-03-09 stsp #include "got_error.h"
34 86c3caaf 2018-03-09 stsp #include "got_repository.h"
35 5261c201 2018-04-01 stsp #include "got_reference.h"
36 9d31a1d8 2018-03-11 stsp #include "got_object.h"
37 86c3caaf 2018-03-09 stsp #include "got_worktree.h"
38 511a516b 2018-05-19 stsp #include "got_opentemp.h"
39 86c3caaf 2018-03-09 stsp
40 718b3ab0 2018-03-17 stsp #include "got_lib_worktree.h"
41 718b3ab0 2018-03-17 stsp #include "got_lib_path.h"
42 718b3ab0 2018-03-17 stsp #include "got_lib_sha1.h"
43 718b3ab0 2018-03-17 stsp #include "got_lib_fileindex.h"
44 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
45 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
46 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
47 9d31a1d8 2018-03-11 stsp
48 9d31a1d8 2018-03-11 stsp #ifndef MIN
49 9d31a1d8 2018-03-11 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
50 9d31a1d8 2018-03-11 stsp #endif
51 86c3caaf 2018-03-09 stsp
52 99724ed4 2018-03-10 stsp static const struct got_error *
53 7ac97322 2018-03-11 stsp create_meta_file(const char *path_got, const char *name, const char *content)
54 99724ed4 2018-03-10 stsp {
55 99724ed4 2018-03-10 stsp const struct got_error *err = NULL;
56 99724ed4 2018-03-10 stsp char *path;
57 99724ed4 2018-03-10 stsp int fd = -1;
58 99724ed4 2018-03-10 stsp
59 7ac97322 2018-03-11 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
60 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
61 99724ed4 2018-03-10 stsp path = NULL;
62 99724ed4 2018-03-10 stsp goto done;
63 99724ed4 2018-03-10 stsp }
64 99724ed4 2018-03-10 stsp
65 73a5ef67 2018-03-11 stsp fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
66 99724ed4 2018-03-10 stsp GOT_DEFAULT_FILE_MODE);
67 99724ed4 2018-03-10 stsp if (fd == -1) {
68 99724ed4 2018-03-10 stsp err = got_error_from_errno();
69 99724ed4 2018-03-10 stsp goto done;
70 99724ed4 2018-03-10 stsp }
71 99724ed4 2018-03-10 stsp
72 99724ed4 2018-03-10 stsp if (content) {
73 99724ed4 2018-03-10 stsp int len = dprintf(fd, "%s\n", content);
74 99724ed4 2018-03-10 stsp if (len != strlen(content) + 1) {
75 99724ed4 2018-03-10 stsp err = got_error_from_errno();
76 99724ed4 2018-03-10 stsp goto done;
77 99724ed4 2018-03-10 stsp }
78 99724ed4 2018-03-10 stsp }
79 99724ed4 2018-03-10 stsp
80 99724ed4 2018-03-10 stsp done:
81 99724ed4 2018-03-10 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
82 99724ed4 2018-03-10 stsp err = got_error_from_errno();
83 99724ed4 2018-03-10 stsp free(path);
84 507dc3bb 2018-12-29 stsp return err;
85 507dc3bb 2018-12-29 stsp }
86 507dc3bb 2018-12-29 stsp
87 507dc3bb 2018-12-29 stsp static const struct got_error *
88 507dc3bb 2018-12-29 stsp update_meta_file(const char *path_got, const char *name, const char *content)
89 507dc3bb 2018-12-29 stsp {
90 507dc3bb 2018-12-29 stsp const struct got_error *err = NULL;
91 507dc3bb 2018-12-29 stsp FILE *tmpfile = NULL;
92 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
93 507dc3bb 2018-12-29 stsp char *path = NULL;
94 507dc3bb 2018-12-29 stsp
95 507dc3bb 2018-12-29 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
96 507dc3bb 2018-12-29 stsp err = got_error_from_errno();
97 507dc3bb 2018-12-29 stsp path = NULL;
98 507dc3bb 2018-12-29 stsp goto done;
99 507dc3bb 2018-12-29 stsp }
100 507dc3bb 2018-12-29 stsp
101 507dc3bb 2018-12-29 stsp err = got_opentemp_named(&tmppath, &tmpfile, path);
102 507dc3bb 2018-12-29 stsp if (err)
103 507dc3bb 2018-12-29 stsp goto done;
104 507dc3bb 2018-12-29 stsp
105 507dc3bb 2018-12-29 stsp if (content) {
106 507dc3bb 2018-12-29 stsp int len = fprintf(tmpfile, "%s\n", content);
107 507dc3bb 2018-12-29 stsp if (len != strlen(content) + 1) {
108 507dc3bb 2018-12-29 stsp err = got_error_from_errno();
109 507dc3bb 2018-12-29 stsp goto done;
110 507dc3bb 2018-12-29 stsp }
111 507dc3bb 2018-12-29 stsp }
112 507dc3bb 2018-12-29 stsp
113 507dc3bb 2018-12-29 stsp if (rename(tmppath, path) != 0) {
114 507dc3bb 2018-12-29 stsp err = got_error_from_errno();
115 507dc3bb 2018-12-29 stsp goto done;
116 507dc3bb 2018-12-29 stsp }
117 507dc3bb 2018-12-29 stsp
118 507dc3bb 2018-12-29 stsp done:
119 507dc3bb 2018-12-29 stsp free(tmppath);
120 507dc3bb 2018-12-29 stsp fclose(tmpfile);
121 99724ed4 2018-03-10 stsp return err;
122 99724ed4 2018-03-10 stsp }
123 99724ed4 2018-03-10 stsp
124 09fe317a 2018-03-11 stsp static const struct got_error *
125 7ac97322 2018-03-11 stsp read_meta_file(char **content, const char *path_got, const char *name)
126 09fe317a 2018-03-11 stsp {
127 09fe317a 2018-03-11 stsp const struct got_error *err = NULL;
128 09fe317a 2018-03-11 stsp char *path;
129 09fe317a 2018-03-11 stsp int fd = -1;
130 09fe317a 2018-03-11 stsp ssize_t n;
131 09fe317a 2018-03-11 stsp struct stat sb;
132 09fe317a 2018-03-11 stsp
133 09fe317a 2018-03-11 stsp *content = NULL;
134 09fe317a 2018-03-11 stsp
135 7ac97322 2018-03-11 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
136 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
137 09fe317a 2018-03-11 stsp path = NULL;
138 09fe317a 2018-03-11 stsp goto done;
139 09fe317a 2018-03-11 stsp }
140 09fe317a 2018-03-11 stsp
141 ef99fdb1 2018-03-11 stsp fd = open(path, O_RDONLY | O_NOFOLLOW);
142 09fe317a 2018-03-11 stsp if (fd == -1) {
143 ef99fdb1 2018-03-11 stsp err = got_error_from_errno();
144 ef99fdb1 2018-03-11 stsp goto done;
145 ef99fdb1 2018-03-11 stsp }
146 ef99fdb1 2018-03-11 stsp if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
147 73a5ef67 2018-03-11 stsp err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
148 73a5ef67 2018-03-11 stsp : got_error_from_errno());
149 09fe317a 2018-03-11 stsp goto done;
150 09fe317a 2018-03-11 stsp }
151 09fe317a 2018-03-11 stsp
152 09fe317a 2018-03-11 stsp stat(path, &sb);
153 09fe317a 2018-03-11 stsp *content = calloc(1, sb.st_size);
154 09fe317a 2018-03-11 stsp if (*content == NULL) {
155 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
156 09fe317a 2018-03-11 stsp goto done;
157 09fe317a 2018-03-11 stsp }
158 09fe317a 2018-03-11 stsp
159 09fe317a 2018-03-11 stsp n = read(fd, *content, sb.st_size);
160 09fe317a 2018-03-11 stsp if (n != sb.st_size) {
161 0605801d 2018-03-11 stsp err = (n == -1 ? got_error_from_errno() :
162 0605801d 2018-03-11 stsp got_error(GOT_ERR_WORKTREE_META));
163 09fe317a 2018-03-11 stsp goto done;
164 09fe317a 2018-03-11 stsp }
165 09fe317a 2018-03-11 stsp if ((*content)[sb.st_size - 1] != '\n') {
166 09fe317a 2018-03-11 stsp err = got_error(GOT_ERR_WORKTREE_META);
167 09fe317a 2018-03-11 stsp goto done;
168 09fe317a 2018-03-11 stsp }
169 09fe317a 2018-03-11 stsp (*content)[sb.st_size - 1] = '\0';
170 09fe317a 2018-03-11 stsp
171 09fe317a 2018-03-11 stsp done:
172 09fe317a 2018-03-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
173 09fe317a 2018-03-11 stsp err = got_error_from_errno();
174 09fe317a 2018-03-11 stsp free(path);
175 09fe317a 2018-03-11 stsp if (err) {
176 09fe317a 2018-03-11 stsp free(*content);
177 09fe317a 2018-03-11 stsp *content = NULL;
178 09fe317a 2018-03-11 stsp }
179 09fe317a 2018-03-11 stsp return err;
180 09fe317a 2018-03-11 stsp }
181 09fe317a 2018-03-11 stsp
182 86c3caaf 2018-03-09 stsp const struct got_error *
183 86c3caaf 2018-03-09 stsp got_worktree_init(const char *path, struct got_reference *head_ref,
184 577ec78f 2018-03-11 stsp const char *prefix, struct got_repository *repo)
185 86c3caaf 2018-03-09 stsp {
186 86c3caaf 2018-03-09 stsp const struct got_error *err = NULL;
187 65596e15 2018-12-24 stsp struct got_object_id *commit_id = NULL;
188 65596e15 2018-12-24 stsp int obj_type;
189 7ac97322 2018-03-11 stsp char *path_got = NULL;
190 08d425ea 2018-12-24 stsp char *refstr = NULL;
191 1451e70d 2018-03-10 stsp char *formatstr = NULL;
192 0bb8a95e 2018-03-12 stsp char *absprefix = NULL;
193 65596e15 2018-12-24 stsp char *basestr = NULL;
194 65596e15 2018-12-24 stsp
195 65596e15 2018-12-24 stsp err = got_ref_resolve(&commit_id, repo, head_ref);
196 65596e15 2018-12-24 stsp if (err)
197 65596e15 2018-12-24 stsp return err;
198 65596e15 2018-12-24 stsp err = got_object_get_type(&obj_type, repo, commit_id);
199 65596e15 2018-12-24 stsp if (err)
200 65596e15 2018-12-24 stsp return err;
201 65596e15 2018-12-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
202 65596e15 2018-12-24 stsp return got_error(GOT_ERR_OBJ_TYPE);
203 86c3caaf 2018-03-09 stsp
204 0bb8a95e 2018-03-12 stsp if (!got_path_is_absolute(prefix)) {
205 0bb8a95e 2018-03-12 stsp if (asprintf(&absprefix, "/%s", prefix) == -1)
206 0a585a0d 2018-03-17 stsp return got_error_from_errno();
207 0bb8a95e 2018-03-12 stsp }
208 577ec78f 2018-03-11 stsp
209 86c3caaf 2018-03-09 stsp /* Create top-level directory (may already exist). */
210 2cb4bacb 2018-03-11 stsp if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
211 86c3caaf 2018-03-09 stsp err = got_error_from_errno();
212 86c3caaf 2018-03-09 stsp goto done;
213 86c3caaf 2018-03-09 stsp }
214 86c3caaf 2018-03-09 stsp
215 86c3caaf 2018-03-09 stsp /* Create .got directory (may already exist). */
216 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
217 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
218 86c3caaf 2018-03-09 stsp goto done;
219 86c3caaf 2018-03-09 stsp }
220 7ac97322 2018-03-11 stsp if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
221 86c3caaf 2018-03-09 stsp err = got_error_from_errno();
222 86c3caaf 2018-03-09 stsp goto done;
223 86c3caaf 2018-03-09 stsp }
224 86c3caaf 2018-03-09 stsp
225 056e7441 2018-03-11 stsp /* Create an empty lock file. */
226 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
227 056e7441 2018-03-11 stsp if (err)
228 056e7441 2018-03-11 stsp goto done;
229 056e7441 2018-03-11 stsp
230 86c3caaf 2018-03-09 stsp /* Create an empty file index. */
231 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
232 99724ed4 2018-03-10 stsp if (err)
233 86c3caaf 2018-03-09 stsp goto done;
234 86c3caaf 2018-03-09 stsp
235 08d425ea 2018-12-24 stsp /* Write the HEAD reference. */
236 08d425ea 2018-12-24 stsp refstr = got_ref_to_str(head_ref);
237 08d425ea 2018-12-24 stsp if (refstr == NULL) {
238 a1a7858a 2018-12-24 stsp err = got_error_from_errno();
239 a1a7858a 2018-12-24 stsp goto done;
240 a1a7858a 2018-12-24 stsp }
241 0f92850e 2018-12-25 stsp err = create_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
242 65596e15 2018-12-24 stsp if (err)
243 65596e15 2018-12-24 stsp goto done;
244 65596e15 2018-12-24 stsp
245 65596e15 2018-12-24 stsp /* Record our base commit. */
246 65596e15 2018-12-24 stsp err = got_object_id_str(&basestr, commit_id);
247 65596e15 2018-12-24 stsp if (err)
248 65596e15 2018-12-24 stsp goto done;
249 0f92850e 2018-12-25 stsp err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
250 99724ed4 2018-03-10 stsp if (err)
251 86c3caaf 2018-03-09 stsp goto done;
252 86c3caaf 2018-03-09 stsp
253 1451e70d 2018-03-10 stsp /* Store path to repository. */
254 7839bc15 2019-01-06 stsp err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
255 7839bc15 2019-01-06 stsp got_repo_get_path(repo));
256 99724ed4 2018-03-10 stsp if (err)
257 86c3caaf 2018-03-09 stsp goto done;
258 86c3caaf 2018-03-09 stsp
259 577ec78f 2018-03-11 stsp /* Store in-repository path prefix. */
260 0bb8a95e 2018-03-12 stsp err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
261 0bb8a95e 2018-03-12 stsp absprefix ? absprefix : prefix);
262 577ec78f 2018-03-11 stsp if (err)
263 577ec78f 2018-03-11 stsp goto done;
264 577ec78f 2018-03-11 stsp
265 9dce68ed 2018-03-10 stsp /* Stamp work tree with format file. */
266 1451e70d 2018-03-10 stsp if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
267 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
268 1451e70d 2018-03-10 stsp goto done;
269 1451e70d 2018-03-10 stsp }
270 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
271 99724ed4 2018-03-10 stsp if (err)
272 1451e70d 2018-03-10 stsp goto done;
273 1451e70d 2018-03-10 stsp
274 86c3caaf 2018-03-09 stsp done:
275 65596e15 2018-12-24 stsp free(commit_id);
276 7ac97322 2018-03-11 stsp free(path_got);
277 1451e70d 2018-03-10 stsp free(formatstr);
278 08d425ea 2018-12-24 stsp free(refstr);
279 0bb8a95e 2018-03-12 stsp free(absprefix);
280 65596e15 2018-12-24 stsp free(basestr);
281 86c3caaf 2018-03-09 stsp return err;
282 86c3caaf 2018-03-09 stsp }
283 86c3caaf 2018-03-09 stsp
284 86c3caaf 2018-03-09 stsp const struct got_error *
285 86c3caaf 2018-03-09 stsp got_worktree_open(struct got_worktree **worktree, const char *path)
286 86c3caaf 2018-03-09 stsp {
287 6d9d28c3 2018-03-11 stsp const struct got_error *err = NULL;
288 7ac97322 2018-03-11 stsp char *path_got;
289 6d9d28c3 2018-03-11 stsp char *formatstr = NULL;
290 056e7441 2018-03-11 stsp char *path_lock = NULL;
291 eaccb85f 2018-12-25 stsp char *base_commit_id_str = NULL;
292 271d2a38 2018-12-25 stsp char *head_ref_str = NULL;
293 6d9d28c3 2018-03-11 stsp int version, fd = -1;
294 6d9d28c3 2018-03-11 stsp const char *errstr;
295 eaccb85f 2018-12-25 stsp struct got_repository *repo = NULL;
296 6d9d28c3 2018-03-11 stsp
297 6d9d28c3 2018-03-11 stsp *worktree = NULL;
298 6d9d28c3 2018-03-11 stsp
299 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
300 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
301 7ac97322 2018-03-11 stsp path_got = NULL;
302 6d9d28c3 2018-03-11 stsp goto done;
303 6d9d28c3 2018-03-11 stsp }
304 6d9d28c3 2018-03-11 stsp
305 7ac97322 2018-03-11 stsp if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
306 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
307 056e7441 2018-03-11 stsp path_lock = NULL;
308 6d9d28c3 2018-03-11 stsp goto done;
309 6d9d28c3 2018-03-11 stsp }
310 6d9d28c3 2018-03-11 stsp
311 056e7441 2018-03-11 stsp fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
312 6d9d28c3 2018-03-11 stsp if (fd == -1) {
313 73a5ef67 2018-03-11 stsp err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
314 73a5ef67 2018-03-11 stsp : got_error_from_errno());
315 6d9d28c3 2018-03-11 stsp goto done;
316 6d9d28c3 2018-03-11 stsp }
317 6d9d28c3 2018-03-11 stsp
318 7ac97322 2018-03-11 stsp err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
319 6d9d28c3 2018-03-11 stsp if (err)
320 6d9d28c3 2018-03-11 stsp goto done;
321 6d9d28c3 2018-03-11 stsp
322 6d9d28c3 2018-03-11 stsp version = strtonum(formatstr, 1, INT_MAX, &errstr);
323 6d9d28c3 2018-03-11 stsp if (errstr) {
324 6d9d28c3 2018-03-11 stsp err = got_error(GOT_ERR_WORKTREE_META);
325 6d9d28c3 2018-03-11 stsp goto done;
326 6d9d28c3 2018-03-11 stsp }
327 6d9d28c3 2018-03-11 stsp if (version != GOT_WORKTREE_FORMAT_VERSION) {
328 6d9d28c3 2018-03-11 stsp err = got_error(GOT_ERR_WORKTREE_VERS);
329 6d9d28c3 2018-03-11 stsp goto done;
330 6d9d28c3 2018-03-11 stsp }
331 6d9d28c3 2018-03-11 stsp
332 6d9d28c3 2018-03-11 stsp *worktree = calloc(1, sizeof(**worktree));
333 6d9d28c3 2018-03-11 stsp if (*worktree == NULL) {
334 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
335 6d9d28c3 2018-03-11 stsp goto done;
336 6d9d28c3 2018-03-11 stsp }
337 056e7441 2018-03-11 stsp (*worktree)->lockfd = -1;
338 6d9d28c3 2018-03-11 stsp
339 c88eb298 2018-03-11 stsp (*worktree)->root_path = strdup(path);
340 c88eb298 2018-03-11 stsp if ((*worktree)->root_path == NULL) {
341 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
342 6d9d28c3 2018-03-11 stsp goto done;
343 6d9d28c3 2018-03-11 stsp }
344 cde76477 2018-03-11 stsp err = read_meta_file(&(*worktree)->repo_path, path_got,
345 6d9d28c3 2018-03-11 stsp GOT_WORKTREE_REPOSITORY);
346 6d9d28c3 2018-03-11 stsp if (err)
347 6d9d28c3 2018-03-11 stsp goto done;
348 eaccb85f 2018-12-25 stsp
349 7ac97322 2018-03-11 stsp err = read_meta_file(&(*worktree)->path_prefix, path_got,
350 6d9d28c3 2018-03-11 stsp GOT_WORKTREE_PATH_PREFIX);
351 93a30277 2018-12-24 stsp if (err)
352 93a30277 2018-12-24 stsp goto done;
353 93a30277 2018-12-24 stsp
354 eaccb85f 2018-12-25 stsp err = read_meta_file(&base_commit_id_str, path_got,
355 0f92850e 2018-12-25 stsp GOT_WORKTREE_BASE_COMMIT);
356 eaccb85f 2018-12-25 stsp if (err)
357 eaccb85f 2018-12-25 stsp goto done;
358 eaccb85f 2018-12-25 stsp
359 eaccb85f 2018-12-25 stsp err = got_repo_open(&repo, (*worktree)->repo_path);
360 eaccb85f 2018-12-25 stsp if (err)
361 eaccb85f 2018-12-25 stsp goto done;
362 eaccb85f 2018-12-25 stsp
363 eaccb85f 2018-12-25 stsp err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
364 eaccb85f 2018-12-25 stsp base_commit_id_str);
365 f5baf295 2018-03-11 stsp if (err)
366 f5baf295 2018-03-11 stsp goto done;
367 f5baf295 2018-03-11 stsp
368 271d2a38 2018-12-25 stsp err = read_meta_file(&head_ref_str, path_got, GOT_WORKTREE_HEAD_REF);
369 f5baf295 2018-03-11 stsp if (err)
370 6d9d28c3 2018-03-11 stsp goto done;
371 6d9d28c3 2018-03-11 stsp
372 271d2a38 2018-12-25 stsp err = got_ref_open(&(*worktree)->head_ref, repo, head_ref_str);
373 6d9d28c3 2018-03-11 stsp done:
374 eaccb85f 2018-12-25 stsp if (repo)
375 eaccb85f 2018-12-25 stsp got_repo_close(repo);
376 7ac97322 2018-03-11 stsp free(path_got);
377 056e7441 2018-03-11 stsp free(path_lock);
378 271d2a38 2018-12-25 stsp free(head_ref_str);
379 eaccb85f 2018-12-25 stsp free(base_commit_id_str);
380 6d9d28c3 2018-03-11 stsp if (err) {
381 6d9d28c3 2018-03-11 stsp if (fd != -1)
382 6d9d28c3 2018-03-11 stsp close(fd);
383 6d9d28c3 2018-03-11 stsp if (*worktree != NULL)
384 6d9d28c3 2018-03-11 stsp got_worktree_close(*worktree);
385 6d9d28c3 2018-03-11 stsp *worktree = NULL;
386 6d9d28c3 2018-03-11 stsp } else
387 056e7441 2018-03-11 stsp (*worktree)->lockfd = fd;
388 6d9d28c3 2018-03-11 stsp
389 6d9d28c3 2018-03-11 stsp return err;
390 86c3caaf 2018-03-09 stsp }
391 86c3caaf 2018-03-09 stsp
392 86c3caaf 2018-03-09 stsp void
393 86c3caaf 2018-03-09 stsp got_worktree_close(struct got_worktree *worktree)
394 86c3caaf 2018-03-09 stsp {
395 c88eb298 2018-03-11 stsp free(worktree->root_path);
396 cde76477 2018-03-11 stsp free(worktree->repo_path);
397 6d9d28c3 2018-03-11 stsp free(worktree->path_prefix);
398 eaccb85f 2018-12-25 stsp free(worktree->base_commit_id);
399 271d2a38 2018-12-25 stsp if (worktree->head_ref)
400 271d2a38 2018-12-25 stsp got_ref_close(worktree->head_ref);
401 056e7441 2018-03-11 stsp if (worktree->lockfd != -1)
402 056e7441 2018-03-11 stsp close(worktree->lockfd);
403 6d9d28c3 2018-03-11 stsp free(worktree);
404 86c3caaf 2018-03-09 stsp }
405 86c3caaf 2018-03-09 stsp
406 2fbdb5ae 2018-12-29 stsp const char *
407 86c3caaf 2018-03-09 stsp got_worktree_get_repo_path(struct got_worktree *worktree)
408 86c3caaf 2018-03-09 stsp {
409 2fbdb5ae 2018-12-29 stsp return worktree->repo_path;
410 49520a32 2018-12-29 stsp }
411 49520a32 2018-12-29 stsp
412 49520a32 2018-12-29 stsp const char *
413 49520a32 2018-12-29 stsp got_worktree_get_path_prefix(struct got_worktree *worktree)
414 49520a32 2018-12-29 stsp {
415 f609be2e 2018-12-29 stsp return worktree->path_prefix;
416 e5dc7198 2018-12-29 stsp }
417 e5dc7198 2018-12-29 stsp
418 e5dc7198 2018-12-29 stsp const struct got_error *
419 e5dc7198 2018-12-29 stsp got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
420 e5dc7198 2018-12-29 stsp const char *path_prefix)
421 e5dc7198 2018-12-29 stsp {
422 e5dc7198 2018-12-29 stsp char *absprefix = NULL;
423 e5dc7198 2018-12-29 stsp
424 e5dc7198 2018-12-29 stsp if (!got_path_is_absolute(path_prefix)) {
425 e5dc7198 2018-12-29 stsp if (asprintf(&absprefix, "/%s", path_prefix) == -1)
426 e5dc7198 2018-12-29 stsp return got_error_from_errno();
427 e5dc7198 2018-12-29 stsp }
428 e5dc7198 2018-12-29 stsp *match = (strcmp(absprefix ? absprefix : path_prefix,
429 e5dc7198 2018-12-29 stsp worktree->path_prefix) == 0);
430 e5dc7198 2018-12-29 stsp free(absprefix);
431 e5dc7198 2018-12-29 stsp return NULL;
432 86c3caaf 2018-03-09 stsp }
433 86c3caaf 2018-03-09 stsp
434 35be1456 2018-03-11 stsp char *
435 35be1456 2018-03-11 stsp got_worktree_get_head_ref_name(struct got_worktree *worktree)
436 86c3caaf 2018-03-09 stsp {
437 271d2a38 2018-12-25 stsp return got_ref_to_str(worktree->head_ref);
438 be7061eb 2018-12-30 stsp }
439 be7061eb 2018-12-30 stsp
440 be7061eb 2018-12-30 stsp struct got_reference *
441 be7061eb 2018-12-30 stsp got_worktree_get_head_ref(struct got_worktree *worktree)
442 be7061eb 2018-12-30 stsp {
443 be7061eb 2018-12-30 stsp return got_ref_dup(worktree->head_ref);
444 507dc3bb 2018-12-29 stsp }
445 507dc3bb 2018-12-29 stsp
446 507dc3bb 2018-12-29 stsp const struct got_object_id *
447 507dc3bb 2018-12-29 stsp got_worktree_get_base_commit_id(struct got_worktree *worktree)
448 507dc3bb 2018-12-29 stsp {
449 507dc3bb 2018-12-29 stsp return worktree->base_commit_id;
450 86c3caaf 2018-03-09 stsp }
451 86c3caaf 2018-03-09 stsp
452 507dc3bb 2018-12-29 stsp const struct got_error *
453 507dc3bb 2018-12-29 stsp got_worktree_set_base_commit_id(struct got_worktree *worktree,
454 507dc3bb 2018-12-29 stsp struct got_repository *repo, struct got_object_id *commit_id)
455 507dc3bb 2018-12-29 stsp {
456 507dc3bb 2018-12-29 stsp const struct got_error *err;
457 507dc3bb 2018-12-29 stsp struct got_object *obj = NULL;
458 507dc3bb 2018-12-29 stsp char *id_str = NULL;
459 507dc3bb 2018-12-29 stsp char *path_got = NULL;
460 507dc3bb 2018-12-29 stsp
461 507dc3bb 2018-12-29 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
462 507dc3bb 2018-12-29 stsp GOT_WORKTREE_GOT_DIR) == -1) {
463 507dc3bb 2018-12-29 stsp err = got_error_from_errno();
464 507dc3bb 2018-12-29 stsp path_got = NULL;
465 507dc3bb 2018-12-29 stsp goto done;
466 507dc3bb 2018-12-29 stsp }
467 507dc3bb 2018-12-29 stsp
468 507dc3bb 2018-12-29 stsp err = got_object_open(&obj, repo, commit_id);
469 507dc3bb 2018-12-29 stsp if (err)
470 507dc3bb 2018-12-29 stsp return err;
471 507dc3bb 2018-12-29 stsp
472 507dc3bb 2018-12-29 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT) {
473 507dc3bb 2018-12-29 stsp err = got_error(GOT_ERR_OBJ_TYPE);
474 507dc3bb 2018-12-29 stsp goto done;
475 507dc3bb 2018-12-29 stsp }
476 507dc3bb 2018-12-29 stsp
477 507dc3bb 2018-12-29 stsp /* Record our base commit. */
478 507dc3bb 2018-12-29 stsp err = got_object_id_str(&id_str, commit_id);
479 507dc3bb 2018-12-29 stsp if (err)
480 507dc3bb 2018-12-29 stsp goto done;
481 507dc3bb 2018-12-29 stsp err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
482 507dc3bb 2018-12-29 stsp if (err)
483 507dc3bb 2018-12-29 stsp goto done;
484 507dc3bb 2018-12-29 stsp
485 507dc3bb 2018-12-29 stsp free(worktree->base_commit_id);
486 507dc3bb 2018-12-29 stsp worktree->base_commit_id = got_object_id_dup(commit_id);
487 507dc3bb 2018-12-29 stsp if (worktree->base_commit_id == NULL) {
488 507dc3bb 2018-12-29 stsp err = got_error_from_errno();
489 507dc3bb 2018-12-29 stsp goto done;
490 507dc3bb 2018-12-29 stsp }
491 507dc3bb 2018-12-29 stsp done:
492 507dc3bb 2018-12-29 stsp if (obj)
493 507dc3bb 2018-12-29 stsp got_object_close(obj);
494 507dc3bb 2018-12-29 stsp free(id_str);
495 507dc3bb 2018-12-29 stsp free(path_got);
496 507dc3bb 2018-12-29 stsp return err;
497 507dc3bb 2018-12-29 stsp }
498 507dc3bb 2018-12-29 stsp
499 9d31a1d8 2018-03-11 stsp static const struct got_error *
500 9d31a1d8 2018-03-11 stsp lock_worktree(struct got_worktree *worktree, int operation)
501 86c3caaf 2018-03-09 stsp {
502 9d31a1d8 2018-03-11 stsp if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
503 9d31a1d8 2018-03-11 stsp return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
504 9d31a1d8 2018-03-11 stsp : got_error_from_errno());
505 86c3caaf 2018-03-09 stsp return NULL;
506 86c3caaf 2018-03-09 stsp }
507 9d31a1d8 2018-03-11 stsp
508 9d31a1d8 2018-03-11 stsp static const char *
509 9d31a1d8 2018-03-11 stsp apply_path_prefix(struct got_worktree *worktree, const char *path)
510 9d31a1d8 2018-03-11 stsp {
511 9d31a1d8 2018-03-11 stsp const char *p = path;
512 9d31a1d8 2018-03-11 stsp p += strlen(worktree->path_prefix);
513 9d31a1d8 2018-03-11 stsp if (*p == '/')
514 9d31a1d8 2018-03-11 stsp p++;
515 9d31a1d8 2018-03-11 stsp return p;
516 9d31a1d8 2018-03-11 stsp }
517 9d31a1d8 2018-03-11 stsp
518 9d31a1d8 2018-03-11 stsp static const struct got_error *
519 a207cf0a 2018-12-29 stsp blob_checkout(struct got_worktree *worktree, struct got_fileindex *fileindex,
520 eac9755f 2018-12-29 stsp struct got_fileindex_entry *entry, const char *path,
521 eac9755f 2018-12-29 stsp struct got_blob_object *blob, struct got_repository *repo,
522 d7b62c98 2018-12-27 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
523 d7b62c98 2018-12-27 stsp const char *progress_path)
524 9d31a1d8 2018-03-11 stsp {
525 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
526 c34b20a2 2018-03-12 stsp char *ondisk_path;
527 507dc3bb 2018-12-29 stsp int fd = -1;
528 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
529 507dc3bb 2018-12-29 stsp int update = 0;
530 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
531 9d31a1d8 2018-03-11 stsp
532 c34b20a2 2018-03-12 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path,
533 9d31a1d8 2018-03-11 stsp apply_path_prefix(worktree, path)) == -1)
534 0a585a0d 2018-03-17 stsp return got_error_from_errno();
535 144ad43a 2018-12-29 stsp
536 c34b20a2 2018-03-12 stsp fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
537 9d31a1d8 2018-03-11 stsp GOT_DEFAULT_FILE_MODE);
538 9d31a1d8 2018-03-11 stsp if (fd == -1) {
539 9d31a1d8 2018-03-11 stsp err = got_error_from_errno();
540 9d31a1d8 2018-03-11 stsp if (errno == EEXIST) {
541 9d31a1d8 2018-03-11 stsp struct stat sb;
542 c34b20a2 2018-03-12 stsp if (lstat(ondisk_path, &sb) == -1) {
543 9d31a1d8 2018-03-11 stsp err = got_error_from_errno();
544 507dc3bb 2018-12-29 stsp goto done;
545 9d31a1d8 2018-03-11 stsp } else if (!S_ISREG(sb.st_mode)) {
546 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
547 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_FILE_OBSTRUCTED);
548 507dc3bb 2018-12-29 stsp goto done;
549 d70b8e30 2018-12-27 stsp } else {
550 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
551 507dc3bb 2018-12-29 stsp ondisk_path);
552 507dc3bb 2018-12-29 stsp if (err)
553 507dc3bb 2018-12-29 stsp goto done;
554 507dc3bb 2018-12-29 stsp update = 1;
555 9d31a1d8 2018-03-11 stsp }
556 507dc3bb 2018-12-29 stsp } else
557 507dc3bb 2018-12-29 stsp return err;
558 9d31a1d8 2018-03-11 stsp }
559 9d31a1d8 2018-03-11 stsp
560 507dc3bb 2018-12-29 stsp (*progress_cb)(progress_arg,
561 507dc3bb 2018-12-29 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, progress_path);
562 d7b62c98 2018-12-27 stsp
563 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
564 9d31a1d8 2018-03-11 stsp do {
565 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
566 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
567 9d31a1d8 2018-03-11 stsp if (err)
568 9d31a1d8 2018-03-11 stsp break;
569 9d31a1d8 2018-03-11 stsp if (len > 0) {
570 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
571 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
572 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
573 9d31a1d8 2018-03-11 stsp err = got_error_from_errno();
574 b87c6f83 2018-12-24 stsp goto done;
575 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
576 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
577 b87c6f83 2018-12-24 stsp goto done;
578 9d31a1d8 2018-03-11 stsp }
579 61d6eaa3 2018-12-24 stsp hdrlen = 0;
580 9d31a1d8 2018-03-11 stsp }
581 9d31a1d8 2018-03-11 stsp } while (len != 0);
582 9d31a1d8 2018-03-11 stsp
583 9d31a1d8 2018-03-11 stsp fsync(fd);
584 9d31a1d8 2018-03-11 stsp
585 507dc3bb 2018-12-29 stsp if (update) {
586 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
587 507dc3bb 2018-12-29 stsp err = got_error_from_errno();
588 507dc3bb 2018-12-29 stsp goto done;
589 507dc3bb 2018-12-29 stsp }
590 507dc3bb 2018-12-29 stsp }
591 507dc3bb 2018-12-29 stsp
592 51514078 2018-12-25 stsp if (entry)
593 51514078 2018-12-25 stsp err = got_fileindex_entry_update(entry, ondisk_path,
594 51514078 2018-12-25 stsp blob->id.sha1, worktree->base_commit_id->sha1);
595 51514078 2018-12-25 stsp else {
596 51514078 2018-12-25 stsp err = got_fileindex_entry_alloc(&entry, ondisk_path,
597 51514078 2018-12-25 stsp apply_path_prefix(worktree, path), blob->id.sha1,
598 51514078 2018-12-25 stsp worktree->base_commit_id->sha1);
599 51514078 2018-12-25 stsp if (err)
600 51514078 2018-12-25 stsp goto done;
601 51514078 2018-12-25 stsp err = got_fileindex_entry_add(fileindex, entry);
602 51514078 2018-12-25 stsp }
603 9d31a1d8 2018-03-11 stsp if (err)
604 9d31a1d8 2018-03-11 stsp goto done;
605 9d31a1d8 2018-03-11 stsp done:
606 507dc3bb 2018-12-29 stsp if (fd != -1)
607 507dc3bb 2018-12-29 stsp close(fd);
608 c34b20a2 2018-03-12 stsp free(ondisk_path);
609 507dc3bb 2018-12-29 stsp free(tmppath);
610 9d31a1d8 2018-03-11 stsp return err;
611 9d31a1d8 2018-03-11 stsp }
612 9d31a1d8 2018-03-11 stsp
613 9d31a1d8 2018-03-11 stsp static const struct got_error *
614 9d31a1d8 2018-03-11 stsp add_dir_on_disk(struct got_worktree *worktree, const char *path)
615 9d31a1d8 2018-03-11 stsp {
616 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
617 9d31a1d8 2018-03-11 stsp char *abspath;
618 9d31a1d8 2018-03-11 stsp
619 9d31a1d8 2018-03-11 stsp if (asprintf(&abspath, "%s/%s", worktree->root_path,
620 9d31a1d8 2018-03-11 stsp apply_path_prefix(worktree, path)) == -1)
621 0a585a0d 2018-03-17 stsp return got_error_from_errno();
622 9d31a1d8 2018-03-11 stsp
623 9d31a1d8 2018-03-11 stsp /* XXX queue work rather than editing disk directly? */
624 9d31a1d8 2018-03-11 stsp if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1) {
625 9d31a1d8 2018-03-11 stsp struct stat sb;
626 9d31a1d8 2018-03-11 stsp
627 9d31a1d8 2018-03-11 stsp if (errno != EEXIST) {
628 9d31a1d8 2018-03-11 stsp err = got_error_from_errno();
629 9d31a1d8 2018-03-11 stsp goto done;
630 9d31a1d8 2018-03-11 stsp }
631 9d31a1d8 2018-03-11 stsp
632 9d31a1d8 2018-03-11 stsp if (lstat(abspath, &sb) == -1) {
633 9d31a1d8 2018-03-11 stsp err = got_error_from_errno();
634 9d31a1d8 2018-03-11 stsp goto done;
635 9d31a1d8 2018-03-11 stsp }
636 9d31a1d8 2018-03-11 stsp
637 9d31a1d8 2018-03-11 stsp if (!S_ISDIR(sb.st_mode)) {
638 9d31a1d8 2018-03-11 stsp /* TODO directory is obstructed; do something */
639 9d31a1d8 2018-03-11 stsp return got_error(GOT_ERR_FILE_OBSTRUCTED);
640 9d31a1d8 2018-03-11 stsp }
641 9d31a1d8 2018-03-11 stsp }
642 9d31a1d8 2018-03-11 stsp
643 9d31a1d8 2018-03-11 stsp done:
644 9d31a1d8 2018-03-11 stsp free(abspath);
645 9d31a1d8 2018-03-11 stsp return err;
646 9d31a1d8 2018-03-11 stsp }
647 9d31a1d8 2018-03-11 stsp
648 9d31a1d8 2018-03-11 stsp static const struct got_error *
649 9d31a1d8 2018-03-11 stsp tree_checkout(struct got_worktree *, struct got_fileindex *,
650 92a684f4 2018-03-12 stsp struct got_tree_object *, const char *, struct got_repository *,
651 99437157 2018-11-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
652 99437157 2018-11-11 stsp got_worktree_cancel_cb cancel_cb, void *cancel_arg);
653 9d31a1d8 2018-03-11 stsp
654 9d31a1d8 2018-03-11 stsp static const struct got_error *
655 9d31a1d8 2018-03-11 stsp tree_checkout_entry(struct got_worktree *worktree,
656 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex, struct got_tree_entry *te,
657 92a684f4 2018-03-12 stsp const char *parent, struct got_repository *repo,
658 99437157 2018-11-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
659 99437157 2018-11-11 stsp got_worktree_cancel_cb cancel_cb, void *cancel_arg)
660 9d31a1d8 2018-03-11 stsp {
661 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
662 9d31a1d8 2018-03-11 stsp struct got_object *obj = NULL;
663 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
664 eac9755f 2018-12-29 stsp struct got_fileindex_entry *entry = NULL;
665 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
666 9d31a1d8 2018-03-11 stsp char *path = NULL;
667 f7b38925 2018-04-01 stsp char *progress_path = NULL;
668 9d31a1d8 2018-03-11 stsp size_t len;
669 9d31a1d8 2018-03-11 stsp
670 9d31a1d8 2018-03-11 stsp if (parent[0] == '/' && parent[1] == '\0')
671 9d31a1d8 2018-03-11 stsp parent = "";
672 9d31a1d8 2018-03-11 stsp if (asprintf(&path, "%s/%s", parent, te->name) == -1)
673 0a585a0d 2018-03-17 stsp return got_error_from_errno();
674 9d31a1d8 2018-03-11 stsp
675 9d31a1d8 2018-03-11 stsp /* Skip this entry if it is outside of our path prefix. */
676 9d31a1d8 2018-03-11 stsp len = MIN(strlen(worktree->path_prefix), strlen(path));
677 9d31a1d8 2018-03-11 stsp if (strncmp(path, worktree->path_prefix, len) != 0) {
678 9d31a1d8 2018-03-11 stsp free(path);
679 9d31a1d8 2018-03-11 stsp return NULL;
680 9d31a1d8 2018-03-11 stsp }
681 9d31a1d8 2018-03-11 stsp
682 9d31a1d8 2018-03-11 stsp err = got_object_open(&obj, repo, te->id);
683 9d31a1d8 2018-03-11 stsp if (err)
684 9d31a1d8 2018-03-11 stsp goto done;
685 9d31a1d8 2018-03-11 stsp
686 f7b38925 2018-04-01 stsp progress_path = path;
687 f7b38925 2018-04-01 stsp if (strncmp(progress_path, worktree->path_prefix, len) == 0)
688 f7b38925 2018-04-01 stsp progress_path += len;
689 92a684f4 2018-03-12 stsp
690 15a94983 2018-12-23 stsp switch (obj->type) {
691 9d31a1d8 2018-03-11 stsp case GOT_OBJ_TYPE_BLOB:
692 9d31a1d8 2018-03-11 stsp if (strlen(worktree->path_prefix) >= strlen(path))
693 9d31a1d8 2018-03-11 stsp break;
694 eac9755f 2018-12-29 stsp entry = got_fileindex_entry_get(fileindex,
695 eac9755f 2018-12-29 stsp apply_path_prefix(worktree, path));
696 eac9755f 2018-12-29 stsp if (entry &&
697 eac9755f 2018-12-29 stsp memcmp(entry->commit_sha1, worktree->base_commit_id->sha1,
698 507dc3bb 2018-12-29 stsp SHA1_DIGEST_LENGTH) == 0) {
699 507dc3bb 2018-12-29 stsp (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
700 507dc3bb 2018-12-29 stsp progress_path);
701 507dc3bb 2018-12-29 stsp break;
702 507dc3bb 2018-12-29 stsp }
703 507dc3bb 2018-12-29 stsp if (entry && memcmp(entry->blob_sha1, obj->id.sha1,
704 eac9755f 2018-12-29 stsp SHA1_DIGEST_LENGTH) == 0)
705 507dc3bb 2018-12-29 stsp break;
706 9d31a1d8 2018-03-11 stsp err = got_object_blob_open(&blob, repo, obj, 8192);
707 9d31a1d8 2018-03-11 stsp if (err)
708 9d31a1d8 2018-03-11 stsp goto done;
709 eac9755f 2018-12-29 stsp err = blob_checkout(worktree, fileindex, entry, path, blob,
710 eac9755f 2018-12-29 stsp repo, progress_cb, progress_arg, progress_path);
711 9d31a1d8 2018-03-11 stsp break;
712 9d31a1d8 2018-03-11 stsp case GOT_OBJ_TYPE_TREE:
713 9d31a1d8 2018-03-11 stsp if (strlen(worktree->path_prefix) < strlen(path)) {
714 9d31a1d8 2018-03-11 stsp err = add_dir_on_disk(worktree, path);
715 9d31a1d8 2018-03-11 stsp if (err)
716 9d31a1d8 2018-03-11 stsp break;
717 9d31a1d8 2018-03-11 stsp }
718 381be7cc 2018-12-29 stsp err = got_object_tree_open(&tree, repo, obj);
719 381be7cc 2018-12-29 stsp if (err)
720 381be7cc 2018-12-29 stsp goto done;
721 99437157 2018-11-11 stsp /* XXX infinite recursion possible */
722 92a684f4 2018-03-12 stsp err = tree_checkout(worktree, fileindex, tree, path, repo,
723 99437157 2018-11-11 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
724 9d31a1d8 2018-03-11 stsp break;
725 9d31a1d8 2018-03-11 stsp default:
726 9d31a1d8 2018-03-11 stsp break;
727 9d31a1d8 2018-03-11 stsp }
728 9d31a1d8 2018-03-11 stsp
729 9d31a1d8 2018-03-11 stsp done:
730 9d31a1d8 2018-03-11 stsp if (blob)
731 9d31a1d8 2018-03-11 stsp got_object_blob_close(blob);
732 9d31a1d8 2018-03-11 stsp if (tree)
733 9d31a1d8 2018-03-11 stsp got_object_tree_close(tree);
734 9d31a1d8 2018-03-11 stsp if (obj)
735 9d31a1d8 2018-03-11 stsp got_object_close(obj);
736 9d31a1d8 2018-03-11 stsp free(path);
737 512f0d0e 2019-01-02 stsp return err;
738 512f0d0e 2019-01-02 stsp }
739 512f0d0e 2019-01-02 stsp
740 512f0d0e 2019-01-02 stsp struct collect_missing_entry_args {
741 512f0d0e 2019-01-02 stsp struct got_fileindex *fileindex;
742 512f0d0e 2019-01-02 stsp const struct got_tree_entries *entries;
743 512f0d0e 2019-01-02 stsp struct got_fileindex missing_entries;
744 1f46d0af 2019-01-06 stsp const char *current_subdir;
745 512f0d0e 2019-01-02 stsp };
746 512f0d0e 2019-01-02 stsp
747 512f0d0e 2019-01-02 stsp static const struct got_error *
748 512f0d0e 2019-01-02 stsp collect_missing_file(void *args, struct got_fileindex_entry *entry)
749 512f0d0e 2019-01-02 stsp {
750 512f0d0e 2019-01-02 stsp struct collect_missing_entry_args *a = args;
751 f5c49f82 2019-01-06 stsp char *start, *end;
752 f5c49f82 2019-01-06 stsp ptrdiff_t len;
753 512f0d0e 2019-01-02 stsp struct got_tree_entry *te;
754 512f0d0e 2019-01-02 stsp int found = 0;
755 512f0d0e 2019-01-02 stsp
756 1f46d0af 2019-01-06 stsp if (a->current_subdir[0] != '\0' &&
757 1f46d0af 2019-01-06 stsp strncmp(a->current_subdir, entry->path,
758 1f46d0af 2019-01-06 stsp strlen(a->current_subdir)) != 0)
759 512f0d0e 2019-01-02 stsp return NULL;
760 512f0d0e 2019-01-02 stsp
761 1f46d0af 2019-01-06 stsp start = entry->path + strlen(a->current_subdir);
762 f5c49f82 2019-01-06 stsp while (start[0] == '/')
763 f5c49f82 2019-01-06 stsp start++;
764 f5c49f82 2019-01-06 stsp end = strchr(start, '/');
765 f5c49f82 2019-01-06 stsp if (end == NULL) {
766 f5c49f82 2019-01-06 stsp end = strchr(start, '\0');
767 f5c49f82 2019-01-06 stsp if (end == NULL)
768 f5c49f82 2019-01-06 stsp return got_error(GOT_ERR_BAD_PATH);
769 f5c49f82 2019-01-06 stsp }
770 f5c49f82 2019-01-06 stsp len = end - start;
771 512f0d0e 2019-01-02 stsp
772 512f0d0e 2019-01-02 stsp SIMPLEQ_FOREACH(te, &a->entries->head, entry) {
773 f5c49f82 2019-01-06 stsp if (strncmp(start, te->name, len) == 0 &&
774 f5c49f82 2019-01-06 stsp te->name[len] == '\0') {
775 512f0d0e 2019-01-02 stsp found = 1;
776 512f0d0e 2019-01-02 stsp break;
777 512f0d0e 2019-01-02 stsp }
778 512f0d0e 2019-01-02 stsp }
779 512f0d0e 2019-01-02 stsp
780 512f0d0e 2019-01-02 stsp if (found)
781 512f0d0e 2019-01-02 stsp return NULL;
782 512f0d0e 2019-01-02 stsp
783 512f0d0e 2019-01-02 stsp got_fileindex_entry_remove(a->fileindex, entry);
784 512f0d0e 2019-01-02 stsp return got_fileindex_entry_add(&a->missing_entries, entry);
785 512f0d0e 2019-01-02 stsp }
786 512f0d0e 2019-01-02 stsp
787 512f0d0e 2019-01-02 stsp /* Remove files which exist in the file index but not in the tree. */
788 512f0d0e 2019-01-02 stsp static const struct got_error *
789 512f0d0e 2019-01-02 stsp remove_missing_files(struct got_worktree *worktree, const char *path,
790 512f0d0e 2019-01-02 stsp struct got_fileindex *fileindex, const struct got_tree_entries *entries,
791 512f0d0e 2019-01-02 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
792 512f0d0e 2019-01-02 stsp got_worktree_cancel_cb cancel_cb, void *cancel_arg)
793 512f0d0e 2019-01-02 stsp {
794 512f0d0e 2019-01-02 stsp const struct got_error *err = NULL;
795 512f0d0e 2019-01-02 stsp struct collect_missing_entry_args a;
796 512f0d0e 2019-01-02 stsp struct got_fileindex_entry *entry, *tmp;
797 512f0d0e 2019-01-02 stsp
798 512f0d0e 2019-01-02 stsp a.fileindex = fileindex;
799 512f0d0e 2019-01-02 stsp a.entries = entries;
800 512f0d0e 2019-01-02 stsp a.missing_entries.nentries = 0;
801 5cc266ba 2019-01-06 stsp a.current_subdir = apply_path_prefix(worktree, path);
802 512f0d0e 2019-01-02 stsp TAILQ_INIT(&a.missing_entries.entries);
803 e1ed7f77 2019-01-06 stsp err = got_fileindex_for_each_entry_safe(fileindex,
804 e1ed7f77 2019-01-06 stsp collect_missing_file, &a);
805 512f0d0e 2019-01-02 stsp if (err)
806 512f0d0e 2019-01-02 stsp return err;
807 512f0d0e 2019-01-02 stsp
808 512f0d0e 2019-01-02 stsp TAILQ_FOREACH_SAFE(entry, &a.missing_entries.entries, entry, tmp) {
809 512f0d0e 2019-01-02 stsp char *ondisk_path = NULL;
810 512f0d0e 2019-01-02 stsp
811 512f0d0e 2019-01-02 stsp if (cancel_cb) {
812 512f0d0e 2019-01-02 stsp err = (*cancel_cb)(cancel_arg);
813 512f0d0e 2019-01-02 stsp if (err)
814 512f0d0e 2019-01-02 stsp break;
815 512f0d0e 2019-01-02 stsp }
816 512f0d0e 2019-01-02 stsp
817 512f0d0e 2019-01-02 stsp (*progress_cb)(progress_arg, GOT_STATUS_DELETE, entry->path);
818 512f0d0e 2019-01-02 stsp
819 512f0d0e 2019-01-02 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path,
820 512f0d0e 2019-01-02 stsp entry->path) == -1) {
821 512f0d0e 2019-01-02 stsp err = got_error_from_errno();
822 512f0d0e 2019-01-02 stsp break;
823 512f0d0e 2019-01-02 stsp }
824 512f0d0e 2019-01-02 stsp
825 512f0d0e 2019-01-02 stsp if (unlink(ondisk_path) == -1)
826 512f0d0e 2019-01-02 stsp err = got_error_from_errno();
827 f5c49f82 2019-01-06 stsp else {
828 f5c49f82 2019-01-06 stsp char *parent = dirname(ondisk_path);
829 f5c49f82 2019-01-06 stsp if (rmdir(parent) == -1 && errno != ENOTEMPTY)
830 f5c49f82 2019-01-06 stsp err = got_error_from_errno();
831 f5c49f82 2019-01-06 stsp }
832 512f0d0e 2019-01-02 stsp free(ondisk_path);
833 512f0d0e 2019-01-02 stsp if (err)
834 512f0d0e 2019-01-02 stsp break;
835 512f0d0e 2019-01-02 stsp
836 512f0d0e 2019-01-02 stsp TAILQ_REMOVE(&a.missing_entries.entries, entry, entry);
837 512f0d0e 2019-01-02 stsp got_fileindex_entry_free(entry);
838 512f0d0e 2019-01-02 stsp }
839 512f0d0e 2019-01-02 stsp
840 512f0d0e 2019-01-02 stsp if (err) {
841 512f0d0e 2019-01-02 stsp while (!TAILQ_EMPTY(&a.missing_entries.entries)) {
842 512f0d0e 2019-01-02 stsp entry = TAILQ_FIRST(&a.missing_entries.entries);
843 512f0d0e 2019-01-02 stsp TAILQ_REMOVE(&a.missing_entries.entries, entry, entry);
844 512f0d0e 2019-01-02 stsp got_fileindex_entry_free(entry);
845 512f0d0e 2019-01-02 stsp }
846 512f0d0e 2019-01-02 stsp }
847 512f0d0e 2019-01-02 stsp
848 9d31a1d8 2018-03-11 stsp return err;
849 9d31a1d8 2018-03-11 stsp }
850 9d31a1d8 2018-03-11 stsp
851 9d31a1d8 2018-03-11 stsp static const struct got_error *
852 9d31a1d8 2018-03-11 stsp tree_checkout(struct got_worktree *worktree,
853 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex, struct got_tree_object *tree,
854 92a684f4 2018-03-12 stsp const char *path, struct got_repository *repo,
855 99437157 2018-11-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
856 99437157 2018-11-11 stsp got_worktree_cancel_cb cancel_cb, void *cancel_arg)
857 9d31a1d8 2018-03-11 stsp {
858 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
859 883f0469 2018-06-23 stsp const struct got_tree_entries *entries;
860 9d31a1d8 2018-03-11 stsp struct got_tree_entry *te;
861 9d31a1d8 2018-03-11 stsp size_t len;
862 9d31a1d8 2018-03-11 stsp
863 82d71325 2019-01-06 stsp /* Skip this tree if it shares no path components with the prefix. */
864 9d31a1d8 2018-03-11 stsp len = MIN(strlen(worktree->path_prefix), strlen(path));
865 9d31a1d8 2018-03-11 stsp if (strncmp(path, worktree->path_prefix, len) != 0)
866 9d31a1d8 2018-03-11 stsp return NULL;
867 9d31a1d8 2018-03-11 stsp
868 883f0469 2018-06-23 stsp entries = got_object_tree_get_entries(tree);
869 883f0469 2018-06-23 stsp SIMPLEQ_FOREACH(te, &entries->head, entry) {
870 99437157 2018-11-11 stsp if (cancel_cb) {
871 99437157 2018-11-11 stsp err = (*cancel_cb)(cancel_arg);
872 99437157 2018-11-11 stsp if (err)
873 512f0d0e 2019-01-02 stsp return err;
874 99437157 2018-11-11 stsp }
875 92a684f4 2018-03-12 stsp err = tree_checkout_entry(worktree, fileindex, te, path, repo,
876 99437157 2018-11-11 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
877 9d31a1d8 2018-03-11 stsp if (err)
878 512f0d0e 2019-01-02 stsp return err;
879 9d31a1d8 2018-03-11 stsp }
880 9d31a1d8 2018-03-11 stsp
881 512f0d0e 2019-01-02 stsp len = strlen(worktree->path_prefix);
882 512f0d0e 2019-01-02 stsp if (strncmp(worktree->path_prefix, path, len) == 0) {
883 512f0d0e 2019-01-02 stsp err = remove_missing_files(worktree, path, fileindex, entries,
884 512f0d0e 2019-01-02 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
885 512f0d0e 2019-01-02 stsp if (err)
886 512f0d0e 2019-01-02 stsp return err;
887 512f0d0e 2019-01-02 stsp }
888 512f0d0e 2019-01-02 stsp
889 9d31a1d8 2018-03-11 stsp return err;
890 9d31a1d8 2018-03-11 stsp }
891 9d31a1d8 2018-03-11 stsp
892 9d31a1d8 2018-03-11 stsp const struct got_error *
893 9d31a1d8 2018-03-11 stsp got_worktree_checkout_files(struct got_worktree *worktree,
894 93a30277 2018-12-24 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
895 93a30277 2018-12-24 stsp void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
896 9d31a1d8 2018-03-11 stsp {
897 a143fb78 2018-12-25 stsp const struct got_error *err = NULL, *unlockerr, *checkout_err = NULL;
898 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
899 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
900 9d31a1d8 2018-03-11 stsp char *fileindex_path = NULL, *new_fileindex_path = NULL;
901 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
902 51514078 2018-12-25 stsp FILE *index = NULL, *new_index = NULL;
903 9d31a1d8 2018-03-11 stsp
904 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
905 9d31a1d8 2018-03-11 stsp if (err)
906 9d31a1d8 2018-03-11 stsp return err;
907 93a30277 2018-12-24 stsp
908 7426bbfd 2018-12-24 stsp fileindex = got_fileindex_alloc();
909 9d31a1d8 2018-03-11 stsp if (fileindex == NULL) {
910 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
911 9d31a1d8 2018-03-11 stsp goto done;
912 9d31a1d8 2018-03-11 stsp }
913 9d31a1d8 2018-03-11 stsp
914 9d31a1d8 2018-03-11 stsp if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
915 9d31a1d8 2018-03-11 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
916 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
917 9d31a1d8 2018-03-11 stsp fileindex_path = NULL;
918 9d31a1d8 2018-03-11 stsp goto done;
919 9d31a1d8 2018-03-11 stsp }
920 9d31a1d8 2018-03-11 stsp
921 51514078 2018-12-25 stsp /*
922 51514078 2018-12-25 stsp * Read the file index.
923 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
924 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
925 51514078 2018-12-25 stsp */
926 51514078 2018-12-25 stsp index = fopen(fileindex_path, "rb");
927 51514078 2018-12-25 stsp if (index == NULL) {
928 51514078 2018-12-25 stsp err = got_error_from_errno();
929 51514078 2018-12-25 stsp goto done;
930 51514078 2018-12-25 stsp }
931 51514078 2018-12-25 stsp err = got_fileindex_read(fileindex, index);
932 51514078 2018-12-25 stsp fclose(index);
933 51514078 2018-12-25 stsp if (err)
934 51514078 2018-12-25 stsp goto done;
935 51514078 2018-12-25 stsp
936 b8bdcc21 2018-12-24 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
937 b8bdcc21 2018-12-24 stsp fileindex_path);
938 51664889 2018-03-12 stsp if (err)
939 51664889 2018-03-12 stsp goto done;
940 51664889 2018-03-12 stsp
941 eaccb85f 2018-12-25 stsp err = got_object_open_as_commit(&commit, repo,
942 eaccb85f 2018-12-25 stsp worktree->base_commit_id);
943 9d31a1d8 2018-03-11 stsp if (err)
944 9d31a1d8 2018-03-11 stsp goto done;
945 9d31a1d8 2018-03-11 stsp
946 93a30277 2018-12-24 stsp err = got_object_open_as_tree(&tree, repo, commit->tree_id);
947 9d31a1d8 2018-03-11 stsp if (err)
948 9d31a1d8 2018-03-11 stsp goto done;
949 9d31a1d8 2018-03-11 stsp
950 a143fb78 2018-12-25 stsp checkout_err = tree_checkout(worktree, fileindex, tree, "/", repo,
951 a143fb78 2018-12-25 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
952 9d31a1d8 2018-03-11 stsp
953 a143fb78 2018-12-25 stsp /* Try to sync the fileindex back to disk in any case. */
954 b8bdcc21 2018-12-24 stsp err = got_fileindex_write(fileindex, new_index);
955 9d31a1d8 2018-03-11 stsp if (err)
956 9d31a1d8 2018-03-11 stsp goto done;
957 9d31a1d8 2018-03-11 stsp
958 9d31a1d8 2018-03-11 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
959 9d31a1d8 2018-03-11 stsp err = got_error_from_errno();
960 9d31a1d8 2018-03-11 stsp goto done;
961 9d31a1d8 2018-03-11 stsp }
962 9d31a1d8 2018-03-11 stsp
963 9d31a1d8 2018-03-11 stsp free(new_fileindex_path);
964 9d31a1d8 2018-03-11 stsp new_fileindex_path = NULL;
965 9d31a1d8 2018-03-11 stsp
966 9d31a1d8 2018-03-11 stsp done:
967 edfa7d7f 2018-09-11 stsp if (tree)
968 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
969 9d31a1d8 2018-03-11 stsp if (commit)
970 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
971 9d31a1d8 2018-03-11 stsp if (new_fileindex_path)
972 9d31a1d8 2018-03-11 stsp unlink(new_fileindex_path);
973 b8bdcc21 2018-12-24 stsp if (new_index)
974 b8bdcc21 2018-12-24 stsp fclose(new_index);
975 9d31a1d8 2018-03-11 stsp free(new_fileindex_path);
976 9d31a1d8 2018-03-11 stsp free(fileindex_path);
977 7426bbfd 2018-12-24 stsp got_fileindex_free(fileindex);
978 a143fb78 2018-12-25 stsp if (checkout_err)
979 a143fb78 2018-12-25 stsp err = checkout_err;
980 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
981 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
982 9d31a1d8 2018-03-11 stsp err = unlockerr;
983 9d31a1d8 2018-03-11 stsp return err;
984 9d31a1d8 2018-03-11 stsp }