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_worktree_lib.h"
38 #include "got_path_lib.h"
39 #include "got_sha1_lib.h"
40 #include "got_fileindex_lib.h"
41 #include "got_zbuf_lib.h"
42 #include "got_delta_lib.h"
43 #include "got_object_lib.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(GOT_ERR_NO_MEM);
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(GOT_ERR_NO_MEM);
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(GOT_ERR_NO_MEM);
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;
162 if (!got_path_is_absolute(prefix))
163 return got_error(GOT_ERR_BAD_PATH);
165 /* Create top-level directory (may already exist). */
166 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
167 err = got_error_from_errno();
168 goto done;
171 /* Create .got directory (may already exist). */
172 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
173 err = got_error(GOT_ERR_NO_MEM);
174 goto done;
176 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
177 err = got_error_from_errno();
178 goto done;
181 /* Create an empty lock file. */
182 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
183 if (err)
184 goto done;
186 /* Create an empty file index. */
187 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
188 if (err)
189 goto done;
191 /* Write the HEAD reference. */
192 refstr = got_ref_to_str(head_ref);
193 if (refstr == NULL) {
194 err = got_error(GOT_ERR_NO_MEM);
195 goto done;
197 err = create_meta_file(path_got, GOT_WORKTREE_HEAD, refstr);
198 if (err)
199 goto done;
201 /* Store path to repository. */
202 repo_path = got_repo_get_path(repo);
203 if (repo_path == NULL) {
204 err = got_error(GOT_ERR_NO_MEM);
205 goto done;
207 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY, repo_path);
208 if (err)
209 goto done;
211 /* Store in-repository path prefix. */
212 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX, prefix);
213 if (err)
214 goto done;
216 /* Stamp work tree with format file. */
217 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
218 err = got_error(GOT_ERR_NO_MEM);
219 goto done;
221 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
222 if (err)
223 goto done;
225 done:
226 free(path_got);
227 free(formatstr);
228 free(refstr);
229 free(repo_path);
230 return err;
233 const struct got_error *
234 got_worktree_open(struct got_worktree **worktree, const char *path)
236 const struct got_error *err = NULL;
237 char *path_got;
238 char *refstr = NULL;
239 char *formatstr = NULL;
240 char *path_lock = NULL;
241 int version, fd = -1;
242 const char *errstr;
244 *worktree = NULL;
246 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
247 err = got_error(GOT_ERR_NO_MEM);
248 path_got = NULL;
249 goto done;
252 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
253 err = got_error(GOT_ERR_NO_MEM);
254 path_lock = NULL;
255 goto done;
258 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
259 if (fd == -1) {
260 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
261 : got_error_from_errno());
262 goto done;
265 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
266 if (err)
267 goto done;
269 version = strtonum(formatstr, 1, INT_MAX, &errstr);
270 if (errstr) {
271 err = got_error(GOT_ERR_WORKTREE_META);
272 goto done;
274 if (version != GOT_WORKTREE_FORMAT_VERSION) {
275 err = got_error(GOT_ERR_WORKTREE_VERS);
276 goto done;
279 *worktree = calloc(1, sizeof(**worktree));
280 if (*worktree == NULL) {
281 err = got_error(GOT_ERR_NO_MEM);
282 goto done;
284 (*worktree)->lockfd = -1;
286 (*worktree)->root_path = strdup(path);
287 if ((*worktree)->root_path == NULL) {
288 err = got_error(GOT_ERR_NO_MEM);
289 goto done;
291 err = read_meta_file(&(*worktree)->repo_path, path_got,
292 GOT_WORKTREE_REPOSITORY);
293 if (err)
294 goto done;
295 err = read_meta_file(&(*worktree)->path_prefix, path_got,
296 GOT_WORKTREE_PATH_PREFIX);
297 if (err)
298 goto done;
300 err = read_meta_file(&(*worktree)->head_ref, path_got,
301 GOT_WORKTREE_HEAD);
302 if (err)
303 goto done;
305 done:
306 free(path_got);
307 free(path_lock);
308 if (err) {
309 if (fd != -1)
310 close(fd);
311 if (*worktree != NULL)
312 got_worktree_close(*worktree);
313 *worktree = NULL;
314 } else
315 (*worktree)->lockfd = fd;
317 return err;
320 void
321 got_worktree_close(struct got_worktree *worktree)
323 free(worktree->root_path);
324 free(worktree->repo_path);
325 free(worktree->path_prefix);
326 free(worktree->base_commit);
327 free(worktree->head_ref);
328 if (worktree->lockfd != -1)
329 close(worktree->lockfd);
330 free(worktree);
333 char *
334 got_worktree_get_repo_path(struct got_worktree *worktree)
336 return strdup(worktree->repo_path);
339 char *
340 got_worktree_get_head_ref_name(struct got_worktree *worktree)
342 return strdup(worktree->head_ref);
345 static const struct got_error *
346 lock_worktree(struct got_worktree *worktree, int operation)
348 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
349 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
350 : got_error_from_errno());
351 return NULL;
354 static const char *
355 apply_path_prefix(struct got_worktree *worktree, const char *path)
357 const char *p = path;
358 p += strlen(worktree->path_prefix);
359 if (*p == '/')
360 p++;
361 return p;
364 static const struct got_error *
365 add_file_on_disk(struct got_worktree *worktree, struct got_fileindex *fileindex,
366 const char *path, struct got_blob_object *blob, struct got_repository *repo)
368 const struct got_error *err = NULL;
369 char *ondisk_path;
370 int fd;
371 size_t len, hdrlen;
372 struct got_fileindex_entry *entry;
374 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path,
375 apply_path_prefix(worktree, path)) == -1)
376 return got_error(GOT_ERR_NO_MEM);
378 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
379 GOT_DEFAULT_FILE_MODE);
380 if (fd == -1) {
381 err = got_error_from_errno();
382 if (errno == EEXIST) {
383 struct stat sb;
384 if (lstat(ondisk_path, &sb) == -1) {
385 err = got_error_from_errno();
386 } else if (!S_ISREG(sb.st_mode)) {
387 /* TODO file is obstructed; do something */
388 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
391 return err;
394 hdrlen = got_object_blob_get_hdrlen(blob);
395 do {
396 const uint8_t *buf = got_object_blob_get_read_buf(blob);
397 err = got_object_blob_read_block(&len, blob);
398 if (err)
399 break;
400 if (len > 0) {
401 /* Skip blob object header first time around. */
402 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
403 hdrlen = 0;
404 if (outlen == -1) {
405 err = got_error_from_errno();
406 break;
407 } else if (outlen != len) {
408 err = got_error(GOT_ERR_IO);
409 break;
412 } while (len != 0);
414 fsync(fd);
416 err = got_fileindex_entry_open(&entry, ondisk_path,
417 apply_path_prefix(worktree, path), blob->id.sha1);
418 if (err)
419 goto done;
421 err = got_fileindex_entry_add(fileindex, entry);
422 if (err)
423 goto done;
424 done:
425 close(fd);
426 free(ondisk_path);
427 return err;
430 static const struct got_error *
431 add_dir_on_disk(struct got_worktree *worktree, const char *path)
433 const struct got_error *err = NULL;
434 char *abspath;
435 size_t len;
437 if (asprintf(&abspath, "%s/%s", worktree->root_path,
438 apply_path_prefix(worktree, path)) == -1)
439 return got_error(GOT_ERR_NO_MEM);
441 /* XXX queue work rather than editing disk directly? */
442 if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1) {
443 struct stat sb;
445 if (errno != EEXIST) {
446 err = got_error_from_errno();
447 goto done;
450 if (lstat(abspath, &sb) == -1) {
451 err = got_error_from_errno();
452 goto done;
455 if (!S_ISDIR(sb.st_mode)) {
456 /* TODO directory is obstructed; do something */
457 return got_error(GOT_ERR_FILE_OBSTRUCTED);
461 done:
462 free(abspath);
463 return err;
466 static const struct got_error *
467 tree_checkout(struct got_worktree *, struct got_fileindex *,
468 struct got_tree_object *, const char *, struct got_repository *);
470 static const struct got_error *
471 tree_checkout_entry(struct got_worktree *worktree,
472 struct got_fileindex *fileindex, struct got_tree_entry *te,
473 const char *parent, struct got_repository *repo)
475 const struct got_error *err = NULL;
476 struct got_object *obj = NULL;
477 struct got_blob_object *blob = NULL;
478 struct got_tree_object *tree = NULL;
479 char *path = NULL;
480 size_t len;
482 if (parent[0] == '/' && parent[1] == '\0')
483 parent = "";
484 if (asprintf(&path, "%s/%s", parent, te->name) == -1)
485 return got_error(GOT_ERR_NO_MEM);
487 /* Skip this entry if it is outside of our path prefix. */
488 len = MIN(strlen(worktree->path_prefix), strlen(path));
489 if (strncmp(path, worktree->path_prefix, len) != 0) {
490 free(path);
491 return NULL;
494 err = got_object_open(&obj, repo, te->id);
495 if (err)
496 goto done;
498 switch (got_object_get_type(obj)) {
499 case GOT_OBJ_TYPE_BLOB:
500 if (strlen(worktree->path_prefix) >= strlen(path))
501 break;
502 err = got_object_blob_open(&blob, repo, obj, 8192);
503 if (err)
504 goto done;
505 err = add_file_on_disk(worktree, fileindex, path, blob, repo);
506 break;
507 case GOT_OBJ_TYPE_TREE:
508 err = got_object_tree_open(&tree, repo, obj);
509 if (err)
510 goto done;
511 if (strlen(worktree->path_prefix) < strlen(path)) {
512 err = add_dir_on_disk(worktree, path);
513 if (err)
514 break;
516 err = tree_checkout(worktree, fileindex, tree, path, repo);
517 break;
518 default:
519 break;
522 done:
523 if (blob)
524 got_object_blob_close(blob);
525 if (tree)
526 got_object_tree_close(tree);
527 if (obj)
528 got_object_close(obj);
529 free(path);
530 return err;
533 static const struct got_error *
534 tree_checkout(struct got_worktree *worktree,
535 struct got_fileindex *fileindex, struct got_tree_object *tree,
536 const char *path, struct got_repository *repo)
538 const struct got_error *err = NULL;
539 struct got_tree_entry *te;
540 size_t len;
542 /* Skip this tree 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 return NULL;
547 SIMPLEQ_FOREACH(te, &tree->entries, entry) {
548 err = tree_checkout_entry(worktree, fileindex, te, path, repo);
549 if (err)
550 break;
553 return err;
556 const struct got_error *
557 got_worktree_checkout_files(struct got_worktree *worktree,
558 struct got_reference *head_ref, struct got_repository *repo)
560 const struct got_error *err = NULL, *unlockerr;
561 struct got_object_id *commit_id = NULL;
562 struct got_object *obj = NULL;
563 struct got_commit_object *commit = NULL;
564 struct got_tree_object *tree = NULL;
565 char *fileindex_path = NULL, *new_fileindex_path = NULL;
566 struct got_fileindex *fileindex = NULL;
567 FILE *findex = NULL;
569 err = lock_worktree(worktree, LOCK_EX);
570 if (err)
571 return err;
573 fileindex = got_fileindex_open();
574 if (fileindex == NULL) {
575 err = got_error(GOT_ERR_NO_MEM);
576 goto done;
579 err = got_opentemp_named(&new_fileindex_path, &findex, fileindex_path);
580 if (err)
581 goto done;
584 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
585 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
586 err = got_error(GOT_ERR_NO_MEM);
587 fileindex_path = NULL;
588 goto done;
591 err = got_ref_resolve(&commit_id, repo, head_ref);
592 if (err)
593 goto done;
595 err = got_object_open(&obj, repo, commit_id);
596 if (err)
597 goto done;
599 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
600 err = got_error(GOT_ERR_OBJ_TYPE);
601 goto done;
604 err = got_object_commit_open(&commit, repo, obj);
605 if (err)
606 goto done;
608 got_object_close(obj);
609 err = got_object_open(&obj, repo, commit->tree_id);
610 if (err)
611 goto done;
613 if (got_object_get_type(obj) != GOT_OBJ_TYPE_TREE) {
614 err = got_error(GOT_ERR_OBJ_TYPE);
615 goto done;
618 err = got_object_tree_open(&tree, repo, obj);
619 if (err)
620 goto done;
622 err = tree_checkout(worktree, fileindex, tree, "/", repo);
623 if (err)
624 goto done;
626 err = got_fileindex_write(fileindex, findex);
627 if (err)
628 goto done;
630 if (rename(new_fileindex_path, fileindex_path) != 0) {
631 err = got_error_from_errno();
632 goto done;
635 free(new_fileindex_path);
636 new_fileindex_path = NULL;
638 done:
639 if (commit)
640 got_object_commit_close(commit);
641 if (obj)
642 got_object_close(obj);
643 free(commit_id);
644 if (new_fileindex_path)
645 unlink(new_fileindex_path);
646 if (findex)
647 fclose(findex);
648 free(new_fileindex_path);
649 free(fileindex_path);
650 got_fileindex_close(fileindex);
651 unlockerr = lock_worktree(worktree, LOCK_SH);
652 if (unlockerr && err == NULL)
653 err = unlockerr;
654 return err;