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 char *path_got = NULL;
159 char *refstr = NULL;
160 char *repo_path = NULL;
161 char *formatstr = NULL;
162 char *absprefix = NULL;
164 if (!got_path_is_absolute(prefix)) {
165 if (asprintf(&absprefix, "/%s", prefix) == -1)
166 return got_error_from_errno();
169 /* Create top-level directory (may already exist). */
170 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
171 err = got_error_from_errno();
172 goto done;
175 /* Create .got directory (may already exist). */
176 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
177 err = got_error_from_errno();
178 goto done;
180 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
181 err = got_error_from_errno();
182 goto done;
185 /* Create an empty lock file. */
186 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
187 if (err)
188 goto done;
190 /* Create an empty file index. */
191 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
192 if (err)
193 goto done;
195 /* Write the HEAD reference. */
196 refstr = got_ref_to_str(head_ref);
197 if (refstr == NULL) {
198 err = got_error_from_errno();
199 goto done;
201 err = create_meta_file(path_got, GOT_WORKTREE_HEAD, refstr);
202 if (err)
203 goto done;
205 /* Store path to repository. */
206 repo_path = got_repo_get_path(repo);
207 if (repo_path == NULL) {
208 err = got_error_from_errno();
209 goto done;
211 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY, repo_path);
212 if (err)
213 goto done;
215 /* Store in-repository path prefix. */
216 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
217 absprefix ? absprefix : prefix);
218 if (err)
219 goto done;
221 /* Stamp work tree with format file. */
222 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
223 err = got_error_from_errno();
224 goto done;
226 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
227 if (err)
228 goto done;
230 done:
231 free(path_got);
232 free(formatstr);
233 free(refstr);
234 free(repo_path);
235 free(absprefix);
236 return err;
239 const struct got_error *
240 got_worktree_open(struct got_worktree **worktree, const char *path)
242 const struct got_error *err = NULL;
243 char *path_got;
244 char *formatstr = NULL;
245 char *path_lock = NULL;
246 int version, fd = -1;
247 const char *errstr;
249 *worktree = NULL;
251 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
252 err = got_error_from_errno();
253 path_got = NULL;
254 goto done;
257 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
258 err = got_error_from_errno();
259 path_lock = NULL;
260 goto done;
263 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
264 if (fd == -1) {
265 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
266 : got_error_from_errno());
267 goto done;
270 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
271 if (err)
272 goto done;
274 version = strtonum(formatstr, 1, INT_MAX, &errstr);
275 if (errstr) {
276 err = got_error(GOT_ERR_WORKTREE_META);
277 goto done;
279 if (version != GOT_WORKTREE_FORMAT_VERSION) {
280 err = got_error(GOT_ERR_WORKTREE_VERS);
281 goto done;
284 *worktree = calloc(1, sizeof(**worktree));
285 if (*worktree == NULL) {
286 err = got_error_from_errno();
287 goto done;
289 (*worktree)->lockfd = -1;
291 (*worktree)->root_path = strdup(path);
292 if ((*worktree)->root_path == NULL) {
293 err = got_error_from_errno();
294 goto done;
296 err = read_meta_file(&(*worktree)->repo_path, path_got,
297 GOT_WORKTREE_REPOSITORY);
298 if (err)
299 goto done;
300 err = read_meta_file(&(*worktree)->path_prefix, path_got,
301 GOT_WORKTREE_PATH_PREFIX);
302 if (err)
303 goto done;
305 err = read_meta_file(&(*worktree)->head_ref, path_got,
306 GOT_WORKTREE_HEAD);
307 if (err)
308 goto done;
310 done:
311 free(path_got);
312 free(path_lock);
313 if (err) {
314 if (fd != -1)
315 close(fd);
316 if (*worktree != NULL)
317 got_worktree_close(*worktree);
318 *worktree = NULL;
319 } else
320 (*worktree)->lockfd = fd;
322 return err;
325 void
326 got_worktree_close(struct got_worktree *worktree)
328 free(worktree->root_path);
329 free(worktree->repo_path);
330 free(worktree->path_prefix);
331 free(worktree->base_commit);
332 free(worktree->head_ref);
333 if (worktree->lockfd != -1)
334 close(worktree->lockfd);
335 free(worktree);
338 char *
339 got_worktree_get_repo_path(struct got_worktree *worktree)
341 return strdup(worktree->repo_path);
344 char *
345 got_worktree_get_head_ref_name(struct got_worktree *worktree)
347 return strdup(worktree->head_ref);
350 static const struct got_error *
351 lock_worktree(struct got_worktree *worktree, int operation)
353 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
354 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
355 : got_error_from_errno());
356 return NULL;
359 static const char *
360 apply_path_prefix(struct got_worktree *worktree, const char *path)
362 const char *p = path;
363 p += strlen(worktree->path_prefix);
364 if (*p == '/')
365 p++;
366 return p;
369 static const struct got_error *
370 add_file_on_disk(struct got_worktree *worktree, struct got_fileindex *fileindex,
371 const char *path, struct got_blob_object *blob, struct got_repository *repo)
373 const struct got_error *err = NULL;
374 char *ondisk_path;
375 int fd;
376 size_t len, hdrlen;
377 struct got_fileindex_entry *entry;
379 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path,
380 apply_path_prefix(worktree, path)) == -1)
381 return got_error_from_errno();
383 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
384 GOT_DEFAULT_FILE_MODE);
385 if (fd == -1) {
386 err = got_error_from_errno();
387 if (errno == EEXIST) {
388 struct stat sb;
389 if (lstat(ondisk_path, &sb) == -1) {
390 err = got_error_from_errno();
391 } else if (!S_ISREG(sb.st_mode)) {
392 /* TODO file is obstructed; do something */
393 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
396 return err;
399 hdrlen = got_object_blob_get_hdrlen(blob);
400 do {
401 const uint8_t *buf = got_object_blob_get_read_buf(blob);
402 err = got_object_blob_read_block(&len, blob);
403 if (err)
404 break;
405 if (len > 0) {
406 /* Skip blob object header first time around. */
407 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
408 hdrlen = 0;
409 if (outlen == -1) {
410 err = got_error_from_errno();
411 break;
412 } else if (outlen != len) {
413 err = got_error(GOT_ERR_IO);
414 break;
417 } while (len != 0);
419 fsync(fd);
421 err = got_fileindex_entry_open(&entry, ondisk_path,
422 apply_path_prefix(worktree, path), blob->id.sha1);
423 if (err)
424 goto done;
426 err = got_fileindex_entry_add(fileindex, entry);
427 if (err)
428 goto done;
429 done:
430 close(fd);
431 free(ondisk_path);
432 return err;
435 static const struct got_error *
436 add_dir_on_disk(struct got_worktree *worktree, const char *path)
438 const struct got_error *err = NULL;
439 char *abspath;
441 if (asprintf(&abspath, "%s/%s", worktree->root_path,
442 apply_path_prefix(worktree, path)) == -1)
443 return got_error_from_errno();
445 /* XXX queue work rather than editing disk directly? */
446 if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1) {
447 struct stat sb;
449 if (errno != EEXIST) {
450 err = got_error_from_errno();
451 goto done;
454 if (lstat(abspath, &sb) == -1) {
455 err = got_error_from_errno();
456 goto done;
459 if (!S_ISDIR(sb.st_mode)) {
460 /* TODO directory is obstructed; do something */
461 return got_error(GOT_ERR_FILE_OBSTRUCTED);
465 done:
466 free(abspath);
467 return err;
470 static const struct got_error *
471 tree_checkout(struct got_worktree *, struct got_fileindex *,
472 struct got_tree_object *, const char *, struct got_repository *,
473 got_worktree_checkout_cb progress_cb, void *progress_arg);
475 static const struct got_error *
476 tree_checkout_entry(struct got_worktree *worktree,
477 struct got_fileindex *fileindex, struct got_tree_entry *te,
478 const char *parent, struct got_repository *repo,
479 got_worktree_checkout_cb progress_cb, void *progress_arg)
481 const struct got_error *err = NULL;
482 struct got_object *obj = NULL;
483 struct got_blob_object *blob = NULL;
484 struct got_tree_object *tree = NULL;
485 char *path = NULL;
486 char *progress_path = NULL;
487 size_t len;
489 if (parent[0] == '/' && parent[1] == '\0')
490 parent = "";
491 if (asprintf(&path, "%s/%s", parent, te->name) == -1)
492 return got_error_from_errno();
494 /* Skip this entry if it is outside of our path prefix. */
495 len = MIN(strlen(worktree->path_prefix), strlen(path));
496 if (strncmp(path, worktree->path_prefix, len) != 0) {
497 free(path);
498 return NULL;
501 err = got_object_open(&obj, repo, te->id);
502 if (err)
503 goto done;
505 progress_path = path;
506 if (strncmp(progress_path, worktree->path_prefix, len) == 0)
507 progress_path += len;
508 (*progress_cb)(progress_arg, progress_path);
510 switch (got_object_get_type(obj)) {
511 case GOT_OBJ_TYPE_BLOB:
512 if (strlen(worktree->path_prefix) >= strlen(path))
513 break;
514 err = got_object_blob_open(&blob, repo, obj, 8192);
515 if (err)
516 goto done;
517 err = add_file_on_disk(worktree, fileindex, path, blob, repo);
518 break;
519 case GOT_OBJ_TYPE_TREE:
520 err = got_object_tree_open(&tree, repo, obj);
521 if (err)
522 goto done;
523 if (strlen(worktree->path_prefix) < strlen(path)) {
524 err = add_dir_on_disk(worktree, path);
525 if (err)
526 break;
528 err = tree_checkout(worktree, fileindex, tree, path, repo,
529 progress_cb, progress_arg);
530 break;
531 default:
532 break;
535 done:
536 if (blob)
537 got_object_blob_close(blob);
538 if (tree)
539 got_object_tree_close(tree);
540 if (obj)
541 got_object_close(obj);
542 free(path);
543 return err;
546 static const struct got_error *
547 tree_checkout(struct got_worktree *worktree,
548 struct got_fileindex *fileindex, struct got_tree_object *tree,
549 const char *path, struct got_repository *repo,
550 got_worktree_checkout_cb progress_cb, void *progress_arg)
552 const struct got_error *err = NULL;
553 const struct got_tree_entries *entries;
554 struct got_tree_entry *te;
555 size_t len;
557 /* Skip this tree if it is outside of our path prefix. */
558 len = MIN(strlen(worktree->path_prefix), strlen(path));
559 if (strncmp(path, worktree->path_prefix, len) != 0)
560 return NULL;
562 entries = got_object_tree_get_entries(tree);
563 SIMPLEQ_FOREACH(te, &entries->head, entry) {
564 err = tree_checkout_entry(worktree, fileindex, te, path, repo,
565 progress_cb, progress_arg);
566 if (err)
567 break;
570 return err;
573 const struct got_error *
574 got_worktree_checkout_files(struct got_worktree *worktree,
575 struct got_reference *head_ref, struct got_repository *repo,
576 got_worktree_checkout_cb progress_cb, void *progress_arg)
578 const struct got_error *err = NULL, *unlockerr;
579 struct got_object_id *commit_id = NULL;
580 struct got_object *obj = NULL;
581 struct got_commit_object *commit = NULL;
582 struct got_tree_object *tree = NULL;
583 char *fileindex_path = NULL, *new_fileindex_path = NULL;
584 struct got_fileindex *fileindex = NULL;
585 FILE *findex = NULL;
587 err = lock_worktree(worktree, LOCK_EX);
588 if (err)
589 return err;
591 fileindex = got_fileindex_open();
592 if (fileindex == NULL) {
593 err = got_error_from_errno();
594 goto done;
597 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
598 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
599 err = got_error_from_errno();
600 fileindex_path = NULL;
601 goto done;
604 err = got_opentemp_named(&new_fileindex_path, &findex, fileindex_path);
605 if (err)
606 goto done;
608 err = got_ref_resolve(&commit_id, repo, head_ref);
609 if (err)
610 goto done;
612 err = got_object_open(&obj, repo, commit_id);
613 if (err)
614 goto done;
616 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
617 err = got_error(GOT_ERR_OBJ_TYPE);
618 goto done;
621 err = got_object_commit_open(&commit, repo, obj);
622 if (err)
623 goto done;
625 got_object_close(obj);
626 err = got_object_open(&obj, repo, commit->tree_id);
627 if (err)
628 goto done;
630 if (got_object_get_type(obj) != GOT_OBJ_TYPE_TREE) {
631 err = got_error(GOT_ERR_OBJ_TYPE);
632 goto done;
635 err = got_object_tree_open(&tree, repo, obj);
636 if (err)
637 goto done;
639 err = tree_checkout(worktree, fileindex, tree, "/", repo,
640 progress_cb, progress_arg);
641 if (err)
642 goto done;
644 err = got_fileindex_write(fileindex, findex);
645 if (err)
646 goto done;
648 if (rename(new_fileindex_path, fileindex_path) != 0) {
649 err = got_error_from_errno();
650 goto done;
653 free(new_fileindex_path);
654 new_fileindex_path = NULL;
656 done:
657 if (commit)
658 got_object_commit_close(commit);
659 if (obj)
660 got_object_close(obj);
661 free(commit_id);
662 if (new_fileindex_path)
663 unlink(new_fileindex_path);
664 if (findex)
665 fclose(findex);
666 free(new_fileindex_path);
667 free(fileindex_path);
668 got_fileindex_close(fileindex);
669 unlockerr = lock_worktree(worktree, LOCK_SH);
670 if (unlockerr && err == NULL)
671 err = unlockerr;
672 return err;