Blob


1 /*
2 * Copyright (c) 2018 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 fclose(f);
125 return ret;
128 static int
129 check_meta_file_exists(const char *worktree_path, const char *name)
131 struct stat sb;
132 char *path;
133 int ret = 0;
135 if (asprintf(&path, "%s/%s/%s", worktree_path, GOT_WORKTREE_GOT_DIR,
136 name) == -1)
137 return 0;
138 if (stat(path, &sb) == 0)
139 ret = 1;
140 if (verbose) {
141 char *content;
142 if (read_meta_file(&content, path) == 0) {
143 test_printf("%s:\t%s\n", name, content);
144 free(content);
147 free(path);
148 return ret;
151 static int
152 worktree_init(const char *repo_path)
154 const struct got_error *err;
155 struct got_repository *repo = NULL;
156 struct got_reference *head_ref = NULL;
157 char worktree_path[PATH_MAX];
158 int ok = 0;
160 err = got_repo_open(&repo, repo_path);
161 if (err != NULL || repo == NULL)
162 goto done;
163 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
164 if (err != NULL || head_ref == NULL)
165 goto done;
167 strlcpy(worktree_path, "worktree-XXXXXX", sizeof(worktree_path));
168 if (mkdtemp(worktree_path) == NULL)
169 goto done;
171 err = got_worktree_init(worktree_path, head_ref, "/", repo);
172 if (err != NULL)
173 goto done;
175 /* Ensure required files were created. */
176 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_HEAD_REF))
177 goto done;
178 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_BASE_COMMIT))
179 goto done;
180 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_LOCK))
181 goto done;
182 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_FILE_INDEX))
183 goto done;
184 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_REPOSITORY))
185 goto done;
186 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_PATH_PREFIX))
187 goto done;
188 if (!check_meta_file_exists(worktree_path, GOT_WORKTREE_FORMAT))
189 goto done;
191 if (!remove_worktree(worktree_path))
192 goto done;
193 ok = 1;
194 done:
195 if (head_ref)
196 got_ref_close(head_ref);
197 if (repo)
198 got_repo_close(repo);
199 return ok;
202 static int
203 obstruct_meta_file(char **path, const char *worktree_path, const char *name)
205 FILE *f;
206 char *s = "This file should not be here\n";
207 int ret = 1;
209 if (asprintf(path, "%s/%s/%s", worktree_path, GOT_WORKTREE_GOT_DIR,
210 name) == -1)
211 return 0;
212 f = fopen(*path, "w+");
213 if (f == NULL) {
214 free(*path);
215 return 0;
217 if (fwrite(s, 1, strlen(s), f) != strlen(s)) {
218 free(*path);
219 ret = 0;
221 fclose(f);
222 return ret;
225 static int
226 obstruct_meta_file_and_init(int *ok, struct got_repository *repo,
227 const char *worktree_path, char *name)
229 const struct got_error *err;
230 char *path;
231 int ret = 0;
232 struct got_reference *head_ref = NULL;
234 if (!obstruct_meta_file(&path, worktree_path, GOT_WORKTREE_FILE_INDEX))
235 return 0;
237 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
238 if (err != NULL || head_ref == NULL)
239 return 0;
241 err = got_worktree_init(worktree_path, head_ref, "/", repo);
242 if (err != NULL && err->code == GOT_ERR_ERRNO && errno == EEXIST) {
243 (*ok)++;
244 ret = 1;
246 unlink(path);
247 free(path);
248 got_ref_close(head_ref);
249 return ret;
252 static int
253 worktree_init_exists(const char *repo_path)
255 const struct got_error *err;
256 struct got_repository *repo = NULL;
257 char worktree_path[PATH_MAX];
258 char *gotpath = NULL;
259 int ok = 0;
261 err = got_repo_open(&repo, repo_path);
262 if (err != NULL || repo == NULL)
263 goto done;
264 strlcpy(worktree_path, "worktree-XXXXXX", sizeof(worktree_path));
265 if (mkdtemp(worktree_path) == NULL)
266 goto done;
267 if (mkdir(worktree_path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST)
268 goto done;
270 if (asprintf(&gotpath, "%s/%s", worktree_path, GOT_WORKTREE_GOT_DIR)
271 == -1)
272 goto done;
273 if (mkdir(gotpath, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST)
274 goto done;
276 /* Create files which got_worktree_init() will try to create as well. */
277 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
278 GOT_WORKTREE_HEAD_REF))
279 goto done;
280 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
281 GOT_WORKTREE_BASE_COMMIT))
282 goto done;
283 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
284 GOT_WORKTREE_LOCK))
285 goto done;
286 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
287 GOT_WORKTREE_FILE_INDEX))
288 goto done;
289 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
290 GOT_WORKTREE_REPOSITORY))
291 goto done;
292 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
293 GOT_WORKTREE_PATH_PREFIX))
294 goto done;
295 if (!obstruct_meta_file_and_init(&ok, repo, worktree_path,
296 GOT_WORKTREE_FORMAT))
297 goto done;
299 done:
300 if (repo)
301 got_repo_close(repo);
302 free(gotpath);
303 if (ok == 7)
304 remove_worktree(worktree_path);
305 return (ok == 7);
308 static void
309 progress_cb(void *arg, unsigned char status, const char *path)
313 static int
314 worktree_checkout(const char *repo_path)
316 const struct got_error *err;
317 struct got_repository *repo = NULL;
318 struct got_reference *head_ref = NULL;
319 struct got_worktree *worktree = NULL;
320 char *makefile_path = NULL, *cfile_path = NULL;
321 char worktree_path[PATH_MAX];
322 int ok = 0;
323 struct stat sb;
325 err = got_repo_open(&repo, repo_path);
326 if (err != NULL || repo == NULL)
327 goto done;
328 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
329 if (err != NULL || head_ref == NULL)
330 goto done;
332 strlcpy(worktree_path, "worktree-XXXXXX", sizeof(worktree_path));
333 if (mkdtemp(worktree_path) == NULL)
334 goto done;
336 err = got_worktree_init(worktree_path, head_ref, "/regress/worktree",
337 repo);
338 if (err != NULL)
339 goto done;
341 err = got_worktree_open(&worktree, worktree_path);
342 if (err != NULL)
343 goto done;
345 err = got_worktree_checkout_files(worktree, repo, progress_cb, NULL,
346 NULL, NULL);
347 if (err != NULL)
348 goto done;
350 test_printf("checked out %s\n", worktree_path);
352 /* The work tree should contain a Makefile and worktree_test.c. */
353 if (asprintf(&makefile_path, "%s/Makefile", worktree_path) == -1)
354 goto done;
355 if (stat(makefile_path, &sb) != 0)
356 goto done;
357 else
358 unlink(makefile_path);
359 if (asprintf(&cfile_path, "%s/worktree_test.c", worktree_path) == -1)
360 goto done;
361 if (stat(cfile_path, &sb) != 0)
362 goto done;
363 else
364 unlink(cfile_path);
366 if (!remove_worktree(worktree_path))
367 goto done;
369 ok = 1;
370 done:
371 if (worktree)
372 got_worktree_close(worktree);
373 if (head_ref)
374 got_ref_close(head_ref);
375 if (repo)
376 got_repo_close(repo);
377 free(makefile_path);
378 free(cfile_path);
379 return ok;
382 #define RUN_TEST(expr, name) \
383 { test_ok = (expr); \
384 printf("test_%s %s\n", (name), test_ok ? "ok" : "failed"); \
385 failure = (failure || !test_ok); }
388 void
389 usage(void)
391 fprintf(stderr, "usage: worktree_test [-v] [REPO_PATH]\n");
394 int
395 main(int argc, char *argv[])
397 int test_ok = 0, failure = 0;
398 const char *repo_path;
399 char *cwd = NULL;
400 int ch;
402 #ifndef PROFILE
403 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
404 NULL) == -1)
405 err(1, "pledge");
406 #endif
408 while ((ch = getopt(argc, argv, "v")) != -1) {
409 switch (ch) {
410 case 'v':
411 verbose = 1;
412 break;
413 default:
414 usage();
415 return 1;
418 argc -= optind;
419 argv += optind;
421 if (argc == 0)
422 repo_path = GOT_REPO_PATH;
423 else if (argc == 1)
424 repo_path = argv[0];
425 else {
426 usage();
427 return 1;
430 cwd = getcwd(NULL, 0);
431 if (cwd == NULL)
432 err(1, "getcwd");
433 if (unveil(cwd, "rwc") != 0)
434 err(1, "unvail");
435 free(cwd);
437 if (unveil("/tmp", "rwc") != 0)
438 err(1, "unveil");
440 if (unveil(repo_path, "r") != 0)
441 err(1, "unveil");
443 if (got_privsep_unveil_exec_helpers() != NULL)
444 return 1;
446 if (unveil(NULL, NULL) != 0)
447 err(1, "unveil");
449 RUN_TEST(worktree_init(repo_path), "init");
450 RUN_TEST(worktree_init_exists(repo_path), "init exists");
451 RUN_TEST(worktree_checkout(repo_path), "checkout");
453 return failure ? 1 : 0;