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_refs.h"
34 #include "got_object.h"
35 #include "got_worktree.h"
37 #include "got_lib_worktree.h"
38 #include "got_lib_path.h"
39 #include "got_lib_sha1.h"
40 #include "got_lib_fileindex.h"
41 #include "got_lib_zbuf.h"
42 #include "got_lib_delta.h"
43 #include "got_lib_object.h"
45 #ifndef MIN
46 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
47 #endif
49 static const struct got_error *
50 create_meta_file(const char *path_got, const char *name, const char *content)
51 {
52 const struct got_error *err = NULL;
53 char *path;
54 int fd = -1;
55 char buf[4];
56 ssize_t n;
58 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
59 err = got_error_from_errno();
60 path = NULL;
61 goto done;
62 }
64 fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
65 GOT_DEFAULT_FILE_MODE);
66 if (fd == -1) {
67 err = got_error_from_errno();
68 goto done;
69 }
71 /* The file should be empty. */
72 n = read(fd, buf, sizeof(buf));
73 if (n != 0) {
74 err = (n == -1 ? got_error_from_errno() :
75 got_error(GOT_ERR_WORKTREE_EXISTS));
76 goto done;
77 }
79 if (content) {
80 int len = dprintf(fd, "%s\n", content);
81 if (len != strlen(content) + 1) {
82 err = got_error_from_errno();
83 goto done;
84 }
85 }
87 done:
88 if (fd != -1 && close(fd) == -1 && err == NULL)
89 err = got_error_from_errno();
90 free(path);
91 return err;
92 }
94 static const struct got_error *
95 read_meta_file(char **content, const char *path_got, const char *name)
96 {
97 const struct got_error *err = NULL;
98 char *path;
99 int fd = -1;
100 ssize_t n;
101 struct stat sb;
103 *content = NULL;
105 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
106 err = got_error_from_errno();
107 path = NULL;
108 goto done;
111 fd = open(path, O_RDONLY | O_NOFOLLOW);
112 if (fd == -1) {
113 err = got_error_from_errno();
114 goto done;
116 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
117 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
118 : got_error_from_errno());
119 goto done;
122 stat(path, &sb);
123 *content = calloc(1, sb.st_size);
124 if (*content == NULL) {
125 err = got_error_from_errno();
126 goto done;
129 n = read(fd, *content, sb.st_size);
130 if (n != sb.st_size) {
131 err = (n == -1 ? got_error_from_errno() :
132 got_error(GOT_ERR_WORKTREE_META));
133 goto done;
135 if ((*content)[sb.st_size - 1] != '\n') {
136 err = got_error(GOT_ERR_WORKTREE_META);
137 goto done;
139 (*content)[sb.st_size - 1] = '\0';
141 done:
142 if (fd != -1 && close(fd) == -1 && err == NULL)
143 err = got_error_from_errno();
144 free(path);
145 if (err) {
146 free(*content);
147 *content = NULL;
149 return err;
152 const struct got_error *
153 got_worktree_init(const char *path, struct got_reference *head_ref,
154 const char *prefix, struct got_repository *repo)
156 const struct got_error *err = NULL;
157 char *path_got = NULL;
158 char *refstr = NULL;
159 char *repo_path = NULL;
160 char *formatstr = NULL;
161 char *absprefix = NULL;
163 if (!got_path_is_absolute(prefix)) {
164 if (asprintf(&absprefix, "/%s", prefix) == -1)
165 return got_error_from_errno();
168 /* Create top-level directory (may already exist). */
169 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
170 err = got_error_from_errno();
171 goto done;
174 /* Create .got directory (may already exist). */
175 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
176 err = got_error_from_errno();
177 goto done;
179 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
180 err = got_error_from_errno();
181 goto done;
184 /* Create an empty lock file. */
185 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
186 if (err)
187 goto done;
189 /* Create an empty file index. */
190 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
191 if (err)
192 goto done;
194 /* Write the HEAD reference. */
195 refstr = got_ref_to_str(head_ref);
196 if (refstr == NULL) {
197 err = got_error_from_errno();
198 goto done;
200 err = create_meta_file(path_got, GOT_WORKTREE_HEAD, refstr);
201 if (err)
202 goto done;
204 /* Store path to repository. */
205 repo_path = got_repo_get_path(repo);
206 if (repo_path == NULL) {
207 err = got_error_from_errno();
208 goto done;
210 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY, repo_path);
211 if (err)
212 goto done;
214 /* Store in-repository path prefix. */
215 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
216 absprefix ? absprefix : prefix);
217 if (err)
218 goto done;
220 /* Stamp work tree with format file. */
221 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
222 err = got_error_from_errno();
223 goto done;
225 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
226 if (err)
227 goto done;
229 done:
230 free(path_got);
231 free(formatstr);
232 free(refstr);
233 free(repo_path);
234 free(absprefix);
235 return err;
238 const struct got_error *
239 got_worktree_open(struct got_worktree **worktree, const char *path)
241 const struct got_error *err = NULL;
242 char *path_got;
243 char *formatstr = NULL;
244 char *path_lock = NULL;
245 int version, fd = -1;
246 const char *errstr;
248 *worktree = NULL;
250 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
251 err = got_error_from_errno();
252 path_got = NULL;
253 goto done;
256 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
257 err = got_error_from_errno();
258 path_lock = NULL;
259 goto done;
262 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
263 if (fd == -1) {
264 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
265 : got_error_from_errno());
266 goto done;
269 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
270 if (err)
271 goto done;
273 version = strtonum(formatstr, 1, INT_MAX, &errstr);
274 if (errstr) {
275 err = got_error(GOT_ERR_WORKTREE_META);
276 goto done;
278 if (version != GOT_WORKTREE_FORMAT_VERSION) {
279 err = got_error(GOT_ERR_WORKTREE_VERS);
280 goto done;
283 *worktree = calloc(1, sizeof(**worktree));
284 if (*worktree == NULL) {
285 err = got_error_from_errno();
286 goto done;
288 (*worktree)->lockfd = -1;
290 (*worktree)->root_path = strdup(path);
291 if ((*worktree)->root_path == NULL) {
292 err = got_error_from_errno();
293 goto done;
295 err = read_meta_file(&(*worktree)->repo_path, path_got,
296 GOT_WORKTREE_REPOSITORY);
297 if (err)
298 goto done;
299 err = read_meta_file(&(*worktree)->path_prefix, path_got,
300 GOT_WORKTREE_PATH_PREFIX);
301 if (err)
302 goto done;
304 err = read_meta_file(&(*worktree)->head_ref, path_got,
305 GOT_WORKTREE_HEAD);
306 if (err)
307 goto done;
309 done:
310 free(path_got);
311 free(path_lock);
312 if (err) {
313 if (fd != -1)
314 close(fd);
315 if (*worktree != NULL)
316 got_worktree_close(*worktree);
317 *worktree = NULL;
318 } else
319 (*worktree)->lockfd = fd;
321 return err;
324 void
325 got_worktree_close(struct got_worktree *worktree)
327 free(worktree->root_path);
328 free(worktree->repo_path);
329 free(worktree->path_prefix);
330 free(worktree->base_commit);
331 free(worktree->head_ref);
332 if (worktree->lockfd != -1)
333 close(worktree->lockfd);
334 free(worktree);
337 char *
338 got_worktree_get_repo_path(struct got_worktree *worktree)
340 return strdup(worktree->repo_path);
343 char *
344 got_worktree_get_head_ref_name(struct got_worktree *worktree)
346 return strdup(worktree->head_ref);
349 static const struct got_error *
350 lock_worktree(struct got_worktree *worktree, int operation)
352 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
353 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
354 : got_error_from_errno());
355 return NULL;
358 static const char *
359 apply_path_prefix(struct got_worktree *worktree, const char *path)
361 const char *p = path;
362 p += strlen(worktree->path_prefix);
363 if (*p == '/')
364 p++;
365 return p;
368 static const struct got_error *
369 add_file_on_disk(struct got_worktree *worktree, struct got_fileindex *fileindex,
370 const char *path, struct got_blob_object *blob, struct got_repository *repo)
372 const struct got_error *err = NULL;
373 char *ondisk_path;
374 int fd;
375 size_t len, hdrlen;
376 struct got_fileindex_entry *entry;
378 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path,
379 apply_path_prefix(worktree, path)) == -1)
380 return got_error_from_errno();
382 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
383 GOT_DEFAULT_FILE_MODE);
384 if (fd == -1) {
385 err = got_error_from_errno();
386 if (errno == EEXIST) {
387 struct stat sb;
388 if (lstat(ondisk_path, &sb) == -1) {
389 err = got_error_from_errno();
390 } else if (!S_ISREG(sb.st_mode)) {
391 /* TODO file is obstructed; do something */
392 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
395 return err;
398 hdrlen = got_object_blob_get_hdrlen(blob);
399 do {
400 const uint8_t *buf = got_object_blob_get_read_buf(blob);
401 err = got_object_blob_read_block(&len, blob);
402 if (err)
403 break;
404 if (len > 0) {
405 /* Skip blob object header first time around. */
406 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
407 hdrlen = 0;
408 if (outlen == -1) {
409 err = got_error_from_errno();
410 break;
411 } else if (outlen != len) {
412 err = got_error(GOT_ERR_IO);
413 break;
416 } while (len != 0);
418 fsync(fd);
420 err = got_fileindex_entry_open(&entry, ondisk_path,
421 apply_path_prefix(worktree, path), blob->id.sha1);
422 if (err)
423 goto done;
425 err = got_fileindex_entry_add(fileindex, entry);
426 if (err)
427 goto done;
428 done:
429 close(fd);
430 free(ondisk_path);
431 return err;
434 static const struct got_error *
435 add_dir_on_disk(struct got_worktree *worktree, const char *path)
437 const struct got_error *err = NULL;
438 char *abspath;
440 if (asprintf(&abspath, "%s/%s", worktree->root_path,
441 apply_path_prefix(worktree, path)) == -1)
442 return got_error_from_errno();
444 /* XXX queue work rather than editing disk directly? */
445 if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1) {
446 struct stat sb;
448 if (errno != EEXIST) {
449 err = got_error_from_errno();
450 goto done;
453 if (lstat(abspath, &sb) == -1) {
454 err = got_error_from_errno();
455 goto done;
458 if (!S_ISDIR(sb.st_mode)) {
459 /* TODO directory is obstructed; do something */
460 return got_error(GOT_ERR_FILE_OBSTRUCTED);
464 done:
465 free(abspath);
466 return err;
469 static const struct got_error *
470 tree_checkout(struct got_worktree *, struct got_fileindex *,
471 struct got_tree_object *, const char *, struct got_repository *,
472 got_worktree_checkout_cb progress_cb, void *progress_arg);
474 static const struct got_error *
475 tree_checkout_entry(struct got_worktree *worktree,
476 struct got_fileindex *fileindex, struct got_tree_entry *te,
477 const char *parent, struct got_repository *repo,
478 got_worktree_checkout_cb progress_cb, void *progress_arg)
480 const struct got_error *err = NULL;
481 struct got_object *obj = NULL;
482 struct got_blob_object *blob = NULL;
483 struct got_tree_object *tree = NULL;
484 char *path = NULL;
485 size_t len;
487 if (parent[0] == '/' && parent[1] == '\0')
488 parent = "";
489 if (asprintf(&path, "%s/%s", parent, te->name) == -1)
490 return got_error_from_errno();
492 /* Skip this entry if it is outside of our path prefix. */
493 len = MIN(strlen(worktree->path_prefix), strlen(path));
494 if (strncmp(path, worktree->path_prefix, len) != 0) {
495 free(path);
496 return NULL;
499 err = got_object_open(&obj, repo, te->id);
500 if (err)
501 goto done;
503 (*progress_cb)(progress_arg, path);
505 switch (got_object_get_type(obj)) {
506 case GOT_OBJ_TYPE_BLOB:
507 if (strlen(worktree->path_prefix) >= strlen(path))
508 break;
509 err = got_object_blob_open(&blob, repo, obj, 8192);
510 if (err)
511 goto done;
512 err = add_file_on_disk(worktree, fileindex, path, blob, repo);
513 break;
514 case GOT_OBJ_TYPE_TREE:
515 err = got_object_tree_open(&tree, repo, obj);
516 if (err)
517 goto done;
518 if (strlen(worktree->path_prefix) < strlen(path)) {
519 err = add_dir_on_disk(worktree, path);
520 if (err)
521 break;
523 err = tree_checkout(worktree, fileindex, tree, path, repo,
524 progress_cb, progress_arg);
525 break;
526 default:
527 break;
530 done:
531 if (blob)
532 got_object_blob_close(blob);
533 if (tree)
534 got_object_tree_close(tree);
535 if (obj)
536 got_object_close(obj);
537 free(path);
538 return err;
541 static const struct got_error *
542 tree_checkout(struct got_worktree *worktree,
543 struct got_fileindex *fileindex, struct got_tree_object *tree,
544 const char *path, struct got_repository *repo,
545 got_worktree_checkout_cb progress_cb, void *progress_arg)
547 const struct got_error *err = NULL;
548 struct got_tree_entry *te;
549 size_t len;
551 /* Skip this tree if it is outside of our path prefix. */
552 len = MIN(strlen(worktree->path_prefix), strlen(path));
553 if (strncmp(path, worktree->path_prefix, len) != 0)
554 return NULL;
556 SIMPLEQ_FOREACH(te, &tree->entries, entry) {
557 err = tree_checkout_entry(worktree, fileindex, te, path, repo,
558 progress_cb, progress_arg);
559 if (err)
560 break;
563 return err;
566 const struct got_error *
567 got_worktree_checkout_files(struct got_worktree *worktree,
568 struct got_reference *head_ref, struct got_repository *repo,
569 got_worktree_checkout_cb progress_cb, void *progress_arg)
571 const struct got_error *err = NULL, *unlockerr;
572 struct got_object_id *commit_id = NULL;
573 struct got_object *obj = NULL;
574 struct got_commit_object *commit = NULL;
575 struct got_tree_object *tree = NULL;
576 char *fileindex_path = NULL, *new_fileindex_path = NULL;
577 struct got_fileindex *fileindex = NULL;
578 FILE *findex = NULL;
580 err = lock_worktree(worktree, LOCK_EX);
581 if (err)
582 return err;
584 fileindex = got_fileindex_open();
585 if (fileindex == NULL) {
586 err = got_error_from_errno();
587 goto done;
590 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
591 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
592 err = got_error_from_errno();
593 fileindex_path = NULL;
594 goto done;
597 err = got_opentemp_named(&new_fileindex_path, &findex, fileindex_path);
598 if (err)
599 goto done;
601 err = got_ref_resolve(&commit_id, repo, head_ref);
602 if (err)
603 goto done;
605 err = got_object_open(&obj, repo, commit_id);
606 if (err)
607 goto done;
609 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
610 err = got_error(GOT_ERR_OBJ_TYPE);
611 goto done;
614 err = got_object_commit_open(&commit, repo, obj);
615 if (err)
616 goto done;
618 got_object_close(obj);
619 err = got_object_open(&obj, repo, commit->tree_id);
620 if (err)
621 goto done;
623 if (got_object_get_type(obj) != GOT_OBJ_TYPE_TREE) {
624 err = got_error(GOT_ERR_OBJ_TYPE);
625 goto done;
628 err = got_object_tree_open(&tree, repo, obj);
629 if (err)
630 goto done;
632 err = tree_checkout(worktree, fileindex, tree, "/", repo,
633 progress_cb, progress_arg);
634 if (err)
635 goto done;
637 err = got_fileindex_write(fileindex, findex);
638 if (err)
639 goto done;
641 if (rename(new_fileindex_path, fileindex_path) != 0) {
642 err = got_error_from_errno();
643 goto done;
646 free(new_fileindex_path);
647 new_fileindex_path = NULL;
649 done:
650 if (commit)
651 got_object_commit_close(commit);
652 if (obj)
653 got_object_close(obj);
654 free(commit_id);
655 if (new_fileindex_path)
656 unlink(new_fileindex_path);
657 if (findex)
658 fclose(findex);
659 free(new_fileindex_path);
660 free(fileindex_path);
661 got_fileindex_close(fileindex);
662 unlockerr = lock_worktree(worktree, LOCK_SH);
663 if (unlockerr && err == NULL)
664 err = unlockerr;
665 return err;