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 6353ad76 2019-02-08 stsp #include "got_lib_diff.h"
50 9d31a1d8 2018-03-11 stsp
51 9d31a1d8 2018-03-11 stsp #ifndef MIN
52 9d31a1d8 2018-03-11 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
53 9d31a1d8 2018-03-11 stsp #endif
54 86c3caaf 2018-03-09 stsp
55 99724ed4 2018-03-10 stsp static const struct got_error *
56 7ac97322 2018-03-11 stsp create_meta_file(const char *path_got, const char *name, const char *content)
57 99724ed4 2018-03-10 stsp {
58 99724ed4 2018-03-10 stsp const struct got_error *err = NULL;
59 99724ed4 2018-03-10 stsp char *path;
60 99724ed4 2018-03-10 stsp int fd = -1;
61 99724ed4 2018-03-10 stsp
62 7ac97322 2018-03-11 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
63 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
64 99724ed4 2018-03-10 stsp path = NULL;
65 99724ed4 2018-03-10 stsp goto done;
66 99724ed4 2018-03-10 stsp }
67 99724ed4 2018-03-10 stsp
68 73a5ef67 2018-03-11 stsp fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
69 99724ed4 2018-03-10 stsp GOT_DEFAULT_FILE_MODE);
70 99724ed4 2018-03-10 stsp if (fd == -1) {
71 99724ed4 2018-03-10 stsp err = got_error_from_errno();
72 99724ed4 2018-03-10 stsp goto done;
73 99724ed4 2018-03-10 stsp }
74 99724ed4 2018-03-10 stsp
75 99724ed4 2018-03-10 stsp if (content) {
76 99724ed4 2018-03-10 stsp int len = dprintf(fd, "%s\n", content);
77 99724ed4 2018-03-10 stsp if (len != strlen(content) + 1) {
78 99724ed4 2018-03-10 stsp err = got_error_from_errno();
79 99724ed4 2018-03-10 stsp goto done;
80 99724ed4 2018-03-10 stsp }
81 99724ed4 2018-03-10 stsp }
82 99724ed4 2018-03-10 stsp
83 99724ed4 2018-03-10 stsp done:
84 99724ed4 2018-03-10 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
85 99724ed4 2018-03-10 stsp err = got_error_from_errno();
86 99724ed4 2018-03-10 stsp free(path);
87 507dc3bb 2018-12-29 stsp return err;
88 507dc3bb 2018-12-29 stsp }
89 507dc3bb 2018-12-29 stsp
90 507dc3bb 2018-12-29 stsp static const struct got_error *
91 507dc3bb 2018-12-29 stsp update_meta_file(const char *path_got, const char *name, const char *content)
92 507dc3bb 2018-12-29 stsp {
93 507dc3bb 2018-12-29 stsp const struct got_error *err = NULL;
94 507dc3bb 2018-12-29 stsp FILE *tmpfile = NULL;
95 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
96 507dc3bb 2018-12-29 stsp char *path = NULL;
97 507dc3bb 2018-12-29 stsp
98 507dc3bb 2018-12-29 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
99 507dc3bb 2018-12-29 stsp err = got_error_from_errno();
100 507dc3bb 2018-12-29 stsp path = NULL;
101 507dc3bb 2018-12-29 stsp goto done;
102 507dc3bb 2018-12-29 stsp }
103 507dc3bb 2018-12-29 stsp
104 507dc3bb 2018-12-29 stsp err = got_opentemp_named(&tmppath, &tmpfile, path);
105 507dc3bb 2018-12-29 stsp if (err)
106 507dc3bb 2018-12-29 stsp goto done;
107 507dc3bb 2018-12-29 stsp
108 507dc3bb 2018-12-29 stsp if (content) {
109 507dc3bb 2018-12-29 stsp int len = fprintf(tmpfile, "%s\n", content);
110 507dc3bb 2018-12-29 stsp if (len != strlen(content) + 1) {
111 507dc3bb 2018-12-29 stsp err = got_error_from_errno();
112 507dc3bb 2018-12-29 stsp goto done;
113 507dc3bb 2018-12-29 stsp }
114 507dc3bb 2018-12-29 stsp }
115 507dc3bb 2018-12-29 stsp
116 507dc3bb 2018-12-29 stsp if (rename(tmppath, path) != 0) {
117 507dc3bb 2018-12-29 stsp err = got_error_from_errno();
118 2a57020b 2019-02-20 stsp unlink(tmppath);
119 507dc3bb 2018-12-29 stsp goto done;
120 507dc3bb 2018-12-29 stsp }
121 507dc3bb 2018-12-29 stsp
122 507dc3bb 2018-12-29 stsp done:
123 507dc3bb 2018-12-29 stsp free(tmppath);
124 fb43ecf1 2019-02-11 stsp if (fclose(tmpfile) != 0 && err == NULL)
125 fb43ecf1 2019-02-11 stsp err = got_error_from_errno();
126 99724ed4 2018-03-10 stsp return err;
127 99724ed4 2018-03-10 stsp }
128 99724ed4 2018-03-10 stsp
129 09fe317a 2018-03-11 stsp static const struct got_error *
130 7ac97322 2018-03-11 stsp read_meta_file(char **content, const char *path_got, const char *name)
131 09fe317a 2018-03-11 stsp {
132 09fe317a 2018-03-11 stsp const struct got_error *err = NULL;
133 09fe317a 2018-03-11 stsp char *path;
134 09fe317a 2018-03-11 stsp int fd = -1;
135 09fe317a 2018-03-11 stsp ssize_t n;
136 09fe317a 2018-03-11 stsp struct stat sb;
137 09fe317a 2018-03-11 stsp
138 09fe317a 2018-03-11 stsp *content = NULL;
139 09fe317a 2018-03-11 stsp
140 7ac97322 2018-03-11 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
141 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
142 09fe317a 2018-03-11 stsp path = NULL;
143 09fe317a 2018-03-11 stsp goto done;
144 09fe317a 2018-03-11 stsp }
145 09fe317a 2018-03-11 stsp
146 ef99fdb1 2018-03-11 stsp fd = open(path, O_RDONLY | O_NOFOLLOW);
147 09fe317a 2018-03-11 stsp if (fd == -1) {
148 ef99fdb1 2018-03-11 stsp err = got_error_from_errno();
149 ef99fdb1 2018-03-11 stsp goto done;
150 ef99fdb1 2018-03-11 stsp }
151 ef99fdb1 2018-03-11 stsp if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
152 73a5ef67 2018-03-11 stsp err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
153 73a5ef67 2018-03-11 stsp : got_error_from_errno());
154 09fe317a 2018-03-11 stsp goto done;
155 09fe317a 2018-03-11 stsp }
156 09fe317a 2018-03-11 stsp
157 d10c9b58 2019-02-19 stsp if (lstat(path, &sb) != 0) {
158 d10c9b58 2019-02-19 stsp err = got_error_from_errno();
159 d10c9b58 2019-02-19 stsp goto done;
160 d10c9b58 2019-02-19 stsp }
161 09fe317a 2018-03-11 stsp *content = calloc(1, sb.st_size);
162 09fe317a 2018-03-11 stsp if (*content == NULL) {
163 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
164 09fe317a 2018-03-11 stsp goto done;
165 09fe317a 2018-03-11 stsp }
166 09fe317a 2018-03-11 stsp
167 09fe317a 2018-03-11 stsp n = read(fd, *content, sb.st_size);
168 09fe317a 2018-03-11 stsp if (n != sb.st_size) {
169 0605801d 2018-03-11 stsp err = (n == -1 ? got_error_from_errno() :
170 0605801d 2018-03-11 stsp got_error(GOT_ERR_WORKTREE_META));
171 09fe317a 2018-03-11 stsp goto done;
172 09fe317a 2018-03-11 stsp }
173 09fe317a 2018-03-11 stsp if ((*content)[sb.st_size - 1] != '\n') {
174 09fe317a 2018-03-11 stsp err = got_error(GOT_ERR_WORKTREE_META);
175 09fe317a 2018-03-11 stsp goto done;
176 09fe317a 2018-03-11 stsp }
177 09fe317a 2018-03-11 stsp (*content)[sb.st_size - 1] = '\0';
178 09fe317a 2018-03-11 stsp
179 09fe317a 2018-03-11 stsp done:
180 09fe317a 2018-03-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
181 09fe317a 2018-03-11 stsp err = got_error_from_errno();
182 09fe317a 2018-03-11 stsp free(path);
183 09fe317a 2018-03-11 stsp if (err) {
184 09fe317a 2018-03-11 stsp free(*content);
185 09fe317a 2018-03-11 stsp *content = NULL;
186 09fe317a 2018-03-11 stsp }
187 09fe317a 2018-03-11 stsp return err;
188 09fe317a 2018-03-11 stsp }
189 09fe317a 2018-03-11 stsp
190 86c3caaf 2018-03-09 stsp const struct got_error *
191 86c3caaf 2018-03-09 stsp got_worktree_init(const char *path, struct got_reference *head_ref,
192 577ec78f 2018-03-11 stsp const char *prefix, struct got_repository *repo)
193 86c3caaf 2018-03-09 stsp {
194 86c3caaf 2018-03-09 stsp const struct got_error *err = NULL;
195 65596e15 2018-12-24 stsp struct got_object_id *commit_id = NULL;
196 65596e15 2018-12-24 stsp int obj_type;
197 7ac97322 2018-03-11 stsp char *path_got = NULL;
198 08d425ea 2018-12-24 stsp char *refstr = NULL;
199 1451e70d 2018-03-10 stsp char *formatstr = NULL;
200 0bb8a95e 2018-03-12 stsp char *absprefix = NULL;
201 65596e15 2018-12-24 stsp char *basestr = NULL;
202 65596e15 2018-12-24 stsp
203 65596e15 2018-12-24 stsp err = got_ref_resolve(&commit_id, repo, head_ref);
204 65596e15 2018-12-24 stsp if (err)
205 65596e15 2018-12-24 stsp return err;
206 65596e15 2018-12-24 stsp err = got_object_get_type(&obj_type, repo, commit_id);
207 65596e15 2018-12-24 stsp if (err)
208 65596e15 2018-12-24 stsp return err;
209 65596e15 2018-12-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
210 65596e15 2018-12-24 stsp return got_error(GOT_ERR_OBJ_TYPE);
211 86c3caaf 2018-03-09 stsp
212 0bb8a95e 2018-03-12 stsp if (!got_path_is_absolute(prefix)) {
213 0bb8a95e 2018-03-12 stsp if (asprintf(&absprefix, "/%s", prefix) == -1)
214 0a585a0d 2018-03-17 stsp return got_error_from_errno();
215 0bb8a95e 2018-03-12 stsp }
216 577ec78f 2018-03-11 stsp
217 86c3caaf 2018-03-09 stsp /* Create top-level directory (may already exist). */
218 2cb4bacb 2018-03-11 stsp if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
219 86c3caaf 2018-03-09 stsp err = got_error_from_errno();
220 86c3caaf 2018-03-09 stsp goto done;
221 86c3caaf 2018-03-09 stsp }
222 86c3caaf 2018-03-09 stsp
223 86c3caaf 2018-03-09 stsp /* Create .got directory (may already exist). */
224 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
225 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
226 86c3caaf 2018-03-09 stsp goto done;
227 86c3caaf 2018-03-09 stsp }
228 7ac97322 2018-03-11 stsp if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
229 86c3caaf 2018-03-09 stsp err = got_error_from_errno();
230 86c3caaf 2018-03-09 stsp goto done;
231 86c3caaf 2018-03-09 stsp }
232 86c3caaf 2018-03-09 stsp
233 056e7441 2018-03-11 stsp /* Create an empty lock file. */
234 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
235 056e7441 2018-03-11 stsp if (err)
236 056e7441 2018-03-11 stsp goto done;
237 056e7441 2018-03-11 stsp
238 86c3caaf 2018-03-09 stsp /* Create an empty file index. */
239 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
240 99724ed4 2018-03-10 stsp if (err)
241 86c3caaf 2018-03-09 stsp goto done;
242 86c3caaf 2018-03-09 stsp
243 08d425ea 2018-12-24 stsp /* Write the HEAD reference. */
244 08d425ea 2018-12-24 stsp refstr = got_ref_to_str(head_ref);
245 08d425ea 2018-12-24 stsp if (refstr == NULL) {
246 a1a7858a 2018-12-24 stsp err = got_error_from_errno();
247 a1a7858a 2018-12-24 stsp goto done;
248 a1a7858a 2018-12-24 stsp }
249 0f92850e 2018-12-25 stsp err = create_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
250 65596e15 2018-12-24 stsp if (err)
251 65596e15 2018-12-24 stsp goto done;
252 65596e15 2018-12-24 stsp
253 65596e15 2018-12-24 stsp /* Record our base commit. */
254 65596e15 2018-12-24 stsp err = got_object_id_str(&basestr, commit_id);
255 65596e15 2018-12-24 stsp if (err)
256 65596e15 2018-12-24 stsp goto done;
257 0f92850e 2018-12-25 stsp err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
258 99724ed4 2018-03-10 stsp if (err)
259 86c3caaf 2018-03-09 stsp goto done;
260 86c3caaf 2018-03-09 stsp
261 1451e70d 2018-03-10 stsp /* Store path to repository. */
262 7839bc15 2019-01-06 stsp err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
263 7839bc15 2019-01-06 stsp got_repo_get_path(repo));
264 99724ed4 2018-03-10 stsp if (err)
265 86c3caaf 2018-03-09 stsp goto done;
266 86c3caaf 2018-03-09 stsp
267 577ec78f 2018-03-11 stsp /* Store in-repository path prefix. */
268 0bb8a95e 2018-03-12 stsp err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
269 0bb8a95e 2018-03-12 stsp absprefix ? absprefix : prefix);
270 577ec78f 2018-03-11 stsp if (err)
271 577ec78f 2018-03-11 stsp goto done;
272 577ec78f 2018-03-11 stsp
273 9dce68ed 2018-03-10 stsp /* Stamp work tree with format file. */
274 1451e70d 2018-03-10 stsp if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
275 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
276 1451e70d 2018-03-10 stsp goto done;
277 1451e70d 2018-03-10 stsp }
278 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
279 99724ed4 2018-03-10 stsp if (err)
280 1451e70d 2018-03-10 stsp goto done;
281 1451e70d 2018-03-10 stsp
282 86c3caaf 2018-03-09 stsp done:
283 65596e15 2018-12-24 stsp free(commit_id);
284 7ac97322 2018-03-11 stsp free(path_got);
285 1451e70d 2018-03-10 stsp free(formatstr);
286 08d425ea 2018-12-24 stsp free(refstr);
287 0bb8a95e 2018-03-12 stsp free(absprefix);
288 65596e15 2018-12-24 stsp free(basestr);
289 86c3caaf 2018-03-09 stsp return err;
290 86c3caaf 2018-03-09 stsp }
291 86c3caaf 2018-03-09 stsp
292 247140b2 2019-02-05 stsp static const struct got_error *
293 247140b2 2019-02-05 stsp open_worktree(struct got_worktree **worktree, const char *path)
294 86c3caaf 2018-03-09 stsp {
295 6d9d28c3 2018-03-11 stsp const struct got_error *err = NULL;
296 7ac97322 2018-03-11 stsp char *path_got;
297 6d9d28c3 2018-03-11 stsp char *formatstr = NULL;
298 056e7441 2018-03-11 stsp char *path_lock = NULL;
299 eaccb85f 2018-12-25 stsp char *base_commit_id_str = NULL;
300 271d2a38 2018-12-25 stsp char *head_ref_str = NULL;
301 6d9d28c3 2018-03-11 stsp int version, fd = -1;
302 6d9d28c3 2018-03-11 stsp const char *errstr;
303 eaccb85f 2018-12-25 stsp struct got_repository *repo = NULL;
304 6d9d28c3 2018-03-11 stsp
305 6d9d28c3 2018-03-11 stsp *worktree = NULL;
306 6d9d28c3 2018-03-11 stsp
307 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
308 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
309 7ac97322 2018-03-11 stsp path_got = NULL;
310 6d9d28c3 2018-03-11 stsp goto done;
311 6d9d28c3 2018-03-11 stsp }
312 6d9d28c3 2018-03-11 stsp
313 7ac97322 2018-03-11 stsp if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
314 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
315 056e7441 2018-03-11 stsp path_lock = NULL;
316 6d9d28c3 2018-03-11 stsp goto done;
317 6d9d28c3 2018-03-11 stsp }
318 6d9d28c3 2018-03-11 stsp
319 056e7441 2018-03-11 stsp fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
320 6d9d28c3 2018-03-11 stsp if (fd == -1) {
321 73a5ef67 2018-03-11 stsp err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
322 73a5ef67 2018-03-11 stsp : got_error_from_errno());
323 6d9d28c3 2018-03-11 stsp goto done;
324 6d9d28c3 2018-03-11 stsp }
325 6d9d28c3 2018-03-11 stsp
326 7ac97322 2018-03-11 stsp err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
327 6d9d28c3 2018-03-11 stsp if (err)
328 6d9d28c3 2018-03-11 stsp goto done;
329 6d9d28c3 2018-03-11 stsp
330 6d9d28c3 2018-03-11 stsp version = strtonum(formatstr, 1, INT_MAX, &errstr);
331 6d9d28c3 2018-03-11 stsp if (errstr) {
332 6d9d28c3 2018-03-11 stsp err = got_error(GOT_ERR_WORKTREE_META);
333 6d9d28c3 2018-03-11 stsp goto done;
334 6d9d28c3 2018-03-11 stsp }
335 6d9d28c3 2018-03-11 stsp if (version != GOT_WORKTREE_FORMAT_VERSION) {
336 6d9d28c3 2018-03-11 stsp err = got_error(GOT_ERR_WORKTREE_VERS);
337 6d9d28c3 2018-03-11 stsp goto done;
338 6d9d28c3 2018-03-11 stsp }
339 6d9d28c3 2018-03-11 stsp
340 6d9d28c3 2018-03-11 stsp *worktree = calloc(1, sizeof(**worktree));
341 6d9d28c3 2018-03-11 stsp if (*worktree == 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 056e7441 2018-03-11 stsp (*worktree)->lockfd = -1;
346 6d9d28c3 2018-03-11 stsp
347 c88eb298 2018-03-11 stsp (*worktree)->root_path = strdup(path);
348 c88eb298 2018-03-11 stsp if ((*worktree)->root_path == NULL) {
349 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
350 6d9d28c3 2018-03-11 stsp goto done;
351 6d9d28c3 2018-03-11 stsp }
352 cde76477 2018-03-11 stsp err = read_meta_file(&(*worktree)->repo_path, path_got,
353 6d9d28c3 2018-03-11 stsp GOT_WORKTREE_REPOSITORY);
354 6d9d28c3 2018-03-11 stsp if (err)
355 6d9d28c3 2018-03-11 stsp goto done;
356 eaccb85f 2018-12-25 stsp
357 7ac97322 2018-03-11 stsp err = read_meta_file(&(*worktree)->path_prefix, path_got,
358 6d9d28c3 2018-03-11 stsp GOT_WORKTREE_PATH_PREFIX);
359 93a30277 2018-12-24 stsp if (err)
360 93a30277 2018-12-24 stsp goto done;
361 93a30277 2018-12-24 stsp
362 eaccb85f 2018-12-25 stsp err = read_meta_file(&base_commit_id_str, path_got,
363 0f92850e 2018-12-25 stsp GOT_WORKTREE_BASE_COMMIT);
364 eaccb85f 2018-12-25 stsp if (err)
365 eaccb85f 2018-12-25 stsp goto done;
366 eaccb85f 2018-12-25 stsp
367 eaccb85f 2018-12-25 stsp err = got_repo_open(&repo, (*worktree)->repo_path);
368 eaccb85f 2018-12-25 stsp if (err)
369 eaccb85f 2018-12-25 stsp goto done;
370 eaccb85f 2018-12-25 stsp
371 eaccb85f 2018-12-25 stsp err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
372 eaccb85f 2018-12-25 stsp base_commit_id_str);
373 f5baf295 2018-03-11 stsp if (err)
374 f5baf295 2018-03-11 stsp goto done;
375 f5baf295 2018-03-11 stsp
376 271d2a38 2018-12-25 stsp err = read_meta_file(&head_ref_str, path_got, GOT_WORKTREE_HEAD_REF);
377 f5baf295 2018-03-11 stsp if (err)
378 6d9d28c3 2018-03-11 stsp goto done;
379 6d9d28c3 2018-03-11 stsp
380 271d2a38 2018-12-25 stsp err = got_ref_open(&(*worktree)->head_ref, repo, head_ref_str);
381 6d9d28c3 2018-03-11 stsp done:
382 eaccb85f 2018-12-25 stsp if (repo)
383 eaccb85f 2018-12-25 stsp got_repo_close(repo);
384 7ac97322 2018-03-11 stsp free(path_got);
385 056e7441 2018-03-11 stsp free(path_lock);
386 271d2a38 2018-12-25 stsp free(head_ref_str);
387 eaccb85f 2018-12-25 stsp free(base_commit_id_str);
388 6d9d28c3 2018-03-11 stsp if (err) {
389 6d9d28c3 2018-03-11 stsp if (fd != -1)
390 6d9d28c3 2018-03-11 stsp close(fd);
391 6d9d28c3 2018-03-11 stsp if (*worktree != NULL)
392 6d9d28c3 2018-03-11 stsp got_worktree_close(*worktree);
393 6d9d28c3 2018-03-11 stsp *worktree = NULL;
394 6d9d28c3 2018-03-11 stsp } else
395 056e7441 2018-03-11 stsp (*worktree)->lockfd = fd;
396 6d9d28c3 2018-03-11 stsp
397 6d9d28c3 2018-03-11 stsp return err;
398 86c3caaf 2018-03-09 stsp }
399 247140b2 2019-02-05 stsp
400 247140b2 2019-02-05 stsp const struct got_error *
401 247140b2 2019-02-05 stsp got_worktree_open(struct got_worktree **worktree, const char *path)
402 247140b2 2019-02-05 stsp {
403 247140b2 2019-02-05 stsp const struct got_error *err = NULL;
404 86c3caaf 2018-03-09 stsp
405 247140b2 2019-02-05 stsp do {
406 247140b2 2019-02-05 stsp err = open_worktree(worktree, path);
407 247140b2 2019-02-05 stsp if (err && (err->code != GOT_ERR_ERRNO && errno != ENOENT))
408 247140b2 2019-02-05 stsp return err;
409 247140b2 2019-02-05 stsp if (*worktree)
410 247140b2 2019-02-05 stsp return NULL;
411 247140b2 2019-02-05 stsp path = dirname(path);
412 d1542a27 2019-02-05 stsp if (path == NULL)
413 d1542a27 2019-02-05 stsp return got_error_from_errno();
414 d1542a27 2019-02-05 stsp } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
415 247140b2 2019-02-05 stsp
416 247140b2 2019-02-05 stsp return got_error(GOT_ERR_NOT_WORKTREE);
417 247140b2 2019-02-05 stsp }
418 247140b2 2019-02-05 stsp
419 3a6ce05a 2019-02-11 stsp const struct got_error *
420 86c3caaf 2018-03-09 stsp got_worktree_close(struct got_worktree *worktree)
421 86c3caaf 2018-03-09 stsp {
422 3a6ce05a 2019-02-11 stsp const struct got_error *err = NULL;
423 c88eb298 2018-03-11 stsp free(worktree->root_path);
424 cde76477 2018-03-11 stsp free(worktree->repo_path);
425 6d9d28c3 2018-03-11 stsp free(worktree->path_prefix);
426 eaccb85f 2018-12-25 stsp free(worktree->base_commit_id);
427 271d2a38 2018-12-25 stsp if (worktree->head_ref)
428 271d2a38 2018-12-25 stsp got_ref_close(worktree->head_ref);
429 056e7441 2018-03-11 stsp if (worktree->lockfd != -1)
430 3a6ce05a 2019-02-11 stsp if (close(worktree->lockfd) != 0)
431 3a6ce05a 2019-02-11 stsp err = got_error_from_errno();
432 6d9d28c3 2018-03-11 stsp free(worktree);
433 3a6ce05a 2019-02-11 stsp return err;
434 86c3caaf 2018-03-09 stsp }
435 86c3caaf 2018-03-09 stsp
436 2fbdb5ae 2018-12-29 stsp const char *
437 c7f4312f 2019-02-05 stsp got_worktree_get_root_path(struct got_worktree *worktree)
438 c7f4312f 2019-02-05 stsp {
439 c7f4312f 2019-02-05 stsp return worktree->root_path;
440 c7f4312f 2019-02-05 stsp }
441 c7f4312f 2019-02-05 stsp
442 c7f4312f 2019-02-05 stsp const char *
443 86c3caaf 2018-03-09 stsp got_worktree_get_repo_path(struct got_worktree *worktree)
444 86c3caaf 2018-03-09 stsp {
445 2fbdb5ae 2018-12-29 stsp return worktree->repo_path;
446 49520a32 2018-12-29 stsp }
447 49520a32 2018-12-29 stsp
448 49520a32 2018-12-29 stsp const char *
449 49520a32 2018-12-29 stsp got_worktree_get_path_prefix(struct got_worktree *worktree)
450 49520a32 2018-12-29 stsp {
451 f609be2e 2018-12-29 stsp return worktree->path_prefix;
452 e5dc7198 2018-12-29 stsp }
453 e5dc7198 2018-12-29 stsp
454 e5dc7198 2018-12-29 stsp const struct got_error *
455 e5dc7198 2018-12-29 stsp got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
456 e5dc7198 2018-12-29 stsp const char *path_prefix)
457 e5dc7198 2018-12-29 stsp {
458 e5dc7198 2018-12-29 stsp char *absprefix = NULL;
459 e5dc7198 2018-12-29 stsp
460 e5dc7198 2018-12-29 stsp if (!got_path_is_absolute(path_prefix)) {
461 e5dc7198 2018-12-29 stsp if (asprintf(&absprefix, "/%s", path_prefix) == -1)
462 e5dc7198 2018-12-29 stsp return got_error_from_errno();
463 e5dc7198 2018-12-29 stsp }
464 e5dc7198 2018-12-29 stsp *match = (strcmp(absprefix ? absprefix : path_prefix,
465 e5dc7198 2018-12-29 stsp worktree->path_prefix) == 0);
466 e5dc7198 2018-12-29 stsp free(absprefix);
467 e5dc7198 2018-12-29 stsp return NULL;
468 86c3caaf 2018-03-09 stsp }
469 86c3caaf 2018-03-09 stsp
470 35be1456 2018-03-11 stsp char *
471 35be1456 2018-03-11 stsp got_worktree_get_head_ref_name(struct got_worktree *worktree)
472 86c3caaf 2018-03-09 stsp {
473 271d2a38 2018-12-25 stsp return got_ref_to_str(worktree->head_ref);
474 be7061eb 2018-12-30 stsp }
475 be7061eb 2018-12-30 stsp
476 be7061eb 2018-12-30 stsp struct got_reference *
477 be7061eb 2018-12-30 stsp got_worktree_get_head_ref(struct got_worktree *worktree)
478 be7061eb 2018-12-30 stsp {
479 be7061eb 2018-12-30 stsp return got_ref_dup(worktree->head_ref);
480 507dc3bb 2018-12-29 stsp }
481 507dc3bb 2018-12-29 stsp
482 b72f483a 2019-02-05 stsp struct got_object_id *
483 507dc3bb 2018-12-29 stsp got_worktree_get_base_commit_id(struct got_worktree *worktree)
484 507dc3bb 2018-12-29 stsp {
485 507dc3bb 2018-12-29 stsp return worktree->base_commit_id;
486 86c3caaf 2018-03-09 stsp }
487 86c3caaf 2018-03-09 stsp
488 507dc3bb 2018-12-29 stsp const struct got_error *
489 507dc3bb 2018-12-29 stsp got_worktree_set_base_commit_id(struct got_worktree *worktree,
490 507dc3bb 2018-12-29 stsp struct got_repository *repo, struct got_object_id *commit_id)
491 507dc3bb 2018-12-29 stsp {
492 507dc3bb 2018-12-29 stsp const struct got_error *err;
493 507dc3bb 2018-12-29 stsp struct got_object *obj = NULL;
494 507dc3bb 2018-12-29 stsp char *id_str = NULL;
495 507dc3bb 2018-12-29 stsp char *path_got = NULL;
496 507dc3bb 2018-12-29 stsp
497 507dc3bb 2018-12-29 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
498 507dc3bb 2018-12-29 stsp GOT_WORKTREE_GOT_DIR) == -1) {
499 507dc3bb 2018-12-29 stsp err = got_error_from_errno();
500 507dc3bb 2018-12-29 stsp path_got = NULL;
501 507dc3bb 2018-12-29 stsp goto done;
502 507dc3bb 2018-12-29 stsp }
503 507dc3bb 2018-12-29 stsp
504 507dc3bb 2018-12-29 stsp err = got_object_open(&obj, repo, commit_id);
505 507dc3bb 2018-12-29 stsp if (err)
506 507dc3bb 2018-12-29 stsp return err;
507 507dc3bb 2018-12-29 stsp
508 507dc3bb 2018-12-29 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT) {
509 507dc3bb 2018-12-29 stsp err = got_error(GOT_ERR_OBJ_TYPE);
510 507dc3bb 2018-12-29 stsp goto done;
511 507dc3bb 2018-12-29 stsp }
512 507dc3bb 2018-12-29 stsp
513 507dc3bb 2018-12-29 stsp /* Record our base commit. */
514 507dc3bb 2018-12-29 stsp err = got_object_id_str(&id_str, commit_id);
515 507dc3bb 2018-12-29 stsp if (err)
516 507dc3bb 2018-12-29 stsp goto done;
517 507dc3bb 2018-12-29 stsp err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
518 507dc3bb 2018-12-29 stsp if (err)
519 507dc3bb 2018-12-29 stsp goto done;
520 507dc3bb 2018-12-29 stsp
521 507dc3bb 2018-12-29 stsp free(worktree->base_commit_id);
522 507dc3bb 2018-12-29 stsp worktree->base_commit_id = got_object_id_dup(commit_id);
523 507dc3bb 2018-12-29 stsp if (worktree->base_commit_id == NULL) {
524 507dc3bb 2018-12-29 stsp err = got_error_from_errno();
525 507dc3bb 2018-12-29 stsp goto done;
526 507dc3bb 2018-12-29 stsp }
527 507dc3bb 2018-12-29 stsp done:
528 507dc3bb 2018-12-29 stsp if (obj)
529 507dc3bb 2018-12-29 stsp got_object_close(obj);
530 507dc3bb 2018-12-29 stsp free(id_str);
531 507dc3bb 2018-12-29 stsp free(path_got);
532 507dc3bb 2018-12-29 stsp return err;
533 507dc3bb 2018-12-29 stsp }
534 507dc3bb 2018-12-29 stsp
535 9d31a1d8 2018-03-11 stsp static const struct got_error *
536 9d31a1d8 2018-03-11 stsp lock_worktree(struct got_worktree *worktree, int operation)
537 86c3caaf 2018-03-09 stsp {
538 9d31a1d8 2018-03-11 stsp if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
539 9d31a1d8 2018-03-11 stsp return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
540 9d31a1d8 2018-03-11 stsp : got_error_from_errno());
541 86c3caaf 2018-03-09 stsp return NULL;
542 9d31a1d8 2018-03-11 stsp }
543 9d31a1d8 2018-03-11 stsp
544 9d31a1d8 2018-03-11 stsp static const struct got_error *
545 21908da4 2019-01-13 stsp make_parent_dirs(const char *abspath)
546 21908da4 2019-01-13 stsp {
547 21908da4 2019-01-13 stsp const struct got_error *err = NULL;
548 21908da4 2019-01-13 stsp
549 21908da4 2019-01-13 stsp char *parent = dirname(abspath);
550 21908da4 2019-01-13 stsp if (parent == NULL)
551 21908da4 2019-01-13 stsp return NULL;
552 21908da4 2019-01-13 stsp
553 21908da4 2019-01-13 stsp if (mkdir(parent, GOT_DEFAULT_DIR_MODE) == -1) {
554 21908da4 2019-01-13 stsp if (errno == ENOENT) {
555 21908da4 2019-01-13 stsp err = make_parent_dirs(parent);
556 21908da4 2019-01-13 stsp if (err)
557 21908da4 2019-01-13 stsp return err;
558 c65fcef2 2019-01-13 stsp if (mkdir(parent, GOT_DEFAULT_DIR_MODE) == -1)
559 c65fcef2 2019-01-13 stsp return got_error_from_errno();
560 21908da4 2019-01-13 stsp } else
561 c65fcef2 2019-01-13 stsp err = got_error_from_errno();
562 21908da4 2019-01-13 stsp }
563 21908da4 2019-01-13 stsp
564 c65fcef2 2019-01-13 stsp return err;
565 21908da4 2019-01-13 stsp }
566 21908da4 2019-01-13 stsp
567 21908da4 2019-01-13 stsp static const struct got_error *
568 4a1ddfc2 2019-01-12 stsp add_dir_on_disk(struct got_worktree *worktree, const char *path)
569 4a1ddfc2 2019-01-12 stsp {
570 4a1ddfc2 2019-01-12 stsp const struct got_error *err = NULL;
571 4a1ddfc2 2019-01-12 stsp char *abspath;
572 4a1ddfc2 2019-01-12 stsp
573 4a1ddfc2 2019-01-12 stsp if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
574 4a1ddfc2 2019-01-12 stsp return got_error_from_errno();
575 4a1ddfc2 2019-01-12 stsp
576 4a1ddfc2 2019-01-12 stsp /* XXX queue work rather than editing disk directly? */
577 4a1ddfc2 2019-01-12 stsp if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1) {
578 4a1ddfc2 2019-01-12 stsp struct stat sb;
579 4a1ddfc2 2019-01-12 stsp
580 21908da4 2019-01-13 stsp if (errno == EEXIST) {
581 21908da4 2019-01-13 stsp if (lstat(abspath, &sb) == -1) {
582 21908da4 2019-01-13 stsp err = got_error_from_errno();
583 21908da4 2019-01-13 stsp goto done;
584 21908da4 2019-01-13 stsp }
585 21908da4 2019-01-13 stsp
586 21908da4 2019-01-13 stsp if (!S_ISDIR(sb.st_mode)) {
587 21908da4 2019-01-13 stsp /* TODO directory is obstructed; do something */
588 c65fcef2 2019-01-13 stsp err = got_error(GOT_ERR_FILE_OBSTRUCTED);
589 c65fcef2 2019-01-13 stsp goto done;
590 21908da4 2019-01-13 stsp }
591 21908da4 2019-01-13 stsp
592 21908da4 2019-01-13 stsp return NULL;
593 c65fcef2 2019-01-13 stsp } else if (errno == ENOENT) {
594 21908da4 2019-01-13 stsp err = make_parent_dirs(abspath);
595 21908da4 2019-01-13 stsp if (err)
596 c65fcef2 2019-01-13 stsp goto done;
597 c65fcef2 2019-01-13 stsp if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1)
598 c65fcef2 2019-01-13 stsp err = got_error_from_errno();
599 c65fcef2 2019-01-13 stsp } else
600 4a1ddfc2 2019-01-12 stsp err = got_error_from_errno();
601 4a1ddfc2 2019-01-12 stsp }
602 4a1ddfc2 2019-01-12 stsp
603 4a1ddfc2 2019-01-12 stsp done:
604 4a1ddfc2 2019-01-12 stsp free(abspath);
605 68c76935 2019-02-19 stsp return err;
606 68c76935 2019-02-19 stsp }
607 68c76935 2019-02-19 stsp
608 68c76935 2019-02-19 stsp static const struct got_error *
609 68c76935 2019-02-19 stsp check_file_contents_equal(int *same, FILE *f1, FILE *f2)
610 68c76935 2019-02-19 stsp {
611 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
612 68c76935 2019-02-19 stsp uint8_t fbuf1[8192];
613 68c76935 2019-02-19 stsp uint8_t fbuf2[8192];
614 68c76935 2019-02-19 stsp size_t flen1 = 0, flen2 = 0;
615 68c76935 2019-02-19 stsp
616 68c76935 2019-02-19 stsp *same = 1;
617 68c76935 2019-02-19 stsp
618 68c76935 2019-02-19 stsp while (1) {
619 68c76935 2019-02-19 stsp flen1 = fread(fbuf1, 1, sizeof(fbuf1), f1);
620 68c76935 2019-02-19 stsp if (flen1 == 0 && ferror(f1)) {
621 68c76935 2019-02-19 stsp err = got_error_from_errno();
622 68c76935 2019-02-19 stsp break;
623 68c76935 2019-02-19 stsp }
624 68c76935 2019-02-19 stsp flen2 = fread(fbuf2, 1, sizeof(fbuf2), f2);
625 68c76935 2019-02-19 stsp if (flen2 == 0 && ferror(f2)) {
626 68c76935 2019-02-19 stsp err = got_error_from_errno();
627 68c76935 2019-02-19 stsp break;
628 68c76935 2019-02-19 stsp }
629 68c76935 2019-02-19 stsp if (flen1 == 0) {
630 68c76935 2019-02-19 stsp if (flen2 != 0)
631 68c76935 2019-02-19 stsp *same = 0;
632 68c76935 2019-02-19 stsp break;
633 68c76935 2019-02-19 stsp } else if (flen2 == 0) {
634 68c76935 2019-02-19 stsp if (flen1 != 0)
635 68c76935 2019-02-19 stsp *same = 0;
636 68c76935 2019-02-19 stsp break;
637 68c76935 2019-02-19 stsp } else if (flen1 == flen2) {
638 68c76935 2019-02-19 stsp if (memcmp(fbuf1, fbuf2, flen2) != 0) {
639 68c76935 2019-02-19 stsp *same = 0;
640 68c76935 2019-02-19 stsp break;
641 68c76935 2019-02-19 stsp }
642 68c76935 2019-02-19 stsp } else {
643 68c76935 2019-02-19 stsp *same = 0;
644 68c76935 2019-02-19 stsp break;
645 68c76935 2019-02-19 stsp }
646 68c76935 2019-02-19 stsp }
647 68c76935 2019-02-19 stsp
648 68c76935 2019-02-19 stsp return err;
649 68c76935 2019-02-19 stsp }
650 68c76935 2019-02-19 stsp
651 68c76935 2019-02-19 stsp static const struct got_error *
652 68c76935 2019-02-19 stsp check_files_equal(int *same, const char *f1_path, const char *f2_path)
653 68c76935 2019-02-19 stsp {
654 68c76935 2019-02-19 stsp const struct got_error *err = NULL;
655 68c76935 2019-02-19 stsp struct stat sb;
656 68c76935 2019-02-19 stsp size_t size1, size2;
657 68c76935 2019-02-19 stsp FILE *f1 = NULL, *f2 = NULL;
658 68c76935 2019-02-19 stsp
659 68c76935 2019-02-19 stsp *same = 1;
660 68c76935 2019-02-19 stsp
661 68c76935 2019-02-19 stsp if (lstat(f1_path, &sb) != 0) {
662 68c76935 2019-02-19 stsp err = got_error_from_errno();
663 68c76935 2019-02-19 stsp goto done;
664 68c76935 2019-02-19 stsp }
665 68c76935 2019-02-19 stsp size1 = sb.st_size;
666 68c76935 2019-02-19 stsp
667 68c76935 2019-02-19 stsp if (lstat(f2_path, &sb) != 0) {
668 68c76935 2019-02-19 stsp err = got_error_from_errno();
669 68c76935 2019-02-19 stsp goto done;
670 68c76935 2019-02-19 stsp }
671 68c76935 2019-02-19 stsp size2 = sb.st_size;
672 68c76935 2019-02-19 stsp
673 68c76935 2019-02-19 stsp if (size1 != size2) {
674 68c76935 2019-02-19 stsp *same = 0;
675 68c76935 2019-02-19 stsp return NULL;
676 68c76935 2019-02-19 stsp }
677 68c76935 2019-02-19 stsp
678 68c76935 2019-02-19 stsp f1 = fopen(f1_path, "r");
679 68c76935 2019-02-19 stsp if (f1 == NULL)
680 68c76935 2019-02-19 stsp return got_error_from_errno();
681 68c76935 2019-02-19 stsp
682 68c76935 2019-02-19 stsp f2 = fopen(f2_path, "r");
683 68c76935 2019-02-19 stsp if (f2 == NULL) {
684 68c76935 2019-02-19 stsp err = got_error_from_errno();
685 68c76935 2019-02-19 stsp goto done;
686 68c76935 2019-02-19 stsp }
687 68c76935 2019-02-19 stsp
688 68c76935 2019-02-19 stsp err = check_file_contents_equal(same, f1, f2);
689 68c76935 2019-02-19 stsp done:
690 68c76935 2019-02-19 stsp if (f1 && fclose(f1) != 0 && err == NULL)
691 68c76935 2019-02-19 stsp err = got_error_from_errno();
692 68c76935 2019-02-19 stsp if (f2 && fclose(f2) != 0 && err == NULL)
693 68c76935 2019-02-19 stsp err = got_error_from_errno();
694 68c76935 2019-02-19 stsp
695 6353ad76 2019-02-08 stsp return err;
696 6353ad76 2019-02-08 stsp }
697 6353ad76 2019-02-08 stsp
698 6353ad76 2019-02-08 stsp /*
699 6353ad76 2019-02-08 stsp * Perform a 3-way merge where the file's version in the file index (blob2)
700 6353ad76 2019-02-08 stsp * acts as the common ancestor, the incoming blob (blob1) acts as the first
701 6353ad76 2019-02-08 stsp * derived version, and the file on disk acts as the second derived version.
702 6353ad76 2019-02-08 stsp */
703 6353ad76 2019-02-08 stsp static const struct got_error *
704 6353ad76 2019-02-08 stsp merge_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
705 6353ad76 2019-02-08 stsp struct got_fileindex_entry *ie, const char *ondisk_path, const char *path,
706 b8f41171 2019-02-10 stsp uint16_t te_mode, uint16_t st_mode, struct got_blob_object *blob1,
707 b8f41171 2019-02-10 stsp struct got_repository *repo,
708 6353ad76 2019-02-08 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
709 6353ad76 2019-02-08 stsp {
710 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
711 6353ad76 2019-02-08 stsp int merged_fd = -1;
712 6353ad76 2019-02-08 stsp struct got_blob_object *blob2 = NULL;
713 6353ad76 2019-02-08 stsp FILE *f1 = NULL, *f2 = NULL;
714 6353ad76 2019-02-08 stsp char *blob1_path = NULL, *blob2_path = NULL;
715 af54ae4a 2019-02-19 stsp char *merged_path = NULL, *base_path = NULL;
716 6353ad76 2019-02-08 stsp struct got_object_id id2;
717 6353ad76 2019-02-08 stsp char *id_str = NULL;
718 6353ad76 2019-02-08 stsp char *label1 = NULL;
719 68c76935 2019-02-19 stsp int overlapcnt = 0, update_timestamps = 0;
720 af54ae4a 2019-02-19 stsp char *parent;
721 6353ad76 2019-02-08 stsp
722 af54ae4a 2019-02-19 stsp parent = dirname(ondisk_path);
723 af54ae4a 2019-02-19 stsp if (parent == NULL)
724 af54ae4a 2019-02-19 stsp return got_error_from_errno();
725 af54ae4a 2019-02-19 stsp
726 af54ae4a 2019-02-19 stsp if (asprintf(&base_path, "%s/got-merged", parent) == -1)
727 af54ae4a 2019-02-19 stsp return got_error_from_errno();
728 af54ae4a 2019-02-19 stsp
729 af54ae4a 2019-02-19 stsp err = got_opentemp_named_fd(&merged_path, &merged_fd, base_path);
730 6353ad76 2019-02-08 stsp if (err)
731 af54ae4a 2019-02-19 stsp goto done;
732 af54ae4a 2019-02-19 stsp
733 af54ae4a 2019-02-19 stsp free(base_path);
734 af54ae4a 2019-02-19 stsp if (asprintf(&base_path, "%s/got-merge-blob1", parent) == -1) {
735 af54ae4a 2019-02-19 stsp err = got_error_from_errno();
736 af54ae4a 2019-02-19 stsp base_path = NULL;
737 af54ae4a 2019-02-19 stsp goto done;
738 af54ae4a 2019-02-19 stsp }
739 af54ae4a 2019-02-19 stsp
740 af54ae4a 2019-02-19 stsp err = got_opentemp_named(&blob1_path, &f1, base_path);
741 6353ad76 2019-02-08 stsp if (err)
742 6353ad76 2019-02-08 stsp goto done;
743 6353ad76 2019-02-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, f1, blob1);
744 6353ad76 2019-02-08 stsp if (err)
745 6353ad76 2019-02-08 stsp goto done;
746 6353ad76 2019-02-08 stsp
747 af54ae4a 2019-02-19 stsp free(base_path);
748 af54ae4a 2019-02-19 stsp if (asprintf(&base_path, "%s/got-merge-blob2", parent) == -1) {
749 af54ae4a 2019-02-19 stsp err = got_error_from_errno();
750 af54ae4a 2019-02-19 stsp base_path = NULL;
751 af54ae4a 2019-02-19 stsp goto done;
752 af54ae4a 2019-02-19 stsp }
753 af54ae4a 2019-02-19 stsp
754 af54ae4a 2019-02-19 stsp err = got_opentemp_named(&blob2_path, &f2, base_path);
755 6353ad76 2019-02-08 stsp if (err)
756 6353ad76 2019-02-08 stsp goto done;
757 6353ad76 2019-02-08 stsp
758 6353ad76 2019-02-08 stsp memcpy(id2.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
759 6353ad76 2019-02-08 stsp err = got_object_open_as_blob(&blob2, repo, &id2, 8192);
760 6353ad76 2019-02-08 stsp if (err)
761 6353ad76 2019-02-08 stsp goto done;
762 6353ad76 2019-02-08 stsp err = got_object_blob_dump_to_file(NULL, NULL, f2, blob2);
763 6353ad76 2019-02-08 stsp if (err)
764 6353ad76 2019-02-08 stsp goto done;
765 6353ad76 2019-02-08 stsp
766 6353ad76 2019-02-08 stsp err = got_object_id_str(&id_str, worktree->base_commit_id);
767 6353ad76 2019-02-08 stsp if (err)
768 6353ad76 2019-02-08 stsp goto done;
769 6353ad76 2019-02-08 stsp if (asprintf(&label1, "commit %s", id_str) == -1) {
770 6353ad76 2019-02-08 stsp err = got_error_from_errno();
771 6353ad76 2019-02-08 stsp goto done;
772 6353ad76 2019-02-08 stsp }
773 6353ad76 2019-02-08 stsp
774 6353ad76 2019-02-08 stsp err = got_merge_diff3(&overlapcnt, merged_fd, blob1_path,
775 6353ad76 2019-02-08 stsp blob2_path, ondisk_path, label1, path);
776 6353ad76 2019-02-08 stsp if (err)
777 6353ad76 2019-02-08 stsp goto done;
778 6353ad76 2019-02-08 stsp
779 6353ad76 2019-02-08 stsp (*progress_cb)(progress_arg,
780 6353ad76 2019-02-08 stsp overlapcnt > 0 ? GOT_STATUS_CONFLICT : GOT_STATUS_MERGE, path);
781 6353ad76 2019-02-08 stsp
782 6353ad76 2019-02-08 stsp
783 816dc654 2019-02-16 stsp if (fsync(merged_fd) != 0) {
784 816dc654 2019-02-16 stsp err = got_error_from_errno();
785 816dc654 2019-02-16 stsp goto done;
786 68c76935 2019-02-19 stsp }
787 68c76935 2019-02-19 stsp
788 68c76935 2019-02-19 stsp /* Check if a clean merge has subsumed all local changes. */
789 68c76935 2019-02-19 stsp if (overlapcnt == 0) {
790 68c76935 2019-02-19 stsp err = check_files_equal(&update_timestamps, blob1_path,
791 68c76935 2019-02-19 stsp merged_path);
792 68c76935 2019-02-19 stsp if (err)
793 68c76935 2019-02-19 stsp goto done;
794 816dc654 2019-02-16 stsp }
795 6353ad76 2019-02-08 stsp
796 70a0c8ec 2019-02-20 stsp if (chmod(merged_path, st_mode) != 0) {
797 70a0c8ec 2019-02-20 stsp err = got_error_from_errno();
798 70a0c8ec 2019-02-20 stsp goto done;
799 70a0c8ec 2019-02-20 stsp }
800 70a0c8ec 2019-02-20 stsp
801 6353ad76 2019-02-08 stsp if (rename(merged_path, ondisk_path) != 0) {
802 6353ad76 2019-02-08 stsp err = got_error_from_errno();
803 2a57020b 2019-02-20 stsp unlink(merged_path);
804 6353ad76 2019-02-08 stsp goto done;
805 6353ad76 2019-02-08 stsp }
806 6353ad76 2019-02-08 stsp
807 02c07007 2019-02-10 stsp /*
808 02c07007 2019-02-10 stsp * Do not update timestamps of already modified files. Otherwise,
809 65ad4e61 2019-02-20 stsp * a future status walk would treat them as unmodified files again.
810 02c07007 2019-02-10 stsp */
811 6353ad76 2019-02-08 stsp err = got_fileindex_entry_update(ie, ondisk_path,
812 68c76935 2019-02-19 stsp blob1->id.sha1, worktree->base_commit_id->sha1, update_timestamps);
813 6353ad76 2019-02-08 stsp done:
814 3a6ce05a 2019-02-11 stsp if (merged_fd != -1 && close(merged_fd) != 0 && err == NULL)
815 3a6ce05a 2019-02-11 stsp err = got_error_from_errno();
816 3a6ce05a 2019-02-11 stsp if (f1 && fclose(f1) != 0 && err == NULL)
817 3a6ce05a 2019-02-11 stsp err = got_error_from_errno();
818 3a6ce05a 2019-02-11 stsp if (f2 && fclose(f2) != 0 && err == NULL)
819 3a6ce05a 2019-02-11 stsp err = got_error_from_errno();
820 6353ad76 2019-02-08 stsp if (blob2)
821 6353ad76 2019-02-08 stsp got_object_blob_close(blob2);
822 6353ad76 2019-02-08 stsp free(merged_path);
823 af54ae4a 2019-02-19 stsp free(base_path);
824 6353ad76 2019-02-08 stsp if (blob1_path) {
825 6353ad76 2019-02-08 stsp unlink(blob1_path);
826 6353ad76 2019-02-08 stsp free(blob1_path);
827 6353ad76 2019-02-08 stsp }
828 6353ad76 2019-02-08 stsp if (blob2_path) {
829 6353ad76 2019-02-08 stsp unlink(blob2_path);
830 6353ad76 2019-02-08 stsp free(blob2_path);
831 6353ad76 2019-02-08 stsp }
832 6353ad76 2019-02-08 stsp free(id_str);
833 6353ad76 2019-02-08 stsp free(label1);
834 4a1ddfc2 2019-01-12 stsp return err;
835 4a1ddfc2 2019-01-12 stsp }
836 4a1ddfc2 2019-01-12 stsp
837 4a1ddfc2 2019-01-12 stsp static const struct got_error *
838 8da9e5f4 2019-01-12 stsp install_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
839 6353ad76 2019-02-08 stsp struct got_fileindex_entry *entry, const char *ondisk_path, const char *path,
840 b8f41171 2019-02-10 stsp uint16_t te_mode, uint16_t st_mode, struct got_blob_object *blob,
841 b8f41171 2019-02-10 stsp int restoring_missing_file, struct got_repository *repo,
842 b8f41171 2019-02-10 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
843 9d31a1d8 2018-03-11 stsp {
844 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
845 507dc3bb 2018-12-29 stsp int fd = -1;
846 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
847 507dc3bb 2018-12-29 stsp int update = 0;
848 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
849 9d31a1d8 2018-03-11 stsp
850 c34b20a2 2018-03-12 stsp fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
851 9d31a1d8 2018-03-11 stsp GOT_DEFAULT_FILE_MODE);
852 9d31a1d8 2018-03-11 stsp if (fd == -1) {
853 21908da4 2019-01-13 stsp if (errno == ENOENT) {
854 21908da4 2019-01-13 stsp char *parent = dirname(path);
855 21908da4 2019-01-13 stsp if (parent == NULL)
856 21908da4 2019-01-13 stsp return got_error_from_errno();
857 21908da4 2019-01-13 stsp err = add_dir_on_disk(worktree, parent);
858 21908da4 2019-01-13 stsp if (err)
859 21908da4 2019-01-13 stsp return err;
860 21908da4 2019-01-13 stsp fd = open(ondisk_path,
861 21908da4 2019-01-13 stsp O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
862 21908da4 2019-01-13 stsp GOT_DEFAULT_FILE_MODE);
863 21908da4 2019-01-13 stsp if (fd == -1)
864 21908da4 2019-01-13 stsp return got_error_from_errno();
865 21908da4 2019-01-13 stsp } else if (errno == EEXIST) {
866 b8f41171 2019-02-10 stsp if (!S_ISREG(st_mode)) {
867 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
868 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_FILE_OBSTRUCTED);
869 507dc3bb 2018-12-29 stsp goto done;
870 d70b8e30 2018-12-27 stsp } else {
871 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
872 507dc3bb 2018-12-29 stsp ondisk_path);
873 507dc3bb 2018-12-29 stsp if (err)
874 507dc3bb 2018-12-29 stsp goto done;
875 507dc3bb 2018-12-29 stsp update = 1;
876 9d31a1d8 2018-03-11 stsp }
877 507dc3bb 2018-12-29 stsp } else
878 7e7c1e4c 2019-01-12 stsp return got_error_from_errno();
879 9d31a1d8 2018-03-11 stsp }
880 9d31a1d8 2018-03-11 stsp
881 a378724f 2019-02-10 stsp if (restoring_missing_file)
882 a378724f 2019-02-10 stsp (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
883 a378724f 2019-02-10 stsp else
884 a378724f 2019-02-10 stsp (*progress_cb)(progress_arg,
885 a378724f 2019-02-10 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
886 d7b62c98 2018-12-27 stsp
887 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
888 9d31a1d8 2018-03-11 stsp do {
889 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
890 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
891 9d31a1d8 2018-03-11 stsp if (err)
892 9d31a1d8 2018-03-11 stsp break;
893 9d31a1d8 2018-03-11 stsp if (len > 0) {
894 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
895 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
896 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
897 9d31a1d8 2018-03-11 stsp err = got_error_from_errno();
898 b87c6f83 2018-12-24 stsp goto done;
899 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
900 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
901 b87c6f83 2018-12-24 stsp goto done;
902 9d31a1d8 2018-03-11 stsp }
903 61d6eaa3 2018-12-24 stsp hdrlen = 0;
904 9d31a1d8 2018-03-11 stsp }
905 9d31a1d8 2018-03-11 stsp } while (len != 0);
906 9d31a1d8 2018-03-11 stsp
907 816dc654 2019-02-16 stsp if (fsync(fd) != 0) {
908 816dc654 2019-02-16 stsp err = got_error_from_errno();
909 816dc654 2019-02-16 stsp goto done;
910 816dc654 2019-02-16 stsp }
911 9d31a1d8 2018-03-11 stsp
912 507dc3bb 2018-12-29 stsp if (update) {
913 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
914 68ed9ba5 2019-02-10 stsp err = got_error_from_errno();
915 2a57020b 2019-02-20 stsp unlink(tmppath);
916 68ed9ba5 2019-02-10 stsp goto done;
917 68ed9ba5 2019-02-10 stsp }
918 68ed9ba5 2019-02-10 stsp }
919 ba8a0d4d 2019-02-10 stsp
920 b8f41171 2019-02-10 stsp if (te_mode & S_IXUSR) {
921 b8f41171 2019-02-10 stsp if (chmod(ondisk_path, st_mode | S_IXUSR) == -1) {
922 507dc3bb 2018-12-29 stsp err = got_error_from_errno();
923 507dc3bb 2018-12-29 stsp goto done;
924 507dc3bb 2018-12-29 stsp }
925 ba8a0d4d 2019-02-10 stsp } else {
926 b8f41171 2019-02-10 stsp if (chmod(ondisk_path, st_mode & ~S_IXUSR) == -1) {
927 68ed9ba5 2019-02-10 stsp err = got_error_from_errno();
928 68ed9ba5 2019-02-10 stsp goto done;
929 68ed9ba5 2019-02-10 stsp }
930 507dc3bb 2018-12-29 stsp }
931 507dc3bb 2018-12-29 stsp
932 5f9fef37 2019-01-13 stsp if (entry == NULL)
933 5f9fef37 2019-01-13 stsp entry = got_fileindex_entry_get(fileindex, path);
934 51514078 2018-12-25 stsp if (entry)
935 51514078 2018-12-25 stsp err = got_fileindex_entry_update(entry, ondisk_path,
936 02c07007 2019-02-10 stsp blob->id.sha1, worktree->base_commit_id->sha1, 1);
937 51514078 2018-12-25 stsp else {
938 51514078 2018-12-25 stsp err = got_fileindex_entry_alloc(&entry, ondisk_path,
939 8da9e5f4 2019-01-12 stsp path, blob->id.sha1, worktree->base_commit_id->sha1);
940 51514078 2018-12-25 stsp if (err)
941 51514078 2018-12-25 stsp goto done;
942 51514078 2018-12-25 stsp err = got_fileindex_entry_add(fileindex, entry);
943 51514078 2018-12-25 stsp }
944 9d31a1d8 2018-03-11 stsp done:
945 3a6ce05a 2019-02-11 stsp if (fd != -1 && close(fd) != 0 && err == NULL)
946 3a6ce05a 2019-02-11 stsp err = got_error_from_errno();
947 507dc3bb 2018-12-29 stsp free(tmppath);
948 6353ad76 2019-02-08 stsp return err;
949 6353ad76 2019-02-08 stsp }
950 6353ad76 2019-02-08 stsp
951 6353ad76 2019-02-08 stsp static const struct got_error *
952 b8f41171 2019-02-10 stsp get_file_status(unsigned char *status, struct stat *sb,
953 b8f41171 2019-02-10 stsp struct got_fileindex_entry *ie, const char *abspath,
954 b8f41171 2019-02-10 stsp struct got_repository *repo)
955 6353ad76 2019-02-08 stsp {
956 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
957 6353ad76 2019-02-08 stsp struct got_object_id id;
958 6353ad76 2019-02-08 stsp size_t hdrlen;
959 6353ad76 2019-02-08 stsp FILE *f = NULL;
960 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
961 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
962 6353ad76 2019-02-08 stsp size_t flen, blen;
963 6353ad76 2019-02-08 stsp
964 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
965 6353ad76 2019-02-08 stsp
966 b8f41171 2019-02-10 stsp if (lstat(abspath, sb) == -1) {
967 a378724f 2019-02-10 stsp if (errno == ENOENT) {
968 b8f41171 2019-02-10 stsp if (ie) {
969 b8f41171 2019-02-10 stsp *status = GOT_STATUS_MISSING;
970 b8f41171 2019-02-10 stsp sb->st_mode =
971 b8f41171 2019-02-10 stsp ((ie->mode >> GOT_FILEIDX_MODE_PERMS_SHIFT)
972 b8f41171 2019-02-10 stsp & (S_IRWXU | S_IRWXG | S_IRWXO));
973 b8f41171 2019-02-10 stsp } else
974 b8f41171 2019-02-10 stsp sb->st_mode = GOT_DEFAULT_FILE_MODE;
975 a378724f 2019-02-10 stsp return NULL;
976 a378724f 2019-02-10 stsp }
977 6353ad76 2019-02-08 stsp return got_error_from_errno();
978 a378724f 2019-02-10 stsp }
979 6353ad76 2019-02-08 stsp
980 b8f41171 2019-02-10 stsp if (!S_ISREG(sb->st_mode)) {
981 6353ad76 2019-02-08 stsp *status = GOT_STATUS_OBSTRUCTED;
982 6353ad76 2019-02-08 stsp return NULL;
983 6353ad76 2019-02-08 stsp }
984 6353ad76 2019-02-08 stsp
985 b8f41171 2019-02-10 stsp if (ie == NULL)
986 b8f41171 2019-02-10 stsp return NULL;
987 b8f41171 2019-02-10 stsp
988 b8f41171 2019-02-10 stsp if (ie->ctime_sec == sb->st_ctime &&
989 b8f41171 2019-02-10 stsp ie->ctime_nsec == sb->st_ctimensec &&
990 b8f41171 2019-02-10 stsp ie->mtime_sec == sb->st_mtime &&
991 b8f41171 2019-02-10 stsp ie->mtime_sec == sb->st_mtime &&
992 b8f41171 2019-02-10 stsp ie->mtime_nsec == sb->st_mtimensec &&
993 b8f41171 2019-02-10 stsp ie->size == (sb->st_size & 0xffffffff))
994 6353ad76 2019-02-08 stsp return NULL;
995 6353ad76 2019-02-08 stsp
996 6353ad76 2019-02-08 stsp memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
997 6353ad76 2019-02-08 stsp err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
998 6353ad76 2019-02-08 stsp if (err)
999 6353ad76 2019-02-08 stsp return err;
1000 6353ad76 2019-02-08 stsp
1001 6353ad76 2019-02-08 stsp f = fopen(abspath, "r");
1002 6353ad76 2019-02-08 stsp if (f == NULL) {
1003 6353ad76 2019-02-08 stsp err = got_error_from_errno();
1004 6353ad76 2019-02-08 stsp goto done;
1005 6353ad76 2019-02-08 stsp }
1006 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1007 6353ad76 2019-02-08 stsp while (1) {
1008 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1009 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
1010 6353ad76 2019-02-08 stsp if (err)
1011 6353ad76 2019-02-08 stsp break;
1012 3cbbd752 2019-02-19 stsp /* Skip length of blob object header first time around. */
1013 3cbbd752 2019-02-19 stsp flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1014 80c5c120 2019-02-19 stsp if (flen == 0 && ferror(f)) {
1015 80c5c120 2019-02-19 stsp err = got_error_from_errno();
1016 80c5c120 2019-02-19 stsp break;
1017 80c5c120 2019-02-19 stsp }
1018 6353ad76 2019-02-08 stsp if (blen == 0) {
1019 6353ad76 2019-02-08 stsp if (flen != 0)
1020 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1021 6353ad76 2019-02-08 stsp break;
1022 6353ad76 2019-02-08 stsp } else if (flen == 0) {
1023 6353ad76 2019-02-08 stsp if (blen != 0)
1024 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1025 6353ad76 2019-02-08 stsp break;
1026 6353ad76 2019-02-08 stsp } else if (blen - hdrlen == flen) {
1027 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
1028 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1029 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1030 6353ad76 2019-02-08 stsp break;
1031 6353ad76 2019-02-08 stsp }
1032 6353ad76 2019-02-08 stsp } else {
1033 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1034 6353ad76 2019-02-08 stsp break;
1035 6353ad76 2019-02-08 stsp }
1036 6353ad76 2019-02-08 stsp hdrlen = 0;
1037 6353ad76 2019-02-08 stsp }
1038 6353ad76 2019-02-08 stsp done:
1039 6353ad76 2019-02-08 stsp if (blob)
1040 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
1041 6353ad76 2019-02-08 stsp if (f)
1042 6353ad76 2019-02-08 stsp fclose(f);
1043 9d31a1d8 2018-03-11 stsp return err;
1044 9d31a1d8 2018-03-11 stsp }
1045 9d31a1d8 2018-03-11 stsp
1046 9d31a1d8 2018-03-11 stsp static const struct got_error *
1047 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1048 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1049 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1050 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1051 8da9e5f4 2019-01-12 stsp void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1052 9d31a1d8 2018-03-11 stsp {
1053 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1054 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1055 6353ad76 2019-02-08 stsp char *ondisk_path;
1056 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1057 b8f41171 2019-02-10 stsp struct stat sb;
1058 9d31a1d8 2018-03-11 stsp
1059 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1060 6353ad76 2019-02-08 stsp return got_error_from_errno();
1061 6353ad76 2019-02-08 stsp
1062 b8f41171 2019-02-10 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1063 b8f41171 2019-02-10 stsp if (err)
1064 b8f41171 2019-02-10 stsp goto done;
1065 6353ad76 2019-02-08 stsp
1066 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1067 b8f41171 2019-02-10 stsp (*progress_cb)(progress_arg, status, path);
1068 b8f41171 2019-02-10 stsp goto done;
1069 b8f41171 2019-02-10 stsp }
1070 b8f41171 2019-02-10 stsp
1071 b8f41171 2019-02-10 stsp if (ie && status != GOT_STATUS_MISSING) {
1072 b8f41171 2019-02-10 stsp if (memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1073 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
1074 b8f41171 2019-02-10 stsp (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1075 b8f41171 2019-02-10 stsp path);
1076 6353ad76 2019-02-08 stsp goto done;
1077 a378724f 2019-02-10 stsp }
1078 b8f41171 2019-02-10 stsp if (memcmp(ie->blob_sha1,
1079 b8f41171 2019-02-10 stsp te->id->sha1, SHA1_DIGEST_LENGTH) == 0)
1080 b8f41171 2019-02-10 stsp goto done;
1081 9d31a1d8 2018-03-11 stsp }
1082 9d31a1d8 2018-03-11 stsp
1083 8da9e5f4 2019-01-12 stsp err = got_object_open_as_blob(&blob, repo, te->id, 8192);
1084 8da9e5f4 2019-01-12 stsp if (err)
1085 6353ad76 2019-02-08 stsp goto done;
1086 512f0d0e 2019-01-02 stsp
1087 276262e8 2019-02-08 stsp if (status == GOT_STATUS_MODIFY)
1088 6353ad76 2019-02-08 stsp err = merge_blob(worktree, fileindex, ie, ondisk_path, path,
1089 b8f41171 2019-02-10 stsp te->mode, sb.st_mode, blob, repo, progress_cb,
1090 b8f41171 2019-02-10 stsp progress_arg);
1091 6353ad76 2019-02-08 stsp else
1092 6353ad76 2019-02-08 stsp err = install_blob(worktree, fileindex, ie, ondisk_path, path,
1093 b8f41171 2019-02-10 stsp te->mode, sb.st_mode, blob, status == GOT_STATUS_MISSING,
1094 b8f41171 2019-02-10 stsp repo, progress_cb, progress_arg);
1095 6353ad76 2019-02-08 stsp
1096 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
1097 6353ad76 2019-02-08 stsp done:
1098 6353ad76 2019-02-08 stsp free(ondisk_path);
1099 8da9e5f4 2019-01-12 stsp return err;
1100 90285c3b 2019-01-08 stsp }
1101 90285c3b 2019-01-08 stsp
1102 90285c3b 2019-01-08 stsp static const struct got_error *
1103 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
1104 90285c3b 2019-01-08 stsp {
1105 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
1106 90285c3b 2019-01-08 stsp char *ondisk_path = NULL;
1107 90285c3b 2019-01-08 stsp
1108 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1109 90285c3b 2019-01-08 stsp return got_error_from_errno();
1110 90285c3b 2019-01-08 stsp
1111 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
1112 c97665ae 2019-01-13 stsp if (errno != ENOENT)
1113 c97665ae 2019-01-13 stsp err = got_error_from_errno();
1114 c97665ae 2019-01-13 stsp } else {
1115 90285c3b 2019-01-08 stsp char *parent = dirname(ondisk_path);
1116 7a9df742 2019-01-08 stsp while (parent && strcmp(parent, root_path) != 0) {
1117 26c4ac4d 2019-01-08 stsp if (rmdir(parent) == -1) {
1118 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
1119 26c4ac4d 2019-01-08 stsp err = got_error_from_errno();
1120 90285c3b 2019-01-08 stsp break;
1121 90285c3b 2019-01-08 stsp }
1122 90285c3b 2019-01-08 stsp parent = dirname(parent);
1123 90285c3b 2019-01-08 stsp }
1124 90285c3b 2019-01-08 stsp }
1125 90285c3b 2019-01-08 stsp free(ondisk_path);
1126 90285c3b 2019-01-08 stsp return err;
1127 512f0d0e 2019-01-02 stsp }
1128 512f0d0e 2019-01-02 stsp
1129 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
1130 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
1131 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
1132 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
1133 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
1134 8da9e5f4 2019-01-12 stsp void *progress_arg;
1135 8da9e5f4 2019-01-12 stsp got_worktree_cancel_cb cancel_cb;
1136 8da9e5f4 2019-01-12 stsp void *cancel_arg;
1137 8da9e5f4 2019-01-12 stsp };
1138 8da9e5f4 2019-01-12 stsp
1139 512f0d0e 2019-01-02 stsp static const struct got_error *
1140 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
1141 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
1142 512f0d0e 2019-01-02 stsp {
1143 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
1144 512f0d0e 2019-01-02 stsp
1145 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
1146 8da9e5f4 2019-01-12 stsp ie->path, a->repo, a->progress_cb, a->progress_arg,
1147 8da9e5f4 2019-01-12 stsp a->cancel_cb, a->cancel_arg);
1148 8da9e5f4 2019-01-12 stsp }
1149 512f0d0e 2019-01-02 stsp
1150 8da9e5f4 2019-01-12 stsp static const struct got_error *
1151 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1152 8da9e5f4 2019-01-12 stsp {
1153 8da9e5f4 2019-01-12 stsp const struct got_error *err;
1154 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
1155 7a9df742 2019-01-08 stsp
1156 8da9e5f4 2019-01-12 stsp (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE, ie->path);
1157 7a9df742 2019-01-08 stsp
1158 8da9e5f4 2019-01-12 stsp err = remove_ondisk_file(a->worktree->root_path, ie->path);
1159 8da9e5f4 2019-01-12 stsp if (err)
1160 8da9e5f4 2019-01-12 stsp return err;
1161 8da9e5f4 2019-01-12 stsp got_fileindex_entry_remove(a->fileindex, ie);
1162 8da9e5f4 2019-01-12 stsp return NULL;
1163 9d31a1d8 2018-03-11 stsp }
1164 9d31a1d8 2018-03-11 stsp
1165 9d31a1d8 2018-03-11 stsp static const struct got_error *
1166 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1167 9d31a1d8 2018-03-11 stsp {
1168 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
1169 8da9e5f4 2019-01-12 stsp const struct got_error *err;
1170 8da9e5f4 2019-01-12 stsp char *path;
1171 9d31a1d8 2018-03-11 stsp
1172 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
1173 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
1174 8da9e5f4 2019-01-12 stsp == -1)
1175 8da9e5f4 2019-01-12 stsp return got_error_from_errno();
1176 9d31a1d8 2018-03-11 stsp
1177 8da9e5f4 2019-01-12 stsp if (S_ISDIR(te->mode))
1178 8da9e5f4 2019-01-12 stsp err = add_dir_on_disk(a->worktree, path);
1179 8da9e5f4 2019-01-12 stsp else
1180 8da9e5f4 2019-01-12 stsp err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1181 8da9e5f4 2019-01-12 stsp a->repo, a->progress_cb, a->progress_arg,
1182 8da9e5f4 2019-01-12 stsp a->cancel_cb, a->cancel_arg);
1183 9d31a1d8 2018-03-11 stsp
1184 8da9e5f4 2019-01-12 stsp free(path);
1185 9d31a1d8 2018-03-11 stsp return err;
1186 9d31a1d8 2018-03-11 stsp }
1187 9d31a1d8 2018-03-11 stsp
1188 9d31a1d8 2018-03-11 stsp const struct got_error *
1189 9d31a1d8 2018-03-11 stsp got_worktree_checkout_files(struct got_worktree *worktree,
1190 93a30277 2018-12-24 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1191 93a30277 2018-12-24 stsp void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1192 9d31a1d8 2018-03-11 stsp {
1193 a143fb78 2018-12-25 stsp const struct got_error *err = NULL, *unlockerr, *checkout_err = NULL;
1194 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
1195 8da9e5f4 2019-01-12 stsp struct got_object_id *tree_id = NULL;
1196 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
1197 9d31a1d8 2018-03-11 stsp char *fileindex_path = NULL, *new_fileindex_path = NULL;
1198 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
1199 51514078 2018-12-25 stsp FILE *index = NULL, *new_index = NULL;
1200 f44ffd20 2019-02-04 stsp struct got_fileindex_diff_tree_cb diff_cb;
1201 8da9e5f4 2019-01-12 stsp struct diff_cb_arg arg;
1202 9d31a1d8 2018-03-11 stsp
1203 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
1204 9d31a1d8 2018-03-11 stsp if (err)
1205 9d31a1d8 2018-03-11 stsp return err;
1206 93a30277 2018-12-24 stsp
1207 133d2798 2019-01-08 stsp fileindex = got_fileindex_alloc();
1208 133d2798 2019-01-08 stsp if (fileindex == NULL) {
1209 133d2798 2019-01-08 stsp err = got_error_from_errno();
1210 9d31a1d8 2018-03-11 stsp goto done;
1211 133d2798 2019-01-08 stsp }
1212 9d31a1d8 2018-03-11 stsp
1213 9d31a1d8 2018-03-11 stsp if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1214 9d31a1d8 2018-03-11 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1215 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
1216 9d31a1d8 2018-03-11 stsp fileindex_path = NULL;
1217 9d31a1d8 2018-03-11 stsp goto done;
1218 9d31a1d8 2018-03-11 stsp }
1219 9d31a1d8 2018-03-11 stsp
1220 51514078 2018-12-25 stsp /*
1221 51514078 2018-12-25 stsp * Read the file index.
1222 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
1223 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
1224 51514078 2018-12-25 stsp */
1225 51514078 2018-12-25 stsp index = fopen(fileindex_path, "rb");
1226 51514078 2018-12-25 stsp if (index == NULL) {
1227 cf61d818 2019-01-12 stsp if (errno != ENOENT) {
1228 cf61d818 2019-01-12 stsp err = got_error_from_errno();
1229 cf61d818 2019-01-12 stsp goto done;
1230 cf61d818 2019-01-12 stsp }
1231 cf61d818 2019-01-12 stsp } else {
1232 cf61d818 2019-01-12 stsp err = got_fileindex_read(fileindex, index);
1233 cf61d818 2019-01-12 stsp fclose(index);
1234 cf61d818 2019-01-12 stsp if (err)
1235 cf61d818 2019-01-12 stsp goto done;
1236 51514078 2018-12-25 stsp }
1237 51514078 2018-12-25 stsp
1238 b8bdcc21 2018-12-24 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
1239 b8bdcc21 2018-12-24 stsp fileindex_path);
1240 51664889 2018-03-12 stsp if (err)
1241 51664889 2018-03-12 stsp goto done;
1242 51664889 2018-03-12 stsp
1243 eaccb85f 2018-12-25 stsp err = got_object_open_as_commit(&commit, repo,
1244 eaccb85f 2018-12-25 stsp worktree->base_commit_id);
1245 9d31a1d8 2018-03-11 stsp if (err)
1246 9d31a1d8 2018-03-11 stsp goto done;
1247 9d31a1d8 2018-03-11 stsp
1248 8da9e5f4 2019-01-12 stsp err = got_object_id_by_path(&tree_id, repo,
1249 8da9e5f4 2019-01-12 stsp worktree->base_commit_id, worktree->path_prefix);
1250 9d31a1d8 2018-03-11 stsp if (err)
1251 9d31a1d8 2018-03-11 stsp goto done;
1252 9d31a1d8 2018-03-11 stsp
1253 8da9e5f4 2019-01-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
1254 8da9e5f4 2019-01-12 stsp if (err)
1255 8da9e5f4 2019-01-12 stsp goto done;
1256 9d31a1d8 2018-03-11 stsp
1257 8da9e5f4 2019-01-12 stsp diff_cb.diff_old_new = diff_old_new;
1258 8da9e5f4 2019-01-12 stsp diff_cb.diff_old = diff_old;
1259 8da9e5f4 2019-01-12 stsp diff_cb.diff_new = diff_new;
1260 8da9e5f4 2019-01-12 stsp arg.fileindex = fileindex;
1261 8da9e5f4 2019-01-12 stsp arg.worktree = worktree;
1262 8da9e5f4 2019-01-12 stsp arg.repo = repo;
1263 8da9e5f4 2019-01-12 stsp arg.progress_cb = progress_cb;
1264 8da9e5f4 2019-01-12 stsp arg.progress_arg = progress_arg;
1265 8da9e5f4 2019-01-12 stsp arg.cancel_cb = cancel_cb;
1266 8da9e5f4 2019-01-12 stsp arg.cancel_arg = cancel_arg;
1267 8da9e5f4 2019-01-12 stsp checkout_err = got_fileindex_diff_tree(fileindex, tree, repo,
1268 8da9e5f4 2019-01-12 stsp &diff_cb, &arg);
1269 8da9e5f4 2019-01-12 stsp
1270 a143fb78 2018-12-25 stsp /* Try to sync the fileindex back to disk in any case. */
1271 b8bdcc21 2018-12-24 stsp err = got_fileindex_write(fileindex, new_index);
1272 9d31a1d8 2018-03-11 stsp if (err)
1273 9d31a1d8 2018-03-11 stsp goto done;
1274 9d31a1d8 2018-03-11 stsp
1275 9d31a1d8 2018-03-11 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
1276 9d31a1d8 2018-03-11 stsp err = got_error_from_errno();
1277 2a57020b 2019-02-20 stsp unlink(new_fileindex_path);
1278 9d31a1d8 2018-03-11 stsp goto done;
1279 9d31a1d8 2018-03-11 stsp }
1280 9d31a1d8 2018-03-11 stsp
1281 9d31a1d8 2018-03-11 stsp free(new_fileindex_path);
1282 9d31a1d8 2018-03-11 stsp new_fileindex_path = NULL;
1283 9d31a1d8 2018-03-11 stsp
1284 9d31a1d8 2018-03-11 stsp done:
1285 edfa7d7f 2018-09-11 stsp if (tree)
1286 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
1287 9d31a1d8 2018-03-11 stsp if (commit)
1288 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
1289 9d31a1d8 2018-03-11 stsp if (new_fileindex_path)
1290 9d31a1d8 2018-03-11 stsp unlink(new_fileindex_path);
1291 b8bdcc21 2018-12-24 stsp if (new_index)
1292 b8bdcc21 2018-12-24 stsp fclose(new_index);
1293 9d31a1d8 2018-03-11 stsp free(new_fileindex_path);
1294 9d31a1d8 2018-03-11 stsp free(fileindex_path);
1295 7426bbfd 2018-12-24 stsp got_fileindex_free(fileindex);
1296 a143fb78 2018-12-25 stsp if (checkout_err)
1297 a143fb78 2018-12-25 stsp err = checkout_err;
1298 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
1299 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
1300 9d31a1d8 2018-03-11 stsp err = unlockerr;
1301 f8d1f275 2019-02-04 stsp return err;
1302 f8d1f275 2019-02-04 stsp }
1303 f8d1f275 2019-02-04 stsp
1304 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
1305 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
1306 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
1307 927df6b7 2019-02-10 stsp const char *status_path;
1308 927df6b7 2019-02-10 stsp size_t status_path_len;
1309 f8d1f275 2019-02-04 stsp struct got_repository *repo;
1310 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
1311 f8d1f275 2019-02-04 stsp void *status_arg;
1312 f8d1f275 2019-02-04 stsp got_worktree_cancel_cb cancel_cb;
1313 f8d1f275 2019-02-04 stsp void *cancel_arg;
1314 f8d1f275 2019-02-04 stsp };
1315 f8d1f275 2019-02-04 stsp
1316 f8d1f275 2019-02-04 stsp static const struct got_error *
1317 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
1318 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
1319 927df6b7 2019-02-10 stsp struct got_repository *repo)
1320 927df6b7 2019-02-10 stsp {
1321 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
1322 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1323 927df6b7 2019-02-10 stsp struct stat sb;
1324 927df6b7 2019-02-10 stsp struct got_object_id id;
1325 927df6b7 2019-02-10 stsp
1326 927df6b7 2019-02-10 stsp err = get_file_status(&status, &sb, ie, abspath, repo);
1327 927df6b7 2019-02-10 stsp if (err == NULL && status != GOT_STATUS_NO_CHANGE) {
1328 927df6b7 2019-02-10 stsp memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1329 927df6b7 2019-02-10 stsp err = (*status_cb)(status_arg, status, ie->path, &id);
1330 927df6b7 2019-02-10 stsp }
1331 927df6b7 2019-02-10 stsp return err;
1332 927df6b7 2019-02-10 stsp }
1333 927df6b7 2019-02-10 stsp
1334 927df6b7 2019-02-10 stsp static const struct got_error *
1335 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
1336 f8d1f275 2019-02-04 stsp struct dirent *de, const char *parent_path)
1337 f8d1f275 2019-02-04 stsp {
1338 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
1339 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
1340 f8d1f275 2019-02-04 stsp char *abspath;
1341 f8d1f275 2019-02-04 stsp
1342 927df6b7 2019-02-10 stsp if (got_path_cmp(parent_path, a->status_path) != 0 &&
1343 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
1344 927df6b7 2019-02-10 stsp return NULL;
1345 927df6b7 2019-02-10 stsp
1346 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
1347 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
1348 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
1349 f8d1f275 2019-02-04 stsp return got_error_from_errno();
1350 f8d1f275 2019-02-04 stsp } else {
1351 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
1352 f8d1f275 2019-02-04 stsp de->d_name) == -1)
1353 f8d1f275 2019-02-04 stsp return got_error_from_errno();
1354 f8d1f275 2019-02-04 stsp }
1355 f8d1f275 2019-02-04 stsp
1356 927df6b7 2019-02-10 stsp err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
1357 927df6b7 2019-02-10 stsp a->repo);
1358 f8d1f275 2019-02-04 stsp free(abspath);
1359 9d31a1d8 2018-03-11 stsp return err;
1360 9d31a1d8 2018-03-11 stsp }
1361 f8d1f275 2019-02-04 stsp
1362 f8d1f275 2019-02-04 stsp static const struct got_error *
1363 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1364 f8d1f275 2019-02-04 stsp {
1365 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
1366 b72f483a 2019-02-05 stsp struct got_object_id id;
1367 927df6b7 2019-02-10 stsp
1368 927df6b7 2019-02-10 stsp if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
1369 927df6b7 2019-02-10 stsp return NULL;
1370 927df6b7 2019-02-10 stsp
1371 b72f483a 2019-02-05 stsp memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1372 b72f483a 2019-02-05 stsp return (*a->status_cb)(a->status_arg, GOT_STATUS_MISSING, ie->path,
1373 b72f483a 2019-02-05 stsp &id);
1374 f8d1f275 2019-02-04 stsp }
1375 f8d1f275 2019-02-04 stsp
1376 f8d1f275 2019-02-04 stsp static const struct got_error *
1377 f8d1f275 2019-02-04 stsp status_new(void *arg, struct dirent *de, const char *parent_path)
1378 f8d1f275 2019-02-04 stsp {
1379 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
1380 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
1381 f8d1f275 2019-02-04 stsp char *path = NULL;
1382 f8d1f275 2019-02-04 stsp
1383 f8d1f275 2019-02-04 stsp if (de->d_type == DT_DIR)
1384 2c201a36 2019-02-10 stsp return NULL;
1385 2c201a36 2019-02-10 stsp
1386 2c201a36 2019-02-10 stsp /* XXX ignore symlinks for now */
1387 2c201a36 2019-02-10 stsp if (de->d_type == DT_LNK)
1388 f8d1f275 2019-02-04 stsp return NULL;
1389 f8d1f275 2019-02-04 stsp
1390 927df6b7 2019-02-10 stsp if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
1391 927df6b7 2019-02-10 stsp return NULL;
1392 927df6b7 2019-02-10 stsp
1393 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
1394 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
1395 f8d1f275 2019-02-04 stsp return got_error_from_errno();
1396 f8d1f275 2019-02-04 stsp } else {
1397 f8d1f275 2019-02-04 stsp path = de->d_name;
1398 f8d1f275 2019-02-04 stsp }
1399 f8d1f275 2019-02-04 stsp
1400 b72f483a 2019-02-05 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED, path,
1401 b72f483a 2019-02-05 stsp NULL);
1402 f8d1f275 2019-02-04 stsp if (parent_path[0])
1403 f8d1f275 2019-02-04 stsp free(path);
1404 b72f483a 2019-02-05 stsp return err;
1405 f8d1f275 2019-02-04 stsp }
1406 f8d1f275 2019-02-04 stsp
1407 f8d1f275 2019-02-04 stsp const struct got_error *
1408 927df6b7 2019-02-10 stsp got_worktree_status(struct got_worktree *worktree, const char *path,
1409 f8d1f275 2019-02-04 stsp struct got_repository *repo, got_worktree_status_cb status_cb,
1410 f8d1f275 2019-02-04 stsp void *status_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1411 f8d1f275 2019-02-04 stsp {
1412 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
1413 f8d1f275 2019-02-04 stsp DIR *workdir = NULL;
1414 f8d1f275 2019-02-04 stsp char *fileindex_path = NULL;
1415 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex = NULL;
1416 f8d1f275 2019-02-04 stsp FILE *index = NULL;
1417 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
1418 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
1419 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
1420 f8d1f275 2019-02-04 stsp
1421 f8d1f275 2019-02-04 stsp fileindex = got_fileindex_alloc();
1422 f8d1f275 2019-02-04 stsp if (fileindex == NULL) {
1423 f8d1f275 2019-02-04 stsp err = got_error_from_errno();
1424 f8d1f275 2019-02-04 stsp goto done;
1425 f8d1f275 2019-02-04 stsp }
1426 f8d1f275 2019-02-04 stsp
1427 f8d1f275 2019-02-04 stsp if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1428 f8d1f275 2019-02-04 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1429 f8d1f275 2019-02-04 stsp err = got_error_from_errno();
1430 f8d1f275 2019-02-04 stsp fileindex_path = NULL;
1431 f8d1f275 2019-02-04 stsp goto done;
1432 f8d1f275 2019-02-04 stsp }
1433 f8d1f275 2019-02-04 stsp
1434 f8d1f275 2019-02-04 stsp index = fopen(fileindex_path, "rb");
1435 f8d1f275 2019-02-04 stsp if (index == NULL) {
1436 f8d1f275 2019-02-04 stsp if (errno != ENOENT) {
1437 f8d1f275 2019-02-04 stsp err = got_error_from_errno();
1438 f8d1f275 2019-02-04 stsp goto done;
1439 f8d1f275 2019-02-04 stsp }
1440 f8d1f275 2019-02-04 stsp } else {
1441 f8d1f275 2019-02-04 stsp err = got_fileindex_read(fileindex, index);
1442 f8d1f275 2019-02-04 stsp fclose(index);
1443 f8d1f275 2019-02-04 stsp if (err)
1444 f8d1f275 2019-02-04 stsp goto done;
1445 f8d1f275 2019-02-04 stsp }
1446 f8d1f275 2019-02-04 stsp
1447 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
1448 927df6b7 2019-02-10 stsp worktree->root_path, path[0] ? "/" : "", path) == -1) {
1449 c513d110 2019-02-05 stsp err = got_error_from_errno();
1450 c513d110 2019-02-05 stsp goto done;
1451 c513d110 2019-02-05 stsp }
1452 927df6b7 2019-02-10 stsp workdir = opendir(ondisk_path);
1453 927df6b7 2019-02-10 stsp if (workdir == NULL) {
1454 927df6b7 2019-02-10 stsp if (errno == ENOTDIR) {
1455 927df6b7 2019-02-10 stsp struct got_fileindex_entry *ie;
1456 927df6b7 2019-02-10 stsp ie = got_fileindex_entry_get(fileindex, path);
1457 927df6b7 2019-02-10 stsp if (ie == NULL) {
1458 927df6b7 2019-02-10 stsp err = got_error(GOT_ERR_BAD_PATH);
1459 927df6b7 2019-02-10 stsp goto done;
1460 927df6b7 2019-02-10 stsp }
1461 927df6b7 2019-02-10 stsp err = report_file_status(ie, ondisk_path,
1462 927df6b7 2019-02-10 stsp status_cb, status_arg, repo);
1463 927df6b7 2019-02-10 stsp goto done;
1464 927df6b7 2019-02-10 stsp } else {
1465 927df6b7 2019-02-10 stsp err = got_error_from_errno();
1466 927df6b7 2019-02-10 stsp goto done;
1467 927df6b7 2019-02-10 stsp }
1468 927df6b7 2019-02-10 stsp }
1469 d43a8a88 2019-02-05 stsp fdiff_cb.diff_old_new = status_old_new;
1470 d43a8a88 2019-02-05 stsp fdiff_cb.diff_old = status_old;
1471 d43a8a88 2019-02-05 stsp fdiff_cb.diff_new = status_new;
1472 f8d1f275 2019-02-04 stsp arg.fileindex = fileindex;
1473 f8d1f275 2019-02-04 stsp arg.worktree = worktree;
1474 927df6b7 2019-02-10 stsp arg.status_path = path;
1475 927df6b7 2019-02-10 stsp arg.status_path_len = strlen(path);
1476 f8d1f275 2019-02-04 stsp arg.repo = repo;
1477 f8d1f275 2019-02-04 stsp arg.status_cb = status_cb;
1478 f8d1f275 2019-02-04 stsp arg.status_arg = status_arg;
1479 f8d1f275 2019-02-04 stsp arg.cancel_cb = cancel_cb;
1480 f8d1f275 2019-02-04 stsp arg.cancel_arg = cancel_arg;
1481 c7f4312f 2019-02-05 stsp err = got_fileindex_diff_dir(fileindex, workdir, worktree->root_path,
1482 927df6b7 2019-02-10 stsp path, repo, &fdiff_cb, &arg);
1483 f8d1f275 2019-02-04 stsp done:
1484 f8d1f275 2019-02-04 stsp if (workdir)
1485 f8d1f275 2019-02-04 stsp closedir(workdir);
1486 927df6b7 2019-02-10 stsp free(ondisk_path);
1487 f8d1f275 2019-02-04 stsp free(fileindex_path);
1488 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
1489 f8d1f275 2019-02-04 stsp return err;
1490 f8d1f275 2019-02-04 stsp }