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>
18 #include <sys/limits.h>
20 #include <string.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <fcntl.h>
24 #include <errno.h>
25 #include <unistd.h>
27 #include "got_error.h"
28 #include "got_repository.h"
29 #include "got_refs.h"
30 #include "got_worktree.h"
32 #include "got_worktree_priv.h"
33 #include "got_path_priv.h"
35 static const struct got_error *
36 create_meta_file(const char *gotpath, const char *name, const char *content)
37 {
38 const struct got_error *err = NULL;
39 char *path;
40 int fd = -1;
41 char buf[4];
42 ssize_t n;
44 if (asprintf(&path, "%s/%s", gotpath, name) == -1) {
45 err = got_error(GOT_ERR_NO_MEM);
46 path = NULL;
47 goto done;
48 }
50 fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
51 GOT_DEFAULT_FILE_MODE);
52 if (fd == -1) {
53 err = got_error_from_errno();
54 goto done;
55 }
57 /* The file should be empty. */
58 n = read(fd, buf, sizeof(buf));
59 if (n != 0) {
60 err = (n == -1 ? got_error_from_errno() :
61 got_error(GOT_ERR_WORKTREE_EXISTS));
62 goto done;
63 }
65 if (content) {
66 int len = dprintf(fd, "%s\n", content);
67 if (len != strlen(content) + 1) {
68 err = got_error_from_errno();
69 goto done;
70 }
71 }
73 done:
74 if (fd != -1 && close(fd) == -1 && err == NULL)
75 err = got_error_from_errno();
76 free(path);
77 return err;
78 }
80 static const struct got_error *
81 read_meta_file(char **content, const char *gotpath, const char *name)
82 {
83 const struct got_error *err = NULL;
84 char *path;
85 int fd = -1;
86 ssize_t n;
87 struct stat sb;
89 *content = NULL;
91 if (asprintf(&path, "%s/%s", gotpath, name) == -1) {
92 err = got_error(GOT_ERR_NO_MEM);
93 path = NULL;
94 goto done;
95 }
97 fd = open(path, O_RDONLY | O_NOFOLLOW);
98 if (fd == -1) {
99 err = got_error_from_errno();
100 goto done;
102 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
103 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
104 : got_error_from_errno());
105 goto done;
108 stat(path, &sb);
109 *content = calloc(1, sb.st_size);
110 if (*content == NULL) {
111 err = got_error(GOT_ERR_NO_MEM);
112 goto done;
115 n = read(fd, *content, sb.st_size);
116 if (n != sb.st_size) {
117 err = got_error_from_errno();
118 goto done;
120 if ((*content)[sb.st_size - 1] != '\n') {
121 err = got_error(GOT_ERR_WORKTREE_META);
122 goto done;
124 (*content)[sb.st_size - 1] = '\0';
126 done:
127 if (fd != -1 && close(fd) == -1 && err == NULL)
128 err = got_error_from_errno();
129 free(path);
130 if (err) {
131 free(*content);
132 *content = NULL;
134 return err;
137 const struct got_error *
138 got_worktree_init(const char *path, struct got_reference *head_ref,
139 const char *prefix, struct got_repository *repo)
141 const struct got_error *err = NULL;
142 char *gotpath = NULL;
143 char *refstr = NULL;
144 char *path_repos = NULL;
145 char *formatstr = NULL;
147 if (!got_path_is_absolute(prefix))
148 return got_error(GOT_ERR_BAD_PATH);
150 /* Create top-level directory (may already exist). */
151 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
152 err = got_error_from_errno();
153 goto done;
156 /* Create .got directory (may already exist). */
157 if (asprintf(&gotpath, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
158 err = got_error(GOT_ERR_NO_MEM);
159 goto done;
161 if (mkdir(gotpath, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
162 err = got_error_from_errno();
163 goto done;
166 /* Create an empty lock file. */
167 err = create_meta_file(gotpath, GOT_WORKTREE_LOCK, NULL);
168 if (err)
169 goto done;
171 /* Create an empty file index. */
172 err = create_meta_file(gotpath, GOT_WORKTREE_FILE_INDEX, NULL);
173 if (err)
174 goto done;
176 /* Set base commit to empty. */
177 err = create_meta_file(gotpath, GOT_WORKTREE_BASE_COMMIT, NULL);
178 if (err)
179 goto done;
181 /* Write the HEAD reference. */
182 refstr = got_ref_to_str(head_ref);
183 if (refstr == NULL) {
184 err = got_error(GOT_ERR_NO_MEM);
185 goto done;
187 err = create_meta_file(gotpath, GOT_WORKTREE_HEAD, refstr);
188 if (err)
189 goto done;
191 /* Store path to repository. */
192 path_repos = got_repo_get_path(repo);
193 if (path_repos == NULL) {
194 err = got_error(GOT_ERR_NO_MEM);
195 goto done;
197 err = create_meta_file(gotpath, GOT_WORKTREE_REPOSITORY, path_repos);
198 if (err)
199 goto done;
201 /* Store in-repository path prefix. */
202 err = create_meta_file(gotpath, GOT_WORKTREE_PATH_PREFIX, prefix);
203 if (err)
204 goto done;
206 /* Stamp work tree with format file. */
207 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
208 err = got_error(GOT_ERR_NO_MEM);
209 goto done;
211 err = create_meta_file(gotpath, GOT_WORKTREE_FORMAT, formatstr);
212 if (err)
213 goto done;
215 done:
216 free(gotpath);
217 free(formatstr);
218 free(refstr);
219 free(path_repos);
220 return err;
223 const struct got_error *
224 got_worktree_open(struct got_worktree **worktree, const char *path)
226 const struct got_error *err = NULL;
227 char *gotpath;
228 char *refstr = NULL;
229 char *path_repos = NULL;
230 char *formatstr = NULL;
231 char *path_lock = NULL;
232 int version, fd = -1;
233 const char *errstr;
235 *worktree = NULL;
237 if (asprintf(&gotpath, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
238 err = got_error(GOT_ERR_NO_MEM);
239 gotpath = NULL;
240 goto done;
243 if (asprintf(&path_lock, "%s/%s", gotpath, GOT_WORKTREE_LOCK) == -1) {
244 err = got_error(GOT_ERR_NO_MEM);
245 path_lock = NULL;
246 goto done;
249 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
250 if (fd == -1) {
251 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
252 : got_error_from_errno());
253 goto done;
256 err = read_meta_file(&formatstr, gotpath, GOT_WORKTREE_FORMAT);
257 if (err)
258 goto done;
260 version = strtonum(formatstr, 1, INT_MAX, &errstr);
261 if (errstr) {
262 err = got_error(GOT_ERR_WORKTREE_META);
263 goto done;
265 if (version != GOT_WORKTREE_FORMAT_VERSION) {
266 err = got_error(GOT_ERR_WORKTREE_VERS);
267 goto done;
270 *worktree = calloc(1, sizeof(**worktree));
271 if (*worktree == NULL) {
272 err = got_error(GOT_ERR_NO_MEM);
273 goto done;
275 (*worktree)->lockfd = -1;
277 (*worktree)->path_worktree_root = strdup(path);
278 if ((*worktree)->path_worktree_root == NULL) {
279 err = got_error(GOT_ERR_NO_MEM);
280 goto done;
282 err = read_meta_file(&(*worktree)->path_repo, gotpath,
283 GOT_WORKTREE_REPOSITORY);
284 if (err)
285 goto done;
286 err = read_meta_file(&(*worktree)->path_prefix, gotpath,
287 GOT_WORKTREE_PATH_PREFIX);
288 goto done;
290 done:
291 free(gotpath);
292 free(path_lock);
293 if (err) {
294 if (fd != -1)
295 close(fd);
296 if (*worktree != NULL)
297 got_worktree_close(*worktree);
298 *worktree = NULL;
299 } else
300 (*worktree)->lockfd = fd;
302 return err;
305 void
306 got_worktree_close(struct got_worktree *worktree)
308 free(worktree->path_worktree_root);
309 free(worktree->path_repo);
310 free(worktree->path_prefix);
311 if (worktree->lockfd != -1)
312 close(worktree->lockfd);
313 free(worktree);
316 char *
317 got_worktree_get_repo_path(struct got_worktree *worktree)
319 return strdup(worktree->path_repo);
322 struct got_reference *
323 got_worktree_get_head(struct got_worktree *worktree)
325 return NULL;
328 const struct got_error *
329 got_worktree_change_head(struct got_worktree *worktree, struct got_reference *head,
330 struct got_repository *repo)
332 return NULL;
335 const struct got_error *
336 got_worktree_checkout_files(struct got_worktree *worktree,
337 struct got_repository *repo)
339 return NULL;