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)->base, path_got, GOT_WORKTREE_BASE);
328 if (err)
329 goto done;
331 err = read_meta_file(&(*worktree)->head_ref, path_got,
332 GOT_WORKTREE_HEAD);
333 if (err)
334 goto done;
336 done:
337 free(path_got);
338 free(path_lock);
339 if (err) {
340 if (fd != -1)
341 close(fd);
342 if (*worktree != NULL)
343 got_worktree_close(*worktree);
344 *worktree = NULL;
345 } else
346 (*worktree)->lockfd = fd;
348 return err;
351 void
352 got_worktree_close(struct got_worktree *worktree)
354 free(worktree->root_path);
355 free(worktree->repo_path);
356 free(worktree->path_prefix);
357 free(worktree->base);
358 free(worktree->head_ref);
359 if (worktree->lockfd != -1)
360 close(worktree->lockfd);
361 free(worktree);
364 char *
365 got_worktree_get_repo_path(struct got_worktree *worktree)
367 return strdup(worktree->repo_path);
370 char *
371 got_worktree_get_head_ref_name(struct got_worktree *worktree)
373 return strdup(worktree->head_ref);
376 static const struct got_error *
377 lock_worktree(struct got_worktree *worktree, int operation)
379 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
380 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
381 : got_error_from_errno());
382 return NULL;
385 static const char *
386 apply_path_prefix(struct got_worktree *worktree, const char *path)
388 const char *p = path;
389 p += strlen(worktree->path_prefix);
390 if (*p == '/')
391 p++;
392 return p;
395 static const struct got_error *
396 add_file_on_disk(struct got_worktree *worktree, struct got_fileindex *fileindex,
397 const char *path, struct got_blob_object *blob, struct got_repository *repo)
399 const struct got_error *err = NULL;
400 char *ondisk_path;
401 int fd;
402 size_t len, hdrlen;
403 struct got_fileindex_entry *entry;
405 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path,
406 apply_path_prefix(worktree, path)) == -1)
407 return got_error_from_errno();
409 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
410 GOT_DEFAULT_FILE_MODE);
411 if (fd == -1) {
412 err = got_error_from_errno();
413 if (errno == EEXIST) {
414 struct stat sb;
415 if (lstat(ondisk_path, &sb) == -1) {
416 err = got_error_from_errno();
417 } else if (!S_ISREG(sb.st_mode)) {
418 /* TODO file is obstructed; do something */
419 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
422 return err;
425 hdrlen = got_object_blob_get_hdrlen(blob);
426 do {
427 const uint8_t *buf = got_object_blob_get_read_buf(blob);
428 err = got_object_blob_read_block(&len, blob);
429 if (err)
430 break;
431 if (len > 0) {
432 /* Skip blob object header first time around. */
433 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
434 if (outlen == -1) {
435 err = got_error_from_errno();
436 goto done;
437 } else if (outlen != len - hdrlen) {
438 err = got_error(GOT_ERR_IO);
439 goto done;
441 hdrlen = 0;
443 } while (len != 0);
445 fsync(fd);
447 err = got_fileindex_entry_alloc(&entry, ondisk_path,
448 apply_path_prefix(worktree, path), blob->id.sha1);
449 if (err)
450 goto done;
452 err = got_fileindex_entry_add(fileindex, entry);
453 if (err)
454 goto done;
455 done:
456 close(fd);
457 free(ondisk_path);
458 return err;
461 static const struct got_error *
462 add_dir_on_disk(struct got_worktree *worktree, const char *path)
464 const struct got_error *err = NULL;
465 char *abspath;
467 if (asprintf(&abspath, "%s/%s", worktree->root_path,
468 apply_path_prefix(worktree, path)) == -1)
469 return got_error_from_errno();
471 /* XXX queue work rather than editing disk directly? */
472 if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1) {
473 struct stat sb;
475 if (errno != EEXIST) {
476 err = got_error_from_errno();
477 goto done;
480 if (lstat(abspath, &sb) == -1) {
481 err = got_error_from_errno();
482 goto done;
485 if (!S_ISDIR(sb.st_mode)) {
486 /* TODO directory is obstructed; do something */
487 return got_error(GOT_ERR_FILE_OBSTRUCTED);
491 done:
492 free(abspath);
493 return err;
496 static const struct got_error *
497 tree_checkout(struct got_worktree *, struct got_fileindex *,
498 struct got_tree_object *, const char *, struct got_repository *,
499 got_worktree_checkout_cb progress_cb, void *progress_arg,
500 got_worktree_cancel_cb cancel_cb, void *cancel_arg);
502 static const struct got_error *
503 tree_checkout_entry(struct got_worktree *worktree,
504 struct got_fileindex *fileindex, struct got_tree_entry *te,
505 const char *parent, struct got_repository *repo,
506 got_worktree_checkout_cb progress_cb, void *progress_arg,
507 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
509 const struct got_error *err = NULL;
510 struct got_object *obj = NULL;
511 struct got_blob_object *blob = NULL;
512 struct got_tree_object *tree = NULL;
513 char *path = NULL;
514 char *progress_path = NULL;
515 size_t len;
517 if (parent[0] == '/' && parent[1] == '\0')
518 parent = "";
519 if (asprintf(&path, "%s/%s", parent, te->name) == -1)
520 return got_error_from_errno();
522 /* Skip this entry if it is outside of our path prefix. */
523 len = MIN(strlen(worktree->path_prefix), strlen(path));
524 if (strncmp(path, worktree->path_prefix, len) != 0) {
525 free(path);
526 return NULL;
529 err = got_object_open(&obj, repo, te->id);
530 if (err)
531 goto done;
533 progress_path = path;
534 if (strncmp(progress_path, worktree->path_prefix, len) == 0)
535 progress_path += len;
536 (*progress_cb)(progress_arg, progress_path);
538 switch (obj->type) {
539 case GOT_OBJ_TYPE_BLOB:
540 if (strlen(worktree->path_prefix) >= strlen(path))
541 break;
542 err = got_object_blob_open(&blob, repo, obj, 8192);
543 if (err)
544 goto done;
545 err = add_file_on_disk(worktree, fileindex, path, blob, repo);
546 break;
547 case GOT_OBJ_TYPE_TREE:
548 err = got_object_tree_open(&tree, repo, obj);
549 if (err)
550 goto done;
551 if (strlen(worktree->path_prefix) < strlen(path)) {
552 err = add_dir_on_disk(worktree, path);
553 if (err)
554 break;
556 /* XXX infinite recursion possible */
557 err = tree_checkout(worktree, fileindex, tree, path, repo,
558 progress_cb, progress_arg, cancel_cb, cancel_arg);
559 break;
560 default:
561 break;
564 done:
565 if (blob)
566 got_object_blob_close(blob);
567 if (tree)
568 got_object_tree_close(tree);
569 if (obj)
570 got_object_close(obj);
571 free(path);
572 return err;
575 static const struct got_error *
576 tree_checkout(struct got_worktree *worktree,
577 struct got_fileindex *fileindex, struct got_tree_object *tree,
578 const char *path, struct got_repository *repo,
579 got_worktree_checkout_cb progress_cb, void *progress_arg,
580 got_worktree_cancel_cb cancel_cb, void *cancel_arg)
582 const struct got_error *err = NULL;
583 const struct got_tree_entries *entries;
584 struct got_tree_entry *te;
585 size_t len;
587 /* Skip this tree if it is outside of our path prefix. */
588 len = MIN(strlen(worktree->path_prefix), strlen(path));
589 if (strncmp(path, worktree->path_prefix, len) != 0)
590 return NULL;
592 entries = got_object_tree_get_entries(tree);
593 SIMPLEQ_FOREACH(te, &entries->head, entry) {
594 if (cancel_cb) {
595 err = (*cancel_cb)(cancel_arg);
596 if (err)
597 break;
599 err = tree_checkout_entry(worktree, fileindex, te, path, repo,
600 progress_cb, progress_arg, cancel_cb, cancel_arg);
601 if (err)
602 break;
605 return err;
608 const struct got_error *
609 got_worktree_checkout_files(struct got_worktree *worktree,
610 struct got_repository *repo, got_worktree_checkout_cb progress_cb,
611 void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
613 const struct got_error *err = NULL, *unlockerr;
614 struct got_object_id *commit_id = NULL;
615 struct got_commit_object *commit = NULL;
616 struct got_tree_object *tree = NULL;
617 char *fileindex_path = NULL, *new_fileindex_path = NULL;
618 struct got_fileindex *fileindex = NULL;
619 FILE *new_index = NULL;
621 err = lock_worktree(worktree, LOCK_EX);
622 if (err)
623 return err;
625 err = got_object_resolve_id_str(&commit_id, repo, worktree->base);
626 if (err)
627 goto done;
629 fileindex = got_fileindex_alloc();
630 if (fileindex == NULL) {
631 err = got_error_from_errno();
632 goto done;
635 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
636 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
637 err = got_error_from_errno();
638 fileindex_path = NULL;
639 goto done;
642 err = got_opentemp_named(&new_fileindex_path, &new_index,
643 fileindex_path);
644 if (err)
645 goto done;
647 err = got_object_open_as_commit(&commit, repo, commit_id);
648 if (err)
649 goto done;
651 err = got_object_open_as_tree(&tree, repo, commit->tree_id);
652 if (err)
653 goto done;
655 err = tree_checkout(worktree, fileindex, tree, "/", repo,
656 progress_cb, progress_arg, cancel_cb, cancel_arg);
657 if (err)
658 goto done;
660 err = got_fileindex_write(fileindex, new_index);
661 if (err)
662 goto done;
664 if (rename(new_fileindex_path, fileindex_path) != 0) {
665 err = got_error_from_errno();
666 goto done;
669 free(new_fileindex_path);
670 new_fileindex_path = NULL;
672 done:
673 if (tree)
674 got_object_tree_close(tree);
675 if (commit)
676 got_object_commit_close(commit);
677 free(commit_id);
678 if (new_fileindex_path)
679 unlink(new_fileindex_path);
680 if (new_index)
681 fclose(new_index);
682 free(new_fileindex_path);
683 free(fileindex_path);
684 got_fileindex_free(fileindex);
685 unlockerr = lock_worktree(worktree, LOCK_SH);
686 if (unlockerr && err == NULL)
687 err = unlockerr;
688 return err;