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