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