Blame


1 86c3caaf 2018-03-09 stsp /*
2 86c3caaf 2018-03-09 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 86c3caaf 2018-03-09 stsp *
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.
7 86c3caaf 2018-03-09 stsp *
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.
15 86c3caaf 2018-03-09 stsp */
16 86c3caaf 2018-03-09 stsp
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>
20 86c3caaf 2018-03-09 stsp
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>
30 86c3caaf 2018-03-09 stsp
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"
37 86c3caaf 2018-03-09 stsp
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 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
43 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
44 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
45 9d31a1d8 2018-03-11 stsp
46 9d31a1d8 2018-03-11 stsp #ifndef MIN
47 9d31a1d8 2018-03-11 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
48 9d31a1d8 2018-03-11 stsp #endif
49 86c3caaf 2018-03-09 stsp
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)
52 99724ed4 2018-03-10 stsp {
53 99724ed4 2018-03-10 stsp const struct got_error *err = NULL;
54 99724ed4 2018-03-10 stsp char *path;
55 99724ed4 2018-03-10 stsp int fd = -1;
56 99724ed4 2018-03-10 stsp
57 7ac97322 2018-03-11 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
58 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
59 99724ed4 2018-03-10 stsp path = NULL;
60 99724ed4 2018-03-10 stsp goto done;
61 99724ed4 2018-03-10 stsp }
62 99724ed4 2018-03-10 stsp
63 73a5ef67 2018-03-11 stsp fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
64 99724ed4 2018-03-10 stsp GOT_DEFAULT_FILE_MODE);
65 99724ed4 2018-03-10 stsp if (fd == -1) {
66 99724ed4 2018-03-10 stsp err = got_error_from_errno();
67 99724ed4 2018-03-10 stsp goto done;
68 99724ed4 2018-03-10 stsp }
69 99724ed4 2018-03-10 stsp
70 99724ed4 2018-03-10 stsp if (content) {
71 99724ed4 2018-03-10 stsp int len = dprintf(fd, "%s\n", content);
72 99724ed4 2018-03-10 stsp if (len != strlen(content) + 1) {
73 99724ed4 2018-03-10 stsp err = got_error_from_errno();
74 99724ed4 2018-03-10 stsp goto done;
75 99724ed4 2018-03-10 stsp }
76 99724ed4 2018-03-10 stsp }
77 99724ed4 2018-03-10 stsp
78 99724ed4 2018-03-10 stsp done:
79 99724ed4 2018-03-10 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
80 99724ed4 2018-03-10 stsp err = got_error_from_errno();
81 99724ed4 2018-03-10 stsp free(path);
82 99724ed4 2018-03-10 stsp return err;
83 99724ed4 2018-03-10 stsp }
84 99724ed4 2018-03-10 stsp
85 09fe317a 2018-03-11 stsp static const struct got_error *
86 7ac97322 2018-03-11 stsp read_meta_file(char **content, const char *path_got, const char *name)
87 09fe317a 2018-03-11 stsp {
88 09fe317a 2018-03-11 stsp const struct got_error *err = NULL;
89 09fe317a 2018-03-11 stsp char *path;
90 09fe317a 2018-03-11 stsp int fd = -1;
91 09fe317a 2018-03-11 stsp ssize_t n;
92 09fe317a 2018-03-11 stsp struct stat sb;
93 09fe317a 2018-03-11 stsp
94 09fe317a 2018-03-11 stsp *content = NULL;
95 09fe317a 2018-03-11 stsp
96 7ac97322 2018-03-11 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
97 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
98 09fe317a 2018-03-11 stsp path = NULL;
99 09fe317a 2018-03-11 stsp goto done;
100 09fe317a 2018-03-11 stsp }
101 09fe317a 2018-03-11 stsp
102 ef99fdb1 2018-03-11 stsp fd = open(path, O_RDONLY | O_NOFOLLOW);
103 09fe317a 2018-03-11 stsp if (fd == -1) {
104 ef99fdb1 2018-03-11 stsp err = got_error_from_errno();
105 ef99fdb1 2018-03-11 stsp goto done;
106 ef99fdb1 2018-03-11 stsp }
107 ef99fdb1 2018-03-11 stsp if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
108 73a5ef67 2018-03-11 stsp err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
109 73a5ef67 2018-03-11 stsp : got_error_from_errno());
110 09fe317a 2018-03-11 stsp goto done;
111 09fe317a 2018-03-11 stsp }
112 09fe317a 2018-03-11 stsp
113 09fe317a 2018-03-11 stsp stat(path, &sb);
114 09fe317a 2018-03-11 stsp *content = calloc(1, sb.st_size);
115 09fe317a 2018-03-11 stsp if (*content == NULL) {
116 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
117 09fe317a 2018-03-11 stsp goto done;
118 09fe317a 2018-03-11 stsp }
119 09fe317a 2018-03-11 stsp
120 09fe317a 2018-03-11 stsp n = read(fd, *content, sb.st_size);
121 09fe317a 2018-03-11 stsp if (n != sb.st_size) {
122 0605801d 2018-03-11 stsp err = (n == -1 ? got_error_from_errno() :
123 0605801d 2018-03-11 stsp got_error(GOT_ERR_WORKTREE_META));
124 09fe317a 2018-03-11 stsp goto done;
125 09fe317a 2018-03-11 stsp }
126 09fe317a 2018-03-11 stsp if ((*content)[sb.st_size - 1] != '\n') {
127 09fe317a 2018-03-11 stsp err = got_error(GOT_ERR_WORKTREE_META);
128 09fe317a 2018-03-11 stsp goto done;
129 09fe317a 2018-03-11 stsp }
130 09fe317a 2018-03-11 stsp (*content)[sb.st_size - 1] = '\0';
131 09fe317a 2018-03-11 stsp
132 09fe317a 2018-03-11 stsp done:
133 09fe317a 2018-03-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
134 09fe317a 2018-03-11 stsp err = got_error_from_errno();
135 09fe317a 2018-03-11 stsp free(path);
136 09fe317a 2018-03-11 stsp if (err) {
137 09fe317a 2018-03-11 stsp free(*content);
138 09fe317a 2018-03-11 stsp *content = NULL;
139 09fe317a 2018-03-11 stsp }
140 09fe317a 2018-03-11 stsp return err;
141 09fe317a 2018-03-11 stsp }
142 09fe317a 2018-03-11 stsp
143 86c3caaf 2018-03-09 stsp const struct got_error *
144 86c3caaf 2018-03-09 stsp got_worktree_init(const char *path, struct got_reference *head_ref,
145 577ec78f 2018-03-11 stsp const char *prefix, struct got_repository *repo)
146 86c3caaf 2018-03-09 stsp {
147 86c3caaf 2018-03-09 stsp const struct got_error *err = NULL;
148 65596e15 2018-12-24 stsp struct got_object_id *commit_id = NULL;
149 65596e15 2018-12-24 stsp int obj_type;
150 7ac97322 2018-03-11 stsp char *path_got = NULL;
151 08d425ea 2018-12-24 stsp char *refstr = NULL;
152 cde76477 2018-03-11 stsp char *repo_path = NULL;
153 1451e70d 2018-03-10 stsp char *formatstr = NULL;
154 0bb8a95e 2018-03-12 stsp char *absprefix = NULL;
155 65596e15 2018-12-24 stsp char *basestr = NULL;
156 65596e15 2018-12-24 stsp
157 65596e15 2018-12-24 stsp err = got_ref_resolve(&commit_id, repo, head_ref);
158 65596e15 2018-12-24 stsp if (err)
159 65596e15 2018-12-24 stsp return err;
160 65596e15 2018-12-24 stsp err = got_object_get_type(&obj_type, repo, commit_id);
161 65596e15 2018-12-24 stsp if (err)
162 65596e15 2018-12-24 stsp return err;
163 65596e15 2018-12-24 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
164 65596e15 2018-12-24 stsp return got_error(GOT_ERR_OBJ_TYPE);
165 86c3caaf 2018-03-09 stsp
166 0bb8a95e 2018-03-12 stsp if (!got_path_is_absolute(prefix)) {
167 0bb8a95e 2018-03-12 stsp if (asprintf(&absprefix, "/%s", prefix) == -1)
168 0a585a0d 2018-03-17 stsp return got_error_from_errno();
169 0bb8a95e 2018-03-12 stsp }
170 577ec78f 2018-03-11 stsp
171 86c3caaf 2018-03-09 stsp /* Create top-level directory (may already exist). */
172 2cb4bacb 2018-03-11 stsp if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
173 86c3caaf 2018-03-09 stsp err = got_error_from_errno();
174 86c3caaf 2018-03-09 stsp goto done;
175 86c3caaf 2018-03-09 stsp }
176 86c3caaf 2018-03-09 stsp
177 86c3caaf 2018-03-09 stsp /* Create .got directory (may already exist). */
178 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
179 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
180 86c3caaf 2018-03-09 stsp goto done;
181 86c3caaf 2018-03-09 stsp }
182 7ac97322 2018-03-11 stsp if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
183 86c3caaf 2018-03-09 stsp err = got_error_from_errno();
184 86c3caaf 2018-03-09 stsp goto done;
185 86c3caaf 2018-03-09 stsp }
186 86c3caaf 2018-03-09 stsp
187 056e7441 2018-03-11 stsp /* Create an empty lock file. */
188 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
189 056e7441 2018-03-11 stsp if (err)
190 056e7441 2018-03-11 stsp goto done;
191 056e7441 2018-03-11 stsp
192 86c3caaf 2018-03-09 stsp /* Create an empty file index. */
193 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
194 99724ed4 2018-03-10 stsp if (err)
195 86c3caaf 2018-03-09 stsp goto done;
196 86c3caaf 2018-03-09 stsp
197 08d425ea 2018-12-24 stsp /* Write the HEAD reference. */
198 08d425ea 2018-12-24 stsp refstr = got_ref_to_str(head_ref);
199 08d425ea 2018-12-24 stsp if (refstr == NULL) {
200 a1a7858a 2018-12-24 stsp err = got_error_from_errno();
201 a1a7858a 2018-12-24 stsp goto done;
202 a1a7858a 2018-12-24 stsp }
203 0f92850e 2018-12-25 stsp err = create_meta_file(path_got, GOT_WORKTREE_HEAD_REF, refstr);
204 65596e15 2018-12-24 stsp if (err)
205 65596e15 2018-12-24 stsp goto done;
206 65596e15 2018-12-24 stsp
207 65596e15 2018-12-24 stsp /* Record our base commit. */
208 65596e15 2018-12-24 stsp err = got_object_id_str(&basestr, commit_id);
209 65596e15 2018-12-24 stsp if (err)
210 65596e15 2018-12-24 stsp goto done;
211 0f92850e 2018-12-25 stsp err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT, basestr);
212 99724ed4 2018-03-10 stsp if (err)
213 86c3caaf 2018-03-09 stsp goto done;
214 86c3caaf 2018-03-09 stsp
215 1451e70d 2018-03-10 stsp /* Store path to repository. */
216 cde76477 2018-03-11 stsp repo_path = got_repo_get_path(repo);
217 cde76477 2018-03-11 stsp if (repo_path == NULL) {
218 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
219 86c3caaf 2018-03-09 stsp goto done;
220 86c3caaf 2018-03-09 stsp }
221 cde76477 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY, repo_path);
222 99724ed4 2018-03-10 stsp if (err)
223 86c3caaf 2018-03-09 stsp goto done;
224 86c3caaf 2018-03-09 stsp
225 577ec78f 2018-03-11 stsp /* Store in-repository path prefix. */
226 0bb8a95e 2018-03-12 stsp err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
227 0bb8a95e 2018-03-12 stsp absprefix ? absprefix : prefix);
228 577ec78f 2018-03-11 stsp if (err)
229 577ec78f 2018-03-11 stsp goto done;
230 577ec78f 2018-03-11 stsp
231 9dce68ed 2018-03-10 stsp /* Stamp work tree with format file. */
232 1451e70d 2018-03-10 stsp if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
233 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
234 1451e70d 2018-03-10 stsp goto done;
235 1451e70d 2018-03-10 stsp }
236 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
237 99724ed4 2018-03-10 stsp if (err)
238 1451e70d 2018-03-10 stsp goto done;
239 1451e70d 2018-03-10 stsp
240 86c3caaf 2018-03-09 stsp done:
241 65596e15 2018-12-24 stsp free(commit_id);
242 7ac97322 2018-03-11 stsp free(path_got);
243 1451e70d 2018-03-10 stsp free(formatstr);
244 08d425ea 2018-12-24 stsp free(refstr);
245 cde76477 2018-03-11 stsp free(repo_path);
246 0bb8a95e 2018-03-12 stsp free(absprefix);
247 65596e15 2018-12-24 stsp free(basestr);
248 86c3caaf 2018-03-09 stsp return err;
249 86c3caaf 2018-03-09 stsp }
250 86c3caaf 2018-03-09 stsp
251 86c3caaf 2018-03-09 stsp const struct got_error *
252 86c3caaf 2018-03-09 stsp got_worktree_open(struct got_worktree **worktree, const char *path)
253 86c3caaf 2018-03-09 stsp {
254 6d9d28c3 2018-03-11 stsp const struct got_error *err = NULL;
255 7ac97322 2018-03-11 stsp char *path_got;
256 6d9d28c3 2018-03-11 stsp char *formatstr = NULL;
257 056e7441 2018-03-11 stsp char *path_lock = NULL;
258 eaccb85f 2018-12-25 stsp char *base_commit_id_str = NULL;
259 271d2a38 2018-12-25 stsp char *head_ref_str = NULL;
260 6d9d28c3 2018-03-11 stsp int version, fd = -1;
261 6d9d28c3 2018-03-11 stsp const char *errstr;
262 eaccb85f 2018-12-25 stsp struct got_repository *repo = NULL;
263 6d9d28c3 2018-03-11 stsp
264 6d9d28c3 2018-03-11 stsp *worktree = NULL;
265 6d9d28c3 2018-03-11 stsp
266 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
267 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
268 7ac97322 2018-03-11 stsp path_got = NULL;
269 6d9d28c3 2018-03-11 stsp goto done;
270 6d9d28c3 2018-03-11 stsp }
271 6d9d28c3 2018-03-11 stsp
272 7ac97322 2018-03-11 stsp if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
273 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
274 056e7441 2018-03-11 stsp path_lock = NULL;
275 6d9d28c3 2018-03-11 stsp goto done;
276 6d9d28c3 2018-03-11 stsp }
277 6d9d28c3 2018-03-11 stsp
278 056e7441 2018-03-11 stsp fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
279 6d9d28c3 2018-03-11 stsp if (fd == -1) {
280 73a5ef67 2018-03-11 stsp err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
281 73a5ef67 2018-03-11 stsp : got_error_from_errno());
282 6d9d28c3 2018-03-11 stsp goto done;
283 6d9d28c3 2018-03-11 stsp }
284 6d9d28c3 2018-03-11 stsp
285 7ac97322 2018-03-11 stsp err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
286 6d9d28c3 2018-03-11 stsp if (err)
287 6d9d28c3 2018-03-11 stsp goto done;
288 6d9d28c3 2018-03-11 stsp
289 6d9d28c3 2018-03-11 stsp version = strtonum(formatstr, 1, INT_MAX, &errstr);
290 6d9d28c3 2018-03-11 stsp if (errstr) {
291 6d9d28c3 2018-03-11 stsp err = got_error(GOT_ERR_WORKTREE_META);
292 6d9d28c3 2018-03-11 stsp goto done;
293 6d9d28c3 2018-03-11 stsp }
294 6d9d28c3 2018-03-11 stsp if (version != GOT_WORKTREE_FORMAT_VERSION) {
295 6d9d28c3 2018-03-11 stsp err = got_error(GOT_ERR_WORKTREE_VERS);
296 6d9d28c3 2018-03-11 stsp goto done;
297 6d9d28c3 2018-03-11 stsp }
298 6d9d28c3 2018-03-11 stsp
299 6d9d28c3 2018-03-11 stsp *worktree = calloc(1, sizeof(**worktree));
300 6d9d28c3 2018-03-11 stsp if (*worktree == NULL) {
301 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
302 6d9d28c3 2018-03-11 stsp goto done;
303 6d9d28c3 2018-03-11 stsp }
304 056e7441 2018-03-11 stsp (*worktree)->lockfd = -1;
305 6d9d28c3 2018-03-11 stsp
306 c88eb298 2018-03-11 stsp (*worktree)->root_path = strdup(path);
307 c88eb298 2018-03-11 stsp if ((*worktree)->root_path == NULL) {
308 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
309 6d9d28c3 2018-03-11 stsp goto done;
310 6d9d28c3 2018-03-11 stsp }
311 cde76477 2018-03-11 stsp err = read_meta_file(&(*worktree)->repo_path, path_got,
312 6d9d28c3 2018-03-11 stsp GOT_WORKTREE_REPOSITORY);
313 6d9d28c3 2018-03-11 stsp if (err)
314 6d9d28c3 2018-03-11 stsp goto done;
315 eaccb85f 2018-12-25 stsp
316 7ac97322 2018-03-11 stsp err = read_meta_file(&(*worktree)->path_prefix, path_got,
317 6d9d28c3 2018-03-11 stsp GOT_WORKTREE_PATH_PREFIX);
318 93a30277 2018-12-24 stsp if (err)
319 93a30277 2018-12-24 stsp goto done;
320 93a30277 2018-12-24 stsp
321 eaccb85f 2018-12-25 stsp err = read_meta_file(&base_commit_id_str, path_got,
322 0f92850e 2018-12-25 stsp GOT_WORKTREE_BASE_COMMIT);
323 eaccb85f 2018-12-25 stsp if (err)
324 eaccb85f 2018-12-25 stsp goto done;
325 eaccb85f 2018-12-25 stsp
326 eaccb85f 2018-12-25 stsp err = got_repo_open(&repo, (*worktree)->repo_path);
327 eaccb85f 2018-12-25 stsp if (err)
328 eaccb85f 2018-12-25 stsp goto done;
329 eaccb85f 2018-12-25 stsp
330 eaccb85f 2018-12-25 stsp err = got_object_resolve_id_str(&(*worktree)->base_commit_id, repo,
331 eaccb85f 2018-12-25 stsp base_commit_id_str);
332 f5baf295 2018-03-11 stsp if (err)
333 f5baf295 2018-03-11 stsp goto done;
334 f5baf295 2018-03-11 stsp
335 271d2a38 2018-12-25 stsp err = read_meta_file(&head_ref_str, path_got, GOT_WORKTREE_HEAD_REF);
336 f5baf295 2018-03-11 stsp if (err)
337 6d9d28c3 2018-03-11 stsp goto done;
338 6d9d28c3 2018-03-11 stsp
339 271d2a38 2018-12-25 stsp err = got_ref_open(&(*worktree)->head_ref, repo, head_ref_str);
340 6d9d28c3 2018-03-11 stsp done:
341 eaccb85f 2018-12-25 stsp if (repo)
342 eaccb85f 2018-12-25 stsp got_repo_close(repo);
343 7ac97322 2018-03-11 stsp free(path_got);
344 056e7441 2018-03-11 stsp free(path_lock);
345 271d2a38 2018-12-25 stsp free(head_ref_str);
346 eaccb85f 2018-12-25 stsp free(base_commit_id_str);
347 6d9d28c3 2018-03-11 stsp if (err) {
348 6d9d28c3 2018-03-11 stsp if (fd != -1)
349 6d9d28c3 2018-03-11 stsp close(fd);
350 6d9d28c3 2018-03-11 stsp if (*worktree != NULL)
351 6d9d28c3 2018-03-11 stsp got_worktree_close(*worktree);
352 6d9d28c3 2018-03-11 stsp *worktree = NULL;
353 6d9d28c3 2018-03-11 stsp } else
354 056e7441 2018-03-11 stsp (*worktree)->lockfd = fd;
355 6d9d28c3 2018-03-11 stsp
356 6d9d28c3 2018-03-11 stsp return err;
357 86c3caaf 2018-03-09 stsp }
358 86c3caaf 2018-03-09 stsp
359 86c3caaf 2018-03-09 stsp void
360 86c3caaf 2018-03-09 stsp got_worktree_close(struct got_worktree *worktree)
361 86c3caaf 2018-03-09 stsp {
362 c88eb298 2018-03-11 stsp free(worktree->root_path);
363 cde76477 2018-03-11 stsp free(worktree->repo_path);
364 6d9d28c3 2018-03-11 stsp free(worktree->path_prefix);
365 eaccb85f 2018-12-25 stsp free(worktree->base_commit_id);
366 271d2a38 2018-12-25 stsp if (worktree->head_ref)
367 271d2a38 2018-12-25 stsp got_ref_close(worktree->head_ref);
368 056e7441 2018-03-11 stsp if (worktree->lockfd != -1)
369 056e7441 2018-03-11 stsp close(worktree->lockfd);
370 6d9d28c3 2018-03-11 stsp free(worktree);
371 86c3caaf 2018-03-09 stsp }
372 86c3caaf 2018-03-09 stsp
373 86c3caaf 2018-03-09 stsp char *
374 86c3caaf 2018-03-09 stsp got_worktree_get_repo_path(struct got_worktree *worktree)
375 86c3caaf 2018-03-09 stsp {
376 cde76477 2018-03-11 stsp return strdup(worktree->repo_path);
377 86c3caaf 2018-03-09 stsp }
378 86c3caaf 2018-03-09 stsp
379 35be1456 2018-03-11 stsp char *
380 35be1456 2018-03-11 stsp got_worktree_get_head_ref_name(struct got_worktree *worktree)
381 86c3caaf 2018-03-09 stsp {
382 271d2a38 2018-12-25 stsp return got_ref_to_str(worktree->head_ref);
383 86c3caaf 2018-03-09 stsp }
384 86c3caaf 2018-03-09 stsp
385 9d31a1d8 2018-03-11 stsp static const struct got_error *
386 9d31a1d8 2018-03-11 stsp lock_worktree(struct got_worktree *worktree, int operation)
387 86c3caaf 2018-03-09 stsp {
388 9d31a1d8 2018-03-11 stsp if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
389 9d31a1d8 2018-03-11 stsp return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
390 9d31a1d8 2018-03-11 stsp : got_error_from_errno());
391 86c3caaf 2018-03-09 stsp return NULL;
392 86c3caaf 2018-03-09 stsp }
393 9d31a1d8 2018-03-11 stsp
394 9d31a1d8 2018-03-11 stsp static const char *
395 9d31a1d8 2018-03-11 stsp apply_path_prefix(struct got_worktree *worktree, const char *path)
396 9d31a1d8 2018-03-11 stsp {
397 9d31a1d8 2018-03-11 stsp const char *p = path;
398 9d31a1d8 2018-03-11 stsp p += strlen(worktree->path_prefix);
399 9d31a1d8 2018-03-11 stsp if (*p == '/')
400 9d31a1d8 2018-03-11 stsp p++;
401 9d31a1d8 2018-03-11 stsp return p;
402 9d31a1d8 2018-03-11 stsp }
403 9d31a1d8 2018-03-11 stsp
404 9d31a1d8 2018-03-11 stsp static const struct got_error *
405 a207cf0a 2018-12-29 stsp blob_checkout(struct got_worktree *worktree, struct got_fileindex *fileindex,
406 d7b62c98 2018-12-27 stsp const char *path, struct got_blob_object *blob, struct got_repository *repo,
407 d7b62c98 2018-12-27 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
408 d7b62c98 2018-12-27 stsp const char *progress_path)
409 9d31a1d8 2018-03-11 stsp {
410 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
411 c34b20a2 2018-03-12 stsp char *ondisk_path;
412 9d31a1d8 2018-03-11 stsp int fd;
413 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
414 9d31a1d8 2018-03-11 stsp struct got_fileindex_entry *entry;
415 9d31a1d8 2018-03-11 stsp
416 c34b20a2 2018-03-12 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path,
417 9d31a1d8 2018-03-11 stsp apply_path_prefix(worktree, path)) == -1)
418 0a585a0d 2018-03-17 stsp return got_error_from_errno();
419 144ad43a 2018-12-29 stsp
420 144ad43a 2018-12-29 stsp entry = got_fileindex_entry_get(fileindex,
421 144ad43a 2018-12-29 stsp apply_path_prefix(worktree, path));
422 144ad43a 2018-12-29 stsp if (entry && memcmp(entry->commit_sha1, worktree->base_commit_id->sha1,
423 144ad43a 2018-12-29 stsp SHA1_DIGEST_LENGTH) == 0)
424 144ad43a 2018-12-29 stsp return NULL;
425 9d31a1d8 2018-03-11 stsp
426 c34b20a2 2018-03-12 stsp fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
427 9d31a1d8 2018-03-11 stsp GOT_DEFAULT_FILE_MODE);
428 9d31a1d8 2018-03-11 stsp if (fd == -1) {
429 9d31a1d8 2018-03-11 stsp err = got_error_from_errno();
430 9d31a1d8 2018-03-11 stsp if (errno == EEXIST) {
431 9d31a1d8 2018-03-11 stsp struct stat sb;
432 c34b20a2 2018-03-12 stsp if (lstat(ondisk_path, &sb) == -1) {
433 9d31a1d8 2018-03-11 stsp err = got_error_from_errno();
434 9d31a1d8 2018-03-11 stsp } else if (!S_ISREG(sb.st_mode)) {
435 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
436 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_FILE_OBSTRUCTED);
437 d70b8e30 2018-12-27 stsp } else {
438 d70b8e30 2018-12-27 stsp /* TODO: Merge the file! */
439 eecfbcd1 2018-12-29 stsp (*progress_cb)(progress_arg, GOT_STATUS_EXISTS,
440 d7b62c98 2018-12-27 stsp progress_path);
441 d70b8e30 2018-12-27 stsp return NULL;
442 9d31a1d8 2018-03-11 stsp }
443 9d31a1d8 2018-03-11 stsp }
444 9d31a1d8 2018-03-11 stsp return err;
445 9d31a1d8 2018-03-11 stsp }
446 9d31a1d8 2018-03-11 stsp
447 eecfbcd1 2018-12-29 stsp (*progress_cb)(progress_arg, GOT_STATUS_ADD, progress_path);
448 d7b62c98 2018-12-27 stsp
449 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
450 9d31a1d8 2018-03-11 stsp do {
451 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
452 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
453 9d31a1d8 2018-03-11 stsp if (err)
454 9d31a1d8 2018-03-11 stsp break;
455 9d31a1d8 2018-03-11 stsp if (len > 0) {
456 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
457 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
458 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
459 9d31a1d8 2018-03-11 stsp err = got_error_from_errno();
460 b87c6f83 2018-12-24 stsp goto done;
461 61d6eaa3 2018-12-24 stsp } else if (outlen != len - hdrlen) {
462 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
463 b87c6f83 2018-12-24 stsp goto done;
464 9d31a1d8 2018-03-11 stsp }
465 61d6eaa3 2018-12-24 stsp hdrlen = 0;
466 9d31a1d8 2018-03-11 stsp }
467 9d31a1d8 2018-03-11 stsp } while (len != 0);
468 9d31a1d8 2018-03-11 stsp
469 9d31a1d8 2018-03-11 stsp fsync(fd);
470 9d31a1d8 2018-03-11 stsp
471 51514078 2018-12-25 stsp if (entry)
472 51514078 2018-12-25 stsp err = got_fileindex_entry_update(entry, ondisk_path,
473 51514078 2018-12-25 stsp blob->id.sha1, worktree->base_commit_id->sha1);
474 51514078 2018-12-25 stsp else {
475 51514078 2018-12-25 stsp err = got_fileindex_entry_alloc(&entry, ondisk_path,
476 51514078 2018-12-25 stsp apply_path_prefix(worktree, path), blob->id.sha1,
477 51514078 2018-12-25 stsp worktree->base_commit_id->sha1);
478 51514078 2018-12-25 stsp if (err)
479 51514078 2018-12-25 stsp goto done;
480 51514078 2018-12-25 stsp err = got_fileindex_entry_add(fileindex, entry);
481 51514078 2018-12-25 stsp }
482 9d31a1d8 2018-03-11 stsp if (err)
483 9d31a1d8 2018-03-11 stsp goto done;
484 9d31a1d8 2018-03-11 stsp done:
485 9d31a1d8 2018-03-11 stsp close(fd);
486 c34b20a2 2018-03-12 stsp free(ondisk_path);
487 9d31a1d8 2018-03-11 stsp return err;
488 9d31a1d8 2018-03-11 stsp }
489 9d31a1d8 2018-03-11 stsp
490 9d31a1d8 2018-03-11 stsp static const struct got_error *
491 9d31a1d8 2018-03-11 stsp add_dir_on_disk(struct got_worktree *worktree, const char *path)
492 9d31a1d8 2018-03-11 stsp {
493 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
494 9d31a1d8 2018-03-11 stsp char *abspath;
495 9d31a1d8 2018-03-11 stsp
496 9d31a1d8 2018-03-11 stsp if (asprintf(&abspath, "%s/%s", worktree->root_path,
497 9d31a1d8 2018-03-11 stsp apply_path_prefix(worktree, path)) == -1)
498 0a585a0d 2018-03-17 stsp return got_error_from_errno();
499 9d31a1d8 2018-03-11 stsp
500 9d31a1d8 2018-03-11 stsp /* XXX queue work rather than editing disk directly? */
501 9d31a1d8 2018-03-11 stsp if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1) {
502 9d31a1d8 2018-03-11 stsp struct stat sb;
503 9d31a1d8 2018-03-11 stsp
504 9d31a1d8 2018-03-11 stsp if (errno != EEXIST) {
505 9d31a1d8 2018-03-11 stsp err = got_error_from_errno();
506 9d31a1d8 2018-03-11 stsp goto done;
507 9d31a1d8 2018-03-11 stsp }
508 9d31a1d8 2018-03-11 stsp
509 9d31a1d8 2018-03-11 stsp if (lstat(abspath, &sb) == -1) {
510 9d31a1d8 2018-03-11 stsp err = got_error_from_errno();
511 9d31a1d8 2018-03-11 stsp goto done;
512 9d31a1d8 2018-03-11 stsp }
513 9d31a1d8 2018-03-11 stsp
514 9d31a1d8 2018-03-11 stsp if (!S_ISDIR(sb.st_mode)) {
515 9d31a1d8 2018-03-11 stsp /* TODO directory is obstructed; do something */
516 9d31a1d8 2018-03-11 stsp return got_error(GOT_ERR_FILE_OBSTRUCTED);
517 9d31a1d8 2018-03-11 stsp }
518 9d31a1d8 2018-03-11 stsp }
519 9d31a1d8 2018-03-11 stsp
520 9d31a1d8 2018-03-11 stsp done:
521 9d31a1d8 2018-03-11 stsp free(abspath);
522 9d31a1d8 2018-03-11 stsp return err;
523 9d31a1d8 2018-03-11 stsp }
524 9d31a1d8 2018-03-11 stsp
525 9d31a1d8 2018-03-11 stsp static const struct got_error *
526 9d31a1d8 2018-03-11 stsp tree_checkout(struct got_worktree *, struct got_fileindex *,
527 92a684f4 2018-03-12 stsp struct got_tree_object *, const char *, struct got_repository *,
528 99437157 2018-11-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
529 99437157 2018-11-11 stsp got_worktree_cancel_cb cancel_cb, void *cancel_arg);
530 9d31a1d8 2018-03-11 stsp
531 9d31a1d8 2018-03-11 stsp static const struct got_error *
532 9d31a1d8 2018-03-11 stsp tree_checkout_entry(struct got_worktree *worktree,
533 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex, struct got_tree_entry *te,
534 92a684f4 2018-03-12 stsp const char *parent, struct got_repository *repo,
535 99437157 2018-11-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
536 99437157 2018-11-11 stsp got_worktree_cancel_cb cancel_cb, void *cancel_arg)
537 9d31a1d8 2018-03-11 stsp {
538 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
539 9d31a1d8 2018-03-11 stsp struct got_object *obj = NULL;
540 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
541 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
542 9d31a1d8 2018-03-11 stsp char *path = NULL;
543 f7b38925 2018-04-01 stsp char *progress_path = NULL;
544 9d31a1d8 2018-03-11 stsp size_t len;
545 9d31a1d8 2018-03-11 stsp
546 9d31a1d8 2018-03-11 stsp if (parent[0] == '/' && parent[1] == '\0')
547 9d31a1d8 2018-03-11 stsp parent = "";
548 9d31a1d8 2018-03-11 stsp if (asprintf(&path, "%s/%s", parent, te->name) == -1)
549 0a585a0d 2018-03-17 stsp return got_error_from_errno();
550 9d31a1d8 2018-03-11 stsp
551 9d31a1d8 2018-03-11 stsp /* Skip this entry if it is outside of our path prefix. */
552 9d31a1d8 2018-03-11 stsp len = MIN(strlen(worktree->path_prefix), strlen(path));
553 9d31a1d8 2018-03-11 stsp if (strncmp(path, worktree->path_prefix, len) != 0) {
554 9d31a1d8 2018-03-11 stsp free(path);
555 9d31a1d8 2018-03-11 stsp return NULL;
556 9d31a1d8 2018-03-11 stsp }
557 9d31a1d8 2018-03-11 stsp
558 9d31a1d8 2018-03-11 stsp err = got_object_open(&obj, repo, te->id);
559 9d31a1d8 2018-03-11 stsp if (err)
560 9d31a1d8 2018-03-11 stsp goto done;
561 9d31a1d8 2018-03-11 stsp
562 f7b38925 2018-04-01 stsp progress_path = path;
563 f7b38925 2018-04-01 stsp if (strncmp(progress_path, worktree->path_prefix, len) == 0)
564 f7b38925 2018-04-01 stsp progress_path += len;
565 92a684f4 2018-03-12 stsp
566 15a94983 2018-12-23 stsp switch (obj->type) {
567 9d31a1d8 2018-03-11 stsp case GOT_OBJ_TYPE_BLOB:
568 9d31a1d8 2018-03-11 stsp if (strlen(worktree->path_prefix) >= strlen(path))
569 9d31a1d8 2018-03-11 stsp break;
570 9d31a1d8 2018-03-11 stsp err = got_object_blob_open(&blob, repo, obj, 8192);
571 9d31a1d8 2018-03-11 stsp if (err)
572 9d31a1d8 2018-03-11 stsp goto done;
573 a207cf0a 2018-12-29 stsp err = blob_checkout(worktree, fileindex, path, blob, repo,
574 d7b62c98 2018-12-27 stsp progress_cb, progress_arg, progress_path);
575 9d31a1d8 2018-03-11 stsp break;
576 9d31a1d8 2018-03-11 stsp case GOT_OBJ_TYPE_TREE:
577 9d31a1d8 2018-03-11 stsp err = got_object_tree_open(&tree, repo, obj);
578 9d31a1d8 2018-03-11 stsp if (err)
579 9d31a1d8 2018-03-11 stsp goto done;
580 9d31a1d8 2018-03-11 stsp if (strlen(worktree->path_prefix) < strlen(path)) {
581 9d31a1d8 2018-03-11 stsp err = add_dir_on_disk(worktree, path);
582 9d31a1d8 2018-03-11 stsp if (err)
583 9d31a1d8 2018-03-11 stsp break;
584 9d31a1d8 2018-03-11 stsp }
585 99437157 2018-11-11 stsp /* XXX infinite recursion possible */
586 92a684f4 2018-03-12 stsp err = tree_checkout(worktree, fileindex, tree, path, repo,
587 99437157 2018-11-11 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
588 9d31a1d8 2018-03-11 stsp break;
589 9d31a1d8 2018-03-11 stsp default:
590 9d31a1d8 2018-03-11 stsp break;
591 9d31a1d8 2018-03-11 stsp }
592 9d31a1d8 2018-03-11 stsp
593 9d31a1d8 2018-03-11 stsp done:
594 9d31a1d8 2018-03-11 stsp if (blob)
595 9d31a1d8 2018-03-11 stsp got_object_blob_close(blob);
596 9d31a1d8 2018-03-11 stsp if (tree)
597 9d31a1d8 2018-03-11 stsp got_object_tree_close(tree);
598 9d31a1d8 2018-03-11 stsp if (obj)
599 9d31a1d8 2018-03-11 stsp got_object_close(obj);
600 9d31a1d8 2018-03-11 stsp free(path);
601 9d31a1d8 2018-03-11 stsp return err;
602 9d31a1d8 2018-03-11 stsp }
603 9d31a1d8 2018-03-11 stsp
604 9d31a1d8 2018-03-11 stsp static const struct got_error *
605 9d31a1d8 2018-03-11 stsp tree_checkout(struct got_worktree *worktree,
606 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex, struct got_tree_object *tree,
607 92a684f4 2018-03-12 stsp const char *path, struct got_repository *repo,
608 99437157 2018-11-11 stsp got_worktree_checkout_cb progress_cb, void *progress_arg,
609 99437157 2018-11-11 stsp got_worktree_cancel_cb cancel_cb, void *cancel_arg)
610 9d31a1d8 2018-03-11 stsp {
611 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
612 883f0469 2018-06-23 stsp const struct got_tree_entries *entries;
613 9d31a1d8 2018-03-11 stsp struct got_tree_entry *te;
614 9d31a1d8 2018-03-11 stsp size_t len;
615 9d31a1d8 2018-03-11 stsp
616 9d31a1d8 2018-03-11 stsp /* Skip this tree if it is outside of our path prefix. */
617 9d31a1d8 2018-03-11 stsp len = MIN(strlen(worktree->path_prefix), strlen(path));
618 9d31a1d8 2018-03-11 stsp if (strncmp(path, worktree->path_prefix, len) != 0)
619 9d31a1d8 2018-03-11 stsp return NULL;
620 9d31a1d8 2018-03-11 stsp
621 883f0469 2018-06-23 stsp entries = got_object_tree_get_entries(tree);
622 883f0469 2018-06-23 stsp SIMPLEQ_FOREACH(te, &entries->head, entry) {
623 99437157 2018-11-11 stsp if (cancel_cb) {
624 99437157 2018-11-11 stsp err = (*cancel_cb)(cancel_arg);
625 99437157 2018-11-11 stsp if (err)
626 99437157 2018-11-11 stsp break;
627 99437157 2018-11-11 stsp }
628 92a684f4 2018-03-12 stsp err = tree_checkout_entry(worktree, fileindex, te, path, repo,
629 99437157 2018-11-11 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
630 9d31a1d8 2018-03-11 stsp if (err)
631 9d31a1d8 2018-03-11 stsp break;
632 9d31a1d8 2018-03-11 stsp }
633 9d31a1d8 2018-03-11 stsp
634 9d31a1d8 2018-03-11 stsp return err;
635 9d31a1d8 2018-03-11 stsp }
636 9d31a1d8 2018-03-11 stsp
637 9d31a1d8 2018-03-11 stsp const struct got_error *
638 9d31a1d8 2018-03-11 stsp got_worktree_checkout_files(struct got_worktree *worktree,
639 93a30277 2018-12-24 stsp struct got_repository *repo, got_worktree_checkout_cb progress_cb,
640 93a30277 2018-12-24 stsp void *progress_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
641 9d31a1d8 2018-03-11 stsp {
642 a143fb78 2018-12-25 stsp const struct got_error *err = NULL, *unlockerr, *checkout_err = NULL;
643 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
644 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
645 9d31a1d8 2018-03-11 stsp char *fileindex_path = NULL, *new_fileindex_path = NULL;
646 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
647 51514078 2018-12-25 stsp FILE *index = NULL, *new_index = NULL;
648 9d31a1d8 2018-03-11 stsp
649 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
650 9d31a1d8 2018-03-11 stsp if (err)
651 9d31a1d8 2018-03-11 stsp return err;
652 93a30277 2018-12-24 stsp
653 7426bbfd 2018-12-24 stsp fileindex = got_fileindex_alloc();
654 9d31a1d8 2018-03-11 stsp if (fileindex == NULL) {
655 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
656 9d31a1d8 2018-03-11 stsp goto done;
657 9d31a1d8 2018-03-11 stsp }
658 9d31a1d8 2018-03-11 stsp
659 9d31a1d8 2018-03-11 stsp if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
660 9d31a1d8 2018-03-11 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
661 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
662 9d31a1d8 2018-03-11 stsp fileindex_path = NULL;
663 9d31a1d8 2018-03-11 stsp goto done;
664 9d31a1d8 2018-03-11 stsp }
665 9d31a1d8 2018-03-11 stsp
666 51514078 2018-12-25 stsp /*
667 51514078 2018-12-25 stsp * Read the file index.
668 51514078 2018-12-25 stsp * Checking out files is supposed to be an idempotent operation.
669 51514078 2018-12-25 stsp * If the on-disk file index is incomplete we will try to complete it.
670 51514078 2018-12-25 stsp */
671 51514078 2018-12-25 stsp index = fopen(fileindex_path, "rb");
672 51514078 2018-12-25 stsp if (index == NULL) {
673 51514078 2018-12-25 stsp err = got_error_from_errno();
674 51514078 2018-12-25 stsp goto done;
675 51514078 2018-12-25 stsp }
676 51514078 2018-12-25 stsp err = got_fileindex_read(fileindex, index);
677 51514078 2018-12-25 stsp fclose(index);
678 51514078 2018-12-25 stsp if (err)
679 51514078 2018-12-25 stsp goto done;
680 51514078 2018-12-25 stsp
681 b8bdcc21 2018-12-24 stsp err = got_opentemp_named(&new_fileindex_path, &new_index,
682 b8bdcc21 2018-12-24 stsp fileindex_path);
683 51664889 2018-03-12 stsp if (err)
684 51664889 2018-03-12 stsp goto done;
685 51664889 2018-03-12 stsp
686 eaccb85f 2018-12-25 stsp err = got_object_open_as_commit(&commit, repo,
687 eaccb85f 2018-12-25 stsp worktree->base_commit_id);
688 9d31a1d8 2018-03-11 stsp if (err)
689 9d31a1d8 2018-03-11 stsp goto done;
690 9d31a1d8 2018-03-11 stsp
691 93a30277 2018-12-24 stsp err = got_object_open_as_tree(&tree, repo, commit->tree_id);
692 9d31a1d8 2018-03-11 stsp if (err)
693 9d31a1d8 2018-03-11 stsp goto done;
694 9d31a1d8 2018-03-11 stsp
695 a143fb78 2018-12-25 stsp checkout_err = tree_checkout(worktree, fileindex, tree, "/", repo,
696 a143fb78 2018-12-25 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
697 9d31a1d8 2018-03-11 stsp
698 a143fb78 2018-12-25 stsp /* Try to sync the fileindex back to disk in any case. */
699 b8bdcc21 2018-12-24 stsp err = got_fileindex_write(fileindex, new_index);
700 9d31a1d8 2018-03-11 stsp if (err)
701 9d31a1d8 2018-03-11 stsp goto done;
702 9d31a1d8 2018-03-11 stsp
703 9d31a1d8 2018-03-11 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
704 9d31a1d8 2018-03-11 stsp err = got_error_from_errno();
705 9d31a1d8 2018-03-11 stsp goto done;
706 9d31a1d8 2018-03-11 stsp }
707 9d31a1d8 2018-03-11 stsp
708 9d31a1d8 2018-03-11 stsp free(new_fileindex_path);
709 9d31a1d8 2018-03-11 stsp new_fileindex_path = NULL;
710 9d31a1d8 2018-03-11 stsp
711 9d31a1d8 2018-03-11 stsp done:
712 edfa7d7f 2018-09-11 stsp if (tree)
713 edfa7d7f 2018-09-11 stsp got_object_tree_close(tree);
714 9d31a1d8 2018-03-11 stsp if (commit)
715 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
716 9d31a1d8 2018-03-11 stsp if (new_fileindex_path)
717 9d31a1d8 2018-03-11 stsp unlink(new_fileindex_path);
718 b8bdcc21 2018-12-24 stsp if (new_index)
719 b8bdcc21 2018-12-24 stsp fclose(new_index);
720 9d31a1d8 2018-03-11 stsp free(new_fileindex_path);
721 9d31a1d8 2018-03-11 stsp free(fileindex_path);
722 7426bbfd 2018-12-24 stsp got_fileindex_free(fileindex);
723 a143fb78 2018-12-25 stsp if (checkout_err)
724 a143fb78 2018-12-25 stsp err = checkout_err;
725 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
726 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
727 9d31a1d8 2018-03-11 stsp err = unlockerr;
728 9d31a1d8 2018-03-11 stsp return err;
729 9d31a1d8 2018-03-11 stsp }