2 5d56da81 2019-01-13 stsp * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
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.
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.
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>
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>
35 86c3caaf 2018-03-09 stsp #include "got_error.h"
36 86c3caaf 2018-03-09 stsp #include "got_repository.h"
37 5261c201 2018-04-01 stsp #include "got_reference.h"
38 9d31a1d8 2018-03-11 stsp #include "got_object.h"
39 86c3caaf 2018-03-09 stsp #include "got_worktree.h"
40 511a516b 2018-05-19 stsp #include "got_opentemp.h"
42 718b3ab0 2018-03-17 stsp #include "got_lib_worktree.h"
43 718b3ab0 2018-03-17 stsp #include "got_lib_path.h"
44 718b3ab0 2018-03-17 stsp #include "got_lib_sha1.h"
45 718b3ab0 2018-03-17 stsp #include "got_lib_fileindex.h"
46 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
47 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
48 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
51 9d31a1d8 2018-03-11 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
54 99724ed4 2018-03-10 stsp static const struct got_error *
55 7ac97322 2018-03-11 stsp create_meta_file(const char *path_got, const char *name, const char *content)
57 99724ed4 2018-03-10 stsp const struct got_error *err = NULL;
59 99724ed4 2018-03-10 stsp int fd = -1;
61 7ac97322 2018-03-11 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
62 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
63 99724ed4 2018-03-10 stsp path = NULL;
67 73a5ef67 2018-03-11 stsp fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
68 99724ed4 2018-03-10 stsp GOT_DEFAULT_FILE_MODE);
69 99724ed4 2018-03-10 stsp if (fd == -1) {
70 99724ed4 2018-03-10 stsp err = got_error_from_errno();
74 99724ed4 2018-03-10 stsp if (content) {
75 99724ed4 2018-03-10 stsp int len = dprintf(fd, "%s\n", content);
76 99724ed4 2018-03-10 stsp if (len != strlen(content) + 1) {
77 99724ed4 2018-03-10 stsp err = got_error_from_errno();
83 99724ed4 2018-03-10 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
84 99724ed4 2018-03-10 stsp err = got_error_from_errno();
89 507dc3bb 2018-12-29 stsp static const struct got_error *
90 507dc3bb 2018-12-29 stsp update_meta_file(const char *path_got, const char *name, const char *content)
92 507dc3bb 2018-12-29 stsp const struct got_error *err = NULL;
93 507dc3bb 2018-12-29 stsp FILE *tmpfile = NULL;
94 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
95 507dc3bb 2018-12-29 stsp char *path = NULL;
97 507dc3bb 2018-12-29 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
98 507dc3bb 2018-12-29 stsp err = got_error_from_errno();
99 507dc3bb 2018-12-29 stsp path = NULL;
103 507dc3bb 2018-12-29 stsp err = got_opentemp_named(&tmppath, &tmpfile, path);
107 507dc3bb 2018-12-29 stsp if (content) {
108 507dc3bb 2018-12-29 stsp int len = fprintf(tmpfile, "%s\n", content);
109 507dc3bb 2018-12-29 stsp if (len != strlen(content) + 1) {
110 507dc3bb 2018-12-29 stsp err = got_error_from_errno();
115 507dc3bb 2018-12-29 stsp if (rename(tmppath, path) != 0) {
116 507dc3bb 2018-12-29 stsp err = got_error_from_errno();
121 507dc3bb 2018-12-29 stsp free(tmppath);
122 507dc3bb 2018-12-29 stsp fclose(tmpfile);
123 99724ed4 2018-03-10 stsp return err;
126 09fe317a 2018-03-11 stsp static const struct got_error *
127 7ac97322 2018-03-11 stsp read_meta_file(char **content, const char *path_got, const char *name)
129 09fe317a 2018-03-11 stsp const struct got_error *err = NULL;
130 09fe317a 2018-03-11 stsp char *path;
131 09fe317a 2018-03-11 stsp int fd = -1;
133 09fe317a 2018-03-11 stsp struct stat sb;
135 09fe317a 2018-03-11 stsp *content = NULL;
137 7ac97322 2018-03-11 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
138 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
139 09fe317a 2018-03-11 stsp path = NULL;
143 ef99fdb1 2018-03-11 stsp fd = open(path, O_RDONLY | O_NOFOLLOW);
144 09fe317a 2018-03-11 stsp if (fd == -1) {
145 ef99fdb1 2018-03-11 stsp err = got_error_from_errno();
148 ef99fdb1 2018-03-11 stsp if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
149 73a5ef67 2018-03-11 stsp err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
150 73a5ef67 2018-03-11 stsp : got_error_from_errno());
154 09fe317a 2018-03-11 stsp stat(path, &sb);
155 09fe317a 2018-03-11 stsp *content = calloc(1, sb.st_size);
156 09fe317a 2018-03-11 stsp if (*content == NULL) {
157 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
161 09fe317a 2018-03-11 stsp n = read(fd, *content, sb.st_size);
162 09fe317a 2018-03-11 stsp if (n != sb.st_size) {
163 0605801d 2018-03-11 stsp err = (n == -1 ? got_error_from_errno() :
164 0605801d 2018-03-11 stsp got_error(GOT_ERR_WORKTREE_META));
167 09fe317a 2018-03-11 stsp if ((*content)[sb.st_size - 1] != '\n') {
168 09fe317a 2018-03-11 stsp err = got_error(GOT_ERR_WORKTREE_META);
171 09fe317a 2018-03-11 stsp (*content)[sb.st_size - 1] = '\0';
174 09fe317a 2018-03-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
175 09fe317a 2018-03-11 stsp err = got_error_from_errno();
176 09fe317a 2018-03-11 stsp free(path);
178 09fe317a 2018-03-11 stsp free(*content);
179 09fe317a 2018-03-11 stsp *content = NULL;
181 09fe317a 2018-03-11 stsp return err;
184 86c3caaf 2018-03-09 stsp const struct got_error *
185 86c3caaf 2018-03-09 stsp got_worktree_init(const char *path, struct got_reference *head_ref,
186 577ec78f 2018-03-11 stsp const char *prefix, struct got_repository *repo)
188 86c3caaf 2018-03-09 stsp const struct got_error *err = NULL;
189 65596e15 2018-12-24 stsp struct got_object_id *commit_id = NULL;
190 65596e15 2018-12-24 stsp int obj_type;
191 7ac97322 2018-03-11 stsp char *path_got = NULL;
192 08d425ea 2018-12-24 stsp char *refstr = NULL;
193 1451e70d 2018-03-10 stsp char *formatstr = NULL;
194 0bb8a95e 2018-03-12 stsp char *absprefix = NULL;
195 65596e15 2018-12-24 stsp char *basestr = NULL;
197 65596e15 2018-12-24 stsp err = got_ref_resolve(&commit_id, repo, head_ref);
199 65596e15 2018-12-24 stsp return err;
200 65596e15 2018-12-24 stsp err = got_object_get_type(&obj_type, repo, commit_id);
202 65596e15 2018-12-24 stsp return err;
203 65596e15 2018-12-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
204 65596e15 2018-12-24 stsp return got_error(GOT_ERR_OBJ_TYPE);
206 0bb8a95e 2018-03-12 stsp if (!got_path_is_absolute(prefix)) {
207 0bb8a95e 2018-03-12 stsp if (asprintf(&absprefix, "/%s", prefix) == -1)
208 0a585a0d 2018-03-17 stsp return got_error_from_errno();
211 86c3caaf 2018-03-09 stsp /* Create top-level directory (may already exist). */
212 2cb4bacb 2018-03-11 stsp if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
213 86c3caaf 2018-03-09 stsp err = got_error_from_errno();
217 86c3caaf 2018-03-09 stsp /* Create .got directory (may already exist). */
218 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
219 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
222 7ac97322 2018-03-11 stsp if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
223 86c3caaf 2018-03-09 stsp err = got_error_from_errno();
227 056e7441 2018-03-11 stsp /* Create an empty lock file. */
228 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
232 86c3caaf 2018-03-09 stsp /* Create an empty file index. */
233 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
237 08d425ea 2018-12-24 stsp /* Write the HEAD reference. */
238 08d425ea 2018-12-24 stsp refstr = got_ref_to_str(head_ref);
239 08d425ea 2018-12-24 stsp if (refstr == NULL) {
240 a1a7858a 2018-12-24 stsp err = got_error_from_errno();
243 0f92850e 2018-12-25 stsp err = create_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
247 65596e15 2018-12-24 stsp /* Record our base commit. */
248 65596e15 2018-12-24 stsp err = got_object_id_str(&basestr, commit_id);
251 0f92850e 2018-12-25 stsp err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
255 1451e70d 2018-03-10 stsp /* Store path to repository. */
256 7839bc15 2019-01-06 stsp err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
257 7839bc15 2019-01-06 stsp got_repo_get_path(repo));
261 577ec78f 2018-03-11 stsp /* Store in-repository path prefix. */
262 0bb8a95e 2018-03-12 stsp err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
263 0bb8a95e 2018-03-12 stsp absprefix ? absprefix : prefix);
267 9dce68ed 2018-03-10 stsp /* Stamp work tree with format file. */
268 1451e70d 2018-03-10 stsp if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
269 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
272 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
277 65596e15 2018-12-24 stsp free(commit_id);
278 7ac97322 2018-03-11 stsp free(path_got);
279 1451e70d 2018-03-10 stsp free(formatstr);
280 08d425ea 2018-12-24 stsp free(refstr);
281 0bb8a95e 2018-03-12 stsp free(absprefix);
282 65596e15 2018-12-24 stsp free(basestr);
283 86c3caaf 2018-03-09 stsp return err;
286 247140b2 2019-02-05 stsp static const struct got_error *
287 247140b2 2019-02-05 stsp open_worktree(struct got_worktree **worktree, const char *path)
289 6d9d28c3 2018-03-11 stsp const struct got_error *err = NULL;
290 7ac97322 2018-03-11 stsp char *path_got;
291 6d9d28c3 2018-03-11 stsp char *formatstr = NULL;
292 056e7441 2018-03-11 stsp char *path_lock = NULL;
293 eaccb85f 2018-12-25 stsp char *base_commit_id_str = NULL;
294 271d2a38 2018-12-25 stsp char *head_ref_str = NULL;
295 6d9d28c3 2018-03-11 stsp int version, fd = -1;
296 6d9d28c3 2018-03-11 stsp const char *errstr;
297 eaccb85f 2018-12-25 stsp struct got_repository *repo = NULL;
299 6d9d28c3 2018-03-11 stsp *worktree = NULL;
301 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
302 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
303 7ac97322 2018-03-11 stsp path_got = NULL;
307 7ac97322 2018-03-11 stsp if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
308 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
309 056e7441 2018-03-11 stsp path_lock = NULL;
313 056e7441 2018-03-11 stsp fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
314 6d9d28c3 2018-03-11 stsp if (fd == -1) {
315 73a5ef67 2018-03-11 stsp err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
316 73a5ef67 2018-03-11 stsp : got_error_from_errno());
320 7ac97322 2018-03-11 stsp err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
324 6d9d28c3 2018-03-11 stsp version = strtonum(formatstr, 1, INT_MAX, &errstr);
325 6d9d28c3 2018-03-11 stsp if (errstr) {
326 6d9d28c3 2018-03-11 stsp err = got_error(GOT_ERR_WORKTREE_META);
329 6d9d28c3 2018-03-11 stsp if (version != GOT_WORKTREE_FORMAT_VERSION) {
330 6d9d28c3 2018-03-11 stsp err = got_error(GOT_ERR_WORKTREE_VERS);
334 6d9d28c3 2018-03-11 stsp *worktree = calloc(1, sizeof(**worktree));
335 6d9d28c3 2018-03-11 stsp if (*worktree == NULL) {
336 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
339 056e7441 2018-03-11 stsp (*worktree)->lockfd = -1;
341 c88eb298 2018-03-11 stsp (*worktree)->root_path = strdup(path);
342 c88eb298 2018-03-11 stsp if ((*worktree)->root_path == NULL) {
343 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
346 cde76477 2018-03-11 stsp err = read_meta_file(&(*worktree)->repo_path, path_got,
347 6d9d28c3 2018-03-11 stsp GOT_WORKTREE_REPOSITORY);
351 7ac97322 2018-03-11 stsp err = read_meta_file(&(*worktree)->path_prefix, path_got,
352 6d9d28c3 2018-03-11 stsp GOT_WORKTREE_PATH_PREFIX);
356 eaccb85f 2018-12-25 stsp err = read_meta_file(&base_commit_id_str, path_got,
357 0f92850e 2018-12-25 stsp GOT_WORKTREE_BASE_COMMIT);
361 eaccb85f 2018-12-25 stsp err = got_repo_open(&repo, (*worktree)->repo_path);
365 eaccb85f 2018-12-25 stsp err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
366 eaccb85f 2018-12-25 stsp base_commit_id_str);
370 271d2a38 2018-12-25 stsp err = read_meta_file(&head_ref_str, path_got, GOT_WORKTREE_HEAD_REF);
374 271d2a38 2018-12-25 stsp err = got_ref_open(&(*worktree)->head_ref, repo, head_ref_str);
377 eaccb85f 2018-12-25 stsp got_repo_close(repo);
378 7ac97322 2018-03-11 stsp free(path_got);
379 056e7441 2018-03-11 stsp free(path_lock);
380 271d2a38 2018-12-25 stsp free(head_ref_str);
381 eaccb85f 2018-12-25 stsp free(base_commit_id_str);
383 6d9d28c3 2018-03-11 stsp if (fd != -1)
385 6d9d28c3 2018-03-11 stsp if (*worktree != NULL)
386 6d9d28c3 2018-03-11 stsp got_worktree_close(*worktree);
387 6d9d28c3 2018-03-11 stsp *worktree = NULL;
389 056e7441 2018-03-11 stsp (*worktree)->lockfd = fd;
391 6d9d28c3 2018-03-11 stsp return err;
394 247140b2 2019-02-05 stsp const struct got_error *
395 247140b2 2019-02-05 stsp got_worktree_open(struct got_worktree **worktree, const char *path)
397 247140b2 2019-02-05 stsp const struct got_error *err = NULL;
400 247140b2 2019-02-05 stsp err = open_worktree(worktree, path);
401 247140b2 2019-02-05 stsp if (err && (err->code != GOT_ERR_ERRNO && errno != ENOENT))
402 247140b2 2019-02-05 stsp return err;
403 247140b2 2019-02-05 stsp if (*worktree)
404 247140b2 2019-02-05 stsp return NULL;
405 247140b2 2019-02-05 stsp path = dirname(path);
406 d1542a27 2019-02-05 stsp if (path == NULL)
407 d1542a27 2019-02-05 stsp return got_error_from_errno();
408 d1542a27 2019-02-05 stsp } while (!((path[0] == '.' || path[0] == '/') && path[1] == '\0'));
410 247140b2 2019-02-05 stsp return got_error(GOT_ERR_NOT_WORKTREE);
414 86c3caaf 2018-03-09 stsp got_worktree_close(struct got_worktree *worktree)
416 c88eb298 2018-03-11 stsp free(worktree->root_path);
417 cde76477 2018-03-11 stsp free(worktree->repo_path);
418 6d9d28c3 2018-03-11 stsp free(worktree->path_prefix);
419 eaccb85f 2018-12-25 stsp free(worktree->base_commit_id);
420 271d2a38 2018-12-25 stsp if (worktree->head_ref)
421 271d2a38 2018-12-25 stsp got_ref_close(worktree->head_ref);
422 056e7441 2018-03-11 stsp if (worktree->lockfd != -1)
423 056e7441 2018-03-11 stsp close(worktree->lockfd);
424 6d9d28c3 2018-03-11 stsp free(worktree);
427 2fbdb5ae 2018-12-29 stsp const char *
428 c7f4312f 2019-02-05 stsp got_worktree_get_root_path(struct got_worktree *worktree)
430 c7f4312f 2019-02-05 stsp return worktree->root_path;
433 c7f4312f 2019-02-05 stsp const char *
434 86c3caaf 2018-03-09 stsp got_worktree_get_repo_path(struct got_worktree *worktree)
436 2fbdb5ae 2018-12-29 stsp return worktree->repo_path;
439 49520a32 2018-12-29 stsp const char *
440 49520a32 2018-12-29 stsp got_worktree_get_path_prefix(struct got_worktree *worktree)
442 f609be2e 2018-12-29 stsp return worktree->path_prefix;
445 e5dc7198 2018-12-29 stsp const struct got_error *
446 e5dc7198 2018-12-29 stsp got_worktree_match_path_prefix(int *match, struct got_worktree *worktree,
447 e5dc7198 2018-12-29 stsp const char *path_prefix)
449 e5dc7198 2018-12-29 stsp char *absprefix = NULL;
451 e5dc7198 2018-12-29 stsp if (!got_path_is_absolute(path_prefix)) {
452 e5dc7198 2018-12-29 stsp if (asprintf(&absprefix, "/%s", path_prefix) == -1)
453 e5dc7198 2018-12-29 stsp return got_error_from_errno();
455 e5dc7198 2018-12-29 stsp *match = (strcmp(absprefix ? absprefix : path_prefix,
456 e5dc7198 2018-12-29 stsp worktree->path_prefix) == 0);
457 e5dc7198 2018-12-29 stsp free(absprefix);
458 e5dc7198 2018-12-29 stsp return NULL;
462 35be1456 2018-03-11 stsp got_worktree_get_head_ref_name(struct got_worktree *worktree)
464 271d2a38 2018-12-25 stsp return got_ref_to_str(worktree->head_ref);
467 be7061eb 2018-12-30 stsp struct got_reference *
468 be7061eb 2018-12-30 stsp got_worktree_get_head_ref(struct got_worktree *worktree)
470 be7061eb 2018-12-30 stsp return got_ref_dup(worktree->head_ref);
473 507dc3bb 2018-12-29 stsp const struct got_object_id *
474 507dc3bb 2018-12-29 stsp got_worktree_get_base_commit_id(struct got_worktree *worktree)
476 507dc3bb 2018-12-29 stsp return worktree->base_commit_id;
479 507dc3bb 2018-12-29 stsp const struct got_error *
480 507dc3bb 2018-12-29 stsp got_worktree_set_base_commit_id(struct got_worktree *worktree,
481 507dc3bb 2018-12-29 stsp struct got_repository *repo, struct got_object_id *commit_id)
483 507dc3bb 2018-12-29 stsp const struct got_error *err;
484 507dc3bb 2018-12-29 stsp struct got_object *obj = NULL;
485 507dc3bb 2018-12-29 stsp char *id_str = NULL;
486 507dc3bb 2018-12-29 stsp char *path_got = NULL;
488 507dc3bb 2018-12-29 stsp if (asprintf(&path_got, "%s/%s", worktree->root_path,
489 507dc3bb 2018-12-29 stsp GOT_WORKTREE_GOT_DIR) == -1) {
490 507dc3bb 2018-12-29 stsp err = got_error_from_errno();
491 507dc3bb 2018-12-29 stsp path_got = NULL;
495 507dc3bb 2018-12-29 stsp err = got_object_open(&obj, repo, commit_id);
497 507dc3bb 2018-12-29 stsp return err;
499 507dc3bb 2018-12-29 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT) {
500 507dc3bb 2018-12-29 stsp err = got_error(GOT_ERR_OBJ_TYPE);
504 507dc3bb 2018-12-29 stsp /* Record our base commit. */
505 507dc3bb 2018-12-29 stsp err = got_object_id_str(&id_str, commit_id);
508 507dc3bb 2018-12-29 stsp err = update_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, id_str);
512 507dc3bb 2018-12-29 stsp free(worktree->base_commit_id);
513 507dc3bb 2018-12-29 stsp worktree->base_commit_id = got_object_id_dup(commit_id);
514 507dc3bb 2018-12-29 stsp if (worktree->base_commit_id == NULL) {
515 507dc3bb 2018-12-29 stsp err = got_error_from_errno();
520 507dc3bb 2018-12-29 stsp got_object_close(obj);
521 507dc3bb 2018-12-29 stsp free(id_str);
522 507dc3bb 2018-12-29 stsp free(path_got);
523 507dc3bb 2018-12-29 stsp return err;
526 9d31a1d8 2018-03-11 stsp static const struct got_error *
527 9d31a1d8 2018-03-11 stsp lock_worktree(struct got_worktree *worktree, int operation)
529 9d31a1d8 2018-03-11 stsp if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
530 9d31a1d8 2018-03-11 stsp return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
531 9d31a1d8 2018-03-11 stsp : got_error_from_errno());
532 86c3caaf 2018-03-09 stsp return NULL;
535 9d31a1d8 2018-03-11 stsp static const struct got_error *
536 21908da4 2019-01-13 stsp make_parent_dirs(const char *abspath)
538 21908da4 2019-01-13 stsp const struct got_error *err = NULL;
540 21908da4 2019-01-13 stsp char *parent = dirname(abspath);
541 21908da4 2019-01-13 stsp if (parent == NULL)
542 21908da4 2019-01-13 stsp return NULL;
544 21908da4 2019-01-13 stsp if (mkdir(parent, GOT_DEFAULT_DIR_MODE) == -1) {
545 21908da4 2019-01-13 stsp if (errno == ENOENT) {
546 21908da4 2019-01-13 stsp err = make_parent_dirs(parent);
548 21908da4 2019-01-13 stsp return err;
549 c65fcef2 2019-01-13 stsp if (mkdir(parent, GOT_DEFAULT_DIR_MODE) == -1)
550 c65fcef2 2019-01-13 stsp return got_error_from_errno();
552 c65fcef2 2019-01-13 stsp err = got_error_from_errno();
555 c65fcef2 2019-01-13 stsp return err;
558 21908da4 2019-01-13 stsp static const struct got_error *
559 4a1ddfc2 2019-01-12 stsp add_dir_on_disk(struct got_worktree *worktree, const char *path)
561 4a1ddfc2 2019-01-12 stsp const struct got_error *err = NULL;
562 4a1ddfc2 2019-01-12 stsp char *abspath;
564 4a1ddfc2 2019-01-12 stsp if (asprintf(&abspath, "%s/%s", worktree->root_path, path) == -1)
565 4a1ddfc2 2019-01-12 stsp return got_error_from_errno();
567 4a1ddfc2 2019-01-12 stsp /* XXX queue work rather than editing disk directly? */
568 4a1ddfc2 2019-01-12 stsp if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1) {
569 4a1ddfc2 2019-01-12 stsp struct stat sb;
571 21908da4 2019-01-13 stsp if (errno == EEXIST) {
572 21908da4 2019-01-13 stsp if (lstat(abspath, &sb) == -1) {
573 21908da4 2019-01-13 stsp err = got_error_from_errno();
577 21908da4 2019-01-13 stsp if (!S_ISDIR(sb.st_mode)) {
578 21908da4 2019-01-13 stsp /* TODO directory is obstructed; do something */
579 c65fcef2 2019-01-13 stsp err = got_error(GOT_ERR_FILE_OBSTRUCTED);
583 21908da4 2019-01-13 stsp return NULL;
584 c65fcef2 2019-01-13 stsp } else if (errno == ENOENT) {
585 21908da4 2019-01-13 stsp err = make_parent_dirs(abspath);
588 c65fcef2 2019-01-13 stsp if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1)
589 c65fcef2 2019-01-13 stsp err = got_error_from_errno();
591 4a1ddfc2 2019-01-12 stsp err = got_error_from_errno();
595 4a1ddfc2 2019-01-12 stsp free(abspath);
596 4a1ddfc2 2019-01-12 stsp return err;
599 4a1ddfc2 2019-01-12 stsp static const struct got_error *
600 8da9e5f4 2019-01-12 stsp install_blob(struct got_worktree *worktree, struct got_fileindex *fileindex,
601 eac9755f 2018-12-29 stsp struct got_fileindex_entry *entry, const char *path,
602 8da9e5f4 2019-01-12 stsp struct got_blob_object *blob,
603 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
604 8da9e5f4 2019-01-12 stsp void *progress_arg)
606 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
607 c34b20a2 2018-03-12 stsp char *ondisk_path;
608 507dc3bb 2018-12-29 stsp int fd = -1;
609 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
610 507dc3bb 2018-12-29 stsp int update = 0;
611 507dc3bb 2018-12-29 stsp char *tmppath = NULL;
613 8da9e5f4 2019-01-12 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
614 0a585a0d 2018-03-17 stsp return got_error_from_errno();
616 c34b20a2 2018-03-12 stsp fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
617 9d31a1d8 2018-03-11 stsp GOT_DEFAULT_FILE_MODE);
618 9d31a1d8 2018-03-11 stsp if (fd == -1) {
619 21908da4 2019-01-13 stsp if (errno == ENOENT) {
620 21908da4 2019-01-13 stsp char *parent = dirname(path);
621 21908da4 2019-01-13 stsp if (parent == NULL)
622 21908da4 2019-01-13 stsp return got_error_from_errno();
623 21908da4 2019-01-13 stsp err = add_dir_on_disk(worktree, parent);
625 21908da4 2019-01-13 stsp return err;
626 21908da4 2019-01-13 stsp fd = open(ondisk_path,
627 21908da4 2019-01-13 stsp O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
628 21908da4 2019-01-13 stsp GOT_DEFAULT_FILE_MODE);
629 21908da4 2019-01-13 stsp if (fd == -1)
630 21908da4 2019-01-13 stsp return got_error_from_errno();
631 21908da4 2019-01-13 stsp } else if (errno == EEXIST) {
632 9d31a1d8 2018-03-11 stsp struct stat sb;
633 c34b20a2 2018-03-12 stsp if (lstat(ondisk_path, &sb) == -1) {
634 9d31a1d8 2018-03-11 stsp err = got_error_from_errno();
636 9d31a1d8 2018-03-11 stsp } else if (!S_ISREG(sb.st_mode)) {
637 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
638 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_FILE_OBSTRUCTED);
641 507dc3bb 2018-12-29 stsp err = got_opentemp_named_fd(&tmppath, &fd,
642 507dc3bb 2018-12-29 stsp ondisk_path);
645 507dc3bb 2018-12-29 stsp update = 1;
648 7e7c1e4c 2019-01-12 stsp return got_error_from_errno();
651 507dc3bb 2018-12-29 stsp (*progress_cb)(progress_arg,
652 8da9e5f4 2019-01-12 stsp update ? GOT_STATUS_UPDATE : GOT_STATUS_ADD, path);
654 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
656 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
657 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
660 9d31a1d8 2018-03-11 stsp if (len > 0) {
661 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
662 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
663 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
664 9d31a1d8 2018-03-11 stsp err = got_error_from_errno();
666 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
667 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
670 61d6eaa3 2018-12-24 stsp hdrlen = 0;
672 9d31a1d8 2018-03-11 stsp } while (len != 0);
676 507dc3bb 2018-12-29 stsp if (update) {
677 507dc3bb 2018-12-29 stsp if (rename(tmppath, ondisk_path) != 0) {
678 507dc3bb 2018-12-29 stsp err = got_error_from_errno();
683 5f9fef37 2019-01-13 stsp if (entry == NULL)
684 5f9fef37 2019-01-13 stsp entry = got_fileindex_entry_get(fileindex, path);
686 51514078 2018-12-25 stsp err = got_fileindex_entry_update(entry, ondisk_path,
687 51514078 2018-12-25 stsp blob->id.sha1, worktree->base_commit_id->sha1);
689 51514078 2018-12-25 stsp err = got_fileindex_entry_alloc(&entry, ondisk_path,
690 8da9e5f4 2019-01-12 stsp path, blob->id.sha1, worktree->base_commit_id->sha1);
693 51514078 2018-12-25 stsp err = got_fileindex_entry_add(fileindex, entry);
696 507dc3bb 2018-12-29 stsp if (fd != -1)
698 c34b20a2 2018-03-12 stsp free(ondisk_path);
699 507dc3bb 2018-12-29 stsp free(tmppath);
700 9d31a1d8 2018-03-11 stsp return err;
703 9d31a1d8 2018-03-11 stsp static const struct got_error *
704 8da9e5f4 2019-01-12 stsp update_blob(struct got_worktree *worktree,
705 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex, struct got_fileindex_entry *ie,
706 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *path,
707 8da9e5f4 2019-01-12 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
708 8da9e5f4 2019-01-12 stsp void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
710 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
711 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
714 8da9e5f4 2019-01-12 stsp if (memcmp(ie->commit_sha1, worktree->base_commit_id->sha1,
715 507dc3bb 2018-12-29 stsp SHA1_DIGEST_LENGTH) == 0) {
716 507dc3bb 2018-12-29 stsp (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
718 8da9e5f4 2019-01-12 stsp return NULL;
720 8da9e5f4 2019-01-12 stsp if (memcmp(ie->blob_sha1,
721 8da9e5f4 2019-01-12 stsp te->id->sha1, SHA1_DIGEST_LENGTH) == 0)
722 8da9e5f4 2019-01-12 stsp return NULL;
725 8da9e5f4 2019-01-12 stsp err = got_object_open_as_blob(&blob, repo, te->id, 8192);
727 8da9e5f4 2019-01-12 stsp return err;
729 8da9e5f4 2019-01-12 stsp err = install_blob(worktree, fileindex, ie, path, blob, repo,
730 8da9e5f4 2019-01-12 stsp progress_cb, progress_arg);
731 8da9e5f4 2019-01-12 stsp got_object_blob_close(blob);
732 8da9e5f4 2019-01-12 stsp return err;
735 90285c3b 2019-01-08 stsp static const struct got_error *
736 7a9df742 2019-01-08 stsp remove_ondisk_file(const char *root_path, const char *path)
738 90285c3b 2019-01-08 stsp const struct got_error *err = NULL;
739 90285c3b 2019-01-08 stsp char *ondisk_path = NULL;
741 7a9df742 2019-01-08 stsp if (asprintf(&ondisk_path, "%s/%s", root_path, path) == -1)
742 90285c3b 2019-01-08 stsp return got_error_from_errno();
744 c97665ae 2019-01-13 stsp if (unlink(ondisk_path) == -1) {
745 c97665ae 2019-01-13 stsp if (errno != ENOENT)
746 c97665ae 2019-01-13 stsp err = got_error_from_errno();
748 90285c3b 2019-01-08 stsp char *parent = dirname(ondisk_path);
749 7a9df742 2019-01-08 stsp while (parent && strcmp(parent, root_path) != 0) {
750 26c4ac4d 2019-01-08 stsp if (rmdir(parent) == -1) {
751 26c4ac4d 2019-01-08 stsp if (errno != ENOTEMPTY)
752 26c4ac4d 2019-01-08 stsp err = got_error_from_errno();
755 90285c3b 2019-01-08 stsp parent = dirname(parent);
758 90285c3b 2019-01-08 stsp free(ondisk_path);
759 90285c3b 2019-01-08 stsp return err;
762 8da9e5f4 2019-01-12 stsp struct diff_cb_arg {
763 8da9e5f4 2019-01-12 stsp struct got_fileindex *fileindex;
764 8da9e5f4 2019-01-12 stsp struct got_worktree *worktree;
765 8da9e5f4 2019-01-12 stsp struct got_repository *repo;
766 8da9e5f4 2019-01-12 stsp got_worktree_checkout_cb progress_cb;
767 8da9e5f4 2019-01-12 stsp void *progress_arg;
768 8da9e5f4 2019-01-12 stsp got_worktree_cancel_cb cancel_cb;
769 8da9e5f4 2019-01-12 stsp void *cancel_arg;
772 512f0d0e 2019-01-02 stsp static const struct got_error *
773 8da9e5f4 2019-01-12 stsp diff_old_new(void *arg, struct got_fileindex_entry *ie,
774 8da9e5f4 2019-01-12 stsp struct got_tree_entry *te, const char *parent_path)
776 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
778 8da9e5f4 2019-01-12 stsp return update_blob(a->worktree, a->fileindex, ie, te,
779 8da9e5f4 2019-01-12 stsp ie->path, a->repo, a->progress_cb, a->progress_arg,
780 8da9e5f4 2019-01-12 stsp a->cancel_cb, a->cancel_arg);
783 8da9e5f4 2019-01-12 stsp static const struct got_error *
784 8da9e5f4 2019-01-12 stsp diff_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
786 8da9e5f4 2019-01-12 stsp const struct got_error *err;
787 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
789 8da9e5f4 2019-01-12 stsp (*a->progress_cb)(a->progress_arg, GOT_STATUS_DELETE, ie->path);
791 8da9e5f4 2019-01-12 stsp err = remove_ondisk_file(a->worktree->root_path, ie->path);
793 8da9e5f4 2019-01-12 stsp return err;
794 8da9e5f4 2019-01-12 stsp got_fileindex_entry_remove(a->fileindex, ie);
795 8da9e5f4 2019-01-12 stsp return NULL;
798 9d31a1d8 2018-03-11 stsp static const struct got_error *
799 8da9e5f4 2019-01-12 stsp diff_new(void *arg, struct got_tree_entry *te, const char *parent_path)
801 8da9e5f4 2019-01-12 stsp struct diff_cb_arg *a = arg;
802 8da9e5f4 2019-01-12 stsp const struct got_error *err;
803 8da9e5f4 2019-01-12 stsp char *path;
805 8da9e5f4 2019-01-12 stsp if (asprintf(&path, "%s%s%s", parent_path,
806 8da9e5f4 2019-01-12 stsp parent_path[0] ? "/" : "", te->name)
808 8da9e5f4 2019-01-12 stsp return got_error_from_errno();
810 8da9e5f4 2019-01-12 stsp if (S_ISDIR(te->mode))
811 8da9e5f4 2019-01-12 stsp err = add_dir_on_disk(a->worktree, path);
813 8da9e5f4 2019-01-12 stsp err = update_blob(a->worktree, a->fileindex, NULL, te, path,
814 8da9e5f4 2019-01-12 stsp a->repo, a->progress_cb, a->progress_arg,
815 8da9e5f4 2019-01-12 stsp a->cancel_cb, a->cancel_arg);
817 8da9e5f4 2019-01-12 stsp free(path);
818 9d31a1d8 2018-03-11 stsp return err;
821 9d31a1d8 2018-03-11 stsp const struct got_error *
822 9d31a1d8 2018-03-11 stsp got_worktree_checkout_files(struct got_worktree *worktree,
823 93a30277 2018-12-24 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
824 93a30277 2018-12-24 stsp void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
826 a143fb78 2018-12-25 stsp const struct got_error *err = NULL, *unlockerr, *checkout_err = NULL;
827 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
828 8da9e5f4 2019-01-12 stsp struct got_object_id *tree_id = NULL;
829 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
830 9d31a1d8 2018-03-11 stsp char *fileindex_path = NULL, *new_fileindex_path = NULL;
831 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
832 51514078 2018-12-25 stsp FILE *index = NULL, *new_index = NULL;
833 f44ffd20 2019-02-04 stsp struct got_fileindex_diff_tree_cb diff_cb;
834 8da9e5f4 2019-01-12 stsp struct diff_cb_arg arg;
836 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
838 9d31a1d8 2018-03-11 stsp return err;
840 133d2798 2019-01-08 stsp fileindex = got_fileindex_alloc();
841 133d2798 2019-01-08 stsp if (fileindex == NULL) {
842 133d2798 2019-01-08 stsp err = got_error_from_errno();
846 9d31a1d8 2018-03-11 stsp if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
847 9d31a1d8 2018-03-11 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
848 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
849 9d31a1d8 2018-03-11 stsp fileindex_path = NULL;
854 51514078 2018-12-25 stsp * Read the file index.
855 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
856 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
858 51514078 2018-12-25 stsp index = fopen(fileindex_path, "rb");
859 51514078 2018-12-25 stsp if (index == NULL) {
860 cf61d818 2019-01-12 stsp if (errno != ENOENT) {
861 cf61d818 2019-01-12 stsp err = got_error_from_errno();
865 cf61d818 2019-01-12 stsp err = got_fileindex_read(fileindex, index);
866 cf61d818 2019-01-12 stsp fclose(index);
871 b8bdcc21 2018-12-24 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
872 b8bdcc21 2018-12-24 stsp fileindex_path);
876 eaccb85f 2018-12-25 stsp err = got_object_open_as_commit(&commit, repo,
877 eaccb85f 2018-12-25 stsp worktree->base_commit_id);
881 8da9e5f4 2019-01-12 stsp err = got_object_id_by_path(&tree_id, repo,
882 8da9e5f4 2019-01-12 stsp worktree->base_commit_id, worktree->path_prefix);
886 8da9e5f4 2019-01-12 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
890 8da9e5f4 2019-01-12 stsp diff_cb.diff_old_new = diff_old_new;
891 8da9e5f4 2019-01-12 stsp diff_cb.diff_old = diff_old;
892 8da9e5f4 2019-01-12 stsp diff_cb.diff_new = diff_new;
893 8da9e5f4 2019-01-12 stsp arg.fileindex = fileindex;
894 8da9e5f4 2019-01-12 stsp arg.worktree = worktree;
895 8da9e5f4 2019-01-12 stsp arg.repo = repo;
896 8da9e5f4 2019-01-12 stsp arg.progress_cb = progress_cb;
897 8da9e5f4 2019-01-12 stsp arg.progress_arg = progress_arg;
898 8da9e5f4 2019-01-12 stsp arg.cancel_cb = cancel_cb;
899 8da9e5f4 2019-01-12 stsp arg.cancel_arg = cancel_arg;
900 8da9e5f4 2019-01-12 stsp checkout_err = got_fileindex_diff_tree(fileindex, tree, repo,
901 8da9e5f4 2019-01-12 stsp &diff_cb, &arg);
903 a143fb78 2018-12-25 stsp /* Try to sync the fileindex back to disk in any case. */
904 b8bdcc21 2018-12-24 stsp err = got_fileindex_write(fileindex, new_index);
908 9d31a1d8 2018-03-11 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
909 9d31a1d8 2018-03-11 stsp err = got_error_from_errno();
913 9d31a1d8 2018-03-11 stsp free(new_fileindex_path);
914 9d31a1d8 2018-03-11 stsp new_fileindex_path = NULL;
918 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
919 9d31a1d8 2018-03-11 stsp if (commit)
920 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
921 9d31a1d8 2018-03-11 stsp if (new_fileindex_path)
922 9d31a1d8 2018-03-11 stsp unlink(new_fileindex_path);
923 b8bdcc21 2018-12-24 stsp if (new_index)
924 b8bdcc21 2018-12-24 stsp fclose(new_index);
925 9d31a1d8 2018-03-11 stsp free(new_fileindex_path);
926 9d31a1d8 2018-03-11 stsp free(fileindex_path);
927 7426bbfd 2018-12-24 stsp got_fileindex_free(fileindex);
928 a143fb78 2018-12-25 stsp if (checkout_err)
929 a143fb78 2018-12-25 stsp err = checkout_err;
930 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
931 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
932 9d31a1d8 2018-03-11 stsp err = unlockerr;
933 f8d1f275 2019-02-04 stsp return err;
936 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg {
937 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex;
938 f8d1f275 2019-02-04 stsp struct got_worktree *worktree;
939 f8d1f275 2019-02-04 stsp struct got_repository *repo;
940 f8d1f275 2019-02-04 stsp got_worktree_status_cb status_cb;
941 f8d1f275 2019-02-04 stsp void *status_arg;
942 f8d1f275 2019-02-04 stsp got_worktree_cancel_cb cancel_cb;
943 f8d1f275 2019-02-04 stsp void *cancel_arg;
946 f8d1f275 2019-02-04 stsp static const struct got_error *
947 bf96b38c 2019-02-05 stsp get_file_status(unsigned char *status, struct got_fileindex_entry *ie,
948 bf96b38c 2019-02-05 stsp const char *abspath, struct got_repository *repo)
950 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
951 f8d1f275 2019-02-04 stsp struct got_object_id id;
952 f8d1f275 2019-02-04 stsp size_t hdrlen;
953 f8d1f275 2019-02-04 stsp FILE *f = NULL;
954 f8d1f275 2019-02-04 stsp uint8_t fbuf[8192];
955 f8d1f275 2019-02-04 stsp struct got_blob_object *blob = NULL;
956 f8d1f275 2019-02-04 stsp size_t flen, blen;
957 bf96b38c 2019-02-05 stsp struct stat sb;
959 f8d1f275 2019-02-04 stsp *status = GOT_STATUS_NO_CHANGE;
961 bf96b38c 2019-02-05 stsp if (lstat(abspath, &sb) == -1)
962 f8d1f275 2019-02-04 stsp return got_error_from_errno();
964 0dbc2271 2019-02-05 stsp if (!S_ISREG(sb.st_mode)) {
965 0dbc2271 2019-02-05 stsp *status = GOT_STATUS_OBSTRUCTED;
966 bf96b38c 2019-02-05 stsp return NULL;
969 bf96b38c 2019-02-05 stsp if (ie->ctime_sec == sb.st_ctime &&
970 bf96b38c 2019-02-05 stsp ie->ctime_nsec == sb.st_ctimensec &&
971 bf96b38c 2019-02-05 stsp ie->mtime_sec == sb.st_mtime &&
972 bf96b38c 2019-02-05 stsp ie->mtime_sec == sb.st_mtime &&
973 bf96b38c 2019-02-05 stsp ie->mtime_nsec == sb.st_mtimensec &&
974 bf96b38c 2019-02-05 stsp ie->size == (sb.st_size & 0xffffffff))
975 bf96b38c 2019-02-05 stsp return NULL;
977 f8d1f275 2019-02-04 stsp memcpy(id.sha1, ie->blob_sha1, sizeof(id.sha1));
978 f8d1f275 2019-02-04 stsp err = got_object_open_as_blob(&blob, repo, &id, sizeof(fbuf));
980 bf96b38c 2019-02-05 stsp return err;
982 f8d1f275 2019-02-04 stsp f = fopen(abspath, "r");
983 f8d1f275 2019-02-04 stsp if (f == NULL) {
984 f8d1f275 2019-02-04 stsp err = got_error_from_errno();
987 f8d1f275 2019-02-04 stsp hdrlen = got_object_blob_get_hdrlen(blob);
988 f8d1f275 2019-02-04 stsp while (1) {
989 f8d1f275 2019-02-04 stsp const uint8_t *bbuf = got_object_blob_get_read_buf(blob);
990 f8d1f275 2019-02-04 stsp err = got_object_blob_read_block(&blen, blob);
993 f8d1f275 2019-02-04 stsp flen = fread(fbuf, 1, sizeof(fbuf), f);
994 f8d1f275 2019-02-04 stsp if (blen == 0) {
995 f8d1f275 2019-02-04 stsp if (flen != 0)
996 f8d1f275 2019-02-04 stsp *status = GOT_STATUS_MODIFIY;
998 f8d1f275 2019-02-04 stsp } else if (flen == 0) {
999 f8d1f275 2019-02-04 stsp if (blen != 0)
1000 f8d1f275 2019-02-04 stsp *status = GOT_STATUS_MODIFIY;
1002 f8d1f275 2019-02-04 stsp } else if (blen == flen) {
1003 f8d1f275 2019-02-04 stsp /* Skip blob object header first time around. */
1004 f8d1f275 2019-02-04 stsp if (memcmp(bbuf + hdrlen, fbuf, flen) != 0) {
1005 f8d1f275 2019-02-04 stsp *status = GOT_STATUS_MODIFIY;
1009 f8d1f275 2019-02-04 stsp *status = GOT_STATUS_MODIFIY;
1012 f8d1f275 2019-02-04 stsp hdrlen = 0;
1016 f8d1f275 2019-02-04 stsp got_object_blob_close(blob);
1018 f8d1f275 2019-02-04 stsp fclose(f);
1019 f8d1f275 2019-02-04 stsp return err;
1022 f8d1f275 2019-02-04 stsp static const struct got_error *
1023 f8d1f275 2019-02-04 stsp status_old_new(void *arg, struct got_fileindex_entry *ie,
1024 f8d1f275 2019-02-04 stsp struct dirent *de, const char *parent_path)
1026 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
1027 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
1028 f8d1f275 2019-02-04 stsp char *abspath;
1029 f8d1f275 2019-02-04 stsp unsigned char status = GOT_STATUS_NO_CHANGE;
1031 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
1032 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s/%s", a->worktree->root_path,
1033 f8d1f275 2019-02-04 stsp parent_path, de->d_name) == -1)
1034 f8d1f275 2019-02-04 stsp return got_error_from_errno();
1036 f8d1f275 2019-02-04 stsp if (asprintf(&abspath, "%s/%s", a->worktree->root_path,
1037 f8d1f275 2019-02-04 stsp de->d_name) == -1)
1038 f8d1f275 2019-02-04 stsp return got_error_from_errno();
1041 bf96b38c 2019-02-05 stsp err = get_file_status(&status, ie, abspath, a->repo);
1042 bf96b38c 2019-02-05 stsp if (err == NULL && status != GOT_STATUS_NO_CHANGE)
1043 f8d1f275 2019-02-04 stsp (*a->status_cb)(a->status_arg, status, ie->path);
1044 f8d1f275 2019-02-04 stsp free(abspath);
1045 9d31a1d8 2018-03-11 stsp return err;
1048 f8d1f275 2019-02-04 stsp static const struct got_error *
1049 f8d1f275 2019-02-04 stsp status_old(void *arg, struct got_fileindex_entry *ie, const char *parent_path)
1051 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
1052 f8d1f275 2019-02-04 stsp (*a->status_cb)(a->status_arg, GOT_STATUS_MISSING, ie->path);
1053 f8d1f275 2019-02-04 stsp return NULL;
1056 f8d1f275 2019-02-04 stsp static const struct got_error *
1057 f8d1f275 2019-02-04 stsp status_new(void *arg, struct dirent *de, const char *parent_path)
1059 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg *a = arg;
1060 f8d1f275 2019-02-04 stsp char *path = NULL;
1062 f8d1f275 2019-02-04 stsp if (de->d_type == DT_DIR)
1063 f8d1f275 2019-02-04 stsp return NULL;
1065 f8d1f275 2019-02-04 stsp if (parent_path[0]) {
1066 f8d1f275 2019-02-04 stsp if (asprintf(&path, "%s/%s", parent_path, de->d_name) == -1)
1067 f8d1f275 2019-02-04 stsp return got_error_from_errno();
1069 f8d1f275 2019-02-04 stsp path = de->d_name;
1072 f8d1f275 2019-02-04 stsp (*a->status_cb)(a->status_arg, GOT_STATUS_UNVERSIONED, path);
1073 f8d1f275 2019-02-04 stsp if (parent_path[0])
1074 f8d1f275 2019-02-04 stsp free(path);
1075 f8d1f275 2019-02-04 stsp return NULL;
1078 f8d1f275 2019-02-04 stsp const struct got_error *
1079 f8d1f275 2019-02-04 stsp got_worktree_status(struct got_worktree *worktree,
1080 f8d1f275 2019-02-04 stsp struct got_repository *repo, got_worktree_status_cb status_cb,
1081 f8d1f275 2019-02-04 stsp void *status_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
1083 f8d1f275 2019-02-04 stsp const struct got_error *err = NULL;
1084 f8d1f275 2019-02-04 stsp DIR *workdir = NULL;
1085 f8d1f275 2019-02-04 stsp char *fileindex_path = NULL;
1086 f8d1f275 2019-02-04 stsp struct got_fileindex *fileindex = NULL;
1087 f8d1f275 2019-02-04 stsp FILE *index = NULL;
1088 d43a8a88 2019-02-05 stsp struct got_fileindex_diff_dir_cb fdiff_cb;
1089 f8d1f275 2019-02-04 stsp struct diff_dir_cb_arg arg;
1091 f8d1f275 2019-02-04 stsp fileindex = got_fileindex_alloc();
1092 f8d1f275 2019-02-04 stsp if (fileindex == NULL) {
1093 f8d1f275 2019-02-04 stsp err = got_error_from_errno();
1094 f8d1f275 2019-02-04 stsp goto done;
1097 f8d1f275 2019-02-04 stsp if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
1098 f8d1f275 2019-02-04 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
1099 f8d1f275 2019-02-04 stsp err = got_error_from_errno();
1100 f8d1f275 2019-02-04 stsp fileindex_path = NULL;
1101 f8d1f275 2019-02-04 stsp goto done;
1104 f8d1f275 2019-02-04 stsp index = fopen(fileindex_path, "rb");
1105 f8d1f275 2019-02-04 stsp if (index == NULL) {
1106 f8d1f275 2019-02-04 stsp if (errno != ENOENT) {
1107 f8d1f275 2019-02-04 stsp err = got_error_from_errno();
1108 f8d1f275 2019-02-04 stsp goto done;
1111 f8d1f275 2019-02-04 stsp err = got_fileindex_read(fileindex, index);
1112 f8d1f275 2019-02-04 stsp fclose(index);
1114 f8d1f275 2019-02-04 stsp goto done;
1117 f8d1f275 2019-02-04 stsp workdir = opendir(worktree->root_path);
1118 c513d110 2019-02-05 stsp if (workdir == NULL) {
1119 c513d110 2019-02-05 stsp err = got_error_from_errno();
1120 c513d110 2019-02-05 stsp goto done;
1122 d43a8a88 2019-02-05 stsp fdiff_cb.diff_old_new = status_old_new;
1123 d43a8a88 2019-02-05 stsp fdiff_cb.diff_old = status_old;
1124 d43a8a88 2019-02-05 stsp fdiff_cb.diff_new = status_new;
1125 f8d1f275 2019-02-04 stsp arg.fileindex = fileindex;
1126 f8d1f275 2019-02-04 stsp arg.worktree = worktree;
1127 f8d1f275 2019-02-04 stsp arg.repo = repo;
1128 f8d1f275 2019-02-04 stsp arg.status_cb = status_cb;
1129 f8d1f275 2019-02-04 stsp arg.status_arg = status_arg;
1130 f8d1f275 2019-02-04 stsp arg.cancel_cb = cancel_cb;
1131 f8d1f275 2019-02-04 stsp arg.cancel_arg = cancel_arg;
1132 c7f4312f 2019-02-05 stsp err = got_fileindex_diff_dir(fileindex, workdir, worktree->root_path,
1133 c7f4312f 2019-02-05 stsp repo, &fdiff_cb, &arg);
1135 f8d1f275 2019-02-04 stsp if (workdir)
1136 f8d1f275 2019-02-04 stsp closedir(workdir);
1137 f8d1f275 2019-02-04 stsp free(fileindex_path);
1138 f8d1f275 2019-02-04 stsp got_fileindex_free(fileindex);
1139 f8d1f275 2019-02-04 stsp return err;