Blob


1 /*
2 * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/stat.h>
18 #include <sys/limits.h>
19 #include <sys/queue.h>
21 #include <string.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <fcntl.h>
25 #include <errno.h>
26 #include <unistd.h>
27 #include <sha1.h>
28 #include <zlib.h>
29 #include <fnmatch.h>
31 #include "got_error.h"
32 #include "got_repository.h"
33 #include "got_reference.h"
34 #include "got_object.h"
35 #include "got_worktree.h"
36 #include "got_opentemp.h"
38 #include "got_lib_worktree.h"
39 #include "got_lib_path.h"
40 #include "got_lib_sha1.h"
41 #include "got_lib_fileindex.h"
42 #include "got_lib_inflate.h"
43 #include "got_lib_delta.h"
44 #include "got_lib_object.h"
46 #ifndef MIN
47 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
48 #endif
50 static const struct got_error *
51 create_meta_file(const char *path_got, const char *name, const char *content)
52 {
53 const struct got_error *err = NULL;
54 char *path;
55 int fd = -1;
57 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
58 err = got_error_from_errno();
59 path = NULL;
60 goto done;
61 }
63 fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
64 GOT_DEFAULT_FILE_MODE);
65 if (fd == -1) {
66 err = got_error_from_errno();
67 goto done;
68 }
70 if (content) {
71 int len = dprintf(fd, "%s\n", content);
72 if (len != strlen(content) + 1) {
73 err = got_error_from_errno();
74 goto done;
75 }
76 }
78 done:
79 if (fd != -1 && close(fd) == -1 && err == NULL)
80 err = got_error_from_errno();
81 free(path);
82 return err;
83 }
85 static const struct got_error *
86 read_meta_file(char **content, const char *path_got, const char *name)
87 {
88 const struct got_error *err = NULL;
89 char *path;
90 int fd = -1;
91 ssize_t n;
92 struct stat sb;
94 *content = NULL;
96 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
97 err = got_error_from_errno();
98 path = NULL;
99 goto done;
102 fd = open(path, O_RDONLY | O_NOFOLLOW);
103 if (fd == -1) {
104 err = got_error_from_errno();
105 goto done;
107 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
108 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
109 : got_error_from_errno());
110 goto done;
113 stat(path, &sb);
114 *content = calloc(1, sb.st_size);
115 if (*content == NULL) {
116 err = got_error_from_errno();
117 goto done;
120 n = read(fd, *content, sb.st_size);
121 if (n != sb.st_size) {
122 err = (n == -1 ? got_error_from_errno() :
123 got_error(GOT_ERR_WORKTREE_META));
124 goto done;
126 if ((*content)[sb.st_size - 1] != '\n') {
127 err = got_error(GOT_ERR_WORKTREE_META);
128 goto done;
130 (*content)[sb.st_size - 1] = '\0';
132 done:
133 if (fd != -1 && close(fd) == -1 && err == NULL)
134 err = got_error_from_errno();
135 free(path);
136 if (err) {
137 free(*content);
138 *content = NULL;
140 return err;
143 const struct got_error *
144 got_worktree_init(const char *path, struct got_reference *head_ref,
145 const char *prefix, struct got_repository *repo)
147 const struct got_error *err = NULL;
148 struct got_object_id *commit_id = NULL;
149 int obj_type;
150 char *path_got = NULL;
151 char *refstr = NULL;
152 char *repo_path = NULL;
153 char *formatstr = NULL;
154 char *absprefix = NULL;
155 char *basestr = NULL;
157 err = got_ref_resolve(&commit_id, repo, head_ref);
158 if (err)
159 return err;
160 err = got_object_get_type(&obj_type, repo, commit_id);
161 if (err)
162 return err;
163 if (obj_type != GOT_OBJ_TYPE_COMMIT)
164 return got_error(GOT_ERR_OBJ_TYPE);
166 if (!got_path_is_absolute(prefix)) {
167 if (asprintf(&absprefix, "/%s", prefix) == -1)
168 return got_error_from_errno();
171 /* Create top-level directory (may already exist). */
172 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
173 err = got_error_from_errno();
174 goto done;
177 /* Create .got directory (may already exist). */
178 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
179 err = got_error_from_errno();
180 goto done;
182 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
183 err = got_error_from_errno();
184 goto done;
187 /* Create an empty lock file. */
188 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
189 if (err)
190 goto done;
192 /* Create an empty file index. */
193 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
194 if (err)
195 goto done;
197 /* Write the HEAD reference. */
198 refstr = got_ref_to_str(head_ref);
199 if (refstr == NULL) {
200 err = got_error_from_errno();
201 goto done;
203 err = create_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
204 if (err)
205 goto done;
207 /* Record our base commit. */
208 err = got_object_id_str(&basestr, commit_id);
209 if (err)
210 goto done;
211 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
212 if (err)
213 goto done;
215 /* Store path to repository. */
216 repo_path = got_repo_get_path(repo);
217 if (repo_path == NULL) {
218 err = got_error_from_errno();
219 goto done;
221 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY, repo_path);
222 if (err)
223 goto done;
225 /* Store in-repository path prefix. */
226 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
227 absprefix ? absprefix : prefix);
228 if (err)
229 goto done;
231 /* Stamp work tree with format file. */
232 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
233 err = got_error_from_errno();
234 goto done;
236 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
237 if (err)
238 goto done;
240 done:
241 free(commit_id);
242 free(path_got);
243 free(formatstr);
244 free(refstr);
245 free(repo_path);
246 free(absprefix);
247 free(basestr);
248 return err;
251 const struct got_error *
252 got_worktree_open(struct got_worktree **worktree, const char *path)
254 const struct got_error *err = NULL;
255 char *path_got;
256 char *formatstr = NULL;
257 char *path_lock = NULL;
258 char *base_commit_id_str = NULL;
259 char *head_ref_str = NULL;
260 int version, fd = -1;
261 const char *errstr;
262 struct got_repository *repo = NULL;
264 *worktree = NULL;
266 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
267 err = got_error_from_errno();
268 path_got = NULL;
269 goto done;
272 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
273 err = got_error_from_errno();
274 path_lock = NULL;
275 goto done;
278 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
279 if (fd == -1) {
280 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
281 : got_error_from_errno());
282 goto done;
285 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
286 if (err)
287 goto done;
289 version = strtonum(formatstr, 1, INT_MAX, &errstr);
290 if (errstr) {
291 err = got_error(GOT_ERR_WORKTREE_META);
292 goto done;
294 if (version != GOT_WORKTREE_FORMAT_VERSION) {
295 err = got_error(GOT_ERR_WORKTREE_VERS);
296 goto done;
299 *worktree = calloc(1, sizeof(**worktree));
300 if (*worktree == NULL) {
301 err = got_error_from_errno();
302 goto done;
304 (*worktree)->lockfd = -1;
306 (*worktree)->root_path = strdup(path);
307 if ((*worktree)->root_path == NULL) {
308 err = got_error_from_errno();
309 goto done;
311 err = read_meta_file(&(*worktree)->repo_path, path_got,
312 GOT_WORKTREE_REPOSITORY);
313 if (err)
314 goto done;
316 err = read_meta_file(&(*worktree)->path_prefix, path_got,
317 GOT_WORKTREE_PATH_PREFIX);
318 if (err)
319 goto done;
321 err = read_meta_file(&base_commit_id_str, path_got,
322 GOT_WORKTREE_BASE_COMMIT);
323 if (err)
324 goto done;
326 err = got_repo_open(&repo, (*worktree)->repo_path);
327 if (err)
328 goto done;
330 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
331 base_commit_id_str);
332 if (err)
333 goto done;
335 err = read_meta_file(&head_ref_str, path_got, GOT_WORKTREE_HEAD_REF);
336 if (err)
337 goto done;
339 err = got_ref_open(&(*worktree)->head_ref, repo, head_ref_str);
340 done:
341 if (repo)
342 got_repo_close(repo);
343 free(path_got);
344 free(path_lock);
345 free(head_ref_str);
346 free(base_commit_id_str);
347 if (err) {
348 if (fd != -1)
349 close(fd);
350 if (*worktree != NULL)
351 got_worktree_close(*worktree);
352 *worktree = NULL;
353 } else
354 (*worktree)->lockfd = fd;
356 return err;
359 void
360 got_worktree_close(struct got_worktree *worktree)
362 free(worktree->root_path);
363 free(worktree->repo_path);
364 free(worktree->path_prefix);
365 free(worktree->base_commit_id);
366 if (worktree->head_ref)
367 got_ref_close(worktree->head_ref);
368 if (worktree->lockfd != -1)
369 close(worktree->lockfd);
370 free(worktree);
373 const char *
374 got_worktree_get_repo_path(struct got_worktree *worktree)
376 return worktree->repo_path;
379 const char *
380 got_worktree_get_path_prefix(struct got_worktree *worktree)
382 return worktree->path_prefix;
385 char *
386 got_worktree_get_head_ref_name(struct got_worktree *worktree)
388 return got_ref_to_str(worktree->head_ref);
391 static const struct got_error *
392 lock_worktree(struct got_worktree *worktree, int operation)
394 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
395 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
396 : got_error_from_errno());
397 return NULL;
400 static const char *
401 apply_path_prefix(struct got_worktree *worktree, const char *path)
403 const char *p = path;
404 p += strlen(worktree->path_prefix);
405 if (*p == '/')
406 p++;
407 return p;
410 static const struct got_error *
411 blob_checkout(struct got_worktree *worktree, struct got_fileindex *fileindex,
412 struct got_fileindex_entry *entry, const char *path,
413 struct got_blob_object *blob, struct got_repository *repo,
414 got_worktree_checkout_cb progress_cb, void *progress_arg,
415 const char *progress_path)
417 const struct got_error *err = NULL;
418 char *ondisk_path;
419 int fd;
420 size_t len, hdrlen;
422 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path,
423 apply_path_prefix(worktree, path)) == -1)
424 return got_error_from_errno();
426 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
427 GOT_DEFAULT_FILE_MODE);
428 if (fd == -1) {
429 err = got_error_from_errno();
430 if (errno == EEXIST) {
431 struct stat sb;
432 if (lstat(ondisk_path, &sb) == -1) {
433 err = got_error_from_errno();
434 } else if (!S_ISREG(sb.st_mode)) {
435 /* TODO file is obstructed; do something */
436 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
437 } else {
438 /* TODO: Merge the file! */
439 (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
440 progress_path);
441 return NULL;
444 return err;
447 (*progress_cb)(progress_arg, GOT_STATUS_ADD, progress_path);
449 hdrlen = got_object_blob_get_hdrlen(blob);
450 do {
451 const uint8_t *buf = got_object_blob_get_read_buf(blob);
452 err = got_object_blob_read_block(&len, blob);
453 if (err)
454 break;
455 if (len > 0) {
456 /* Skip blob object header first time around. */
457 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
458 if (outlen == -1) {
459 err = got_error_from_errno();
460 goto done;
461 } else if (outlen != len - hdrlen) {
462 err = got_error(GOT_ERR_IO);
463 goto done;
465 hdrlen = 0;
467 } while (len != 0);
469 fsync(fd);
471 if (entry)
472 err = got_fileindex_entry_update(entry, ondisk_path,
473 blob->id.sha1, worktree->base_commit_id->sha1);
474 else {
475 err = got_fileindex_entry_alloc(&entry, ondisk_path,
476 apply_path_prefix(worktree, path), blob->id.sha1,
477 worktree->base_commit_id->sha1);
478 if (err)
479 goto done;
480 err = got_fileindex_entry_add(fileindex, entry);
482 if (err)
483 goto done;
484 done:
485 close(fd);
486 free(ondisk_path);
487 return err;
490 static const struct got_error *
491 add_dir_on_disk(struct got_worktree *worktree, const char *path)
493 const struct got_error *err = NULL;
494 char *abspath;
496 if (asprintf(&abspath, "%s/%s", worktree->root_path,
497 apply_path_prefix(worktree, path)) == -1)
498 return got_error_from_errno();
500 /* XXX queue work rather than editing disk directly? */
501 if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1) {
502 struct stat sb;
504 if (errno != EEXIST) {
505 err = got_error_from_errno();
506 goto done;
509 if (lstat(abspath, &sb) == -1) {
510 err = got_error_from_errno();
511 goto done;
514 if (!S_ISDIR(sb.st_mode)) {
515 /* TODO directory is obstructed; do something */
516 return got_error(GOT_ERR_FILE_OBSTRUCTED);
520 done:
521 free(abspath);
522 return err;
525 static const struct got_error *
526 tree_checkout(struct got_worktree *, struct got_fileindex *,
527 struct got_tree_object *, const char *, struct got_repository *,
528 got_worktree_checkout_cb progress_cb, void *progress_arg,
529 got_worktree_cancel_cb cancel_cb, void *cancel_arg);
531 static const struct got_error *
532 tree_checkout_entry(struct got_worktree *worktree,
533 struct got_fileindex *fileindex, struct got_tree_entry *te,
534 const char *parent, struct got_repository *repo,
535 got_worktree_checkout_cb progress_cb, void *progress_arg,
536 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
538 const struct got_error *err = NULL;
539 struct got_object *obj = NULL;
540 struct got_blob_object *blob = NULL;
541 struct got_fileindex_entry *entry = NULL;
542 struct got_tree_object *tree = NULL;
543 char *path = NULL;
544 char *progress_path = NULL;
545 size_t len;
547 if (parent[0] == '/' && parent[1] == '\0')
548 parent = "";
549 if (asprintf(&path, "%s/%s", parent, te->name) == -1)
550 return got_error_from_errno();
552 /* Skip this entry if it is outside of our path prefix. */
553 len = MIN(strlen(worktree->path_prefix), strlen(path));
554 if (strncmp(path, worktree->path_prefix, len) != 0) {
555 free(path);
556 return NULL;
559 err = got_object_open(&obj, repo, te->id);
560 if (err)
561 goto done;
563 progress_path = path;
564 if (strncmp(progress_path, worktree->path_prefix, len) == 0)
565 progress_path += len;
567 switch (obj->type) {
568 case GOT_OBJ_TYPE_BLOB:
569 if (strlen(worktree->path_prefix) >= strlen(path))
570 break;
571 entry = got_fileindex_entry_get(fileindex,
572 apply_path_prefix(worktree, path));
573 if (entry &&
574 memcmp(entry->commit_sha1, worktree->base_commit_id->sha1,
575 SHA1_DIGEST_LENGTH) == 0)
576 break; /* file already checked out */
577 err = got_object_blob_open(&blob, repo, obj, 8192);
578 if (err)
579 goto done;
580 err = blob_checkout(worktree, fileindex, entry, path, blob,
581 repo, progress_cb, progress_arg, progress_path);
582 break;
583 case GOT_OBJ_TYPE_TREE:
584 if (strlen(worktree->path_prefix) < strlen(path)) {
585 err = add_dir_on_disk(worktree, path);
586 if (err)
587 break;
589 err = got_object_tree_open(&tree, repo, obj);
590 if (err)
591 goto done;
592 /* XXX infinite recursion possible */
593 err = tree_checkout(worktree, fileindex, tree, path, repo,
594 progress_cb, progress_arg, cancel_cb, cancel_arg);
595 break;
596 default:
597 break;
600 done:
601 if (blob)
602 got_object_blob_close(blob);
603 if (tree)
604 got_object_tree_close(tree);
605 if (obj)
606 got_object_close(obj);
607 free(path);
608 return err;
611 static const struct got_error *
612 tree_checkout(struct got_worktree *worktree,
613 struct got_fileindex *fileindex, struct got_tree_object *tree,
614 const char *path, struct got_repository *repo,
615 got_worktree_checkout_cb progress_cb, void *progress_arg,
616 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
618 const struct got_error *err = NULL;
619 const struct got_tree_entries *entries;
620 struct got_tree_entry *te;
621 size_t len;
623 /* Skip this tree if it is outside of our path prefix. */
624 len = MIN(strlen(worktree->path_prefix), strlen(path));
625 if (strncmp(path, worktree->path_prefix, len) != 0)
626 return NULL;
628 entries = got_object_tree_get_entries(tree);
629 SIMPLEQ_FOREACH(te, &entries->head, entry) {
630 if (cancel_cb) {
631 err = (*cancel_cb)(cancel_arg);
632 if (err)
633 break;
635 err = tree_checkout_entry(worktree, fileindex, te, path, repo,
636 progress_cb, progress_arg, cancel_cb, cancel_arg);
637 if (err)
638 break;
641 return err;
644 const struct got_error *
645 got_worktree_checkout_files(struct got_worktree *worktree,
646 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
647 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
649 const struct got_error *err = NULL, *unlockerr, *checkout_err = NULL;
650 struct got_commit_object *commit = NULL;
651 struct got_tree_object *tree = NULL;
652 char *fileindex_path = NULL, *new_fileindex_path = NULL;
653 struct got_fileindex *fileindex = NULL;
654 FILE *index = NULL, *new_index = NULL;
656 err = lock_worktree(worktree, LOCK_EX);
657 if (err)
658 return err;
660 fileindex = got_fileindex_alloc();
661 if (fileindex == NULL) {
662 err = got_error_from_errno();
663 goto done;
666 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
667 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
668 err = got_error_from_errno();
669 fileindex_path = NULL;
670 goto done;
673 /*
674 * Read the file index.
675 * Checking out files is supposed to be an idempotent operation.
676 * If the on-disk file index is incomplete we will try to complete it.
677 */
678 index = fopen(fileindex_path, "rb");
679 if (index == NULL) {
680 err = got_error_from_errno();
681 goto done;
683 err = got_fileindex_read(fileindex, index);
684 fclose(index);
685 if (err)
686 goto done;
688 err = got_opentemp_named(&new_fileindex_path, &new_index,
689 fileindex_path);
690 if (err)
691 goto done;
693 err = got_object_open_as_commit(&commit, repo,
694 worktree->base_commit_id);
695 if (err)
696 goto done;
698 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
699 if (err)
700 goto done;
702 checkout_err = tree_checkout(worktree, fileindex, tree, "/", repo,
703 progress_cb, progress_arg, cancel_cb, cancel_arg);
705 /* Try to sync the fileindex back to disk in any case. */
706 err = got_fileindex_write(fileindex, new_index);
707 if (err)
708 goto done;
710 if (rename(new_fileindex_path, fileindex_path) != 0) {
711 err = got_error_from_errno();
712 goto done;
715 free(new_fileindex_path);
716 new_fileindex_path = NULL;
718 done:
719 if (tree)
720 got_object_tree_close(tree);
721 if (commit)
722 got_object_commit_close(commit);
723 if (new_fileindex_path)
724 unlink(new_fileindex_path);
725 if (new_index)
726 fclose(new_index);
727 free(new_fileindex_path);
728 free(fileindex_path);
729 got_fileindex_free(fileindex);
730 if (checkout_err)
731 err = checkout_err;
732 unlockerr = lock_worktree(worktree, LOCK_SH);
733 if (unlockerr && err == NULL)
734 err = unlockerr;
735 return err;