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