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 13d9040b 2019-03-27 stsp update_blob_fileindex_entry(struct got_worktree *worktree,
848 13d9040b 2019-03-27 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
849 13d9040b 2019-03-27 stsp const char *ondisk_path, const char *path, struct got_blob_object *blob,
850 13d9040b 2019-03-27 stsp int update_timestamps)
851 13d9040b 2019-03-27 stsp {
852 13d9040b 2019-03-27 stsp const struct got_error *err = NULL;
853 13d9040b 2019-03-27 stsp
854 13d9040b 2019-03-27 stsp if (ie == NULL)
855 13d9040b 2019-03-27 stsp ie = got_fileindex_entry_get(fileindex, path);
856 13d9040b 2019-03-27 stsp if (ie)
857 13d9040b 2019-03-27 stsp err = got_fileindex_entry_update(ie, ondisk_path,
858 13d9040b 2019-03-27 stsp blob->id.sha1, worktree->base_commit_id->sha1,
859 13d9040b 2019-03-27 stsp update_timestamps);
860 13d9040b 2019-03-27 stsp else {
861 13d9040b 2019-03-27 stsp struct got_fileindex_entry *new_ie;
862 13d9040b 2019-03-27 stsp err = got_fileindex_entry_alloc(&new_ie, ondisk_path,
863 13d9040b 2019-03-27 stsp path, blob->id.sha1, worktree->base_commit_id->sha1);
864 13d9040b 2019-03-27 stsp if (!err)
865 13d9040b 2019-03-27 stsp err = got_fileindex_entry_add(fileindex, new_ie);
866 13d9040b 2019-03-27 stsp }
867 13d9040b 2019-03-27 stsp return err;
868 13d9040b 2019-03-27 stsp }
869 13d9040b 2019-03-27 stsp
870 13d9040b 2019-03-27 stsp static const struct got_error *
871 13d9040b 2019-03-27 stsp install_blob(struct got_worktree *worktree, const char *ondisk_path,
872 13d9040b 2019-03-27 stsp const char *path, uint16_t te_mode, uint16_t st_mode,
873 13d9040b 2019-03-27 stsp struct got_blob_object *blob, int restoring_missing_file,
874 13d9040b 2019-03-27 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
875 13d9040b 2019-03-27 stsp void *progress_arg)
876 9d31a1d8 2018-03-11 stsp {
877 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
878 507dc3bb 2018-12-29 stsp int fd = -1;
879 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
880 507dc3bb 2018-12-29 stsp int update = 0;
881 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
882 9d31a1d8 2018-03-11 stsp
883 c34b20a2 2018-03-12 stsp fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
884 9d31a1d8 2018-03-11 stsp GOT_DEFAULT_FILE_MODE);
885 9d31a1d8 2018-03-11 stsp if (fd == -1) {
886 21908da4 2019-01-13 stsp if (errno == ENOENT) {
887 21908da4 2019-01-13 stsp char *parent = dirname(path);
888 21908da4 2019-01-13 stsp if (parent == NULL)
889 21908da4 2019-01-13 stsp return got_error_from_errno();
890 21908da4 2019-01-13 stsp err = add_dir_on_disk(worktree, parent);
891 21908da4 2019-01-13 stsp if (err)
892 21908da4 2019-01-13 stsp return err;
893 21908da4 2019-01-13 stsp fd = open(ondisk_path,
894 21908da4 2019-01-13 stsp O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
895 21908da4 2019-01-13 stsp GOT_DEFAULT_FILE_MODE);
896 21908da4 2019-01-13 stsp if (fd == -1)
897 21908da4 2019-01-13 stsp return got_error_from_errno();
898 21908da4 2019-01-13 stsp } else if (errno == EEXIST) {
899 b8f41171 2019-02-10 stsp if (!S_ISREG(st_mode)) {
900 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
901 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_FILE_OBSTRUCTED);
902 507dc3bb 2018-12-29 stsp goto done;
903 d70b8e30 2018-12-27 stsp } else {
904 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
905 507dc3bb 2018-12-29 stsp ondisk_path);
906 507dc3bb 2018-12-29 stsp if (err)
907 507dc3bb 2018-12-29 stsp goto done;
908 507dc3bb 2018-12-29 stsp update = 1;
909 9d31a1d8 2018-03-11 stsp }
910 507dc3bb 2018-12-29 stsp } else
911 7e7c1e4c 2019-01-12 stsp return got_error_from_errno();
912 9d31a1d8 2018-03-11 stsp }
913 9d31a1d8 2018-03-11 stsp
914 a378724f 2019-02-10 stsp if (restoring_missing_file)
915 a378724f 2019-02-10 stsp (*progress_cb)(progress_arg, GOT_STATUS_MISSING, path);
916 a378724f 2019-02-10 stsp else
917 a378724f 2019-02-10 stsp (*progress_cb)(progress_arg,
918 a378724f 2019-02-10 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
919 d7b62c98 2018-12-27 stsp
920 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
921 9d31a1d8 2018-03-11 stsp do {
922 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
923 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
924 9d31a1d8 2018-03-11 stsp if (err)
925 9d31a1d8 2018-03-11 stsp break;
926 9d31a1d8 2018-03-11 stsp if (len > 0) {
927 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
928 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
929 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
930 9d31a1d8 2018-03-11 stsp err = got_error_from_errno();
931 b87c6f83 2018-12-24 stsp goto done;
932 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
933 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
934 b87c6f83 2018-12-24 stsp goto done;
935 9d31a1d8 2018-03-11 stsp }
936 61d6eaa3 2018-12-24 stsp hdrlen = 0;
937 9d31a1d8 2018-03-11 stsp }
938 9d31a1d8 2018-03-11 stsp } while (len != 0);
939 9d31a1d8 2018-03-11 stsp
940 816dc654 2019-02-16 stsp if (fsync(fd) != 0) {
941 816dc654 2019-02-16 stsp err = got_error_from_errno();
942 816dc654 2019-02-16 stsp goto done;
943 816dc654 2019-02-16 stsp }
944 9d31a1d8 2018-03-11 stsp
945 507dc3bb 2018-12-29 stsp if (update) {
946 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
947 68ed9ba5 2019-02-10 stsp err = got_error_from_errno();
948 2a57020b 2019-02-20 stsp unlink(tmppath);
949 68ed9ba5 2019-02-10 stsp goto done;
950 68ed9ba5 2019-02-10 stsp }
951 68ed9ba5 2019-02-10 stsp }
952 ba8a0d4d 2019-02-10 stsp
953 b8f41171 2019-02-10 stsp if (te_mode & S_IXUSR) {
954 b8f41171 2019-02-10 stsp if (chmod(ondisk_path, st_mode | S_IXUSR) == -1) {
955 507dc3bb 2018-12-29 stsp err = got_error_from_errno();
956 507dc3bb 2018-12-29 stsp goto done;
957 507dc3bb 2018-12-29 stsp }
958 ba8a0d4d 2019-02-10 stsp } else {
959 b8f41171 2019-02-10 stsp if (chmod(ondisk_path, st_mode & ~S_IXUSR) == -1) {
960 68ed9ba5 2019-02-10 stsp err = got_error_from_errno();
961 68ed9ba5 2019-02-10 stsp goto done;
962 68ed9ba5 2019-02-10 stsp }
963 507dc3bb 2018-12-29 stsp }
964 507dc3bb 2018-12-29 stsp
965 9d31a1d8 2018-03-11 stsp done:
966 3a6ce05a 2019-02-11 stsp if (fd != -1 && close(fd) != 0 && err == NULL)
967 3a6ce05a 2019-02-11 stsp err = got_error_from_errno();
968 507dc3bb 2018-12-29 stsp free(tmppath);
969 6353ad76 2019-02-08 stsp return err;
970 6353ad76 2019-02-08 stsp }
971 6353ad76 2019-02-08 stsp
972 7154f6ce 2019-03-27 stsp /* Upgrade STATUS_MODIFY to STATUS_CONFLICT if a conflict marker is found. */
973 6353ad76 2019-02-08 stsp static const struct got_error *
974 7154f6ce 2019-03-27 stsp get_modified_file_content_status(unsigned char *status, FILE *f)
975 7154f6ce 2019-03-27 stsp {
976 7154f6ce 2019-03-27 stsp const struct got_error *err = NULL;
977 7154f6ce 2019-03-27 stsp const char *markers[3] = {
978 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_BEGIN,
979 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_SEP,
980 7154f6ce 2019-03-27 stsp GOT_DIFF_CONFLICT_MARKER_END
981 7154f6ce 2019-03-27 stsp };
982 7154f6ce 2019-03-27 stsp int i = 0;
983 7154f6ce 2019-03-27 stsp char *line;
984 7154f6ce 2019-03-27 stsp size_t len;
985 7154f6ce 2019-03-27 stsp const char delim[3] = {'\0', '\0', '\0'};
986 7154f6ce 2019-03-27 stsp
987 7154f6ce 2019-03-27 stsp while (*status == GOT_STATUS_MODIFY) {
988 7154f6ce 2019-03-27 stsp line = fparseln(f, &len, NULL, delim, 0);
989 7154f6ce 2019-03-27 stsp if (line == NULL) {
990 7154f6ce 2019-03-27 stsp if (feof(f))
991 7154f6ce 2019-03-27 stsp break;
992 7154f6ce 2019-03-27 stsp err = got_ferror(f, GOT_ERR_IO);
993 7154f6ce 2019-03-27 stsp break;
994 7154f6ce 2019-03-27 stsp }
995 7154f6ce 2019-03-27 stsp
996 7154f6ce 2019-03-27 stsp if (strncmp(line, markers[i], strlen(markers[i])) == 0) {
997 7154f6ce 2019-03-27 stsp if (markers[i] == GOT_DIFF_CONFLICT_MARKER_END)
998 7154f6ce 2019-03-27 stsp *status = GOT_STATUS_CONFLICT;
999 7154f6ce 2019-03-27 stsp else
1000 7154f6ce 2019-03-27 stsp i++;
1001 7154f6ce 2019-03-27 stsp }
1002 7154f6ce 2019-03-27 stsp }
1003 7154f6ce 2019-03-27 stsp
1004 7154f6ce 2019-03-27 stsp return err;
1005 7154f6ce 2019-03-27 stsp }
1006 7154f6ce 2019-03-27 stsp
1007 7154f6ce 2019-03-27 stsp static const struct got_error *
1008 b8f41171 2019-02-10 stsp get_file_status(unsigned char *status, struct stat *sb,
1009 b8f41171 2019-02-10 stsp struct got_fileindex_entry *ie, const char *abspath,
1010 b8f41171 2019-02-10 stsp struct got_repository *repo)
1011 6353ad76 2019-02-08 stsp {
1012 6353ad76 2019-02-08 stsp const struct got_error *err = NULL;
1013 6353ad76 2019-02-08 stsp struct got_object_id id;
1014 6353ad76 2019-02-08 stsp size_t hdrlen;
1015 6353ad76 2019-02-08 stsp FILE *f = NULL;
1016 6353ad76 2019-02-08 stsp uint8_t fbuf[8192];
1017 6353ad76 2019-02-08 stsp struct got_blob_object *blob = NULL;
1018 6353ad76 2019-02-08 stsp size_t flen, blen;
1019 6353ad76 2019-02-08 stsp
1020 6353ad76 2019-02-08 stsp *status = GOT_STATUS_NO_CHANGE;
1021 6353ad76 2019-02-08 stsp
1022 b8f41171 2019-02-10 stsp if (lstat(abspath, sb) == -1) {
1023 a378724f 2019-02-10 stsp if (errno == ENOENT) {
1024 b8f41171 2019-02-10 stsp if (ie) {
1025 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1026 2ec1f75b 2019-03-26 stsp *status = GOT_STATUS_MISSING;
1027 2ec1f75b 2019-03-26 stsp else
1028 2ec1f75b 2019-03-26 stsp *status = GOT_STATUS_DELETE;
1029 b8f41171 2019-02-10 stsp sb->st_mode =
1030 b8f41171 2019-02-10 stsp ((ie->mode >> GOT_FILEIDX_MODE_PERMS_SHIFT)
1031 b8f41171 2019-02-10 stsp & (S_IRWXU | S_IRWXG | S_IRWXO));
1032 b8f41171 2019-02-10 stsp } else
1033 b8f41171 2019-02-10 stsp sb->st_mode = GOT_DEFAULT_FILE_MODE;
1034 a378724f 2019-02-10 stsp return NULL;
1035 a378724f 2019-02-10 stsp }
1036 6353ad76 2019-02-08 stsp return got_error_from_errno();
1037 a378724f 2019-02-10 stsp }
1038 6353ad76 2019-02-08 stsp
1039 b8f41171 2019-02-10 stsp if (!S_ISREG(sb->st_mode)) {
1040 6353ad76 2019-02-08 stsp *status = GOT_STATUS_OBSTRUCTED;
1041 6353ad76 2019-02-08 stsp return NULL;
1042 6353ad76 2019-02-08 stsp }
1043 6353ad76 2019-02-08 stsp
1044 b8f41171 2019-02-10 stsp if (ie == NULL)
1045 d00136be 2019-03-26 stsp return NULL;
1046 d00136be 2019-03-26 stsp
1047 2ec1f75b 2019-03-26 stsp if (!got_fileindex_entry_has_file_on_disk(ie)) {
1048 2ec1f75b 2019-03-26 stsp *status = GOT_STATUS_DELETE;
1049 2ec1f75b 2019-03-26 stsp return NULL;
1050 2ec1f75b 2019-03-26 stsp } else if (!got_fileindex_entry_has_blob(ie)) {
1051 d00136be 2019-03-26 stsp *status = GOT_STATUS_ADD;
1052 b8f41171 2019-02-10 stsp return NULL;
1053 d00136be 2019-03-26 stsp }
1054 b8f41171 2019-02-10 stsp
1055 b8f41171 2019-02-10 stsp if (ie->ctime_sec == sb->st_ctime &&
1056 b8f41171 2019-02-10 stsp ie->ctime_nsec == sb->st_ctimensec &&
1057 b8f41171 2019-02-10 stsp ie->mtime_sec == sb->st_mtime &&
1058 b8f41171 2019-02-10 stsp ie->mtime_sec == sb->st_mtime &&
1059 b8f41171 2019-02-10 stsp ie->mtime_nsec == sb->st_mtimensec &&
1060 b8f41171 2019-02-10 stsp ie->size == (sb->st_size & 0xffffffff))
1061 6353ad76 2019-02-08 stsp return NULL;
1062 6353ad76 2019-02-08 stsp
1063 6353ad76 2019-02-08 stsp memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
1064 6353ad76 2019-02-08 stsp err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
1065 6353ad76 2019-02-08 stsp if (err)
1066 6353ad76 2019-02-08 stsp return err;
1067 6353ad76 2019-02-08 stsp
1068 6353ad76 2019-02-08 stsp f = fopen(abspath, "r");
1069 6353ad76 2019-02-08 stsp if (f == NULL) {
1070 6353ad76 2019-02-08 stsp err = got_error_from_errno();
1071 6353ad76 2019-02-08 stsp goto done;
1072 6353ad76 2019-02-08 stsp }
1073 6353ad76 2019-02-08 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1074 6353ad76 2019-02-08 stsp while (1) {
1075 6353ad76 2019-02-08 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
1076 6353ad76 2019-02-08 stsp err = got_object_blob_read_block(&blen, blob);
1077 6353ad76 2019-02-08 stsp if (err)
1078 7154f6ce 2019-03-27 stsp goto done;
1079 3cbbd752 2019-02-19 stsp /* Skip length of blob object header first time around. */
1080 3cbbd752 2019-02-19 stsp flen = fread(fbuf, 1, sizeof(fbuf) - hdrlen, f);
1081 80c5c120 2019-02-19 stsp if (flen == 0 && ferror(f)) {
1082 80c5c120 2019-02-19 stsp err = got_error_from_errno();
1083 7154f6ce 2019-03-27 stsp goto done;
1084 80c5c120 2019-02-19 stsp }
1085 6353ad76 2019-02-08 stsp if (blen == 0) {
1086 6353ad76 2019-02-08 stsp if (flen != 0)
1087 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1088 6353ad76 2019-02-08 stsp break;
1089 6353ad76 2019-02-08 stsp } else if (flen == 0) {
1090 6353ad76 2019-02-08 stsp if (blen != 0)
1091 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1092 6353ad76 2019-02-08 stsp break;
1093 6353ad76 2019-02-08 stsp } else if (blen - hdrlen == flen) {
1094 6353ad76 2019-02-08 stsp /* Skip blob object header first time around. */
1095 6353ad76 2019-02-08 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1096 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1097 6353ad76 2019-02-08 stsp break;
1098 6353ad76 2019-02-08 stsp }
1099 6353ad76 2019-02-08 stsp } else {
1100 276262e8 2019-02-08 stsp *status = GOT_STATUS_MODIFY;
1101 6353ad76 2019-02-08 stsp break;
1102 6353ad76 2019-02-08 stsp }
1103 6353ad76 2019-02-08 stsp hdrlen = 0;
1104 6353ad76 2019-02-08 stsp }
1105 7154f6ce 2019-03-27 stsp
1106 7154f6ce 2019-03-27 stsp if (*status == GOT_STATUS_MODIFY) {
1107 7154f6ce 2019-03-27 stsp rewind(f);
1108 7154f6ce 2019-03-27 stsp err = get_modified_file_content_status(status, f);
1109 7154f6ce 2019-03-27 stsp }
1110 6353ad76 2019-02-08 stsp done:
1111 6353ad76 2019-02-08 stsp if (blob)
1112 6353ad76 2019-02-08 stsp got_object_blob_close(blob);
1113 6353ad76 2019-02-08 stsp if (f)
1114 6353ad76 2019-02-08 stsp fclose(f);
1115 9d31a1d8 2018-03-11 stsp return err;
1116 9d31a1d8 2018-03-11 stsp }
1117 9d31a1d8 2018-03-11 stsp
1118 9d31a1d8 2018-03-11 stsp static const struct got_error *
1119 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
1120 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
1121 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
1122 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1123 8da9e5f4 2019-01-12 stsp void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1124 9d31a1d8 2018-03-11 stsp {
1125 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
1126 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
1127 6353ad76 2019-02-08 stsp char *ondisk_path;
1128 6353ad76 2019-02-08 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1129 b8f41171 2019-02-10 stsp struct stat sb;
1130 9d31a1d8 2018-03-11 stsp
1131 6353ad76 2019-02-08 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
1132 6353ad76 2019-02-08 stsp return got_error_from_errno();
1133 6353ad76 2019-02-08 stsp
1134 b8f41171 2019-02-10 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1135 b8f41171 2019-02-10 stsp if (err)
1136 b8f41171 2019-02-10 stsp goto done;
1137 6353ad76 2019-02-08 stsp
1138 b8f41171 2019-02-10 stsp if (status == GOT_STATUS_OBSTRUCTED) {
1139 b8f41171 2019-02-10 stsp (*progress_cb)(progress_arg, status, path);
1140 b8f41171 2019-02-10 stsp goto done;
1141 b8f41171 2019-02-10 stsp }
1142 b8f41171 2019-02-10 stsp
1143 b8f41171 2019-02-10 stsp if (ie && status != GOT_STATUS_MISSING) {
1144 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_commit(ie) &&
1145 1430b4e0 2019-03-27 stsp memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
1146 b8f41171 2019-02-10 stsp SHA1_DIGEST_LENGTH) == 0) {
1147 b8f41171 2019-02-10 stsp (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
1148 b8f41171 2019-02-10 stsp path);
1149 6353ad76 2019-02-08 stsp goto done;
1150 a378724f 2019-02-10 stsp }
1151 1430b4e0 2019-03-27 stsp if (got_fileindex_entry_has_blob(ie) &&
1152 1430b4e0 2019-03-27 stsp memcmp(ie->blob_sha1, te->id->sha1,
1153 1430b4e0 2019-03-27 stsp SHA1_DIGEST_LENGTH) == 0)
1154 b8f41171 2019-02-10 stsp goto done;
1155 9d31a1d8 2018-03-11 stsp }
1156 9d31a1d8 2018-03-11 stsp
1157 8da9e5f4 2019-01-12 stsp err = got_object_open_as_blob(&blob, repo, te->id, 8192);
1158 8da9e5f4 2019-01-12 stsp if (err)
1159 6353ad76 2019-02-08 stsp goto done;
1160 512f0d0e 2019-01-02 stsp
1161 1430b4e0 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_ADD)
1162 6353ad76 2019-02-08 stsp err = merge_blob(worktree, fileindex, ie, ondisk_path, path,
1163 b8f41171 2019-02-10 stsp te->mode, sb.st_mode, blob, repo, progress_cb,
1164 b8f41171 2019-02-10 stsp progress_arg);
1165 13d9040b 2019-03-27 stsp else if (status == GOT_STATUS_DELETE) {
1166 13d9040b 2019-03-27 stsp (*progress_cb)(progress_arg, GOT_STATUS_MERGE, path);
1167 13d9040b 2019-03-27 stsp err = update_blob_fileindex_entry(worktree, fileindex, ie,
1168 13d9040b 2019-03-27 stsp ondisk_path, path, blob, 0);
1169 13d9040b 2019-03-27 stsp if (err)
1170 13d9040b 2019-03-27 stsp goto done;
1171 13d9040b 2019-03-27 stsp } else {
1172 13d9040b 2019-03-27 stsp err = install_blob(worktree, ondisk_path, path, te->mode,
1173 13d9040b 2019-03-27 stsp sb.st_mode, blob, status == GOT_STATUS_MISSING, repo,
1174 13d9040b 2019-03-27 stsp progress_cb, progress_arg);
1175 13d9040b 2019-03-27 stsp if (err)
1176 13d9040b 2019-03-27 stsp goto done;
1177 13d9040b 2019-03-27 stsp err = update_blob_fileindex_entry(worktree, fileindex, ie,
1178 13d9040b 2019-03-27 stsp ondisk_path, path, blob, 1);
1179 13d9040b 2019-03-27 stsp if (err)
1180 13d9040b 2019-03-27 stsp goto done;
1181 13d9040b 2019-03-27 stsp }
1182 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
1183 6353ad76 2019-02-08 stsp done:
1184 6353ad76 2019-02-08 stsp free(ondisk_path);
1185 8da9e5f4 2019-01-12 stsp return err;
1186 90285c3b 2019-01-08 stsp }
1187 90285c3b 2019-01-08 stsp
1188 90285c3b 2019-01-08 stsp static const struct got_error *
1189 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
1190 90285c3b 2019-01-08 stsp {
1191 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
1192 90285c3b 2019-01-08 stsp char *ondisk_path = NULL;
1193 90285c3b 2019-01-08 stsp
1194 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
1195 90285c3b 2019-01-08 stsp return got_error_from_errno();
1196 90285c3b 2019-01-08 stsp
1197 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
1198 c97665ae 2019-01-13 stsp if (errno != ENOENT)
1199 c97665ae 2019-01-13 stsp err = got_error_from_errno();
1200 c97665ae 2019-01-13 stsp } else {
1201 90285c3b 2019-01-08 stsp char *parent = dirname(ondisk_path);
1202 7a9df742 2019-01-08 stsp while (parent && strcmp(parent, root_path) != 0) {
1203 26c4ac4d 2019-01-08 stsp if (rmdir(parent) == -1) {
1204 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
1205 26c4ac4d 2019-01-08 stsp err = got_error_from_errno();
1206 90285c3b 2019-01-08 stsp break;
1207 90285c3b 2019-01-08 stsp }
1208 90285c3b 2019-01-08 stsp parent = dirname(parent);
1209 90285c3b 2019-01-08 stsp }
1210 90285c3b 2019-01-08 stsp }
1211 90285c3b 2019-01-08 stsp free(ondisk_path);
1212 90285c3b 2019-01-08 stsp return err;
1213 512f0d0e 2019-01-02 stsp }
1214 512f0d0e 2019-01-02 stsp
1215 708d8e67 2019-03-27 stsp static const struct got_error *
1216 708d8e67 2019-03-27 stsp delete_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
1217 708d8e67 2019-03-27 stsp struct got_fileindex_entry *ie, const char *parent_path,
1218 708d8e67 2019-03-27 stsp struct got_repository *repo,
1219 708d8e67 2019-03-27 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
1220 708d8e67 2019-03-27 stsp got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1221 708d8e67 2019-03-27 stsp {
1222 fc6346c4 2019-03-27 stsp const struct got_error *err = NULL;
1223 708d8e67 2019-03-27 stsp unsigned char status;
1224 708d8e67 2019-03-27 stsp struct stat sb;
1225 708d8e67 2019-03-27 stsp char *ondisk_path;
1226 708d8e67 2019-03-27 stsp
1227 708d8e67 2019-03-27 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, ie->path)
1228 708d8e67 2019-03-27 stsp == -1)
1229 708d8e67 2019-03-27 stsp return got_error_from_errno();
1230 708d8e67 2019-03-27 stsp
1231 708d8e67 2019-03-27 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1232 708d8e67 2019-03-27 stsp if (err)
1233 708d8e67 2019-03-27 stsp return err;
1234 708d8e67 2019-03-27 stsp
1235 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_MODIFY || status == GOT_STATUS_CONFLICT ||
1236 fc6346c4 2019-03-27 stsp status == GOT_STATUS_ADD) {
1237 fc6346c4 2019-03-27 stsp (*progress_cb)(progress_arg, GOT_STATUS_MERGE, ie->path);
1238 fc6346c4 2019-03-27 stsp /*
1239 fc6346c4 2019-03-27 stsp * Preserve the working file and change the deleted blob's
1240 fc6346c4 2019-03-27 stsp * entry into a schedule-add entry.
1241 fc6346c4 2019-03-27 stsp */
1242 fc6346c4 2019-03-27 stsp err = got_fileindex_entry_update(ie, ondisk_path, NULL, NULL,
1243 fc6346c4 2019-03-27 stsp 0);
1244 708d8e67 2019-03-27 stsp if (err)
1245 708d8e67 2019-03-27 stsp return err;
1246 fc6346c4 2019-03-27 stsp } else {
1247 fc6346c4 2019-03-27 stsp (*progress_cb)(progress_arg, GOT_STATUS_DELETE, ie->path);
1248 fc6346c4 2019-03-27 stsp if (status == GOT_STATUS_NO_CHANGE) {
1249 fc6346c4 2019-03-27 stsp err = remove_ondisk_file(worktree->root_path, ie->path);
1250 fc6346c4 2019-03-27 stsp if (err)
1251 fc6346c4 2019-03-27 stsp return err;
1252 fc6346c4 2019-03-27 stsp }
1253 fc6346c4 2019-03-27 stsp got_fileindex_entry_remove(fileindex, ie);
1254 708d8e67 2019-03-27 stsp }
1255 708d8e67 2019-03-27 stsp
1256 fc6346c4 2019-03-27 stsp return err;
1257 708d8e67 2019-03-27 stsp }
1258 708d8e67 2019-03-27 stsp
1259 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
1260 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
1261 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
1262 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
1263 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
1264 8da9e5f4 2019-01-12 stsp void *progress_arg;
1265 8da9e5f4 2019-01-12 stsp got_worktree_cancel_cb cancel_cb;
1266 8da9e5f4 2019-01-12 stsp void *cancel_arg;
1267 8da9e5f4 2019-01-12 stsp };
1268 8da9e5f4 2019-01-12 stsp
1269 512f0d0e 2019-01-02 stsp static const struct got_error *
1270 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
1271 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
1272 512f0d0e 2019-01-02 stsp {
1273 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
1274 512f0d0e 2019-01-02 stsp
1275 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
1276 8da9e5f4 2019-01-12 stsp ie->path, a->repo, a->progress_cb, a->progress_arg,
1277 8da9e5f4 2019-01-12 stsp a->cancel_cb, a->cancel_arg);
1278 8da9e5f4 2019-01-12 stsp }
1279 512f0d0e 2019-01-02 stsp
1280 8da9e5f4 2019-01-12 stsp static const struct got_error *
1281 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1282 8da9e5f4 2019-01-12 stsp {
1283 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
1284 7a9df742 2019-01-08 stsp
1285 708d8e67 2019-03-27 stsp return delete_blob(a->worktree, a->fileindex, ie, parent_path,
1286 708d8e67 2019-03-27 stsp a->repo, a->progress_cb, a->progress_arg,
1287 708d8e67 2019-03-27 stsp a->cancel_cb, a->cancel_arg);
1288 9d31a1d8 2018-03-11 stsp }
1289 9d31a1d8 2018-03-11 stsp
1290 9d31a1d8 2018-03-11 stsp static const struct got_error *
1291 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
1292 9d31a1d8 2018-03-11 stsp {
1293 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
1294 8da9e5f4 2019-01-12 stsp const struct got_error *err;
1295 8da9e5f4 2019-01-12 stsp char *path;
1296 9d31a1d8 2018-03-11 stsp
1297 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
1298 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
1299 8da9e5f4 2019-01-12 stsp == -1)
1300 8da9e5f4 2019-01-12 stsp return got_error_from_errno();
1301 9d31a1d8 2018-03-11 stsp
1302 8da9e5f4 2019-01-12 stsp if (S_ISDIR(te->mode))
1303 8da9e5f4 2019-01-12 stsp err = add_dir_on_disk(a->worktree, path);
1304 8da9e5f4 2019-01-12 stsp else
1305 8da9e5f4 2019-01-12 stsp err = update_blob(a->worktree, a->fileindex, NULL, te, path,
1306 8da9e5f4 2019-01-12 stsp a->repo, a->progress_cb, a->progress_arg,
1307 8da9e5f4 2019-01-12 stsp a->cancel_cb, a->cancel_arg);
1308 9d31a1d8 2018-03-11 stsp
1309 8da9e5f4 2019-01-12 stsp free(path);
1310 0cd1c46a 2019-03-11 stsp return err;
1311 0cd1c46a 2019-03-11 stsp }
1312 0cd1c46a 2019-03-11 stsp
1313 517bab73 2019-03-11 stsp const struct got_error *
1314 517bab73 2019-03-11 stsp got_worktree_get_base_ref_name(char **refname, struct got_worktree *worktree)
1315 0cd1c46a 2019-03-11 stsp {
1316 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
1317 0647c563 2019-03-11 stsp char *uuidstr = NULL;
1318 0cd1c46a 2019-03-11 stsp uint32_t uuid_status;
1319 0cd1c46a 2019-03-11 stsp
1320 517bab73 2019-03-11 stsp *refname = NULL;
1321 517bab73 2019-03-11 stsp
1322 0cd1c46a 2019-03-11 stsp uuid_to_string(&worktree->uuid, &uuidstr, &uuid_status);
1323 0cd1c46a 2019-03-11 stsp if (uuid_status != uuid_s_ok)
1324 0cd1c46a 2019-03-11 stsp return got_error_uuid(uuid_status);
1325 0cd1c46a 2019-03-11 stsp
1326 0647c563 2019-03-11 stsp if (asprintf(refname, "%s-%s", GOT_WORKTREE_BASE_REF_PREFIX, uuidstr)
1327 0647c563 2019-03-11 stsp == -1) {
1328 0cd1c46a 2019-03-11 stsp err = got_error_from_errno();
1329 0647c563 2019-03-11 stsp *refname = NULL;
1330 0cd1c46a 2019-03-11 stsp }
1331 517bab73 2019-03-11 stsp free(uuidstr);
1332 517bab73 2019-03-11 stsp return err;
1333 517bab73 2019-03-11 stsp }
1334 517bab73 2019-03-11 stsp
1335 517bab73 2019-03-11 stsp /*
1336 517bab73 2019-03-11 stsp * Prevent Git's garbage collector from deleting our base commit by
1337 517bab73 2019-03-11 stsp * setting a reference to our base commit's ID.
1338 517bab73 2019-03-11 stsp */
1339 517bab73 2019-03-11 stsp static const struct got_error *
1340 517bab73 2019-03-11 stsp ref_base_commit(struct got_worktree *worktree, struct got_repository *repo)
1341 517bab73 2019-03-11 stsp {
1342 517bab73 2019-03-11 stsp const struct got_error *err = NULL;
1343 517bab73 2019-03-11 stsp struct got_reference *ref = NULL;
1344 517bab73 2019-03-11 stsp char *refname;
1345 517bab73 2019-03-11 stsp
1346 517bab73 2019-03-11 stsp err = got_worktree_get_base_ref_name(&refname, worktree);
1347 517bab73 2019-03-11 stsp if (err)
1348 517bab73 2019-03-11 stsp return err;
1349 0cd1c46a 2019-03-11 stsp
1350 0cd1c46a 2019-03-11 stsp err = got_ref_alloc(&ref, refname, worktree->base_commit_id);
1351 0cd1c46a 2019-03-11 stsp if (err)
1352 0cd1c46a 2019-03-11 stsp goto done;
1353 0cd1c46a 2019-03-11 stsp
1354 0cd1c46a 2019-03-11 stsp err = got_ref_write(ref, repo);
1355 0cd1c46a 2019-03-11 stsp done:
1356 0cd1c46a 2019-03-11 stsp free(refname);
1357 0cd1c46a 2019-03-11 stsp if (ref)
1358 0cd1c46a 2019-03-11 stsp got_ref_close(ref);
1359 9d31a1d8 2018-03-11 stsp return err;
1360 9d31a1d8 2018-03-11 stsp }
1361 9d31a1d8 2018-03-11 stsp
1362 0cd1c46a 2019-03-11 stsp
1363 9d31a1d8 2018-03-11 stsp const struct got_error *
1364 9d31a1d8 2018-03-11 stsp got_worktree_checkout_files(struct got_worktree *worktree,
1365 93a30277 2018-12-24 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
1366 93a30277 2018-12-24 stsp void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1367 9d31a1d8 2018-03-11 stsp {
1368 a143fb78 2018-12-25 stsp const struct got_error *err = NULL, *unlockerr, *checkout_err = NULL;
1369 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
1370 8da9e5f4 2019-01-12 stsp struct got_object_id *tree_id = NULL;
1371 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
1372 9d31a1d8 2018-03-11 stsp char *fileindex_path = NULL, *new_fileindex_path = NULL;
1373 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
1374 51514078 2018-12-25 stsp FILE *index = NULL, *new_index = NULL;
1375 f44ffd20 2019-02-04 stsp struct got_fileindex_diff_tree_cb diff_cb;
1376 8da9e5f4 2019-01-12 stsp struct diff_cb_arg arg;
1377 9d31a1d8 2018-03-11 stsp
1378 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
1379 9d31a1d8 2018-03-11 stsp if (err)
1380 9d31a1d8 2018-03-11 stsp return err;
1381 93a30277 2018-12-24 stsp
1382 133d2798 2019-01-08 stsp fileindex = got_fileindex_alloc();
1383 133d2798 2019-01-08 stsp if (fileindex == NULL) {
1384 133d2798 2019-01-08 stsp err = got_error_from_errno();
1385 9d31a1d8 2018-03-11 stsp goto done;
1386 133d2798 2019-01-08 stsp }
1387 9d31a1d8 2018-03-11 stsp
1388 9d31a1d8 2018-03-11 stsp if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1389 9d31a1d8 2018-03-11 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1390 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
1391 9d31a1d8 2018-03-11 stsp fileindex_path = NULL;
1392 9d31a1d8 2018-03-11 stsp goto done;
1393 9d31a1d8 2018-03-11 stsp }
1394 9d31a1d8 2018-03-11 stsp
1395 51514078 2018-12-25 stsp /*
1396 51514078 2018-12-25 stsp * Read the file index.
1397 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
1398 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
1399 51514078 2018-12-25 stsp */
1400 51514078 2018-12-25 stsp index = fopen(fileindex_path, "rb");
1401 51514078 2018-12-25 stsp if (index == NULL) {
1402 cf61d818 2019-01-12 stsp if (errno != ENOENT) {
1403 cf61d818 2019-01-12 stsp err = got_error_from_errno();
1404 cf61d818 2019-01-12 stsp goto done;
1405 cf61d818 2019-01-12 stsp }
1406 cf61d818 2019-01-12 stsp } else {
1407 cf61d818 2019-01-12 stsp err = got_fileindex_read(fileindex, index);
1408 cf61d818 2019-01-12 stsp fclose(index);
1409 cf61d818 2019-01-12 stsp if (err)
1410 cf61d818 2019-01-12 stsp goto done;
1411 51514078 2018-12-25 stsp }
1412 51514078 2018-12-25 stsp
1413 b8bdcc21 2018-12-24 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
1414 b8bdcc21 2018-12-24 stsp fileindex_path);
1415 51664889 2018-03-12 stsp if (err)
1416 51664889 2018-03-12 stsp goto done;
1417 51664889 2018-03-12 stsp
1418 0cd1c46a 2019-03-11 stsp err = ref_base_commit(worktree, repo);
1419 0cd1c46a 2019-03-11 stsp if (err)
1420 0cd1c46a 2019-03-11 stsp goto done;
1421 0cd1c46a 2019-03-11 stsp
1422 eaccb85f 2018-12-25 stsp err = got_object_open_as_commit(&commit, repo,
1423 eaccb85f 2018-12-25 stsp worktree->base_commit_id);
1424 9d31a1d8 2018-03-11 stsp if (err)
1425 9d31a1d8 2018-03-11 stsp goto done;
1426 9d31a1d8 2018-03-11 stsp
1427 8da9e5f4 2019-01-12 stsp err = got_object_id_by_path(&tree_id, repo,
1428 8da9e5f4 2019-01-12 stsp worktree->base_commit_id, worktree->path_prefix);
1429 9d31a1d8 2018-03-11 stsp if (err)
1430 9d31a1d8 2018-03-11 stsp goto done;
1431 9d31a1d8 2018-03-11 stsp
1432 8da9e5f4 2019-01-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
1433 8da9e5f4 2019-01-12 stsp if (err)
1434 8da9e5f4 2019-01-12 stsp goto done;
1435 9d31a1d8 2018-03-11 stsp
1436 8da9e5f4 2019-01-12 stsp diff_cb.diff_old_new = diff_old_new;
1437 8da9e5f4 2019-01-12 stsp diff_cb.diff_old = diff_old;
1438 8da9e5f4 2019-01-12 stsp diff_cb.diff_new = diff_new;
1439 8da9e5f4 2019-01-12 stsp arg.fileindex = fileindex;
1440 8da9e5f4 2019-01-12 stsp arg.worktree = worktree;
1441 8da9e5f4 2019-01-12 stsp arg.repo = repo;
1442 8da9e5f4 2019-01-12 stsp arg.progress_cb = progress_cb;
1443 8da9e5f4 2019-01-12 stsp arg.progress_arg = progress_arg;
1444 8da9e5f4 2019-01-12 stsp arg.cancel_cb = cancel_cb;
1445 8da9e5f4 2019-01-12 stsp arg.cancel_arg = cancel_arg;
1446 8da9e5f4 2019-01-12 stsp checkout_err = got_fileindex_diff_tree(fileindex, tree, repo,
1447 8da9e5f4 2019-01-12 stsp &diff_cb, &arg);
1448 8da9e5f4 2019-01-12 stsp
1449 a143fb78 2018-12-25 stsp /* Try to sync the fileindex back to disk in any case. */
1450 b8bdcc21 2018-12-24 stsp err = got_fileindex_write(fileindex, new_index);
1451 9d31a1d8 2018-03-11 stsp if (err)
1452 9d31a1d8 2018-03-11 stsp goto done;
1453 9d31a1d8 2018-03-11 stsp
1454 9d31a1d8 2018-03-11 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
1455 9d31a1d8 2018-03-11 stsp err = got_error_from_errno();
1456 2a57020b 2019-02-20 stsp unlink(new_fileindex_path);
1457 9d31a1d8 2018-03-11 stsp goto done;
1458 9d31a1d8 2018-03-11 stsp }
1459 9d31a1d8 2018-03-11 stsp
1460 9d31a1d8 2018-03-11 stsp free(new_fileindex_path);
1461 9d31a1d8 2018-03-11 stsp new_fileindex_path = NULL;
1462 9d31a1d8 2018-03-11 stsp
1463 9d31a1d8 2018-03-11 stsp done:
1464 edfa7d7f 2018-09-11 stsp if (tree)
1465 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
1466 9d31a1d8 2018-03-11 stsp if (commit)
1467 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
1468 9d31a1d8 2018-03-11 stsp if (new_fileindex_path)
1469 9d31a1d8 2018-03-11 stsp unlink(new_fileindex_path);
1470 b8bdcc21 2018-12-24 stsp if (new_index)
1471 b8bdcc21 2018-12-24 stsp fclose(new_index);
1472 9d31a1d8 2018-03-11 stsp free(new_fileindex_path);
1473 9d31a1d8 2018-03-11 stsp free(fileindex_path);
1474 7426bbfd 2018-12-24 stsp got_fileindex_free(fileindex);
1475 a143fb78 2018-12-25 stsp if (checkout_err)
1476 a143fb78 2018-12-25 stsp err = checkout_err;
1477 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
1478 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
1479 9d31a1d8 2018-03-11 stsp err = unlockerr;
1480 f8d1f275 2019-02-04 stsp return err;
1481 f8d1f275 2019-02-04 stsp }
1482 f8d1f275 2019-02-04 stsp
1483 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
1484 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
1485 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
1486 927df6b7 2019-02-10 stsp const char *status_path;
1487 927df6b7 2019-02-10 stsp size_t status_path_len;
1488 f8d1f275 2019-02-04 stsp struct got_repository *repo;
1489 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
1490 f8d1f275 2019-02-04 stsp void *status_arg;
1491 f8d1f275 2019-02-04 stsp got_worktree_cancel_cb cancel_cb;
1492 f8d1f275 2019-02-04 stsp void *cancel_arg;
1493 f8d1f275 2019-02-04 stsp };
1494 f8d1f275 2019-02-04 stsp
1495 f8d1f275 2019-02-04 stsp static const struct got_error *
1496 927df6b7 2019-02-10 stsp report_file_status(struct got_fileindex_entry *ie, const char *abspath,
1497 927df6b7 2019-02-10 stsp got_worktree_status_cb status_cb, void *status_arg,
1498 927df6b7 2019-02-10 stsp struct got_repository *repo)
1499 927df6b7 2019-02-10 stsp {
1500 927df6b7 2019-02-10 stsp const struct got_error *err = NULL;
1501 927df6b7 2019-02-10 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1502 927df6b7 2019-02-10 stsp struct stat sb;
1503 927df6b7 2019-02-10 stsp struct got_object_id id;
1504 927df6b7 2019-02-10 stsp
1505 927df6b7 2019-02-10 stsp err = get_file_status(&status, &sb, ie, abspath, repo);
1506 927df6b7 2019-02-10 stsp if (err == NULL && status != GOT_STATUS_NO_CHANGE) {
1507 927df6b7 2019-02-10 stsp memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1508 927df6b7 2019-02-10 stsp err = (*status_cb)(status_arg, status, ie->path, &id);
1509 927df6b7 2019-02-10 stsp }
1510 927df6b7 2019-02-10 stsp return err;
1511 927df6b7 2019-02-10 stsp }
1512 927df6b7 2019-02-10 stsp
1513 927df6b7 2019-02-10 stsp static const struct got_error *
1514 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
1515 f8d1f275 2019-02-04 stsp struct dirent *de, const char *parent_path)
1516 f8d1f275 2019-02-04 stsp {
1517 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
1518 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
1519 f8d1f275 2019-02-04 stsp char *abspath;
1520 f8d1f275 2019-02-04 stsp
1521 927df6b7 2019-02-10 stsp if (got_path_cmp(parent_path, a->status_path) != 0 &&
1522 927df6b7 2019-02-10 stsp !got_path_is_child(parent_path, a->status_path, a->status_path_len))
1523 927df6b7 2019-02-10 stsp return NULL;
1524 927df6b7 2019-02-10 stsp
1525 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
1526 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
1527 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
1528 f8d1f275 2019-02-04 stsp return got_error_from_errno();
1529 f8d1f275 2019-02-04 stsp } else {
1530 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
1531 f8d1f275 2019-02-04 stsp de->d_name) == -1)
1532 f8d1f275 2019-02-04 stsp return got_error_from_errno();
1533 f8d1f275 2019-02-04 stsp }
1534 f8d1f275 2019-02-04 stsp
1535 927df6b7 2019-02-10 stsp err = report_file_status(ie, abspath, a->status_cb, a->status_arg,
1536 927df6b7 2019-02-10 stsp a->repo);
1537 f8d1f275 2019-02-04 stsp free(abspath);
1538 9d31a1d8 2018-03-11 stsp return err;
1539 9d31a1d8 2018-03-11 stsp }
1540 f8d1f275 2019-02-04 stsp
1541 f8d1f275 2019-02-04 stsp static const struct got_error *
1542 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1543 f8d1f275 2019-02-04 stsp {
1544 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
1545 b72f483a 2019-02-05 stsp struct got_object_id id;
1546 2ec1f75b 2019-03-26 stsp unsigned char status;
1547 927df6b7 2019-02-10 stsp
1548 927df6b7 2019-02-10 stsp if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
1549 927df6b7 2019-02-10 stsp return NULL;
1550 927df6b7 2019-02-10 stsp
1551 b72f483a 2019-02-05 stsp memcpy(id.sha1, ie->blob_sha1, SHA1_DIGEST_LENGTH);
1552 2ec1f75b 2019-03-26 stsp if (got_fileindex_entry_has_file_on_disk(ie))
1553 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_MISSING;
1554 2ec1f75b 2019-03-26 stsp else
1555 2ec1f75b 2019-03-26 stsp status = GOT_STATUS_DELETE;
1556 2ec1f75b 2019-03-26 stsp return (*a->status_cb)(a->status_arg, status, ie->path, &id);
1557 f8d1f275 2019-02-04 stsp }
1558 f8d1f275 2019-02-04 stsp
1559 f8d1f275 2019-02-04 stsp static const struct got_error *
1560 f8d1f275 2019-02-04 stsp status_new(void *arg, struct dirent *de, const char *parent_path)
1561 f8d1f275 2019-02-04 stsp {
1562 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
1563 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
1564 f8d1f275 2019-02-04 stsp char *path = NULL;
1565 f8d1f275 2019-02-04 stsp
1566 f8d1f275 2019-02-04 stsp if (de->d_type == DT_DIR)
1567 2c201a36 2019-02-10 stsp return NULL;
1568 2c201a36 2019-02-10 stsp
1569 2c201a36 2019-02-10 stsp /* XXX ignore symlinks for now */
1570 2c201a36 2019-02-10 stsp if (de->d_type == DT_LNK)
1571 f8d1f275 2019-02-04 stsp return NULL;
1572 f8d1f275 2019-02-04 stsp
1573 927df6b7 2019-02-10 stsp if (!got_path_is_child(parent_path, a->status_path, a->status_path_len))
1574 927df6b7 2019-02-10 stsp return NULL;
1575 927df6b7 2019-02-10 stsp
1576 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
1577 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
1578 f8d1f275 2019-02-04 stsp return got_error_from_errno();
1579 f8d1f275 2019-02-04 stsp } else {
1580 f8d1f275 2019-02-04 stsp path = de->d_name;
1581 f8d1f275 2019-02-04 stsp }
1582 f8d1f275 2019-02-04 stsp
1583 b72f483a 2019-02-05 stsp err = (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED, path,
1584 b72f483a 2019-02-05 stsp NULL);
1585 f8d1f275 2019-02-04 stsp if (parent_path[0])
1586 f8d1f275 2019-02-04 stsp free(path);
1587 b72f483a 2019-02-05 stsp return err;
1588 f8d1f275 2019-02-04 stsp }
1589 f8d1f275 2019-02-04 stsp
1590 f8d1f275 2019-02-04 stsp const struct got_error *
1591 927df6b7 2019-02-10 stsp got_worktree_status(struct got_worktree *worktree, const char *path,
1592 f8d1f275 2019-02-04 stsp struct got_repository *repo, got_worktree_status_cb status_cb,
1593 f8d1f275 2019-02-04 stsp void *status_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1594 f8d1f275 2019-02-04 stsp {
1595 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
1596 f8d1f275 2019-02-04 stsp DIR *workdir = NULL;
1597 f8d1f275 2019-02-04 stsp char *fileindex_path = NULL;
1598 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex = NULL;
1599 f8d1f275 2019-02-04 stsp FILE *index = NULL;
1600 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
1601 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
1602 927df6b7 2019-02-10 stsp char *ondisk_path = NULL;
1603 f8d1f275 2019-02-04 stsp
1604 f8d1f275 2019-02-04 stsp fileindex = got_fileindex_alloc();
1605 f8d1f275 2019-02-04 stsp if (fileindex == NULL) {
1606 f8d1f275 2019-02-04 stsp err = got_error_from_errno();
1607 f8d1f275 2019-02-04 stsp goto done;
1608 f8d1f275 2019-02-04 stsp }
1609 f8d1f275 2019-02-04 stsp
1610 f8d1f275 2019-02-04 stsp if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1611 f8d1f275 2019-02-04 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1612 f8d1f275 2019-02-04 stsp err = got_error_from_errno();
1613 f8d1f275 2019-02-04 stsp fileindex_path = NULL;
1614 f8d1f275 2019-02-04 stsp goto done;
1615 f8d1f275 2019-02-04 stsp }
1616 f8d1f275 2019-02-04 stsp
1617 f8d1f275 2019-02-04 stsp index = fopen(fileindex_path, "rb");
1618 f8d1f275 2019-02-04 stsp if (index == NULL) {
1619 f8d1f275 2019-02-04 stsp if (errno != ENOENT) {
1620 f8d1f275 2019-02-04 stsp err = got_error_from_errno();
1621 f8d1f275 2019-02-04 stsp goto done;
1622 f8d1f275 2019-02-04 stsp }
1623 f8d1f275 2019-02-04 stsp } else {
1624 f8d1f275 2019-02-04 stsp err = got_fileindex_read(fileindex, index);
1625 f8d1f275 2019-02-04 stsp fclose(index);
1626 f8d1f275 2019-02-04 stsp if (err)
1627 f8d1f275 2019-02-04 stsp goto done;
1628 f8d1f275 2019-02-04 stsp }
1629 f8d1f275 2019-02-04 stsp
1630 927df6b7 2019-02-10 stsp if (asprintf(&ondisk_path, "%s%s%s",
1631 927df6b7 2019-02-10 stsp worktree->root_path, path[0] ? "/" : "", path) == -1) {
1632 c513d110 2019-02-05 stsp err = got_error_from_errno();
1633 c513d110 2019-02-05 stsp goto done;
1634 c513d110 2019-02-05 stsp }
1635 927df6b7 2019-02-10 stsp workdir = opendir(ondisk_path);
1636 927df6b7 2019-02-10 stsp if (workdir == NULL) {
1637 2ec1f75b 2019-03-26 stsp if (errno == ENOTDIR || errno == ENOENT) {
1638 927df6b7 2019-02-10 stsp struct got_fileindex_entry *ie;
1639 927df6b7 2019-02-10 stsp ie = got_fileindex_entry_get(fileindex, path);
1640 927df6b7 2019-02-10 stsp if (ie == NULL) {
1641 927df6b7 2019-02-10 stsp err = got_error(GOT_ERR_BAD_PATH);
1642 927df6b7 2019-02-10 stsp goto done;
1643 927df6b7 2019-02-10 stsp }
1644 927df6b7 2019-02-10 stsp err = report_file_status(ie, ondisk_path,
1645 927df6b7 2019-02-10 stsp status_cb, status_arg, repo);
1646 927df6b7 2019-02-10 stsp goto done;
1647 927df6b7 2019-02-10 stsp } else {
1648 927df6b7 2019-02-10 stsp err = got_error_from_errno();
1649 927df6b7 2019-02-10 stsp goto done;
1650 927df6b7 2019-02-10 stsp }
1651 927df6b7 2019-02-10 stsp }
1652 d43a8a88 2019-02-05 stsp fdiff_cb.diff_old_new = status_old_new;
1653 d43a8a88 2019-02-05 stsp fdiff_cb.diff_old = status_old;
1654 d43a8a88 2019-02-05 stsp fdiff_cb.diff_new = status_new;
1655 f8d1f275 2019-02-04 stsp arg.fileindex = fileindex;
1656 f8d1f275 2019-02-04 stsp arg.worktree = worktree;
1657 927df6b7 2019-02-10 stsp arg.status_path = path;
1658 927df6b7 2019-02-10 stsp arg.status_path_len = strlen(path);
1659 f8d1f275 2019-02-04 stsp arg.repo = repo;
1660 f8d1f275 2019-02-04 stsp arg.status_cb = status_cb;
1661 f8d1f275 2019-02-04 stsp arg.status_arg = status_arg;
1662 f8d1f275 2019-02-04 stsp arg.cancel_cb = cancel_cb;
1663 f8d1f275 2019-02-04 stsp arg.cancel_arg = cancel_arg;
1664 c7f4312f 2019-02-05 stsp err = got_fileindex_diff_dir(fileindex, workdir, worktree->root_path,
1665 927df6b7 2019-02-10 stsp path, repo, &fdiff_cb, &arg);
1666 f8d1f275 2019-02-04 stsp done:
1667 f8d1f275 2019-02-04 stsp if (workdir)
1668 f8d1f275 2019-02-04 stsp closedir(workdir);
1669 927df6b7 2019-02-10 stsp free(ondisk_path);
1670 f8d1f275 2019-02-04 stsp free(fileindex_path);
1671 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
1672 f8d1f275 2019-02-04 stsp return err;
1673 f8d1f275 2019-02-04 stsp }
1674 6c7ab921 2019-03-18 stsp
1675 6c7ab921 2019-03-18 stsp const struct got_error *
1676 6c7ab921 2019-03-18 stsp got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree,
1677 6c7ab921 2019-03-18 stsp const char *arg)
1678 6c7ab921 2019-03-18 stsp {
1679 6c7ab921 2019-03-18 stsp const struct got_error *err = NULL;
1680 6c7ab921 2019-03-18 stsp char *resolved, *path = NULL;
1681 6c7ab921 2019-03-18 stsp size_t len;
1682 6c7ab921 2019-03-18 stsp
1683 6c7ab921 2019-03-18 stsp *wt_path = NULL;
1684 6c7ab921 2019-03-18 stsp
1685 6c7ab921 2019-03-18 stsp resolved = realpath(arg, NULL);
1686 6c7ab921 2019-03-18 stsp if (resolved == NULL)
1687 6c7ab921 2019-03-18 stsp return got_error_from_errno();
1688 6c7ab921 2019-03-18 stsp
1689 6c7ab921 2019-03-18 stsp if (strncmp(got_worktree_get_root_path(worktree), resolved,
1690 6c7ab921 2019-03-18 stsp strlen(got_worktree_get_root_path(worktree)))) {
1691 6c7ab921 2019-03-18 stsp err = got_error(GOT_ERR_BAD_PATH);
1692 6c7ab921 2019-03-18 stsp goto done;
1693 6c7ab921 2019-03-18 stsp }
1694 6c7ab921 2019-03-18 stsp
1695 6c7ab921 2019-03-18 stsp path = strdup(resolved + strlen(got_worktree_get_root_path(worktree)));
1696 6c7ab921 2019-03-18 stsp if (path == NULL) {
1697 6c7ab921 2019-03-18 stsp err = got_error_from_errno();
1698 6c7ab921 2019-03-18 stsp goto done;
1699 6c7ab921 2019-03-18 stsp }
1700 6c7ab921 2019-03-18 stsp
1701 6c7ab921 2019-03-18 stsp /* XXX status walk can't deal with trailing slash! */
1702 6c7ab921 2019-03-18 stsp len = strlen(path);
1703 6c7ab921 2019-03-18 stsp while (path[len - 1] == '/') {
1704 6c7ab921 2019-03-18 stsp path[len - 1] = '\0';
1705 6c7ab921 2019-03-18 stsp len--;
1706 6c7ab921 2019-03-18 stsp }
1707 6c7ab921 2019-03-18 stsp done:
1708 6c7ab921 2019-03-18 stsp free(resolved);
1709 6c7ab921 2019-03-18 stsp if (err == NULL)
1710 6c7ab921 2019-03-18 stsp *wt_path = path;
1711 6c7ab921 2019-03-18 stsp else
1712 6c7ab921 2019-03-18 stsp free(path);
1713 d00136be 2019-03-26 stsp return err;
1714 d00136be 2019-03-26 stsp }
1715 d00136be 2019-03-26 stsp
1716 d00136be 2019-03-26 stsp const struct got_error *
1717 031a5338 2019-03-26 stsp got_worktree_schedule_add(struct got_worktree *worktree,
1718 031a5338 2019-03-26 stsp const char *ondisk_path, got_worktree_status_cb status_cb, void *status_arg,
1719 031a5338 2019-03-26 stsp struct got_repository *repo)
1720 d00136be 2019-03-26 stsp {
1721 d00136be 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
1722 d00136be 2019-03-26 stsp struct got_fileindex_entry *ie = NULL;
1723 031a5338 2019-03-26 stsp char *relpath, *fileindex_path = NULL, *new_fileindex_path = NULL;
1724 d00136be 2019-03-26 stsp FILE *index = NULL, *new_index = NULL;
1725 d00136be 2019-03-26 stsp const struct got_error *err = NULL, *unlockerr = NULL;
1726 031a5338 2019-03-26 stsp int ie_added = 0;
1727 d00136be 2019-03-26 stsp
1728 d00136be 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
1729 d00136be 2019-03-26 stsp if (err)
1730 d00136be 2019-03-26 stsp return err;
1731 d00136be 2019-03-26 stsp
1732 031a5338 2019-03-26 stsp err = got_path_skip_common_ancestor(&relpath,
1733 d00136be 2019-03-26 stsp got_worktree_get_root_path(worktree), ondisk_path);
1734 d00136be 2019-03-26 stsp if (err)
1735 d00136be 2019-03-26 stsp goto done;
1736 d00136be 2019-03-26 stsp
1737 d00136be 2019-03-26 stsp fileindex = got_fileindex_alloc();
1738 d00136be 2019-03-26 stsp if (fileindex == NULL) {
1739 d00136be 2019-03-26 stsp err = got_error_from_errno();
1740 d00136be 2019-03-26 stsp goto done;
1741 d00136be 2019-03-26 stsp }
1742 d00136be 2019-03-26 stsp
1743 d00136be 2019-03-26 stsp if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1744 d00136be 2019-03-26 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1745 d00136be 2019-03-26 stsp err = got_error_from_errno();
1746 d00136be 2019-03-26 stsp fileindex_path = NULL;
1747 d00136be 2019-03-26 stsp goto done;
1748 d00136be 2019-03-26 stsp }
1749 d00136be 2019-03-26 stsp
1750 d00136be 2019-03-26 stsp index = fopen(fileindex_path, "rb");
1751 d00136be 2019-03-26 stsp if (index == NULL) {
1752 d00136be 2019-03-26 stsp err = got_error_from_errno();
1753 d00136be 2019-03-26 stsp goto done;
1754 d00136be 2019-03-26 stsp }
1755 d00136be 2019-03-26 stsp
1756 d00136be 2019-03-26 stsp err = got_fileindex_read(fileindex, index);
1757 d00136be 2019-03-26 stsp if (err)
1758 d00136be 2019-03-26 stsp goto done;
1759 d00136be 2019-03-26 stsp
1760 5c99ca9f 2019-03-27 stsp if (got_fileindex_entry_get(fileindex, relpath) != NULL) {
1761 5c99ca9f 2019-03-27 stsp err = got_error_set_errno(EEXIST);
1762 5c99ca9f 2019-03-27 stsp goto done;
1763 5c99ca9f 2019-03-27 stsp }
1764 5c99ca9f 2019-03-27 stsp
1765 5c99ca9f 2019-03-27 stsp err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
1766 5c99ca9f 2019-03-27 stsp if (err)
1767 5c99ca9f 2019-03-27 stsp goto done;
1768 5c99ca9f 2019-03-27 stsp
1769 d00136be 2019-03-26 stsp err = got_fileindex_entry_add(fileindex, ie);
1770 d00136be 2019-03-26 stsp if (err)
1771 d00136be 2019-03-26 stsp goto done;
1772 031a5338 2019-03-26 stsp ie_added = 1; /* now owned by fileindex; don't free separately */
1773 d00136be 2019-03-26 stsp
1774 d00136be 2019-03-26 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
1775 d00136be 2019-03-26 stsp fileindex_path);
1776 d00136be 2019-03-26 stsp if (err)
1777 d00136be 2019-03-26 stsp goto done;
1778 d00136be 2019-03-26 stsp
1779 d00136be 2019-03-26 stsp err = got_fileindex_write(fileindex, new_index);
1780 d00136be 2019-03-26 stsp if (err)
1781 d00136be 2019-03-26 stsp goto done;
1782 d00136be 2019-03-26 stsp
1783 d00136be 2019-03-26 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
1784 d00136be 2019-03-26 stsp err = got_error_from_errno();
1785 d00136be 2019-03-26 stsp goto done;
1786 d00136be 2019-03-26 stsp }
1787 d00136be 2019-03-26 stsp
1788 d00136be 2019-03-26 stsp free(new_fileindex_path);
1789 d00136be 2019-03-26 stsp new_fileindex_path = NULL;
1790 031a5338 2019-03-26 stsp
1791 031a5338 2019-03-26 stsp err = report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
1792 d00136be 2019-03-26 stsp done:
1793 d00136be 2019-03-26 stsp if (index) {
1794 d00136be 2019-03-26 stsp if (fclose(index) != 0 && err == NULL)
1795 d00136be 2019-03-26 stsp err = got_error_from_errno();
1796 d00136be 2019-03-26 stsp }
1797 d00136be 2019-03-26 stsp if (new_fileindex_path) {
1798 d00136be 2019-03-26 stsp if (unlink(new_fileindex_path) != 0 && err == NULL)
1799 d00136be 2019-03-26 stsp err = got_error_from_errno();
1800 d00136be 2019-03-26 stsp free(new_fileindex_path);
1801 d00136be 2019-03-26 stsp }
1802 5c99ca9f 2019-03-27 stsp if (ie && !ie_added)
1803 d00136be 2019-03-26 stsp got_fileindex_entry_free(ie);
1804 d00136be 2019-03-26 stsp if (fileindex)
1805 d00136be 2019-03-26 stsp got_fileindex_free(fileindex);
1806 d00136be 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
1807 d00136be 2019-03-26 stsp if (unlockerr && err == NULL)
1808 d00136be 2019-03-26 stsp err = unlockerr;
1809 031a5338 2019-03-26 stsp free(relpath);
1810 6c7ab921 2019-03-18 stsp return err;
1811 6c7ab921 2019-03-18 stsp }
1812 2ec1f75b 2019-03-26 stsp
1813 2ec1f75b 2019-03-26 stsp const struct got_error *
1814 2ec1f75b 2019-03-26 stsp got_worktree_schedule_delete(struct got_worktree *worktree,
1815 2ec1f75b 2019-03-26 stsp const char *ondisk_path, int delete_local_mods,
1816 2ec1f75b 2019-03-26 stsp got_worktree_status_cb status_cb, void *status_arg,
1817 2ec1f75b 2019-03-26 stsp struct got_repository *repo)
1818 2ec1f75b 2019-03-26 stsp {
1819 2ec1f75b 2019-03-26 stsp struct got_fileindex *fileindex = NULL;
1820 2ec1f75b 2019-03-26 stsp struct got_fileindex_entry *ie = NULL;
1821 2ec1f75b 2019-03-26 stsp char *relpath, *fileindex_path = NULL, *new_fileindex_path = NULL;
1822 2ec1f75b 2019-03-26 stsp FILE *index = NULL, *new_index = NULL;
1823 2ec1f75b 2019-03-26 stsp const struct got_error *err = NULL, *unlockerr = NULL;
1824 2ec1f75b 2019-03-26 stsp unsigned char status;
1825 2ec1f75b 2019-03-26 stsp struct stat sb;
1826 2ec1f75b 2019-03-26 stsp
1827 2ec1f75b 2019-03-26 stsp err = lock_worktree(worktree, LOCK_EX);
1828 2ec1f75b 2019-03-26 stsp if (err)
1829 2ec1f75b 2019-03-26 stsp return err;
1830 2ec1f75b 2019-03-26 stsp
1831 2ec1f75b 2019-03-26 stsp err = got_path_skip_common_ancestor(&relpath,
1832 2ec1f75b 2019-03-26 stsp got_worktree_get_root_path(worktree), ondisk_path);
1833 2ec1f75b 2019-03-26 stsp if (err)
1834 2ec1f75b 2019-03-26 stsp goto done;
1835 2ec1f75b 2019-03-26 stsp
1836 2ec1f75b 2019-03-26 stsp fileindex = got_fileindex_alloc();
1837 2ec1f75b 2019-03-26 stsp if (fileindex == NULL) {
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 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1843 2ec1f75b 2019-03-26 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1844 2ec1f75b 2019-03-26 stsp err = got_error_from_errno();
1845 2ec1f75b 2019-03-26 stsp fileindex_path = NULL;
1846 2ec1f75b 2019-03-26 stsp goto done;
1847 2ec1f75b 2019-03-26 stsp }
1848 2ec1f75b 2019-03-26 stsp
1849 2ec1f75b 2019-03-26 stsp index = fopen(fileindex_path, "rb");
1850 2ec1f75b 2019-03-26 stsp if (index == NULL) {
1851 2ec1f75b 2019-03-26 stsp err = got_error_from_errno();
1852 2ec1f75b 2019-03-26 stsp goto done;
1853 2ec1f75b 2019-03-26 stsp }
1854 2ec1f75b 2019-03-26 stsp
1855 2ec1f75b 2019-03-26 stsp err = got_fileindex_read(fileindex, index);
1856 2ec1f75b 2019-03-26 stsp if (err)
1857 2ec1f75b 2019-03-26 stsp goto done;
1858 2ec1f75b 2019-03-26 stsp
1859 2ec1f75b 2019-03-26 stsp ie = got_fileindex_entry_get(fileindex, relpath);
1860 2ec1f75b 2019-03-26 stsp if (ie == NULL) {
1861 2ec1f75b 2019-03-26 stsp err = got_error(GOT_ERR_BAD_PATH);
1862 2ec1f75b 2019-03-26 stsp goto done;
1863 2ec1f75b 2019-03-26 stsp }
1864 2ec1f75b 2019-03-26 stsp
1865 2ec1f75b 2019-03-26 stsp err = get_file_status(&status, &sb, ie, ondisk_path, repo);
1866 2ec1f75b 2019-03-26 stsp if (err)
1867 2ec1f75b 2019-03-26 stsp goto done;
1868 2ec1f75b 2019-03-26 stsp
1869 2ec1f75b 2019-03-26 stsp if (status != GOT_STATUS_NO_CHANGE) {
1870 71a29355 2019-03-27 stsp if (status == GOT_STATUS_DELETE) {
1871 71a29355 2019-03-27 stsp err = got_error_set_errno(ENOENT);
1872 71a29355 2019-03-27 stsp goto done;
1873 71a29355 2019-03-27 stsp }
1874 2ec1f75b 2019-03-26 stsp if (status != GOT_STATUS_MODIFY) {
1875 2ec1f75b 2019-03-26 stsp err = got_error(GOT_ERR_FILE_STATUS);
1876 2ec1f75b 2019-03-26 stsp goto done;
1877 2ec1f75b 2019-03-26 stsp }
1878 2ec1f75b 2019-03-26 stsp if (!delete_local_mods) {
1879 2ec1f75b 2019-03-26 stsp err = got_error(GOT_ERR_FILE_MODIFIED);
1880 2ec1f75b 2019-03-26 stsp goto done;
1881 2ec1f75b 2019-03-26 stsp }
1882 2ec1f75b 2019-03-26 stsp }
1883 2ec1f75b 2019-03-26 stsp
1884 2ec1f75b 2019-03-26 stsp if (unlink(ondisk_path) != 0) {
1885 2ec1f75b 2019-03-26 stsp err = got_error_from_errno();
1886 2ec1f75b 2019-03-26 stsp goto done;
1887 2ec1f75b 2019-03-26 stsp }
1888 2ec1f75b 2019-03-26 stsp
1889 2ec1f75b 2019-03-26 stsp got_fileindex_entry_mark_deleted_from_disk(ie);
1890 2ec1f75b 2019-03-26 stsp
1891 2ec1f75b 2019-03-26 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
1892 2ec1f75b 2019-03-26 stsp fileindex_path);
1893 2ec1f75b 2019-03-26 stsp if (err)
1894 2ec1f75b 2019-03-26 stsp goto done;
1895 2ec1f75b 2019-03-26 stsp
1896 2ec1f75b 2019-03-26 stsp err = got_fileindex_write(fileindex, new_index);
1897 2ec1f75b 2019-03-26 stsp if (err)
1898 2ec1f75b 2019-03-26 stsp goto done;
1899 2ec1f75b 2019-03-26 stsp
1900 2ec1f75b 2019-03-26 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
1901 2ec1f75b 2019-03-26 stsp err = got_error_from_errno();
1902 2ec1f75b 2019-03-26 stsp goto done;
1903 2ec1f75b 2019-03-26 stsp }
1904 2ec1f75b 2019-03-26 stsp
1905 2ec1f75b 2019-03-26 stsp free(new_fileindex_path);
1906 2ec1f75b 2019-03-26 stsp new_fileindex_path = NULL;
1907 2ec1f75b 2019-03-26 stsp
1908 2ec1f75b 2019-03-26 stsp err = report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
1909 2ec1f75b 2019-03-26 stsp done:
1910 2ec1f75b 2019-03-26 stsp free(relpath);
1911 2ec1f75b 2019-03-26 stsp if (index) {
1912 2ec1f75b 2019-03-26 stsp if (fclose(index) != 0 && err == NULL)
1913 2ec1f75b 2019-03-26 stsp err = got_error_from_errno();
1914 2ec1f75b 2019-03-26 stsp }
1915 2ec1f75b 2019-03-26 stsp if (new_fileindex_path) {
1916 2ec1f75b 2019-03-26 stsp if (unlink(new_fileindex_path) != 0 && err == NULL)
1917 2ec1f75b 2019-03-26 stsp err = got_error_from_errno();
1918 2ec1f75b 2019-03-26 stsp free(new_fileindex_path);
1919 2ec1f75b 2019-03-26 stsp }
1920 2ec1f75b 2019-03-26 stsp if (fileindex)
1921 2ec1f75b 2019-03-26 stsp got_fileindex_free(fileindex);
1922 2ec1f75b 2019-03-26 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
1923 2ec1f75b 2019-03-26 stsp if (unlockerr && err == NULL)
1924 2ec1f75b 2019-03-26 stsp err = unlockerr;
1925 2ec1f75b 2019-03-26 stsp return err;
1926 2ec1f75b 2019-03-26 stsp }