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