2 86c3caaf 2018-03-09 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
4 86c3caaf 2018-03-09 stsp * Permission to use, copy, modify, and distribute this software for any
5 86c3caaf 2018-03-09 stsp * purpose with or without fee is hereby granted, provided that the above
6 86c3caaf 2018-03-09 stsp * copyright notice and this permission notice appear in all copies.
8 86c3caaf 2018-03-09 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 86c3caaf 2018-03-09 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 86c3caaf 2018-03-09 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 86c3caaf 2018-03-09 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 86c3caaf 2018-03-09 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 86c3caaf 2018-03-09 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 86c3caaf 2018-03-09 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 86c3caaf 2018-03-09 stsp #include <sys/stat.h>
18 6d9d28c3 2018-03-11 stsp #include <sys/limits.h>
19 9d31a1d8 2018-03-11 stsp #include <sys/queue.h>
21 86c3caaf 2018-03-09 stsp #include <string.h>
22 86c3caaf 2018-03-09 stsp #include <stdio.h>
23 86c3caaf 2018-03-09 stsp #include <stdlib.h>
24 86c3caaf 2018-03-09 stsp #include <fcntl.h>
25 86c3caaf 2018-03-09 stsp #include <errno.h>
26 86c3caaf 2018-03-09 stsp #include <unistd.h>
27 9d31a1d8 2018-03-11 stsp #include <sha1.h>
28 9d31a1d8 2018-03-11 stsp #include <zlib.h>
29 9d31a1d8 2018-03-11 stsp #include <fnmatch.h>
31 86c3caaf 2018-03-09 stsp #include "got_error.h"
32 86c3caaf 2018-03-09 stsp #include "got_repository.h"
33 5261c201 2018-04-01 stsp #include "got_reference.h"
34 9d31a1d8 2018-03-11 stsp #include "got_object.h"
35 86c3caaf 2018-03-09 stsp #include "got_worktree.h"
36 511a516b 2018-05-19 stsp #include "got_opentemp.h"
38 718b3ab0 2018-03-17 stsp #include "got_lib_worktree.h"
39 718b3ab0 2018-03-17 stsp #include "got_lib_path.h"
40 718b3ab0 2018-03-17 stsp #include "got_lib_sha1.h"
41 718b3ab0 2018-03-17 stsp #include "got_lib_fileindex.h"
42 718b3ab0 2018-03-17 stsp #include "got_lib_zbuf.h"
43 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
44 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
47 9d31a1d8 2018-03-11 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
50 99724ed4 2018-03-10 stsp static const struct got_error *
51 7ac97322 2018-03-11 stsp create_meta_file(const char *path_got, const char *name, const char *content)
53 99724ed4 2018-03-10 stsp const struct got_error *err = NULL;
55 99724ed4 2018-03-10 stsp int fd = -1;
56 99724ed4 2018-03-10 stsp char buf[4];
59 7ac97322 2018-03-11 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
60 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
61 99724ed4 2018-03-10 stsp path = NULL;
65 73a5ef67 2018-03-11 stsp fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
66 99724ed4 2018-03-10 stsp GOT_DEFAULT_FILE_MODE);
67 99724ed4 2018-03-10 stsp if (fd == -1) {
68 99724ed4 2018-03-10 stsp err = got_error_from_errno();
72 99724ed4 2018-03-10 stsp /* The file should be empty. */
73 99724ed4 2018-03-10 stsp n = read(fd, buf, sizeof(buf));
74 99724ed4 2018-03-10 stsp if (n != 0) {
75 99724ed4 2018-03-10 stsp err = (n == -1 ? got_error_from_errno() :
76 99724ed4 2018-03-10 stsp got_error(GOT_ERR_WORKTREE_EXISTS));
80 99724ed4 2018-03-10 stsp if (content) {
81 99724ed4 2018-03-10 stsp int len = dprintf(fd, "%s\n", content);
82 99724ed4 2018-03-10 stsp if (len != strlen(content) + 1) {
83 99724ed4 2018-03-10 stsp err = got_error_from_errno();
89 99724ed4 2018-03-10 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
90 99724ed4 2018-03-10 stsp err = got_error_from_errno();
95 09fe317a 2018-03-11 stsp static const struct got_error *
96 7ac97322 2018-03-11 stsp read_meta_file(char **content, const char *path_got, const char *name)
98 09fe317a 2018-03-11 stsp const struct got_error *err = NULL;
100 09fe317a 2018-03-11 stsp int fd = -1;
102 09fe317a 2018-03-11 stsp struct stat sb;
104 09fe317a 2018-03-11 stsp *content = NULL;
106 7ac97322 2018-03-11 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
107 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
108 09fe317a 2018-03-11 stsp path = NULL;
112 ef99fdb1 2018-03-11 stsp fd = open(path, O_RDONLY | O_NOFOLLOW);
113 09fe317a 2018-03-11 stsp if (fd == -1) {
114 ef99fdb1 2018-03-11 stsp err = got_error_from_errno();
117 ef99fdb1 2018-03-11 stsp if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
118 73a5ef67 2018-03-11 stsp err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
119 73a5ef67 2018-03-11 stsp : got_error_from_errno());
123 09fe317a 2018-03-11 stsp stat(path, &sb);
124 09fe317a 2018-03-11 stsp *content = calloc(1, sb.st_size);
125 09fe317a 2018-03-11 stsp if (*content == NULL) {
126 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
130 09fe317a 2018-03-11 stsp n = read(fd, *content, sb.st_size);
131 09fe317a 2018-03-11 stsp if (n != sb.st_size) {
132 0605801d 2018-03-11 stsp err = (n == -1 ? got_error_from_errno() :
133 0605801d 2018-03-11 stsp got_error(GOT_ERR_WORKTREE_META));
136 09fe317a 2018-03-11 stsp if ((*content)[sb.st_size - 1] != '\n') {
137 09fe317a 2018-03-11 stsp err = got_error(GOT_ERR_WORKTREE_META);
140 09fe317a 2018-03-11 stsp (*content)[sb.st_size - 1] = '\0';
143 09fe317a 2018-03-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
144 09fe317a 2018-03-11 stsp err = got_error_from_errno();
145 09fe317a 2018-03-11 stsp free(path);
147 09fe317a 2018-03-11 stsp free(*content);
148 09fe317a 2018-03-11 stsp *content = NULL;
150 09fe317a 2018-03-11 stsp return err;
153 86c3caaf 2018-03-09 stsp const struct got_error *
154 86c3caaf 2018-03-09 stsp got_worktree_init(const char *path, struct got_reference *head_ref,
155 577ec78f 2018-03-11 stsp const char *prefix, struct got_repository *repo)
157 86c3caaf 2018-03-09 stsp const struct got_error *err = NULL;
158 7ac97322 2018-03-11 stsp char *path_got = NULL;
159 86c3caaf 2018-03-09 stsp char *refstr = NULL;
160 cde76477 2018-03-11 stsp char *repo_path = NULL;
161 1451e70d 2018-03-10 stsp char *formatstr = NULL;
162 0bb8a95e 2018-03-12 stsp char *absprefix = NULL;
164 0bb8a95e 2018-03-12 stsp if (!got_path_is_absolute(prefix)) {
165 0bb8a95e 2018-03-12 stsp if (asprintf(&absprefix, "/%s", prefix) == -1)
166 0a585a0d 2018-03-17 stsp return got_error_from_errno();
169 86c3caaf 2018-03-09 stsp /* Create top-level directory (may already exist). */
170 2cb4bacb 2018-03-11 stsp if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
171 86c3caaf 2018-03-09 stsp err = got_error_from_errno();
175 86c3caaf 2018-03-09 stsp /* Create .got directory (may already exist). */
176 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
177 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
180 7ac97322 2018-03-11 stsp if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
181 86c3caaf 2018-03-09 stsp err = got_error_from_errno();
185 056e7441 2018-03-11 stsp /* Create an empty lock file. */
186 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
190 86c3caaf 2018-03-09 stsp /* Create an empty file index. */
191 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
195 86c3caaf 2018-03-09 stsp /* Write the HEAD reference. */
196 86c3caaf 2018-03-09 stsp refstr = got_ref_to_str(head_ref);
197 86c3caaf 2018-03-09 stsp if (refstr == NULL) {
198 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
201 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_HEAD, refstr);
205 1451e70d 2018-03-10 stsp /* Store path to repository. */
206 cde76477 2018-03-11 stsp repo_path = got_repo_get_path(repo);
207 cde76477 2018-03-11 stsp if (repo_path == NULL) {
208 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
211 cde76477 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY, repo_path);
215 577ec78f 2018-03-11 stsp /* Store in-repository path prefix. */
216 0bb8a95e 2018-03-12 stsp err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
217 0bb8a95e 2018-03-12 stsp absprefix ? absprefix : prefix);
221 9dce68ed 2018-03-10 stsp /* Stamp work tree with format file. */
222 1451e70d 2018-03-10 stsp if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
223 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
226 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
231 7ac97322 2018-03-11 stsp free(path_got);
232 1451e70d 2018-03-10 stsp free(formatstr);
233 86c3caaf 2018-03-09 stsp free(refstr);
234 cde76477 2018-03-11 stsp free(repo_path);
235 0bb8a95e 2018-03-12 stsp free(absprefix);
236 86c3caaf 2018-03-09 stsp return err;
239 86c3caaf 2018-03-09 stsp const struct got_error *
240 86c3caaf 2018-03-09 stsp got_worktree_open(struct got_worktree **worktree, const char *path)
242 6d9d28c3 2018-03-11 stsp const struct got_error *err = NULL;
243 7ac97322 2018-03-11 stsp char *path_got;
244 6d9d28c3 2018-03-11 stsp char *formatstr = NULL;
245 056e7441 2018-03-11 stsp char *path_lock = NULL;
246 6d9d28c3 2018-03-11 stsp int version, fd = -1;
247 6d9d28c3 2018-03-11 stsp const char *errstr;
249 6d9d28c3 2018-03-11 stsp *worktree = NULL;
251 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
252 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
253 7ac97322 2018-03-11 stsp path_got = NULL;
257 7ac97322 2018-03-11 stsp if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
258 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
259 056e7441 2018-03-11 stsp path_lock = NULL;
263 056e7441 2018-03-11 stsp fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
264 6d9d28c3 2018-03-11 stsp if (fd == -1) {
265 73a5ef67 2018-03-11 stsp err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
266 73a5ef67 2018-03-11 stsp : got_error_from_errno());
270 7ac97322 2018-03-11 stsp err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
274 6d9d28c3 2018-03-11 stsp version = strtonum(formatstr, 1, INT_MAX, &errstr);
275 6d9d28c3 2018-03-11 stsp if (errstr) {
276 6d9d28c3 2018-03-11 stsp err = got_error(GOT_ERR_WORKTREE_META);
279 6d9d28c3 2018-03-11 stsp if (version != GOT_WORKTREE_FORMAT_VERSION) {
280 6d9d28c3 2018-03-11 stsp err = got_error(GOT_ERR_WORKTREE_VERS);
284 6d9d28c3 2018-03-11 stsp *worktree = calloc(1, sizeof(**worktree));
285 6d9d28c3 2018-03-11 stsp if (*worktree == NULL) {
286 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
289 056e7441 2018-03-11 stsp (*worktree)->lockfd = -1;
291 c88eb298 2018-03-11 stsp (*worktree)->root_path = strdup(path);
292 c88eb298 2018-03-11 stsp if ((*worktree)->root_path == NULL) {
293 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
296 cde76477 2018-03-11 stsp err = read_meta_file(&(*worktree)->repo_path, path_got,
297 6d9d28c3 2018-03-11 stsp GOT_WORKTREE_REPOSITORY);
300 7ac97322 2018-03-11 stsp err = read_meta_file(&(*worktree)->path_prefix, path_got,
301 6d9d28c3 2018-03-11 stsp GOT_WORKTREE_PATH_PREFIX);
305 e8f36958 2018-03-11 stsp err = read_meta_file(&(*worktree)->head_ref, path_got,
306 e8f36958 2018-03-11 stsp GOT_WORKTREE_HEAD);
311 7ac97322 2018-03-11 stsp free(path_got);
312 056e7441 2018-03-11 stsp free(path_lock);
314 6d9d28c3 2018-03-11 stsp if (fd != -1)
316 6d9d28c3 2018-03-11 stsp if (*worktree != NULL)
317 6d9d28c3 2018-03-11 stsp got_worktree_close(*worktree);
318 6d9d28c3 2018-03-11 stsp *worktree = NULL;
320 056e7441 2018-03-11 stsp (*worktree)->lockfd = fd;
322 6d9d28c3 2018-03-11 stsp return err;
326 86c3caaf 2018-03-09 stsp got_worktree_close(struct got_worktree *worktree)
328 c88eb298 2018-03-11 stsp free(worktree->root_path);
329 cde76477 2018-03-11 stsp free(worktree->repo_path);
330 6d9d28c3 2018-03-11 stsp free(worktree->path_prefix);
331 d6c38e0d 2018-03-11 stsp free(worktree->base_commit);
332 e8f36958 2018-03-11 stsp free(worktree->head_ref);
333 056e7441 2018-03-11 stsp if (worktree->lockfd != -1)
334 056e7441 2018-03-11 stsp close(worktree->lockfd);
335 6d9d28c3 2018-03-11 stsp free(worktree);
339 86c3caaf 2018-03-09 stsp got_worktree_get_repo_path(struct got_worktree *worktree)
341 cde76477 2018-03-11 stsp return strdup(worktree->repo_path);
345 35be1456 2018-03-11 stsp got_worktree_get_head_ref_name(struct got_worktree *worktree)
347 35be1456 2018-03-11 stsp return strdup(worktree->head_ref);
350 9d31a1d8 2018-03-11 stsp static const struct got_error *
351 9d31a1d8 2018-03-11 stsp lock_worktree(struct got_worktree *worktree, int operation)
353 9d31a1d8 2018-03-11 stsp if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
354 9d31a1d8 2018-03-11 stsp return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
355 9d31a1d8 2018-03-11 stsp : got_error_from_errno());
356 86c3caaf 2018-03-09 stsp return NULL;
359 9d31a1d8 2018-03-11 stsp static const char *
360 9d31a1d8 2018-03-11 stsp apply_path_prefix(struct got_worktree *worktree, const char *path)
362 9d31a1d8 2018-03-11 stsp const char *p = path;
363 9d31a1d8 2018-03-11 stsp p += strlen(worktree->path_prefix);
364 9d31a1d8 2018-03-11 stsp if (*p == '/')
369 9d31a1d8 2018-03-11 stsp static const struct got_error *
370 9d31a1d8 2018-03-11 stsp add_file_on_disk(struct got_worktree *worktree, struct got_fileindex *fileindex,
371 9d31a1d8 2018-03-11 stsp const char *path, struct got_blob_object *blob, struct got_repository *repo)
373 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
374 c34b20a2 2018-03-12 stsp char *ondisk_path;
376 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
377 9d31a1d8 2018-03-11 stsp struct got_fileindex_entry *entry;
379 c34b20a2 2018-03-12 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path,
380 9d31a1d8 2018-03-11 stsp apply_path_prefix(worktree, path)) == -1)
381 0a585a0d 2018-03-17 stsp return got_error_from_errno();
383 c34b20a2 2018-03-12 stsp fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
384 9d31a1d8 2018-03-11 stsp GOT_DEFAULT_FILE_MODE);
385 9d31a1d8 2018-03-11 stsp if (fd == -1) {
386 9d31a1d8 2018-03-11 stsp err = got_error_from_errno();
387 9d31a1d8 2018-03-11 stsp if (errno == EEXIST) {
388 9d31a1d8 2018-03-11 stsp struct stat sb;
389 c34b20a2 2018-03-12 stsp if (lstat(ondisk_path, &sb) == -1) {
390 9d31a1d8 2018-03-11 stsp err = got_error_from_errno();
391 9d31a1d8 2018-03-11 stsp } else if (!S_ISREG(sb.st_mode)) {
392 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
393 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_FILE_OBSTRUCTED);
396 9d31a1d8 2018-03-11 stsp return err;
399 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
401 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
402 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
405 9d31a1d8 2018-03-11 stsp if (len > 0) {
406 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
407 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
408 9d31a1d8 2018-03-11 stsp hdrlen = 0;
409 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
410 9d31a1d8 2018-03-11 stsp err = got_error_from_errno();
412 9d31a1d8 2018-03-11 stsp } else if (outlen != len) {
413 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
417 9d31a1d8 2018-03-11 stsp } while (len != 0);
421 c34b20a2 2018-03-12 stsp err = got_fileindex_entry_open(&entry, ondisk_path,
422 c34b20a2 2018-03-12 stsp apply_path_prefix(worktree, path), blob->id.sha1);
426 9d31a1d8 2018-03-11 stsp err = got_fileindex_entry_add(fileindex, entry);
431 c34b20a2 2018-03-12 stsp free(ondisk_path);
432 9d31a1d8 2018-03-11 stsp return err;
435 9d31a1d8 2018-03-11 stsp static const struct got_error *
436 9d31a1d8 2018-03-11 stsp add_dir_on_disk(struct got_worktree *worktree, const char *path)
438 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
439 9d31a1d8 2018-03-11 stsp char *abspath;
441 9d31a1d8 2018-03-11 stsp if (asprintf(&abspath, "%s/%s", worktree->root_path,
442 9d31a1d8 2018-03-11 stsp apply_path_prefix(worktree, path)) == -1)
443 0a585a0d 2018-03-17 stsp return got_error_from_errno();
445 9d31a1d8 2018-03-11 stsp /* XXX queue work rather than editing disk directly? */
446 9d31a1d8 2018-03-11 stsp if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1) {
447 9d31a1d8 2018-03-11 stsp struct stat sb;
449 9d31a1d8 2018-03-11 stsp if (errno != EEXIST) {
450 9d31a1d8 2018-03-11 stsp err = got_error_from_errno();
454 9d31a1d8 2018-03-11 stsp if (lstat(abspath, &sb) == -1) {
455 9d31a1d8 2018-03-11 stsp err = got_error_from_errno();
459 9d31a1d8 2018-03-11 stsp if (!S_ISDIR(sb.st_mode)) {
460 9d31a1d8 2018-03-11 stsp /* TODO directory is obstructed; do something */
461 9d31a1d8 2018-03-11 stsp return got_error(GOT_ERR_FILE_OBSTRUCTED);
466 9d31a1d8 2018-03-11 stsp free(abspath);
467 9d31a1d8 2018-03-11 stsp return err;
470 9d31a1d8 2018-03-11 stsp static const struct got_error *
471 9d31a1d8 2018-03-11 stsp tree_checkout(struct got_worktree *, struct got_fileindex *,
472 92a684f4 2018-03-12 stsp struct got_tree_object *, const char *, struct got_repository *,
473 92a684f4 2018-03-12 stsp got_worktree_checkout_cb progress_cb, void *progress_arg);
475 9d31a1d8 2018-03-11 stsp static const struct got_error *
476 9d31a1d8 2018-03-11 stsp tree_checkout_entry(struct got_worktree *worktree,
477 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex, struct got_tree_entry *te,
478 92a684f4 2018-03-12 stsp const char *parent, struct got_repository *repo,
479 92a684f4 2018-03-12 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
481 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
482 9d31a1d8 2018-03-11 stsp struct got_object *obj = NULL;
483 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
484 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
485 9d31a1d8 2018-03-11 stsp char *path = NULL;
486 f7b38925 2018-04-01 stsp char *progress_path = NULL;
487 9d31a1d8 2018-03-11 stsp size_t len;
489 9d31a1d8 2018-03-11 stsp if (parent[0] == '/' && parent[1] == '\0')
490 9d31a1d8 2018-03-11 stsp parent = "";
491 9d31a1d8 2018-03-11 stsp if (asprintf(&path, "%s/%s", parent, te->name) == -1)
492 0a585a0d 2018-03-17 stsp return got_error_from_errno();
494 9d31a1d8 2018-03-11 stsp /* Skip this entry if it is outside of our path prefix. */
495 9d31a1d8 2018-03-11 stsp len = MIN(strlen(worktree->path_prefix), strlen(path));
496 9d31a1d8 2018-03-11 stsp if (strncmp(path, worktree->path_prefix, len) != 0) {
497 9d31a1d8 2018-03-11 stsp free(path);
498 9d31a1d8 2018-03-11 stsp return NULL;
501 9d31a1d8 2018-03-11 stsp err = got_object_open(&obj, repo, te->id);
505 f7b38925 2018-04-01 stsp progress_path = path;
506 f7b38925 2018-04-01 stsp if (strncmp(progress_path, worktree->path_prefix, len) == 0)
507 f7b38925 2018-04-01 stsp progress_path += len;
508 f7b38925 2018-04-01 stsp (*progress_cb)(progress_arg, progress_path);
510 9d31a1d8 2018-03-11 stsp switch (got_object_get_type(obj)) {
511 9d31a1d8 2018-03-11 stsp case GOT_OBJ_TYPE_BLOB:
512 9d31a1d8 2018-03-11 stsp if (strlen(worktree->path_prefix) >= strlen(path))
514 9d31a1d8 2018-03-11 stsp err = got_object_blob_open(&blob, repo, obj, 8192);
517 9d31a1d8 2018-03-11 stsp err = add_file_on_disk(worktree, fileindex, path, blob, repo);
519 9d31a1d8 2018-03-11 stsp case GOT_OBJ_TYPE_TREE:
520 9d31a1d8 2018-03-11 stsp err = got_object_tree_open(&tree, repo, obj);
523 9d31a1d8 2018-03-11 stsp if (strlen(worktree->path_prefix) < strlen(path)) {
524 9d31a1d8 2018-03-11 stsp err = add_dir_on_disk(worktree, path);
528 92a684f4 2018-03-12 stsp err = tree_checkout(worktree, fileindex, tree, path, repo,
529 92a684f4 2018-03-12 stsp progress_cb, progress_arg);
537 9d31a1d8 2018-03-11 stsp got_object_blob_close(blob);
539 9d31a1d8 2018-03-11 stsp got_object_tree_close(tree);
541 9d31a1d8 2018-03-11 stsp got_object_close(obj);
542 9d31a1d8 2018-03-11 stsp free(path);
543 9d31a1d8 2018-03-11 stsp return err;
546 9d31a1d8 2018-03-11 stsp static const struct got_error *
547 9d31a1d8 2018-03-11 stsp tree_checkout(struct got_worktree *worktree,
548 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex, struct got_tree_object *tree,
549 92a684f4 2018-03-12 stsp const char *path, struct got_repository *repo,
550 92a684f4 2018-03-12 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
552 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
553 9d31a1d8 2018-03-11 stsp struct got_tree_entry *te;
554 9d31a1d8 2018-03-11 stsp size_t len;
556 9d31a1d8 2018-03-11 stsp /* Skip this tree if it is outside of our path prefix. */
557 9d31a1d8 2018-03-11 stsp len = MIN(strlen(worktree->path_prefix), strlen(path));
558 9d31a1d8 2018-03-11 stsp if (strncmp(path, worktree->path_prefix, len) != 0)
559 9d31a1d8 2018-03-11 stsp return NULL;
561 9d31a1d8 2018-03-11 stsp SIMPLEQ_FOREACH(te, &tree->entries, entry) {
562 92a684f4 2018-03-12 stsp err = tree_checkout_entry(worktree, fileindex, te, path, repo,
563 92a684f4 2018-03-12 stsp progress_cb, progress_arg);
568 9d31a1d8 2018-03-11 stsp return err;
571 9d31a1d8 2018-03-11 stsp const struct got_error *
572 9d31a1d8 2018-03-11 stsp got_worktree_checkout_files(struct got_worktree *worktree,
573 92a684f4 2018-03-12 stsp struct got_reference *head_ref, struct got_repository *repo,
574 92a684f4 2018-03-12 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
576 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL, *unlockerr;
577 9d31a1d8 2018-03-11 stsp struct got_object_id *commit_id = NULL;
578 9d31a1d8 2018-03-11 stsp struct got_object *obj = NULL;
579 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
580 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
581 9d31a1d8 2018-03-11 stsp char *fileindex_path = NULL, *new_fileindex_path = NULL;
582 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
583 9d31a1d8 2018-03-11 stsp FILE *findex = NULL;
585 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
587 9d31a1d8 2018-03-11 stsp return err;
589 9d31a1d8 2018-03-11 stsp fileindex = got_fileindex_open();
590 9d31a1d8 2018-03-11 stsp if (fileindex == NULL) {
591 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
595 9d31a1d8 2018-03-11 stsp if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
596 9d31a1d8 2018-03-11 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
597 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
598 9d31a1d8 2018-03-11 stsp fileindex_path = NULL;
602 51664889 2018-03-12 stsp err = got_opentemp_named(&new_fileindex_path, &findex, fileindex_path);
606 9d31a1d8 2018-03-11 stsp err = got_ref_resolve(&commit_id, repo, head_ref);
610 9d31a1d8 2018-03-11 stsp err = got_object_open(&obj, repo, commit_id);
614 9d31a1d8 2018-03-11 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
615 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_OBJ_TYPE);
619 9d31a1d8 2018-03-11 stsp err = got_object_commit_open(&commit, repo, obj);
623 9d31a1d8 2018-03-11 stsp got_object_close(obj);
624 9d31a1d8 2018-03-11 stsp err = got_object_open(&obj, repo, commit->tree_id);
628 9d31a1d8 2018-03-11 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_TREE) {
629 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_OBJ_TYPE);
633 9d31a1d8 2018-03-11 stsp err = got_object_tree_open(&tree, repo, obj);
637 92a684f4 2018-03-12 stsp err = tree_checkout(worktree, fileindex, tree, "/", repo,
638 92a684f4 2018-03-12 stsp progress_cb, progress_arg);
642 9d31a1d8 2018-03-11 stsp err = got_fileindex_write(fileindex, findex);
646 9d31a1d8 2018-03-11 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
647 9d31a1d8 2018-03-11 stsp err = got_error_from_errno();
651 9d31a1d8 2018-03-11 stsp free(new_fileindex_path);
652 9d31a1d8 2018-03-11 stsp new_fileindex_path = NULL;
655 9d31a1d8 2018-03-11 stsp if (commit)
656 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
658 9d31a1d8 2018-03-11 stsp got_object_close(obj);
659 9d31a1d8 2018-03-11 stsp free(commit_id);
660 9d31a1d8 2018-03-11 stsp if (new_fileindex_path)
661 9d31a1d8 2018-03-11 stsp unlink(new_fileindex_path);
662 9d31a1d8 2018-03-11 stsp if (findex)
663 9d31a1d8 2018-03-11 stsp fclose(findex);
664 9d31a1d8 2018-03-11 stsp free(new_fileindex_path);
665 9d31a1d8 2018-03-11 stsp free(fileindex_path);
666 9d31a1d8 2018-03-11 stsp got_fileindex_close(fileindex);
667 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
668 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
669 9d31a1d8 2018-03-11 stsp err = unlockerr;
670 9d31a1d8 2018-03-11 stsp return err;