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_path.h"
38 #include "got_worktree.h"
39 #include "got_opentemp.h"
40 #include "got_privsep.h"
42 #include "got_lib_worktree.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;
94 err = got_worktree_get_base_ref_name(&refname, worktree);
95 if (err)
96 return err;
98 err = got_ref_open(&base_ref, repo, refname, 0);
99 if (err)
100 goto done;
102 err = got_ref_delete(base_ref, repo);
103 done:
104 if (base_ref)
105 got_ref_close(base_ref);
106 free(refname);
107 return err;
111 static int
112 remove_worktree(const char *worktree_path)
114 if (!remove_meta_file(worktree_path, GOT_WORKTREE_HEAD_REF))
115 return 0;
116 if (!remove_meta_file(worktree_path, GOT_WORKTREE_BASE_COMMIT))
117 return 0;
118 if (!remove_meta_file(worktree_path, GOT_WORKTREE_FILE_INDEX))
119 return 0;
120 if (!remove_meta_file(worktree_path, GOT_WORKTREE_REPOSITORY))
121 return 0;
122 if (!remove_meta_file(worktree_path, GOT_WORKTREE_PATH_PREFIX))
123 return 0;
124 if (!remove_meta_file(worktree_path, GOT_WORKTREE_LOCK))
125 return 0;
126 if (!remove_meta_file(worktree_path, GOT_WORKTREE_FORMAT))
127 return 0;
128 if (!remove_meta_file(worktree_path, GOT_WORKTREE_UUID))
129 return 0;
130 if (!remove_got_dir(worktree_path))
131 return 0;
132 if (rmdir(worktree_path) == -1)
133 return 0;
134 return 1;
137 static int
138 read_meta_file(char **content, const char *path)
140 FILE *f;
141 size_t len;
142 const char delim[3] = {'\0', '\0', '\0'};
143 int ret = 0;
145 f = fopen(path, "r");
146 if (f == NULL)
147 return errno;
149 *content = fparseln(f, &len, NULL, delim, 0);
150 if (*content == NULL)
151 ret = errno;
152 if (fclose(f) != 0 && ret == 0)
153 ret = errno;
154 return ret;
157 static int
158 check_meta_file_exists(const char *worktree_path, const char *name)
160 struct stat sb;
161 char *path;
162 int ret = 0;
164 if (asprintf(&path, "%s/%s/%s", worktree_path, GOT_WORKTREE_GOT_DIR,
165 name) == -1)
166 return 0;
167 if (stat(path, &sb) == 0)
168 ret = 1;
169 if (verbose) {
170 char *content;
171 if (read_meta_file(&content, path) == 0) {
172 test_printf("%s:\t%s\n", name, content);
173 free(content);
176 free(path);
177 return ret;
180 static int
181 worktree_init(const char *repo_path)
183 const struct got_error *err;
184 struct got_repository *repo = NULL;
185 struct got_reference *head_ref = NULL;
186 char worktree_path[PATH_MAX];
187 int ok = 0;
189 err = got_repo_open(&repo, repo_path);
190 if (err != NULL || repo == NULL)
191 goto done;
192 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
193 if (err != NULL || head_ref == NULL)
194 goto done;
196 strlcpy(worktree_path, "worktree-XXXXXX", sizeof(worktree_path));
197 if (mkdtemp(worktree_path) == NULL)
198 goto done;
200 err = got_worktree_init(worktree_path, head_ref, "/", repo);
201 if (err != NULL)
202 goto done;
204 /* Ensure required files were created. */
205 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_HEAD_REF))
206 goto done;
207 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_BASE_COMMIT))
208 goto done;
209 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_LOCK))
210 goto done;
211 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_FILE_INDEX))
212 goto done;
213 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_REPOSITORY))
214 goto done;
215 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_PATH_PREFIX))
216 goto done;
217 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_FORMAT))
218 goto done;
219 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_UUID))
220 goto done;
222 if (!remove_worktree(worktree_path))
223 goto done;
224 ok = 1;
225 done:
226 if (head_ref)
227 got_ref_close(head_ref);
228 if (repo)
229 got_repo_close(repo);
230 return ok;
233 static int
234 obstruct_meta_file(char **path, const char *worktree_path, const char *name)
236 FILE *f;
237 char *s = "This file should not be here\n";
238 int ret = 1;
240 if (asprintf(path, "%s/%s/%s", worktree_path, GOT_WORKTREE_GOT_DIR,
241 name) == -1)
242 return 0;
243 f = fopen(*path, "w+");
244 if (f == NULL) {
245 free(*path);
246 return 0;
248 if (fwrite(s, 1, strlen(s), f) != strlen(s)) {
249 free(*path);
250 ret = 0;
252 if (fclose(f) != 0)
253 ret = 0;
254 return ret;
257 static int
258 obstruct_meta_file_and_init(int *ok, struct got_repository *repo,
259 const char *worktree_path, char *name)
261 const struct got_error *err;
262 char *path;
263 int ret = 0;
264 struct got_reference *head_ref = NULL;
266 if (!obstruct_meta_file(&path, worktree_path, GOT_WORKTREE_FILE_INDEX))
267 return 0;
269 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
270 if (err != NULL || head_ref == NULL)
271 return 0;
273 err = got_worktree_init(worktree_path, head_ref, "/", repo);
274 if (err != NULL && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
275 (*ok)++;
276 ret = 1;
278 unlink(path);
279 free(path);
280 got_ref_close(head_ref);
281 return ret;
284 static int
285 worktree_init_exists(const char *repo_path)
287 const struct got_error *err;
288 struct got_repository *repo = NULL;
289 char worktree_path[PATH_MAX];
290 char *gotpath = NULL;
291 int ok = 0;
293 err = got_repo_open(&repo, repo_path);
294 if (err != NULL || repo == NULL)
295 goto done;
296 strlcpy(worktree_path, "worktree-XXXXXX", sizeof(worktree_path));
297 if (mkdtemp(worktree_path) == NULL)
298 goto done;
299 if (mkdir(worktree_path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST)
300 goto done;
302 if (asprintf(&gotpath, "%s/%s", worktree_path, GOT_WORKTREE_GOT_DIR)
303 == -1)
304 goto done;
305 if (mkdir(gotpath, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST)
306 goto done;
308 /* Create files which got_worktree_init() will try to create as well. */
309 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
310 GOT_WORKTREE_HEAD_REF))
311 goto done;
312 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
313 GOT_WORKTREE_BASE_COMMIT))
314 goto done;
315 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
316 GOT_WORKTREE_LOCK))
317 goto done;
318 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
319 GOT_WORKTREE_FILE_INDEX))
320 goto done;
321 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
322 GOT_WORKTREE_REPOSITORY))
323 goto done;
324 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
325 GOT_WORKTREE_PATH_PREFIX))
326 goto done;
327 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
328 GOT_WORKTREE_FORMAT))
329 goto done;
331 done:
332 if (repo)
333 got_repo_close(repo);
334 free(gotpath);
335 if (ok == 7)
336 remove_worktree(worktree_path);
337 return (ok == 7);
340 static const struct got_error *
341 progress_cb(void *arg, unsigned char status, const char *path)
343 return NULL;
346 static int
347 worktree_checkout(const char *repo_path)
349 const struct got_error *err;
350 struct got_repository *repo = NULL;
351 struct got_reference *head_ref = NULL;
352 struct got_worktree *worktree = NULL;
353 char *makefile_path = NULL, *cfile_path = NULL;
354 char worktree_path[PATH_MAX];
355 int ok = 0;
356 struct stat sb;
358 err = got_repo_open(&repo, repo_path);
359 if (err != NULL || repo == NULL)
360 goto done;
361 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
362 if (err != NULL || head_ref == NULL)
363 goto done;
365 strlcpy(worktree_path, "worktree-XXXXXX", sizeof(worktree_path));
366 if (mkdtemp(worktree_path) == NULL)
367 goto done;
369 err = got_worktree_init(worktree_path, head_ref, "/regress/worktree",
370 repo);
371 if (err != NULL)
372 goto done;
374 err = got_worktree_open(&worktree, worktree_path);
375 if (err != NULL)
376 goto done;
378 err = got_worktree_checkout_files(worktree, "", repo, progress_cb, NULL,
379 NULL, NULL);
380 if (err != NULL)
381 goto done;
383 test_printf("checked out %s\n", worktree_path);
385 /* The work tree should contain a Makefile and worktree_test.c. */
386 if (asprintf(&makefile_path, "%s/Makefile", worktree_path) == -1)
387 goto done;
388 if (stat(makefile_path, &sb) != 0)
389 goto done;
390 else
391 unlink(makefile_path);
392 if (asprintf(&cfile_path, "%s/worktree_test.c", worktree_path) == -1)
393 goto done;
394 if (stat(cfile_path, &sb) != 0)
395 goto done;
396 else
397 unlink(cfile_path);
399 err = remove_worktree_base_ref(worktree, repo);
400 if (err)
401 goto done;
402 if (!remove_worktree(worktree_path))
403 goto done;
405 ok = 1;
406 done:
407 if (worktree)
408 got_worktree_close(worktree);
409 if (head_ref)
410 got_ref_close(head_ref);
411 if (repo)
412 got_repo_close(repo);
413 free(makefile_path);
414 free(cfile_path);
415 return ok;
418 #define RUN_TEST(expr, name) \
419 { test_ok = (expr); \
420 printf("test_%s %s\n", (name), test_ok ? "ok" : "failed"); \
421 failure = (failure || !test_ok); }
424 void
425 usage(void)
427 fprintf(stderr, "usage: worktree_test [-v] [REPO_PATH]\n");
430 int
431 main(int argc, char *argv[])
433 int test_ok = 0, failure = 0;
434 const char *repo_path;
435 char *cwd = NULL;
436 int ch;
438 #ifndef PROFILE
439 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
440 "unveil", NULL) == -1)
441 err(1, "pledge");
442 #endif
444 while ((ch = getopt(argc, argv, "v")) != -1) {
445 switch (ch) {
446 case 'v':
447 verbose = 1;
448 break;
449 default:
450 usage();
451 return 1;
454 argc -= optind;
455 argv += optind;
457 if (argc == 0)
458 repo_path = GOT_REPO_PATH;
459 else if (argc == 1)
460 repo_path = argv[0];
461 else {
462 usage();
463 return 1;
466 cwd = getcwd(NULL, 0);
467 if (cwd == NULL)
468 err(1, "getcwd");
469 if (unveil(cwd, "rwc") != 0)
470 err(1, "unvail");
471 free(cwd);
473 if (unveil("/tmp", "rwc") != 0)
474 err(1, "unveil");
476 if (unveil(repo_path, "rwc") != 0)
477 err(1, "unveil");
479 if (got_privsep_unveil_exec_helpers() != NULL)
480 return 1;
482 if (unveil(NULL, NULL) != 0)
483 err(1, "unveil");
485 RUN_TEST(worktree_init(repo_path), "init");
486 RUN_TEST(worktree_init_exists(repo_path), "init exists");
487 RUN_TEST(worktree_checkout(repo_path), "checkout");
489 return failure ? 1 : 0;