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_lib.h"
33 #include "got_path_lib.h"
34 #include "got_sha1_lib.h"
36 static const struct got_error *
37 create_meta_file(const char *path_got, const char *name, const char *content)
38 {
39 const struct got_error *err = NULL;
40 char *path;
41 int fd = -1;
42 char buf[4];
43 ssize_t n;
45 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
46 err = got_error(GOT_ERR_NO_MEM);
47 path = NULL;
48 goto done;
49 }
51 fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
52 GOT_DEFAULT_FILE_MODE);
53 if (fd == -1) {
54 err = got_error_from_errno();
55 goto done;
56 }
58 /* The file should be empty. */
59 n = read(fd, buf, sizeof(buf));
60 if (n != 0) {
61 err = (n == -1 ? got_error_from_errno() :
62 got_error(GOT_ERR_WORKTREE_EXISTS));
63 goto done;
64 }
66 if (content) {
67 int len = dprintf(fd, "%s\n", content);
68 if (len != strlen(content) + 1) {
69 err = got_error_from_errno();
70 goto done;
71 }
72 }
74 done:
75 if (fd != -1 && close(fd) == -1 && err == NULL)
76 err = got_error_from_errno();
77 free(path);
78 return err;
79 }
81 static const struct got_error *
82 read_meta_file(char **content, const char *path_got, const char *name)
83 {
84 const struct got_error *err = NULL;
85 char *path;
86 int fd = -1;
87 ssize_t n;
88 struct stat sb;
90 *content = NULL;
92 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
93 err = got_error(GOT_ERR_NO_MEM);
94 path = NULL;
95 goto done;
96 }
98 fd = open(path, O_RDONLY | O_NOFOLLOW);
99 if (fd == -1) {
100 err = got_error_from_errno();
101 goto done;
103 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
104 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
105 : got_error_from_errno());
106 goto done;
109 stat(path, &sb);
110 *content = calloc(1, sb.st_size);
111 if (*content == NULL) {
112 err = got_error(GOT_ERR_NO_MEM);
113 goto done;
116 n = read(fd, *content, sb.st_size);
117 if (n != sb.st_size) {
118 err = (n == -1 ? got_error_from_errno() :
119 got_error(GOT_ERR_WORKTREE_META));
120 goto done;
122 if ((*content)[sb.st_size - 1] != '\n') {
123 err = got_error(GOT_ERR_WORKTREE_META);
124 goto done;
126 (*content)[sb.st_size - 1] = '\0';
128 done:
129 if (fd != -1 && close(fd) == -1 && err == NULL)
130 err = got_error_from_errno();
131 free(path);
132 if (err) {
133 free(*content);
134 *content = NULL;
136 return err;
139 const struct got_error *
140 got_worktree_init(const char *path, struct got_reference *head_ref,
141 const char *prefix, struct got_repository *repo)
143 const struct got_error *err = NULL;
144 char *path_got = NULL;
145 char *refstr = NULL;
146 char *repo_path = NULL;
147 char *formatstr = NULL;
149 if (!got_path_is_absolute(prefix))
150 return got_error(GOT_ERR_BAD_PATH);
152 /* Create top-level directory (may already exist). */
153 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
154 err = got_error_from_errno();
155 goto done;
158 /* Create .got directory (may already exist). */
159 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
160 err = got_error(GOT_ERR_NO_MEM);
161 goto done;
163 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
164 err = got_error_from_errno();
165 goto done;
168 /* Create an empty lock file. */
169 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
170 if (err)
171 goto done;
173 /* Create an empty file index. */
174 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
175 if (err)
176 goto done;
178 /* Save an invalid base commit hash. */
179 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT,
180 GOT_WORKTREE_INVALID_COMMIT_ID);
181 if (err)
182 goto done;
184 /* Write the HEAD reference. */
185 refstr = got_ref_to_str(head_ref);
186 if (refstr == NULL) {
187 err = got_error(GOT_ERR_NO_MEM);
188 goto done;
190 err = create_meta_file(path_got, GOT_WORKTREE_HEAD, refstr);
191 if (err)
192 goto done;
194 /* Store path to repository. */
195 repo_path = got_repo_get_path(repo);
196 if (repo_path == NULL) {
197 err = got_error(GOT_ERR_NO_MEM);
198 goto done;
200 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY, repo_path);
201 if (err)
202 goto done;
204 /* Store in-repository path prefix. */
205 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX, prefix);
206 if (err)
207 goto done;
209 /* Stamp work tree with format file. */
210 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
211 err = got_error(GOT_ERR_NO_MEM);
212 goto done;
214 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
215 if (err)
216 goto done;
218 done:
219 free(path_got);
220 free(formatstr);
221 free(refstr);
222 free(repo_path);
223 return err;
226 const struct got_error *
227 got_worktree_open(struct got_worktree **worktree, const char *path)
229 const struct got_error *err = NULL;
230 char *path_got;
231 char *refstr = NULL;
232 char *formatstr = NULL;
233 char *path_lock = NULL;
234 int version, fd = -1;
235 const char *errstr;
237 *worktree = NULL;
239 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
240 err = got_error(GOT_ERR_NO_MEM);
241 path_got = NULL;
242 goto done;
245 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
246 err = got_error(GOT_ERR_NO_MEM);
247 path_lock = NULL;
248 goto done;
251 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
252 if (fd == -1) {
253 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
254 : got_error_from_errno());
255 goto done;
258 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
259 if (err)
260 goto done;
262 version = strtonum(formatstr, 1, INT_MAX, &errstr);
263 if (errstr) {
264 err = got_error(GOT_ERR_WORKTREE_META);
265 goto done;
267 if (version != GOT_WORKTREE_FORMAT_VERSION) {
268 err = got_error(GOT_ERR_WORKTREE_VERS);
269 goto done;
272 *worktree = calloc(1, sizeof(**worktree));
273 if (*worktree == NULL) {
274 err = got_error(GOT_ERR_NO_MEM);
275 goto done;
277 (*worktree)->lockfd = -1;
279 (*worktree)->root_path = strdup(path);
280 if ((*worktree)->root_path == NULL) {
281 err = got_error(GOT_ERR_NO_MEM);
282 goto done;
284 err = read_meta_file(&(*worktree)->repo_path, path_got,
285 GOT_WORKTREE_REPOSITORY);
286 if (err)
287 goto done;
288 err = read_meta_file(&(*worktree)->path_prefix, path_got,
289 GOT_WORKTREE_PATH_PREFIX);
290 if (err)
291 goto done;
293 err = read_meta_file(&(*worktree)->base_commit, path_got,
294 GOT_WORKTREE_BASE_COMMIT);
295 if (err)
296 goto done;
298 err = read_meta_file(&(*worktree)->head_ref, path_got,
299 GOT_WORKTREE_HEAD);
300 if (err)
301 goto done;
303 done:
304 free(path_got);
305 free(path_lock);
306 if (err) {
307 if (fd != -1)
308 close(fd);
309 if (*worktree != NULL)
310 got_worktree_close(*worktree);
311 *worktree = NULL;
312 } else
313 (*worktree)->lockfd = fd;
315 return err;
318 void
319 got_worktree_close(struct got_worktree *worktree)
321 free(worktree->root_path);
322 free(worktree->repo_path);
323 free(worktree->path_prefix);
324 free(worktree->base_commit);
325 free(worktree->head_ref);
326 if (worktree->lockfd != -1)
327 close(worktree->lockfd);
328 free(worktree);
331 char *
332 got_worktree_get_repo_path(struct got_worktree *worktree)
334 return strdup(worktree->repo_path);
337 struct got_reference *
338 got_worktree_get_head(struct got_worktree *worktree)
340 return NULL;
343 const struct got_error *
344 got_worktree_change_head(struct got_worktree *worktree, struct got_reference *head,
345 struct got_repository *repo)
347 return NULL;
350 const struct got_error *
351 got_worktree_checkout_files(struct got_worktree *worktree,
352 struct got_repository *repo)
354 return NULL;