2 86c3caaf 2018-03-09 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
4 86c3caaf 2018-03-09 stsp * Permission to use, copy, modify, and distribute this software for any
5 86c3caaf 2018-03-09 stsp * purpose with or without fee is hereby granted, provided that the above
6 86c3caaf 2018-03-09 stsp * copyright notice and this permission notice appear in all copies.
8 86c3caaf 2018-03-09 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 86c3caaf 2018-03-09 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 86c3caaf 2018-03-09 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 86c3caaf 2018-03-09 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 86c3caaf 2018-03-09 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 86c3caaf 2018-03-09 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 86c3caaf 2018-03-09 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 86c3caaf 2018-03-09 stsp #include <sys/stat.h>
18 6d9d28c3 2018-03-11 stsp #include <sys/limits.h>
20 86c3caaf 2018-03-09 stsp #include <string.h>
21 86c3caaf 2018-03-09 stsp #include <stdio.h>
22 86c3caaf 2018-03-09 stsp #include <stdlib.h>
23 86c3caaf 2018-03-09 stsp #include <fcntl.h>
24 86c3caaf 2018-03-09 stsp #include <errno.h>
25 86c3caaf 2018-03-09 stsp #include <unistd.h>
27 86c3caaf 2018-03-09 stsp #include "got_error.h"
28 86c3caaf 2018-03-09 stsp #include "got_repository.h"
29 86c3caaf 2018-03-09 stsp #include "got_refs.h"
30 86c3caaf 2018-03-09 stsp #include "got_worktree.h"
32 86c3caaf 2018-03-09 stsp #include "got_worktree_priv.h"
33 86c3caaf 2018-03-09 stsp #include "got_path_priv.h"
34 65e3b818 2018-03-11 stsp #include "got_sha1_priv.h"
36 99724ed4 2018-03-10 stsp static const struct got_error *
37 7ac97322 2018-03-11 stsp create_meta_file(const char *path_got, const char *name, const char *content)
39 99724ed4 2018-03-10 stsp const struct got_error *err = NULL;
41 99724ed4 2018-03-10 stsp int fd = -1;
42 99724ed4 2018-03-10 stsp char buf[4];
45 7ac97322 2018-03-11 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
46 99724ed4 2018-03-10 stsp err = got_error(GOT_ERR_NO_MEM);
47 99724ed4 2018-03-10 stsp path = NULL;
51 73a5ef67 2018-03-11 stsp fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
52 99724ed4 2018-03-10 stsp GOT_DEFAULT_FILE_MODE);
53 99724ed4 2018-03-10 stsp if (fd == -1) {
54 99724ed4 2018-03-10 stsp err = got_error_from_errno();
58 99724ed4 2018-03-10 stsp /* The file should be empty. */
59 99724ed4 2018-03-10 stsp n = read(fd, buf, sizeof(buf));
60 99724ed4 2018-03-10 stsp if (n != 0) {
61 99724ed4 2018-03-10 stsp err = (n == -1 ? got_error_from_errno() :
62 99724ed4 2018-03-10 stsp got_error(GOT_ERR_WORKTREE_EXISTS));
66 99724ed4 2018-03-10 stsp if (content) {
67 99724ed4 2018-03-10 stsp int len = dprintf(fd, "%s\n", content);
68 99724ed4 2018-03-10 stsp if (len != strlen(content) + 1) {
69 99724ed4 2018-03-10 stsp err = got_error_from_errno();
75 99724ed4 2018-03-10 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
76 99724ed4 2018-03-10 stsp err = got_error_from_errno();
81 09fe317a 2018-03-11 stsp static const struct got_error *
82 7ac97322 2018-03-11 stsp read_meta_file(char **content, const char *path_got, const char *name)
84 09fe317a 2018-03-11 stsp const struct got_error *err = NULL;
86 09fe317a 2018-03-11 stsp int fd = -1;
88 09fe317a 2018-03-11 stsp struct stat sb;
90 09fe317a 2018-03-11 stsp *content = NULL;
92 7ac97322 2018-03-11 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
93 09fe317a 2018-03-11 stsp err = got_error(GOT_ERR_NO_MEM);
94 09fe317a 2018-03-11 stsp path = NULL;
98 ef99fdb1 2018-03-11 stsp fd = open(path, O_RDONLY | O_NOFOLLOW);
99 09fe317a 2018-03-11 stsp if (fd == -1) {
100 ef99fdb1 2018-03-11 stsp err = got_error_from_errno();
103 ef99fdb1 2018-03-11 stsp if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
104 73a5ef67 2018-03-11 stsp err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
105 73a5ef67 2018-03-11 stsp : got_error_from_errno());
109 09fe317a 2018-03-11 stsp stat(path, &sb);
110 09fe317a 2018-03-11 stsp *content = calloc(1, sb.st_size);
111 09fe317a 2018-03-11 stsp if (*content == NULL) {
112 09fe317a 2018-03-11 stsp err = got_error(GOT_ERR_NO_MEM);
116 09fe317a 2018-03-11 stsp n = read(fd, *content, sb.st_size);
117 09fe317a 2018-03-11 stsp if (n != sb.st_size) {
118 0605801d 2018-03-11 stsp err = (n == -1 ? got_error_from_errno() :
119 0605801d 2018-03-11 stsp got_error(GOT_ERR_WORKTREE_META));
122 09fe317a 2018-03-11 stsp if ((*content)[sb.st_size - 1] != '\n') {
123 09fe317a 2018-03-11 stsp err = got_error(GOT_ERR_WORKTREE_META);
126 09fe317a 2018-03-11 stsp (*content)[sb.st_size - 1] = '\0';
129 09fe317a 2018-03-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
130 09fe317a 2018-03-11 stsp err = got_error_from_errno();
131 09fe317a 2018-03-11 stsp free(path);
133 09fe317a 2018-03-11 stsp free(*content);
134 09fe317a 2018-03-11 stsp *content = NULL;
136 09fe317a 2018-03-11 stsp return err;
139 86c3caaf 2018-03-09 stsp const struct got_error *
140 86c3caaf 2018-03-09 stsp got_worktree_init(const char *path, struct got_reference *head_ref,
141 577ec78f 2018-03-11 stsp const char *prefix, struct got_repository *repo)
143 86c3caaf 2018-03-09 stsp const struct got_error *err = NULL;
144 7ac97322 2018-03-11 stsp char *path_got = NULL;
145 86c3caaf 2018-03-09 stsp char *refstr = NULL;
146 86c3caaf 2018-03-09 stsp char *path_repos = NULL;
147 1451e70d 2018-03-10 stsp char *formatstr = NULL;
149 577ec78f 2018-03-11 stsp if (!got_path_is_absolute(prefix))
150 577ec78f 2018-03-11 stsp return got_error(GOT_ERR_BAD_PATH);
152 86c3caaf 2018-03-09 stsp /* Create top-level directory (may already exist). */
153 2cb4bacb 2018-03-11 stsp if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
154 86c3caaf 2018-03-09 stsp err = got_error_from_errno();
158 86c3caaf 2018-03-09 stsp /* Create .got directory (may already exist). */
159 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
160 86c3caaf 2018-03-09 stsp err = got_error(GOT_ERR_NO_MEM);
163 7ac97322 2018-03-11 stsp if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
164 86c3caaf 2018-03-09 stsp err = got_error_from_errno();
168 056e7441 2018-03-11 stsp /* Create an empty lock file. */
169 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
173 86c3caaf 2018-03-09 stsp /* Create an empty file index. */
174 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
178 65e3b818 2018-03-11 stsp /* Save an invalid base commit hash. */
179 65e3b818 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT,
180 65e3b818 2018-03-11 stsp GOT_WORKTREE_INVALID_COMMIT_ID);
184 86c3caaf 2018-03-09 stsp /* Write the HEAD reference. */
185 86c3caaf 2018-03-09 stsp refstr = got_ref_to_str(head_ref);
186 86c3caaf 2018-03-09 stsp if (refstr == NULL) {
187 86c3caaf 2018-03-09 stsp err = got_error(GOT_ERR_NO_MEM);
190 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_HEAD, refstr);
194 1451e70d 2018-03-10 stsp /* Store path to repository. */
195 86c3caaf 2018-03-09 stsp path_repos = got_repo_get_path(repo);
196 86c3caaf 2018-03-09 stsp if (path_repos == NULL) {
197 86c3caaf 2018-03-09 stsp err = got_error(GOT_ERR_NO_MEM);
200 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY, path_repos);
204 577ec78f 2018-03-11 stsp /* Store in-repository path prefix. */
205 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX, prefix);
209 9dce68ed 2018-03-10 stsp /* Stamp work tree with format file. */
210 1451e70d 2018-03-10 stsp if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
211 1451e70d 2018-03-10 stsp err = got_error(GOT_ERR_NO_MEM);
214 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
219 7ac97322 2018-03-11 stsp free(path_got);
220 1451e70d 2018-03-10 stsp free(formatstr);
221 86c3caaf 2018-03-09 stsp free(refstr);
222 86c3caaf 2018-03-09 stsp free(path_repos);
223 86c3caaf 2018-03-09 stsp return err;
226 86c3caaf 2018-03-09 stsp const struct got_error *
227 86c3caaf 2018-03-09 stsp got_worktree_open(struct got_worktree **worktree, const char *path)
229 6d9d28c3 2018-03-11 stsp const struct got_error *err = NULL;
230 7ac97322 2018-03-11 stsp char *path_got;
231 6d9d28c3 2018-03-11 stsp char *refstr = NULL;
232 6d9d28c3 2018-03-11 stsp char *path_repos = NULL;
233 6d9d28c3 2018-03-11 stsp char *formatstr = NULL;
234 056e7441 2018-03-11 stsp char *path_lock = NULL;
235 6d9d28c3 2018-03-11 stsp int version, fd = -1;
236 6d9d28c3 2018-03-11 stsp const char *errstr;
238 6d9d28c3 2018-03-11 stsp *worktree = NULL;
240 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
241 6d9d28c3 2018-03-11 stsp err = got_error(GOT_ERR_NO_MEM);
242 7ac97322 2018-03-11 stsp path_got = NULL;
246 7ac97322 2018-03-11 stsp if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
247 6d9d28c3 2018-03-11 stsp err = got_error(GOT_ERR_NO_MEM);
248 056e7441 2018-03-11 stsp path_lock = NULL;
252 056e7441 2018-03-11 stsp fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
253 6d9d28c3 2018-03-11 stsp if (fd == -1) {
254 73a5ef67 2018-03-11 stsp err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
255 73a5ef67 2018-03-11 stsp : got_error_from_errno());
259 7ac97322 2018-03-11 stsp err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
263 6d9d28c3 2018-03-11 stsp version = strtonum(formatstr, 1, INT_MAX, &errstr);
264 6d9d28c3 2018-03-11 stsp if (errstr) {
265 6d9d28c3 2018-03-11 stsp err = got_error(GOT_ERR_WORKTREE_META);
268 6d9d28c3 2018-03-11 stsp if (version != GOT_WORKTREE_FORMAT_VERSION) {
269 6d9d28c3 2018-03-11 stsp err = got_error(GOT_ERR_WORKTREE_VERS);
273 6d9d28c3 2018-03-11 stsp *worktree = calloc(1, sizeof(**worktree));
274 6d9d28c3 2018-03-11 stsp if (*worktree == NULL) {
275 6d9d28c3 2018-03-11 stsp err = got_error(GOT_ERR_NO_MEM);
278 056e7441 2018-03-11 stsp (*worktree)->lockfd = -1;
280 6d9d28c3 2018-03-11 stsp (*worktree)->path_worktree_root = strdup(path);
281 6d9d28c3 2018-03-11 stsp if ((*worktree)->path_worktree_root == NULL) {
282 6d9d28c3 2018-03-11 stsp err = got_error(GOT_ERR_NO_MEM);
285 7ac97322 2018-03-11 stsp err = read_meta_file(&(*worktree)->path_repo, path_got,
286 6d9d28c3 2018-03-11 stsp GOT_WORKTREE_REPOSITORY);
289 7ac97322 2018-03-11 stsp err = read_meta_file(&(*worktree)->path_prefix, path_got,
290 6d9d28c3 2018-03-11 stsp GOT_WORKTREE_PATH_PREFIX);
294 f5baf295 2018-03-11 stsp err = read_meta_file(&(*worktree)->base_commit, path_got,
295 f5baf295 2018-03-11 stsp GOT_WORKTREE_BASE_COMMIT);
299 e8f36958 2018-03-11 stsp err = read_meta_file(&(*worktree)->head_ref, path_got,
300 e8f36958 2018-03-11 stsp GOT_WORKTREE_HEAD);
305 7ac97322 2018-03-11 stsp free(path_got);
306 056e7441 2018-03-11 stsp free(path_lock);
308 6d9d28c3 2018-03-11 stsp if (fd != -1)
310 6d9d28c3 2018-03-11 stsp if (*worktree != NULL)
311 6d9d28c3 2018-03-11 stsp got_worktree_close(*worktree);
312 6d9d28c3 2018-03-11 stsp *worktree = NULL;
314 056e7441 2018-03-11 stsp (*worktree)->lockfd = fd;
316 6d9d28c3 2018-03-11 stsp return err;
320 86c3caaf 2018-03-09 stsp got_worktree_close(struct got_worktree *worktree)
322 6d9d28c3 2018-03-11 stsp free(worktree->path_worktree_root);
323 6d9d28c3 2018-03-11 stsp free(worktree->path_repo);
324 6d9d28c3 2018-03-11 stsp free(worktree->path_prefix);
325 d6c38e0d 2018-03-11 stsp free(worktree->base_commit);
326 e8f36958 2018-03-11 stsp free(worktree->head_ref);
327 056e7441 2018-03-11 stsp if (worktree->lockfd != -1)
328 056e7441 2018-03-11 stsp close(worktree->lockfd);
329 6d9d28c3 2018-03-11 stsp free(worktree);
333 86c3caaf 2018-03-09 stsp got_worktree_get_repo_path(struct got_worktree *worktree)
335 86c3caaf 2018-03-09 stsp return strdup(worktree->path_repo);
338 86c3caaf 2018-03-09 stsp struct got_reference *
339 86c3caaf 2018-03-09 stsp got_worktree_get_head(struct got_worktree *worktree)
341 86c3caaf 2018-03-09 stsp return NULL;
344 86c3caaf 2018-03-09 stsp const struct got_error *
345 4d94df2d 2018-03-11 stsp got_worktree_change_head(struct got_worktree *worktree, struct got_reference *head,
346 86c3caaf 2018-03-09 stsp struct got_repository *repo)
348 86c3caaf 2018-03-09 stsp return NULL;
351 86c3caaf 2018-03-09 stsp const struct got_error *
352 86c3caaf 2018-03-09 stsp got_worktree_checkout_files(struct got_worktree *worktree,
353 86c3caaf 2018-03-09 stsp struct got_repository *repo)
355 86c3caaf 2018-03-09 stsp return NULL;