Blame


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