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