Blob


1 /*
2 * Copyright (c) 2018, 2019 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/param.h>
18 #include <sys/queue.h>
19 #include <sys/limits.h>
20 #include <sys/stat.h>
22 #include <stdarg.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <errno.h>
28 #include <util.h>
29 #include <err.h>
30 #include <unistd.h>
31 #include <uuid.h>
33 #include "got_error.h"
34 #include "got_object.h"
35 #include "got_reference.h"
36 #include "got_repository.h"
37 #include "got_worktree.h"
38 #include "got_opentemp.h"
39 #include "got_privsep.h"
41 #include "got_lib_worktree.h"
42 #include "got_lib_path.h"
44 #define GOT_REPO_PATH "../../../"
46 static int verbose;
48 void
49 test_printf(char *fmt, ...)
50 {
51 va_list ap;
53 if (!verbose)
54 return;
56 va_start(ap, fmt);
57 vprintf(fmt, ap);
58 va_end(ap);
59 }
61 static int
62 remove_got_dir(const char *worktree_path)
63 {
64 char *path;
66 if (asprintf(&path, "%s/%s", worktree_path, GOT_WORKTREE_GOT_DIR) == -1)
67 return 0;
68 rmdir(path);
69 free(path);
70 return 1;
71 }
73 static int
74 remove_meta_file(const char *worktree_path, const char *name)
75 {
76 char *path;
78 if (asprintf(&path, "%s/%s/%s", worktree_path, GOT_WORKTREE_GOT_DIR,
79 name) == -1)
80 return 0;
81 unlink(path);
82 free(path);
83 return 1;
84 }
86 static const struct got_error *
87 remove_worktree_base_ref(struct got_worktree *worktree,
88 struct got_repository *repo)
89 {
90 const struct got_error *err = NULL;
91 struct got_reference *base_ref = NULL;
92 char *refname = NULL, *absrefname = NULL;
94 err = got_worktree_get_base_ref_name(&refname, worktree);
95 if (err)
96 return err;
98 if (asprintf(&absrefname, "refs/%s", refname) == -1) {
99 err = got_error_from_errno();
100 goto done;
102 err = got_ref_open(&base_ref, repo, absrefname);
103 if (err)
104 goto done;
106 err = got_ref_delete(base_ref, repo);
107 done:
108 if (base_ref)
109 got_ref_close(base_ref);
110 free(refname);
111 free(absrefname);
112 return err;
116 static int
117 remove_worktree(const char *worktree_path)
119 if (!remove_meta_file(worktree_path, GOT_WORKTREE_HEAD_REF))
120 return 0;
121 if (!remove_meta_file(worktree_path, GOT_WORKTREE_BASE_COMMIT))
122 return 0;
123 if (!remove_meta_file(worktree_path, GOT_WORKTREE_FILE_INDEX))
124 return 0;
125 if (!remove_meta_file(worktree_path, GOT_WORKTREE_REPOSITORY))
126 return 0;
127 if (!remove_meta_file(worktree_path, GOT_WORKTREE_PATH_PREFIX))
128 return 0;
129 if (!remove_meta_file(worktree_path, GOT_WORKTREE_LOCK))
130 return 0;
131 if (!remove_meta_file(worktree_path, GOT_WORKTREE_FORMAT))
132 return 0;
133 if (!remove_meta_file(worktree_path, GOT_WORKTREE_UUID))
134 return 0;
135 if (!remove_got_dir(worktree_path))
136 return 0;
137 if (rmdir(worktree_path) == -1)
138 return 0;
139 return 1;
142 static int
143 read_meta_file(char **content, const char *path)
145 FILE *f;
146 size_t len;
147 const char delim[3] = {'\0', '\0', '\0'};
148 int ret = 0;
150 f = fopen(path, "r");
151 if (f == NULL)
152 return errno;
154 *content = fparseln(f, &len, NULL, delim, 0);
155 if (*content == NULL)
156 ret = errno;
157 if (fclose(f) != 0 && ret == 0)
158 ret = errno;
159 return ret;
162 static int
163 check_meta_file_exists(const char *worktree_path, const char *name)
165 struct stat sb;
166 char *path;
167 int ret = 0;
169 if (asprintf(&path, "%s/%s/%s", worktree_path, GOT_WORKTREE_GOT_DIR,
170 name) == -1)
171 return 0;
172 if (stat(path, &sb) == 0)
173 ret = 1;
174 if (verbose) {
175 char *content;
176 if (read_meta_file(&content, path) == 0) {
177 test_printf("%s:\t%s\n", name, content);
178 free(content);
181 free(path);
182 return ret;
185 static int
186 worktree_init(const char *repo_path)
188 const struct got_error *err;
189 struct got_repository *repo = NULL;
190 struct got_reference *head_ref = NULL;
191 char worktree_path[PATH_MAX];
192 int ok = 0;
194 err = got_repo_open(&repo, repo_path);
195 if (err != NULL || repo == NULL)
196 goto done;
197 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
198 if (err != NULL || head_ref == NULL)
199 goto done;
201 strlcpy(worktree_path, "worktree-XXXXXX", sizeof(worktree_path));
202 if (mkdtemp(worktree_path) == NULL)
203 goto done;
205 err = got_worktree_init(worktree_path, head_ref, "/", repo);
206 if (err != NULL)
207 goto done;
209 /* Ensure required files were created. */
210 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_HEAD_REF))
211 goto done;
212 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_BASE_COMMIT))
213 goto done;
214 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_LOCK))
215 goto done;
216 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_FILE_INDEX))
217 goto done;
218 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_REPOSITORY))
219 goto done;
220 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_PATH_PREFIX))
221 goto done;
222 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_FORMAT))
223 goto done;
224 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_UUID))
225 goto done;
227 if (!remove_worktree(worktree_path))
228 goto done;
229 ok = 1;
230 done:
231 if (head_ref)
232 got_ref_close(head_ref);
233 if (repo)
234 got_repo_close(repo);
235 return ok;
238 static int
239 obstruct_meta_file(char **path, const char *worktree_path, const char *name)
241 FILE *f;
242 char *s = "This file should not be here\n";
243 int ret = 1;
245 if (asprintf(path, "%s/%s/%s", worktree_path, GOT_WORKTREE_GOT_DIR,
246 name) == -1)
247 return 0;
248 f = fopen(*path, "w+");
249 if (f == NULL) {
250 free(*path);
251 return 0;
253 if (fwrite(s, 1, strlen(s), f) != strlen(s)) {
254 free(*path);
255 ret = 0;
257 if (fclose(f) != 0)
258 ret = 0;
259 return ret;
262 static int
263 obstruct_meta_file_and_init(int *ok, struct got_repository *repo,
264 const char *worktree_path, char *name)
266 const struct got_error *err;
267 char *path;
268 int ret = 0;
269 struct got_reference *head_ref = NULL;
271 if (!obstruct_meta_file(&path, worktree_path, GOT_WORKTREE_FILE_INDEX))
272 return 0;
274 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
275 if (err != NULL || head_ref == NULL)
276 return 0;
278 err = got_worktree_init(worktree_path, head_ref, "/", repo);
279 if (err != NULL && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
280 (*ok)++;
281 ret = 1;
283 unlink(path);
284 free(path);
285 got_ref_close(head_ref);
286 return ret;
289 static int
290 worktree_init_exists(const char *repo_path)
292 const struct got_error *err;
293 struct got_repository *repo = NULL;
294 char worktree_path[PATH_MAX];
295 char *gotpath = NULL;
296 int ok = 0;
298 err = got_repo_open(&repo, repo_path);
299 if (err != NULL || repo == NULL)
300 goto done;
301 strlcpy(worktree_path, "worktree-XXXXXX", sizeof(worktree_path));
302 if (mkdtemp(worktree_path) == NULL)
303 goto done;
304 if (mkdir(worktree_path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST)
305 goto done;
307 if (asprintf(&gotpath, "%s/%s", worktree_path, GOT_WORKTREE_GOT_DIR)
308 == -1)
309 goto done;
310 if (mkdir(gotpath, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST)
311 goto done;
313 /* Create files which got_worktree_init() will try to create as well. */
314 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
315 GOT_WORKTREE_HEAD_REF))
316 goto done;
317 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
318 GOT_WORKTREE_BASE_COMMIT))
319 goto done;
320 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
321 GOT_WORKTREE_LOCK))
322 goto done;
323 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
324 GOT_WORKTREE_FILE_INDEX))
325 goto done;
326 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
327 GOT_WORKTREE_REPOSITORY))
328 goto done;
329 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
330 GOT_WORKTREE_PATH_PREFIX))
331 goto done;
332 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
333 GOT_WORKTREE_FORMAT))
334 goto done;
336 done:
337 if (repo)
338 got_repo_close(repo);
339 free(gotpath);
340 if (ok == 7)
341 remove_worktree(worktree_path);
342 return (ok == 7);
345 static void
346 progress_cb(void *arg, unsigned char status, const char *path)
350 static int
351 worktree_checkout(const char *repo_path)
353 const struct got_error *err;
354 struct got_repository *repo = NULL;
355 struct got_reference *head_ref = NULL;
356 struct got_worktree *worktree = NULL;
357 char *makefile_path = NULL, *cfile_path = NULL;
358 char worktree_path[PATH_MAX];
359 int ok = 0;
360 struct stat sb;
362 err = got_repo_open(&repo, repo_path);
363 if (err != NULL || repo == NULL)
364 goto done;
365 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
366 if (err != NULL || head_ref == NULL)
367 goto done;
369 strlcpy(worktree_path, "worktree-XXXXXX", sizeof(worktree_path));
370 if (mkdtemp(worktree_path) == NULL)
371 goto done;
373 err = got_worktree_init(worktree_path, head_ref, "/regress/worktree",
374 repo);
375 if (err != NULL)
376 goto done;
378 err = got_worktree_open(&worktree, worktree_path);
379 if (err != NULL)
380 goto done;
382 err = got_worktree_checkout_files(worktree, repo, progress_cb, NULL,
383 NULL, NULL);
384 if (err != NULL)
385 goto done;
387 test_printf("checked out %s\n", worktree_path);
389 /* The work tree should contain a Makefile and worktree_test.c. */
390 if (asprintf(&makefile_path, "%s/Makefile", worktree_path) == -1)
391 goto done;
392 if (stat(makefile_path, &sb) != 0)
393 goto done;
394 else
395 unlink(makefile_path);
396 if (asprintf(&cfile_path, "%s/worktree_test.c", worktree_path) == -1)
397 goto done;
398 if (stat(cfile_path, &sb) != 0)
399 goto done;
400 else
401 unlink(cfile_path);
403 err = remove_worktree_base_ref(worktree, repo);
404 if (err)
405 goto done;
406 if (!remove_worktree(worktree_path))
407 goto done;
409 ok = 1;
410 done:
411 if (worktree)
412 got_worktree_close(worktree);
413 if (head_ref)
414 got_ref_close(head_ref);
415 if (repo)
416 got_repo_close(repo);
417 free(makefile_path);
418 free(cfile_path);
419 return ok;
422 #define RUN_TEST(expr, name) \
423 { test_ok = (expr); \
424 printf("test_%s %s\n", (name), test_ok ? "ok" : "failed"); \
425 failure = (failure || !test_ok); }
428 void
429 usage(void)
431 fprintf(stderr, "usage: worktree_test [-v] [REPO_PATH]\n");
434 int
435 main(int argc, char *argv[])
437 int test_ok = 0, failure = 0;
438 const char *repo_path;
439 char *cwd = NULL;
440 int ch;
442 #ifndef PROFILE
443 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
444 "unveil", NULL) == -1)
445 err(1, "pledge");
446 #endif
448 while ((ch = getopt(argc, argv, "v")) != -1) {
449 switch (ch) {
450 case 'v':
451 verbose = 1;
452 break;
453 default:
454 usage();
455 return 1;
458 argc -= optind;
459 argv += optind;
461 if (argc == 0)
462 repo_path = GOT_REPO_PATH;
463 else if (argc == 1)
464 repo_path = argv[0];
465 else {
466 usage();
467 return 1;
470 cwd = getcwd(NULL, 0);
471 if (cwd == NULL)
472 err(1, "getcwd");
473 if (unveil(cwd, "rwc") != 0)
474 err(1, "unvail");
475 free(cwd);
477 if (unveil("/tmp", "rwc") != 0)
478 err(1, "unveil");
480 if (unveil(repo_path, "rwc") != 0)
481 err(1, "unveil");
483 if (got_privsep_unveil_exec_helpers() != NULL)
484 return 1;
486 if (unveil(NULL, NULL) != 0)
487 err(1, "unveil");
489 RUN_TEST(worktree_init(repo_path), "init");
490 RUN_TEST(worktree_init_exists(repo_path), "init exists");
491 RUN_TEST(worktree_checkout(repo_path), "checkout");
493 return failure ? 1 : 0;