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 *formatstr = NULL;
239 char *path_lock = NULL;
240 int version, fd = -1;
241 const char *errstr;
243 *worktree = NULL;
245 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
246 err = got_error(GOT_ERR_NO_MEM);
247 path_got = NULL;
248 goto done;
251 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
252 err = got_error(GOT_ERR_NO_MEM);
253 path_lock = NULL;
254 goto done;
257 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
258 if (fd == -1) {
259 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
260 : got_error_from_errno());
261 goto done;
264 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
265 if (err)
266 goto done;
268 version = strtonum(formatstr, 1, INT_MAX, &errstr);
269 if (errstr) {
270 err = got_error(GOT_ERR_WORKTREE_META);
271 goto done;
273 if (version != GOT_WORKTREE_FORMAT_VERSION) {
274 err = got_error(GOT_ERR_WORKTREE_VERS);
275 goto done;
278 *worktree = calloc(1, sizeof(**worktree));
279 if (*worktree == NULL) {
280 err = got_error(GOT_ERR_NO_MEM);
281 goto done;
283 (*worktree)->lockfd = -1;
285 (*worktree)->root_path = strdup(path);
286 if ((*worktree)->root_path == NULL) {
287 err = got_error(GOT_ERR_NO_MEM);
288 goto done;
290 err = read_meta_file(&(*worktree)->repo_path, path_got,
291 GOT_WORKTREE_REPOSITORY);
292 if (err)
293 goto done;
294 err = read_meta_file(&(*worktree)->path_prefix, path_got,
295 GOT_WORKTREE_PATH_PREFIX);
296 if (err)
297 goto done;
299 err = read_meta_file(&(*worktree)->head_ref, path_got,
300 GOT_WORKTREE_HEAD);
301 if (err)
302 goto done;
304 done:
305 free(path_got);
306 free(path_lock);
307 if (err) {
308 if (fd != -1)
309 close(fd);
310 if (*worktree != NULL)
311 got_worktree_close(*worktree);
312 *worktree = NULL;
313 } else
314 (*worktree)->lockfd = fd;
316 return err;
319 void
320 got_worktree_close(struct got_worktree *worktree)
322 free(worktree->root_path);
323 free(worktree->repo_path);
324 free(worktree->path_prefix);
325 free(worktree->base_commit);
326 free(worktree->head_ref);
327 if (worktree->lockfd != -1)
328 close(worktree->lockfd);
329 free(worktree);
332 char *
333 got_worktree_get_repo_path(struct got_worktree *worktree)
335 return strdup(worktree->repo_path);
338 char *
339 got_worktree_get_head_ref_name(struct got_worktree *worktree)
341 return strdup(worktree->head_ref);
344 static const struct got_error *
345 lock_worktree(struct got_worktree *worktree, int operation)
347 if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
348 return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
349 : got_error_from_errno());
350 return NULL;
353 static const char *
354 apply_path_prefix(struct got_worktree *worktree, const char *path)
356 const char *p = path;
357 p += strlen(worktree->path_prefix);
358 if (*p == '/')
359 p++;
360 return p;
363 static const struct got_error *
364 add_file_on_disk(struct got_worktree *worktree, struct got_fileindex *fileindex,
365 const char *path, struct got_blob_object *blob, struct got_repository *repo)
367 const struct got_error *err = NULL;
368 char *ondisk_path;
369 int fd;
370 size_t len, hdrlen;
371 struct got_fileindex_entry *entry;
373 if (asprintf(&ondisk_path, "%s/%s", worktree->root_path,
374 apply_path_prefix(worktree, path)) == -1)
375 return got_error(GOT_ERR_NO_MEM);
377 fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
378 GOT_DEFAULT_FILE_MODE);
379 if (fd == -1) {
380 err = got_error_from_errno();
381 if (errno == EEXIST) {
382 struct stat sb;
383 if (lstat(ondisk_path, &sb) == -1) {
384 err = got_error_from_errno();
385 } else if (!S_ISREG(sb.st_mode)) {
386 /* TODO file is obstructed; do something */
387 err = got_error(GOT_ERR_FILE_OBSTRUCTED);
390 return err;
393 hdrlen = got_object_blob_get_hdrlen(blob);
394 do {
395 const uint8_t *buf = got_object_blob_get_read_buf(blob);
396 err = got_object_blob_read_block(&len, blob);
397 if (err)
398 break;
399 if (len > 0) {
400 /* Skip blob object header first time around. */
401 ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
402 hdrlen = 0;
403 if (outlen == -1) {
404 err = got_error_from_errno();
405 break;
406 } else if (outlen != len) {
407 err = got_error(GOT_ERR_IO);
408 break;
411 } while (len != 0);
413 fsync(fd);
415 err = got_fileindex_entry_open(&entry, ondisk_path,
416 apply_path_prefix(worktree, path), blob->id.sha1);
417 if (err)
418 goto done;
420 err = got_fileindex_entry_add(fileindex, entry);
421 if (err)
422 goto done;
423 done:
424 close(fd);
425 free(ondisk_path);
426 return err;
429 static const struct got_error *
430 add_dir_on_disk(struct got_worktree *worktree, const char *path)
432 const struct got_error *err = NULL;
433 char *abspath;
435 if (asprintf(&abspath, "%s/%s", worktree->root_path,
436 apply_path_prefix(worktree, path)) == -1)
437 return got_error(GOT_ERR_NO_MEM);
439 /* XXX queue work rather than editing disk directly? */
440 if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1) {
441 struct stat sb;
443 if (errno != EEXIST) {
444 err = got_error_from_errno();
445 goto done;
448 if (lstat(abspath, &sb) == -1) {
449 err = got_error_from_errno();
450 goto done;
453 if (!S_ISDIR(sb.st_mode)) {
454 /* TODO directory is obstructed; do something */
455 return got_error(GOT_ERR_FILE_OBSTRUCTED);
459 done:
460 free(abspath);
461 return err;
464 static const struct got_error *
465 tree_checkout(struct got_worktree *, struct got_fileindex *,
466 struct got_tree_object *, const char *, struct got_repository *);
468 static const struct got_error *
469 tree_checkout_entry(struct got_worktree *worktree,
470 struct got_fileindex *fileindex, struct got_tree_entry *te,
471 const char *parent, struct got_repository *repo)
473 const struct got_error *err = NULL;
474 struct got_object *obj = NULL;
475 struct got_blob_object *blob = NULL;
476 struct got_tree_object *tree = NULL;
477 char *path = NULL;
478 size_t len;
480 if (parent[0] == '/' && parent[1] == '\0')
481 parent = "";
482 if (asprintf(&path, "%s/%s", parent, te->name) == -1)
483 return got_error(GOT_ERR_NO_MEM);
485 /* Skip this entry if it is outside of our path prefix. */
486 len = MIN(strlen(worktree->path_prefix), strlen(path));
487 if (strncmp(path, worktree->path_prefix, len) != 0) {
488 free(path);
489 return NULL;
492 err = got_object_open(&obj, repo, te->id);
493 if (err)
494 goto done;
496 switch (got_object_get_type(obj)) {
497 case GOT_OBJ_TYPE_BLOB:
498 if (strlen(worktree->path_prefix) >= strlen(path))
499 break;
500 err = got_object_blob_open(&blob, repo, obj, 8192);
501 if (err)
502 goto done;
503 err = add_file_on_disk(worktree, fileindex, path, blob, repo);
504 break;
505 case GOT_OBJ_TYPE_TREE:
506 err = got_object_tree_open(&tree, repo, obj);
507 if (err)
508 goto done;
509 if (strlen(worktree->path_prefix) < strlen(path)) {
510 err = add_dir_on_disk(worktree, path);
511 if (err)
512 break;
514 err = tree_checkout(worktree, fileindex, tree, path, repo);
515 break;
516 default:
517 break;
520 done:
521 if (blob)
522 got_object_blob_close(blob);
523 if (tree)
524 got_object_tree_close(tree);
525 if (obj)
526 got_object_close(obj);
527 free(path);
528 return err;
531 static const struct got_error *
532 tree_checkout(struct got_worktree *worktree,
533 struct got_fileindex *fileindex, struct got_tree_object *tree,
534 const char *path, struct got_repository *repo)
536 const struct got_error *err = NULL;
537 struct got_tree_entry *te;
538 size_t len;
540 /* Skip this tree if it is outside of our path prefix. */
541 len = MIN(strlen(worktree->path_prefix), strlen(path));
542 if (strncmp(path, worktree->path_prefix, len) != 0)
543 return NULL;
545 SIMPLEQ_FOREACH(te, &tree->entries, entry) {
546 err = tree_checkout_entry(worktree, fileindex, te, path, repo);
547 if (err)
548 break;
551 return err;
554 const struct got_error *
555 got_worktree_checkout_files(struct got_worktree *worktree,
556 struct got_reference *head_ref, struct got_repository *repo)
558 const struct got_error *err = NULL, *unlockerr;
559 struct got_object_id *commit_id = NULL;
560 struct got_object *obj = NULL;
561 struct got_commit_object *commit = NULL;
562 struct got_tree_object *tree = NULL;
563 char *fileindex_path = NULL, *new_fileindex_path = NULL;
564 struct got_fileindex *fileindex = NULL;
565 FILE *findex = NULL;
567 err = lock_worktree(worktree, LOCK_EX);
568 if (err)
569 return err;
571 fileindex = got_fileindex_open();
572 if (fileindex == NULL) {
573 err = got_error(GOT_ERR_NO_MEM);
574 goto done;
577 if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
578 GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
579 err = got_error(GOT_ERR_NO_MEM);
580 fileindex_path = NULL;
581 goto done;
584 err = got_opentemp_named(&new_fileindex_path, &findex, fileindex_path);
585 if (err)
586 goto done;
588 err = got_ref_resolve(&commit_id, repo, head_ref);
589 if (err)
590 goto done;
592 err = got_object_open(&obj, repo, commit_id);
593 if (err)
594 goto done;
596 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
597 err = got_error(GOT_ERR_OBJ_TYPE);
598 goto done;
601 err = got_object_commit_open(&commit, repo, obj);
602 if (err)
603 goto done;
605 got_object_close(obj);
606 err = got_object_open(&obj, repo, commit->tree_id);
607 if (err)
608 goto done;
610 if (got_object_get_type(obj) != GOT_OBJ_TYPE_TREE) {
611 err = got_error(GOT_ERR_OBJ_TYPE);
612 goto done;
615 err = got_object_tree_open(&tree, repo, obj);
616 if (err)
617 goto done;
619 err = tree_checkout(worktree, fileindex, tree, "/", repo);
620 if (err)
621 goto done;
623 err = got_fileindex_write(fileindex, findex);
624 if (err)
625 goto done;
627 if (rename(new_fileindex_path, fileindex_path) != 0) {
628 err = got_error_from_errno();
629 goto done;
632 free(new_fileindex_path);
633 new_fileindex_path = NULL;
635 done:
636 if (commit)
637 got_object_commit_close(commit);
638 if (obj)
639 got_object_close(obj);
640 free(commit_id);
641 if (new_fileindex_path)
642 unlink(new_fileindex_path);
643 if (findex)
644 fclose(findex);
645 free(new_fileindex_path);
646 free(fileindex_path);
647 got_fileindex_close(fileindex);
648 unlockerr = lock_worktree(worktree, LOCK_SH);
649 if (unlockerr && err == NULL)
650 err = unlockerr;
651 return err;