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/stat.h>
19 #include <string.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <fcntl.h>
23 #include <errno.h>
24 #include <unistd.h>
26 #include "got_error.h"
27 #include "got_repository.h"
28 #include "got_refs.h"
29 #include "got_worktree.h"
31 #include "got_worktree_priv.h"
32 #include "got_path_priv.h"
34 static const struct got_error *
35 create_meta_file(const char *gotpath, const char *name, const char *content)
36 {
37 const struct got_error *err = NULL;
38 char *path;
39 int fd = -1;
40 char buf[4];
41 ssize_t n;
43 if (asprintf(&path, "%s/%s", gotpath, name) == -1) {
44 err = got_error(GOT_ERR_NO_MEM);
45 path = NULL;
46 goto done;
47 }
49 fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_EXLOCK | O_NOFOLLOW,
50 GOT_DEFAULT_FILE_MODE);
51 if (fd == -1) {
52 err = got_error_from_errno();
53 goto done;
54 }
56 /* The file should be empty. */
57 n = read(fd, buf, sizeof(buf));
58 if (n != 0) {
59 err = (n == -1 ? got_error_from_errno() :
60 got_error(GOT_ERR_WORKTREE_EXISTS));
61 goto done;
62 }
64 if (content) {
65 int len = dprintf(fd, "%s\n", content);
66 if (len != strlen(content) + 1) {
67 err = got_error_from_errno();
68 goto done;
69 }
70 }
72 done:
73 if (fd != -1 && close(fd) == -1 && err == NULL)
74 err = got_error_from_errno();
75 free(path);
76 return err;
77 }
79 const struct got_error *
80 got_worktree_init(const char *path, struct got_reference *head_ref,
81 const char *prefix, struct got_repository *repo)
82 {
83 const struct got_error *err = NULL;
84 char *abspath = NULL;
85 char *normpath = NULL;
86 char *gotpath = NULL;
87 char *refstr = NULL;
88 char *path_repos = NULL;
89 char *formatstr = NULL;
91 if (!got_path_is_absolute(prefix))
92 return got_error(GOT_ERR_BAD_PATH);
94 if (got_path_is_absolute(path)) {
95 abspath = strdup(path);
96 if (abspath == NULL)
97 return got_error(GOT_ERR_NO_MEM);
98 } else {
99 abspath = got_path_get_absolute(path);
100 if (abspath == NULL)
101 return got_error(GOT_ERR_BAD_PATH);
104 /* Create top-level directory (may already exist). */
105 normpath = got_path_normalize(abspath);
106 if (normpath == NULL) {
107 err = got_error(GOT_ERR_BAD_PATH);
108 goto done;
110 if (mkdir(normpath, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
111 err = got_error_from_errno();
112 goto done;
115 /* Create .got directory (may already exist). */
116 if (asprintf(&gotpath, "%s/%s", normpath, GOT_WORKTREE_GOT_DIR) == -1) {
117 err = got_error(GOT_ERR_NO_MEM);
118 goto done;
120 if (mkdir(gotpath, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
121 err = got_error_from_errno();
122 goto done;
125 /* Create an empty file index. */
126 err = create_meta_file(gotpath, GOT_WORKTREE_FILE_INDEX, NULL);
127 if (err)
128 goto done;
130 /* Write the HEAD reference. */
131 refstr = got_ref_to_str(head_ref);
132 if (refstr == NULL) {
133 err = got_error(GOT_ERR_NO_MEM);
134 goto done;
136 err = create_meta_file(gotpath, GOT_REF_HEAD, refstr);
137 if (err)
138 goto done;
140 /* Store path to repository. */
141 path_repos = got_repo_get_path(repo);
142 if (path_repos == NULL) {
143 err = got_error(GOT_ERR_NO_MEM);
144 goto done;
146 err = create_meta_file(gotpath, GOT_WORKTREE_REPOSITORY, path_repos);
147 if (err)
148 goto done;
150 /* Store in-repository path prefix. */
151 err = create_meta_file(gotpath, GOT_WORKTREE_PATH_PREFIX, prefix);
152 if (err)
153 goto done;
155 /* Stamp work tree with format file. */
156 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
157 err = got_error(GOT_ERR_NO_MEM);
158 goto done;
160 err = create_meta_file(gotpath, GOT_WORKTREE_FORMAT, formatstr);
161 if (err)
162 goto done;
164 done:
165 free(abspath);
166 free(normpath);
167 free(gotpath);
168 free(formatstr);
169 free(refstr);
170 free(path_repos);
171 return err;
174 const struct got_error *
175 got_worktree_open(struct got_worktree **worktree, const char *path)
177 return NULL;
180 void
181 got_worktree_close(struct got_worktree *worktree)
185 char *
186 got_worktree_get_repo_path(struct got_worktree *worktree)
188 return strdup(worktree->path_repo);
191 struct got_reference *
192 got_worktree_get_head(struct got_worktree *worktree)
194 return NULL;
197 const struct got_error *
198 got_worktree_set_head(struct got_worktree *worktree, struct got_reference *head,
199 struct got_repository *repo)
201 return NULL;
204 const struct got_error *
205 got_worktree_checkout_files(struct got_worktree *worktree,
206 struct got_repository *repo)
208 return NULL;