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;
56 char buf[4];
57 ssize_t n;
59 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
60 err = got_error_from_errno();
61 path = NULL;
62 goto done;
63 }
65 fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
66 GOT_DEFAULT_FILE_MODE);
67 if (fd == -1) {
68 err = got_error_from_errno();
69 goto done;
70 }
72 /* The file should be empty. */
73 n = read(fd, buf, sizeof(buf));
74 if (n != 0) {
75 err = (n == -1 ? got_error_from_errno() :
76 got_error(GOT_ERR_WORKTREE_EXISTS));
77 goto done;
78 }
80 if (content) {
81 int len = dprintf(fd, "%s\n", content);
82 if (len != strlen(content) + 1) {
83 err = got_error_from_errno();
84 goto done;
85 }
86 }
88 done:
89 if (fd != -1 && close(fd) == -1 && err == NULL)
90 err = got_error_from_errno();
91 free(path);
92 return err;
93 }
95 static const struct got_error *
96 read_meta_file(char **content, const char *path_got, const char *name)
97 {
98 const struct got_error *err = NULL;
99 char *path;
100 int fd = -1;
101 ssize_t n;
102 struct stat sb;
104 *content = NULL;
106 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
107 err = got_error_from_errno();
108 path = NULL;
109 goto done;
112 fd = open(path, O_RDONLY | O_NOFOLLOW);
113 if (fd == -1) {
114 err = got_error_from_errno();
115 goto done;
117 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
118 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
119 : got_error_from_errno());
120 goto done;
123 stat(path, &sb);
124 *content = calloc(1, sb.st_size);
125 if (*content == NULL) {
126 err = got_error_from_errno();
127 goto done;
130 n = read(fd, *content, sb.st_size);
131 if (n != sb.st_size) {
132 err = (n == -1 ? got_error_from_errno() :
133 got_error(GOT_ERR_WORKTREE_META));
134 goto done;
136 if ((*content)[sb.st_size - 1] != '\n') {
137 err = got_error(GOT_ERR_WORKTREE_META);
138 goto done;
140 (*content)[sb.st_size - 1] = '\0';
142 done:
143 if (fd != -1 && close(fd) == -1 && err == NULL)
144 err = got_error_from_errno();
145 free(path);
146 if (err) {
147 free(*content);
148 *content = NULL;
150 return err;
153 const struct got_error *
154 got_worktree_init(const char *path, struct got_reference *head_ref,
155 const char *prefix, struct got_repository *repo)
157 const struct got_error *err = NULL;
158 struct got_object_id *commit_id = NULL;
159 int obj_type;
160 char *path_got = NULL;
161 char *refstr = NULL;
162 char *repo_path = NULL;
163 char *formatstr = NULL;
164 char *absprefix = NULL;
165 char *basestr = NULL;
167 err = got_ref_resolve(&commit_id, repo, head_ref);
168 if (err)
169 return err;
170 err = got_object_get_type(&obj_type, repo, commit_id);
171 if (err)
172 return err;
173 if (obj_type != GOT_OBJ_TYPE_COMMIT)
174 return got_error(GOT_ERR_OBJ_TYPE);
176 if (!got_path_is_absolute(prefix)) {
177 if (asprintf(&absprefix, "/%s", prefix) == -1)
178 return got_error_from_errno();
181 /* Create top-level directory (may already exist). */
182 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
183 err = got_error_from_errno();
184 goto done;
187 /* Create .got directory (may already exist). */
188 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
189 err = got_error_from_errno();
190 goto done;
192 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
193 err = got_error_from_errno();
194 goto done;
197 /* Create an empty lock file. */
198 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
199 if (err)
200 goto done;
202 /* Create an empty file index. */
203 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
204 if (err)
205 goto done;
207 /* Write the HEAD reference. */
208 refstr = got_ref_to_str(head_ref);
209 if (refstr == NULL) {
210 err = got_error_from_errno();
211 goto done;
213 err = create_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
214 if (err)
215 goto done;
217 /* Record our base commit. */
218 err = got_object_id_str(&basestr, commit_id);
219 if (err)
220 goto done;
221 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
222 if (err)
223 goto done;
225 /* Store path to repository. */
226 repo_path = got_repo_get_path(repo);
227 if (repo_path == NULL) {
228 err = got_error_from_errno();
229 goto done;
231 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY, repo_path);
232 if (err)
233 goto done;
235 /* Store in-repository path prefix. */
236 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
237 absprefix ? absprefix : prefix);
238 if (err)
239 goto done;
241 /* Stamp work tree with format file. */
242 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
243 err = got_error_from_errno();
244 goto done;
246 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
247 if (err)
248 goto done;
250 done:
251 free(commit_id);
252 free(path_got);
253 free(formatstr);
254 free(refstr);
255 free(repo_path);
256 free(absprefix);
257 free(basestr);
258 return err;
261 const struct got_error *
262 got_worktree_open(struct got_worktree **worktree, const char *path)
264 const struct got_error *err = NULL;
265 char *path_got;
266 char *formatstr = NULL;
267 char *path_lock = NULL;
268 char *base_commit_id_str = NULL;
269 char *head_ref_str = NULL;
270 int version, fd = -1;
271 const char *errstr;
272 struct got_repository *repo = NULL;
274 *worktree = NULL;
276 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
277 err = got_error_from_errno();
278 path_got = NULL;
279 goto done;
282 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
283 err = got_error_from_errno();
284 path_lock = NULL;
285 goto done;
288 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
289 if (fd == -1) {
290 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
291 : got_error_from_errno());
292 goto done;
295 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
296 if (err)
297 goto done;
299 version = strtonum(formatstr, 1, INT_MAX, &errstr);
300 if (errstr) {
301 err = got_error(GOT_ERR_WORKTREE_META);
302 goto done;
304 if (version != GOT_WORKTREE_FORMAT_VERSION) {
305 err = got_error(GOT_ERR_WORKTREE_VERS);
306 goto done;
309 *worktree = calloc(1, sizeof(**worktree));
310 if (*worktree == NULL) {
311 err = got_error_from_errno();
312 goto done;
314 (*worktree)->lockfd = -1;
316 (*worktree)->root_path = strdup(path);
317 if ((*worktree)->root_path == NULL) {
318 err = got_error_from_errno();
319 goto done;
321 err = read_meta_file(&(*worktree)->repo_path, path_got,
322 GOT_WORKTREE_REPOSITORY);
323 if (err)
324 goto done;
326 err = read_meta_file(&(*worktree)->path_prefix, path_got,
327 GOT_WORKTREE_PATH_PREFIX);
328 if (err)
329 goto done;
331 err = read_meta_file(&base_commit_id_str, path_got,
332 GOT_WORKTREE_BASE_COMMIT);
333 if (err)
334 goto done;
336 err = got_repo_open(&repo, (*worktree)->repo_path);
337 if (err)
338 goto done;
340 err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
341 base_commit_id_str);
342 if (err)
343 goto done;
345 err = read_meta_file(&head_ref_str, path_got, GOT_WORKTREE_HEAD_REF);
346 if (err)
347 goto done;
349 err = got_ref_open(&(*worktree)->head_ref, repo, head_ref_str);
350 done:
351 if (repo)
352 got_repo_close(repo);
353 free(path_got);
354 free(path_lock);
355 free(head_ref_str);
356 free(base_commit_id_str);
357 if (err) {
358 if (fd != -1)
359 close(fd);
360 if (*worktree != NULL)
361 got_worktree_close(*worktree);
362 *worktree = NULL;
363 } else
364 (*worktree)->lockfd = fd;
366 return err;
369 void
370 got_worktree_close(struct got_worktree *worktree)
372 free(worktree->root_path);
373 free(worktree->repo_path);
374 free(worktree->path_prefix);
375 free(worktree->base_commit_id);
376 if (worktree->head_ref)
377 got_ref_close(worktree->head_ref);
378 if (worktree->lockfd != -1)
379 close(worktree->lockfd);
380 free(worktree);
383 char *
384 got_worktree_get_repo_path(struct got_worktree *worktree)
386 return strdup(worktree->repo_path);
389 char *
390 got_worktree_get_head_ref_name(struct got_worktree *worktree)
392 return got_ref_to_str(worktree->head_ref);
395 static const struct got_error *
396 lock_worktree(struct got_worktree *worktree, int operation)
398 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
399 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
400 : got_error_from_errno());
401 return NULL;
404 static const char *
405 apply_path_prefix(struct got_worktree *worktree, const char *path)
407 const char *p = path;
408 p += strlen(worktree->path_prefix);
409 if (*p == '/')
410 p++;
411 return p;
414 static const struct got_error *
415 add_file_on_disk(struct got_worktree *worktree, struct got_fileindex *fileindex,
416 const char *path, struct got_blob_object *blob, struct got_repository *repo)
418 const struct got_error *err = NULL;
419 char *ondisk_path;
420 int fd;
421 size_t len, hdrlen;
422 struct got_fileindex_entry *entry;
424 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path,
425 apply_path_prefix(worktree, path)) == -1)
426 return got_error_from_errno();
428 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
429 GOT_DEFAULT_FILE_MODE);
430 if (fd == -1) {
431 err = got_error_from_errno();
432 if (errno == EEXIST) {
433 struct stat sb;
434 if (lstat(ondisk_path, &sb) == -1) {
435 err = got_error_from_errno();
436 } else if (!S_ISREG(sb.st_mode)) {
437 /* TODO file is obstructed; do something */
438 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
441 return err;
444 hdrlen = got_object_blob_get_hdrlen(blob);
445 do {
446 const uint8_t *buf = got_object_blob_get_read_buf(blob);
447 err = got_object_blob_read_block(&len, blob);
448 if (err)
449 break;
450 if (len > 0) {
451 /* Skip blob object header first time around. */
452 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
453 if (outlen == -1) {
454 err = got_error_from_errno();
455 goto done;
456 } else if (outlen != len - hdrlen) {
457 err = got_error(GOT_ERR_IO);
458 goto done;
460 hdrlen = 0;
462 } while (len != 0);
464 fsync(fd);
466 err = got_fileindex_entry_alloc(&entry, ondisk_path,
467 apply_path_prefix(worktree, path), blob->id.sha1,
468 worktree->base_commit_id->sha1);
469 if (err)
470 goto done;
472 err = got_fileindex_entry_add(fileindex, entry);
473 if (err)
474 goto done;
475 done:
476 close(fd);
477 free(ondisk_path);
478 return err;
481 static const struct got_error *
482 add_dir_on_disk(struct got_worktree *worktree, const char *path)
484 const struct got_error *err = NULL;
485 char *abspath;
487 if (asprintf(&abspath, "%s/%s", worktree->root_path,
488 apply_path_prefix(worktree, path)) == -1)
489 return got_error_from_errno();
491 /* XXX queue work rather than editing disk directly? */
492 if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1) {
493 struct stat sb;
495 if (errno != EEXIST) {
496 err = got_error_from_errno();
497 goto done;
500 if (lstat(abspath, &sb) == -1) {
501 err = got_error_from_errno();
502 goto done;
505 if (!S_ISDIR(sb.st_mode)) {
506 /* TODO directory is obstructed; do something */
507 return got_error(GOT_ERR_FILE_OBSTRUCTED);
511 done:
512 free(abspath);
513 return err;
516 static const struct got_error *
517 tree_checkout(struct got_worktree *, struct got_fileindex *,
518 struct got_tree_object *, const char *, struct got_repository *,
519 got_worktree_checkout_cb progress_cb, void *progress_arg,
520 got_worktree_cancel_cb cancel_cb, void *cancel_arg);
522 static const struct got_error *
523 tree_checkout_entry(struct got_worktree *worktree,
524 struct got_fileindex *fileindex, struct got_tree_entry *te,
525 const char *parent, struct got_repository *repo,
526 got_worktree_checkout_cb progress_cb, void *progress_arg,
527 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
529 const struct got_error *err = NULL;
530 struct got_object *obj = NULL;
531 struct got_blob_object *blob = NULL;
532 struct got_tree_object *tree = NULL;
533 char *path = NULL;
534 char *progress_path = NULL;
535 size_t len;
537 if (parent[0] == '/' && parent[1] == '\0')
538 parent = "";
539 if (asprintf(&path, "%s/%s", parent, te->name) == -1)
540 return got_error_from_errno();
542 /* Skip this entry if it is outside of our path prefix. */
543 len = MIN(strlen(worktree->path_prefix), strlen(path));
544 if (strncmp(path, worktree->path_prefix, len) != 0) {
545 free(path);
546 return NULL;
549 err = got_object_open(&obj, repo, te->id);
550 if (err)
551 goto done;
553 progress_path = path;
554 if (strncmp(progress_path, worktree->path_prefix, len) == 0)
555 progress_path += len;
556 (*progress_cb)(progress_arg, progress_path);
558 switch (obj->type) {
559 case GOT_OBJ_TYPE_BLOB:
560 if (strlen(worktree->path_prefix) >= strlen(path))
561 break;
562 err = got_object_blob_open(&blob, repo, obj, 8192);
563 if (err)
564 goto done;
565 err = add_file_on_disk(worktree, fileindex, path, blob, repo);
566 break;
567 case GOT_OBJ_TYPE_TREE:
568 err = got_object_tree_open(&tree, repo, obj);
569 if (err)
570 goto done;
571 if (strlen(worktree->path_prefix) < strlen(path)) {
572 err = add_dir_on_disk(worktree, path);
573 if (err)
574 break;
576 /* XXX infinite recursion possible */
577 err = tree_checkout(worktree, fileindex, tree, path, repo,
578 progress_cb, progress_arg, cancel_cb, cancel_arg);
579 break;
580 default:
581 break;
584 done:
585 if (blob)
586 got_object_blob_close(blob);
587 if (tree)
588 got_object_tree_close(tree);
589 if (obj)
590 got_object_close(obj);
591 free(path);
592 return err;
595 static const struct got_error *
596 tree_checkout(struct got_worktree *worktree,
597 struct got_fileindex *fileindex, struct got_tree_object *tree,
598 const char *path, struct got_repository *repo,
599 got_worktree_checkout_cb progress_cb, void *progress_arg,
600 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
602 const struct got_error *err = NULL;
603 const struct got_tree_entries *entries;
604 struct got_tree_entry *te;
605 size_t len;
607 /* Skip this tree if it is outside of our path prefix. */
608 len = MIN(strlen(worktree->path_prefix), strlen(path));
609 if (strncmp(path, worktree->path_prefix, len) != 0)
610 return NULL;
612 entries = got_object_tree_get_entries(tree);
613 SIMPLEQ_FOREACH(te, &entries->head, entry) {
614 if (cancel_cb) {
615 err = (*cancel_cb)(cancel_arg);
616 if (err)
617 break;
619 err = tree_checkout_entry(worktree, fileindex, te, path, repo,
620 progress_cb, progress_arg, cancel_cb, cancel_arg);
621 if (err)
622 break;
625 return err;
628 const struct got_error *
629 got_worktree_checkout_files(struct got_worktree *worktree,
630 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
631 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
633 const struct got_error *err = NULL, *unlockerr, *checkout_err = NULL;
634 struct got_commit_object *commit = NULL;
635 struct got_tree_object *tree = NULL;
636 char *fileindex_path = NULL, *new_fileindex_path = NULL;
637 struct got_fileindex *fileindex = NULL;
638 FILE *new_index = NULL;
640 err = lock_worktree(worktree, LOCK_EX);
641 if (err)
642 return err;
644 fileindex = got_fileindex_alloc();
645 if (fileindex == NULL) {
646 err = got_error_from_errno();
647 goto done;
650 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
651 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
652 err = got_error_from_errno();
653 fileindex_path = NULL;
654 goto done;
657 err = got_opentemp_named(&new_fileindex_path, &new_index,
658 fileindex_path);
659 if (err)
660 goto done;
662 err = got_object_open_as_commit(&commit, repo,
663 worktree->base_commit_id);
664 if (err)
665 goto done;
667 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
668 if (err)
669 goto done;
671 checkout_err = tree_checkout(worktree, fileindex, tree, "/", repo,
672 progress_cb, progress_arg, cancel_cb, cancel_arg);
674 /* Try to sync the fileindex back to disk in any case. */
675 err = got_fileindex_write(fileindex, new_index);
676 if (err)
677 goto done;
679 if (rename(new_fileindex_path, fileindex_path) != 0) {
680 err = got_error_from_errno();
681 goto done;
684 free(new_fileindex_path);
685 new_fileindex_path = NULL;
687 done:
688 if (tree)
689 got_object_tree_close(tree);
690 if (commit)
691 got_object_commit_close(commit);
692 if (new_fileindex_path)
693 unlink(new_fileindex_path);
694 if (new_index)
695 fclose(new_index);
696 free(new_fileindex_path);
697 free(fileindex_path);
698 got_fileindex_free(fileindex);
699 if (checkout_err)
700 err = checkout_err;
701 unlockerr = lock_worktree(worktree, LOCK_SH);
702 if (unlockerr && err == NULL)
703 err = unlockerr;
704 return err;