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>
32 #include "got_error.h"
33 #include "got_object.h"
34 #include "got_reference.h"
35 #include "got_repository.h"
36 #include "got_worktree.h"
37 #include "got_opentemp.h"
38 #include "got_privsep.h"
40 #include "got_lib_worktree.h"
41 #include "got_lib_path.h"
43 #define GOT_REPO_PATH "../../../"
45 static int verbose;
47 void
48 test_printf(char *fmt, ...)
49 {
50 va_list ap;
52 if (!verbose)
53 return;
55 va_start(ap, fmt);
56 vprintf(fmt, ap);
57 va_end(ap);
58 }
60 static int
61 remove_got_dir(const char *worktree_path)
62 {
63 char *path;
65 if (asprintf(&path, "%s/%s", worktree_path, GOT_WORKTREE_GOT_DIR) == -1)
66 return 0;
67 rmdir(path);
68 free(path);
69 return 1;
70 }
72 static int
73 remove_meta_file(const char *worktree_path, const char *name)
74 {
75 char *path;
77 if (asprintf(&path, "%s/%s/%s", worktree_path, GOT_WORKTREE_GOT_DIR,
78 name) == -1)
79 return 0;
80 unlink(path);
81 free(path);
82 return 1;
83 }
85 static int
86 remove_worktree(const char *worktree_path)
87 {
88 if (!remove_meta_file(worktree_path, GOT_WORKTREE_HEAD_REF))
89 return 0;
90 if (!remove_meta_file(worktree_path, GOT_WORKTREE_BASE_COMMIT))
91 return 0;
92 if (!remove_meta_file(worktree_path, GOT_WORKTREE_FILE_INDEX))
93 return 0;
94 if (!remove_meta_file(worktree_path, GOT_WORKTREE_REPOSITORY))
95 return 0;
96 if (!remove_meta_file(worktree_path, GOT_WORKTREE_PATH_PREFIX))
97 return 0;
98 if (!remove_meta_file(worktree_path, GOT_WORKTREE_LOCK))
99 return 0;
100 if (!remove_meta_file(worktree_path, GOT_WORKTREE_FORMAT))
101 return 0;
102 if (!remove_got_dir(worktree_path))
103 return 0;
104 if (rmdir(worktree_path) == -1)
105 return 0;
106 return 1;
109 static int
110 read_meta_file(char **content, const char *path)
112 FILE *f;
113 size_t len;
114 const char delim[3] = {'\0', '\0', '\0'};
115 int ret = 0;
117 f = fopen(path, "r");
118 if (f == NULL)
119 return errno;
121 *content = fparseln(f, &len, NULL, delim, 0);
122 if (*content == NULL)
123 ret = errno;
124 if (fclose(f) != 0 && ret == 0)
125 ret = errno;
126 return ret;
129 static int
130 check_meta_file_exists(const char *worktree_path, const char *name)
132 struct stat sb;
133 char *path;
134 int ret = 0;
136 if (asprintf(&path, "%s/%s/%s", worktree_path, GOT_WORKTREE_GOT_DIR,
137 name) == -1)
138 return 0;
139 if (stat(path, &sb) == 0)
140 ret = 1;
141 if (verbose) {
142 char *content;
143 if (read_meta_file(&content, path) == 0) {
144 test_printf("%s:\t%s\n", name, content);
145 free(content);
148 free(path);
149 return ret;
152 static int
153 worktree_init(const char *repo_path)
155 const struct got_error *err;
156 struct got_repository *repo = NULL;
157 struct got_reference *head_ref = NULL;
158 char worktree_path[PATH_MAX];
159 int ok = 0;
161 err = got_repo_open(&repo, repo_path);
162 if (err != NULL || repo == NULL)
163 goto done;
164 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
165 if (err != NULL || head_ref == NULL)
166 goto done;
168 strlcpy(worktree_path, "worktree-XXXXXX", sizeof(worktree_path));
169 if (mkdtemp(worktree_path) == NULL)
170 goto done;
172 err = got_worktree_init(worktree_path, head_ref, "/", repo);
173 if (err != NULL)
174 goto done;
176 /* Ensure required files were created. */
177 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_HEAD_REF))
178 goto done;
179 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_BASE_COMMIT))
180 goto done;
181 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_LOCK))
182 goto done;
183 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_FILE_INDEX))
184 goto done;
185 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_REPOSITORY))
186 goto done;
187 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_PATH_PREFIX))
188 goto done;
189 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_FORMAT))
190 goto done;
192 if (!remove_worktree(worktree_path))
193 goto done;
194 ok = 1;
195 done:
196 if (head_ref)
197 got_ref_close(head_ref);
198 if (repo)
199 got_repo_close(repo);
200 return ok;
203 static int
204 obstruct_meta_file(char **path, const char *worktree_path, const char *name)
206 FILE *f;
207 char *s = "This file should not be here\n";
208 int ret = 1;
210 if (asprintf(path, "%s/%s/%s", worktree_path, GOT_WORKTREE_GOT_DIR,
211 name) == -1)
212 return 0;
213 f = fopen(*path, "w+");
214 if (f == NULL) {
215 free(*path);
216 return 0;
218 if (fwrite(s, 1, strlen(s), f) != strlen(s)) {
219 free(*path);
220 ret = 0;
222 if (fclose(f) != 0)
223 ret = 0;
224 return ret;
227 static int
228 obstruct_meta_file_and_init(int *ok, struct got_repository *repo,
229 const char *worktree_path, char *name)
231 const struct got_error *err;
232 char *path;
233 int ret = 0;
234 struct got_reference *head_ref = NULL;
236 if (!obstruct_meta_file(&path, worktree_path, GOT_WORKTREE_FILE_INDEX))
237 return 0;
239 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
240 if (err != NULL || head_ref == NULL)
241 return 0;
243 err = got_worktree_init(worktree_path, head_ref, "/", repo);
244 if (err != NULL && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
245 (*ok)++;
246 ret = 1;
248 unlink(path);
249 free(path);
250 got_ref_close(head_ref);
251 return ret;
254 static int
255 worktree_init_exists(const char *repo_path)
257 const struct got_error *err;
258 struct got_repository *repo = NULL;
259 char worktree_path[PATH_MAX];
260 char *gotpath = NULL;
261 int ok = 0;
263 err = got_repo_open(&repo, repo_path);
264 if (err != NULL || repo == NULL)
265 goto done;
266 strlcpy(worktree_path, "worktree-XXXXXX", sizeof(worktree_path));
267 if (mkdtemp(worktree_path) == NULL)
268 goto done;
269 if (mkdir(worktree_path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST)
270 goto done;
272 if (asprintf(&gotpath, "%s/%s", worktree_path, GOT_WORKTREE_GOT_DIR)
273 == -1)
274 goto done;
275 if (mkdir(gotpath, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST)
276 goto done;
278 /* Create files which got_worktree_init() will try to create as well. */
279 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
280 GOT_WORKTREE_HEAD_REF))
281 goto done;
282 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
283 GOT_WORKTREE_BASE_COMMIT))
284 goto done;
285 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
286 GOT_WORKTREE_LOCK))
287 goto done;
288 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
289 GOT_WORKTREE_FILE_INDEX))
290 goto done;
291 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
292 GOT_WORKTREE_REPOSITORY))
293 goto done;
294 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
295 GOT_WORKTREE_PATH_PREFIX))
296 goto done;
297 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
298 GOT_WORKTREE_FORMAT))
299 goto done;
301 done:
302 if (repo)
303 got_repo_close(repo);
304 free(gotpath);
305 if (ok == 7)
306 remove_worktree(worktree_path);
307 return (ok == 7);
310 static void
311 progress_cb(void *arg, unsigned char status, const char *path)
315 static int
316 worktree_checkout(const char *repo_path)
318 const struct got_error *err;
319 struct got_repository *repo = NULL;
320 struct got_reference *head_ref = NULL;
321 struct got_worktree *worktree = NULL;
322 char *makefile_path = NULL, *cfile_path = NULL;
323 char worktree_path[PATH_MAX];
324 int ok = 0;
325 struct stat sb;
327 err = got_repo_open(&repo, repo_path);
328 if (err != NULL || repo == NULL)
329 goto done;
330 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
331 if (err != NULL || head_ref == NULL)
332 goto done;
334 strlcpy(worktree_path, "worktree-XXXXXX", sizeof(worktree_path));
335 if (mkdtemp(worktree_path) == NULL)
336 goto done;
338 err = got_worktree_init(worktree_path, head_ref, "/regress/worktree",
339 repo);
340 if (err != NULL)
341 goto done;
343 err = got_worktree_open(&worktree, worktree_path);
344 if (err != NULL)
345 goto done;
347 err = got_worktree_checkout_files(worktree, repo, progress_cb, NULL,
348 NULL, NULL);
349 if (err != NULL)
350 goto done;
352 test_printf("checked out %s\n", worktree_path);
354 /* The work tree should contain a Makefile and worktree_test.c. */
355 if (asprintf(&makefile_path, "%s/Makefile", worktree_path) == -1)
356 goto done;
357 if (stat(makefile_path, &sb) != 0)
358 goto done;
359 else
360 unlink(makefile_path);
361 if (asprintf(&cfile_path, "%s/worktree_test.c", worktree_path) == -1)
362 goto done;
363 if (stat(cfile_path, &sb) != 0)
364 goto done;
365 else
366 unlink(cfile_path);
368 if (!remove_worktree(worktree_path))
369 goto done;
371 ok = 1;
372 done:
373 if (worktree)
374 got_worktree_close(worktree);
375 if (head_ref)
376 got_ref_close(head_ref);
377 if (repo)
378 got_repo_close(repo);
379 free(makefile_path);
380 free(cfile_path);
381 return ok;
384 #define RUN_TEST(expr, name) \
385 { test_ok = (expr); \
386 printf("test_%s %s\n", (name), test_ok ? "ok" : "failed"); \
387 failure = (failure || !test_ok); }
390 void
391 usage(void)
393 fprintf(stderr, "usage: worktree_test [-v] [REPO_PATH]\n");
396 int
397 main(int argc, char *argv[])
399 int test_ok = 0, failure = 0;
400 const char *repo_path;
401 char *cwd = NULL;
402 int ch;
404 #ifndef PROFILE
405 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
406 "unveil", NULL) == -1)
407 err(1, "pledge");
408 #endif
410 while ((ch = getopt(argc, argv, "v")) != -1) {
411 switch (ch) {
412 case 'v':
413 verbose = 1;
414 break;
415 default:
416 usage();
417 return 1;
420 argc -= optind;
421 argv += optind;
423 if (argc == 0)
424 repo_path = GOT_REPO_PATH;
425 else if (argc == 1)
426 repo_path = argv[0];
427 else {
428 usage();
429 return 1;
432 cwd = getcwd(NULL, 0);
433 if (cwd == NULL)
434 err(1, "getcwd");
435 if (unveil(cwd, "rwc") != 0)
436 err(1, "unvail");
437 free(cwd);
439 if (unveil("/tmp", "rwc") != 0)
440 err(1, "unveil");
442 if (unveil(repo_path, "r") != 0)
443 err(1, "unveil");
445 if (got_privsep_unveil_exec_helpers() != NULL)
446 return 1;
448 if (unveil(NULL, NULL) != 0)
449 err(1, "unveil");
451 RUN_TEST(worktree_init(repo_path), "init");
452 RUN_TEST(worktree_init_exists(repo_path), "init exists");
453 RUN_TEST(worktree_checkout(repo_path), "checkout");
455 return failure ? 1 : 0;