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