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, 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, 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 int version, fd = -1;
269 const char *errstr;
271 *worktree = NULL;
273 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
274 err = got_error_from_errno();
275 path_got = NULL;
276 goto done;
279 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
280 err = got_error_from_errno();
281 path_lock = NULL;
282 goto done;
285 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
286 if (fd == -1) {
287 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
288 : got_error_from_errno());
289 goto done;
292 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
293 if (err)
294 goto done;
296 version = strtonum(formatstr, 1, INT_MAX, &errstr);
297 if (errstr) {
298 err = got_error(GOT_ERR_WORKTREE_META);
299 goto done;
301 if (version != GOT_WORKTREE_FORMAT_VERSION) {
302 err = got_error(GOT_ERR_WORKTREE_VERS);
303 goto done;
306 *worktree = calloc(1, sizeof(**worktree));
307 if (*worktree == NULL) {
308 err = got_error_from_errno();
309 goto done;
311 (*worktree)->lockfd = -1;
313 (*worktree)->root_path = strdup(path);
314 if ((*worktree)->root_path == NULL) {
315 err = got_error_from_errno();
316 goto done;
318 err = read_meta_file(&(*worktree)->repo_path, path_got,
319 GOT_WORKTREE_REPOSITORY);
320 if (err)
321 goto done;
322 err = read_meta_file(&(*worktree)->path_prefix, path_got,
323 GOT_WORKTREE_PATH_PREFIX);
324 if (err)
325 goto done;
327 err = read_meta_file(&(*worktree)->head_ref, path_got,
328 GOT_WORKTREE_HEAD);
329 if (err)
330 goto done;
332 done:
333 free(path_got);
334 free(path_lock);
335 if (err) {
336 if (fd != -1)
337 close(fd);
338 if (*worktree != NULL)
339 got_worktree_close(*worktree);
340 *worktree = NULL;
341 } else
342 (*worktree)->lockfd = fd;
344 return err;
347 void
348 got_worktree_close(struct got_worktree *worktree)
350 free(worktree->root_path);
351 free(worktree->repo_path);
352 free(worktree->path_prefix);
353 free(worktree->base_commit);
354 free(worktree->head_ref);
355 if (worktree->lockfd != -1)
356 close(worktree->lockfd);
357 free(worktree);
360 char *
361 got_worktree_get_repo_path(struct got_worktree *worktree)
363 return strdup(worktree->repo_path);
366 char *
367 got_worktree_get_head_ref_name(struct got_worktree *worktree)
369 return strdup(worktree->head_ref);
372 static const struct got_error *
373 lock_worktree(struct got_worktree *worktree, int operation)
375 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
376 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
377 : got_error_from_errno());
378 return NULL;
381 static const char *
382 apply_path_prefix(struct got_worktree *worktree, const char *path)
384 const char *p = path;
385 p += strlen(worktree->path_prefix);
386 if (*p == '/')
387 p++;
388 return p;
391 static const struct got_error *
392 add_file_on_disk(struct got_worktree *worktree, struct got_fileindex *fileindex,
393 const char *path, struct got_blob_object *blob, struct got_repository *repo)
395 const struct got_error *err = NULL;
396 char *ondisk_path;
397 int fd;
398 size_t len, hdrlen;
399 struct got_fileindex_entry *entry;
401 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path,
402 apply_path_prefix(worktree, path)) == -1)
403 return got_error_from_errno();
405 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
406 GOT_DEFAULT_FILE_MODE);
407 if (fd == -1) {
408 err = got_error_from_errno();
409 if (errno == EEXIST) {
410 struct stat sb;
411 if (lstat(ondisk_path, &sb) == -1) {
412 err = got_error_from_errno();
413 } else if (!S_ISREG(sb.st_mode)) {
414 /* TODO file is obstructed; do something */
415 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
418 return err;
421 hdrlen = got_object_blob_get_hdrlen(blob);
422 do {
423 const uint8_t *buf = got_object_blob_get_read_buf(blob);
424 err = got_object_blob_read_block(&len, blob);
425 if (err)
426 break;
427 if (len > 0) {
428 /* Skip blob object header first time around. */
429 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
430 if (outlen == -1) {
431 err = got_error_from_errno();
432 goto done;
433 } else if (outlen != len - hdrlen) {
434 err = got_error(GOT_ERR_IO);
435 goto done;
437 hdrlen = 0;
439 } while (len != 0);
441 fsync(fd);
443 err = got_fileindex_entry_alloc(&entry, ondisk_path,
444 apply_path_prefix(worktree, path), blob->id.sha1);
445 if (err)
446 goto done;
448 err = got_fileindex_entry_add(fileindex, entry);
449 if (err)
450 goto done;
451 done:
452 close(fd);
453 free(ondisk_path);
454 return err;
457 static const struct got_error *
458 add_dir_on_disk(struct got_worktree *worktree, const char *path)
460 const struct got_error *err = NULL;
461 char *abspath;
463 if (asprintf(&abspath, "%s/%s", worktree->root_path,
464 apply_path_prefix(worktree, path)) == -1)
465 return got_error_from_errno();
467 /* XXX queue work rather than editing disk directly? */
468 if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1) {
469 struct stat sb;
471 if (errno != EEXIST) {
472 err = got_error_from_errno();
473 goto done;
476 if (lstat(abspath, &sb) == -1) {
477 err = got_error_from_errno();
478 goto done;
481 if (!S_ISDIR(sb.st_mode)) {
482 /* TODO directory is obstructed; do something */
483 return got_error(GOT_ERR_FILE_OBSTRUCTED);
487 done:
488 free(abspath);
489 return err;
492 static const struct got_error *
493 tree_checkout(struct got_worktree *, struct got_fileindex *,
494 struct got_tree_object *, const char *, struct got_repository *,
495 got_worktree_checkout_cb progress_cb, void *progress_arg,
496 got_worktree_cancel_cb cancel_cb, void *cancel_arg);
498 static const struct got_error *
499 tree_checkout_entry(struct got_worktree *worktree,
500 struct got_fileindex *fileindex, struct got_tree_entry *te,
501 const char *parent, struct got_repository *repo,
502 got_worktree_checkout_cb progress_cb, void *progress_arg,
503 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
505 const struct got_error *err = NULL;
506 struct got_object *obj = NULL;
507 struct got_blob_object *blob = NULL;
508 struct got_tree_object *tree = NULL;
509 char *path = NULL;
510 char *progress_path = NULL;
511 size_t len;
513 if (parent[0] == '/' && parent[1] == '\0')
514 parent = "";
515 if (asprintf(&path, "%s/%s", parent, te->name) == -1)
516 return got_error_from_errno();
518 /* Skip this entry if it is outside of our path prefix. */
519 len = MIN(strlen(worktree->path_prefix), strlen(path));
520 if (strncmp(path, worktree->path_prefix, len) != 0) {
521 free(path);
522 return NULL;
525 err = got_object_open(&obj, repo, te->id);
526 if (err)
527 goto done;
529 progress_path = path;
530 if (strncmp(progress_path, worktree->path_prefix, len) == 0)
531 progress_path += len;
532 (*progress_cb)(progress_arg, progress_path);
534 switch (obj->type) {
535 case GOT_OBJ_TYPE_BLOB:
536 if (strlen(worktree->path_prefix) >= strlen(path))
537 break;
538 err = got_object_blob_open(&blob, repo, obj, 8192);
539 if (err)
540 goto done;
541 err = add_file_on_disk(worktree, fileindex, path, blob, repo);
542 break;
543 case GOT_OBJ_TYPE_TREE:
544 err = got_object_tree_open(&tree, repo, obj);
545 if (err)
546 goto done;
547 if (strlen(worktree->path_prefix) < strlen(path)) {
548 err = add_dir_on_disk(worktree, path);
549 if (err)
550 break;
552 /* XXX infinite recursion possible */
553 err = tree_checkout(worktree, fileindex, tree, path, repo,
554 progress_cb, progress_arg, cancel_cb, cancel_arg);
555 break;
556 default:
557 break;
560 done:
561 if (blob)
562 got_object_blob_close(blob);
563 if (tree)
564 got_object_tree_close(tree);
565 if (obj)
566 got_object_close(obj);
567 free(path);
568 return err;
571 static const struct got_error *
572 tree_checkout(struct got_worktree *worktree,
573 struct got_fileindex *fileindex, struct got_tree_object *tree,
574 const char *path, struct got_repository *repo,
575 got_worktree_checkout_cb progress_cb, void *progress_arg,
576 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
578 const struct got_error *err = NULL;
579 const struct got_tree_entries *entries;
580 struct got_tree_entry *te;
581 size_t len;
583 /* Skip this tree if it is outside of our path prefix. */
584 len = MIN(strlen(worktree->path_prefix), strlen(path));
585 if (strncmp(path, worktree->path_prefix, len) != 0)
586 return NULL;
588 entries = got_object_tree_get_entries(tree);
589 SIMPLEQ_FOREACH(te, &entries->head, entry) {
590 if (cancel_cb) {
591 err = (*cancel_cb)(cancel_arg);
592 if (err)
593 break;
595 err = tree_checkout_entry(worktree, fileindex, te, path, repo,
596 progress_cb, progress_arg, cancel_cb, cancel_arg);
597 if (err)
598 break;
601 return err;
604 const struct got_error *
605 got_worktree_checkout_files(struct got_worktree *worktree,
606 struct got_reference *head_ref, struct got_repository *repo,
607 got_worktree_checkout_cb progress_cb, void *progress_arg,
608 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
610 const struct got_error *err = NULL, *unlockerr;
611 struct got_object_id *commit_id = NULL;
612 struct got_object *obj = NULL;
613 struct got_commit_object *commit = NULL;
614 struct got_tree_object *tree = NULL;
615 char *fileindex_path = NULL, *new_fileindex_path = NULL;
616 struct got_fileindex *fileindex = NULL;
617 FILE *new_index = NULL;
619 err = lock_worktree(worktree, LOCK_EX);
620 if (err)
621 return err;
623 fileindex = got_fileindex_alloc();
624 if (fileindex == NULL) {
625 err = got_error_from_errno();
626 goto done;
629 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
630 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
631 err = got_error_from_errno();
632 fileindex_path = NULL;
633 goto done;
636 err = got_opentemp_named(&new_fileindex_path, &new_index,
637 fileindex_path);
638 if (err)
639 goto done;
641 err = got_ref_resolve(&commit_id, repo, head_ref);
642 if (err)
643 goto done;
645 err = got_object_open(&obj, repo, commit_id);
646 if (err)
647 goto done;
649 if (obj->type != GOT_OBJ_TYPE_COMMIT) {
650 err = got_error(GOT_ERR_OBJ_TYPE);
651 goto done;
654 err = got_object_commit_open(&commit, repo, obj);
655 if (err)
656 goto done;
658 got_object_close(obj);
659 err = got_object_open(&obj, repo, commit->tree_id);
660 if (err)
661 goto done;
663 if (obj->type != GOT_OBJ_TYPE_TREE) {
664 err = got_error(GOT_ERR_OBJ_TYPE);
665 goto done;
668 err = got_object_tree_open(&tree, repo, obj);
669 if (err)
670 goto done;
672 err = tree_checkout(worktree, fileindex, tree, "/", repo,
673 progress_cb, progress_arg, cancel_cb, cancel_arg);
674 if (err)
675 goto done;
677 err = got_fileindex_write(fileindex, new_index);
678 if (err)
679 goto done;
681 if (rename(new_fileindex_path, fileindex_path) != 0) {
682 err = got_error_from_errno();
683 goto done;
686 free(new_fileindex_path);
687 new_fileindex_path = NULL;
689 done:
690 if (tree)
691 got_object_tree_close(tree);
692 if (commit)
693 got_object_commit_close(commit);
694 if (obj)
695 got_object_close(obj);
696 free(commit_id);
697 if (new_fileindex_path)
698 unlink(new_fileindex_path);
699 if (new_index)
700 fclose(new_index);
701 free(new_fileindex_path);
702 free(fileindex_path);
703 got_fileindex_free(fileindex);
704 unlockerr = lock_worktree(worktree, LOCK_SH);
705 if (unlockerr && err == NULL)
706 err = unlockerr;
707 return err;