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 cde76477 2018-03-11 stsp char *repo_path = 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 cde76477 2018-03-11 stsp repo_path = got_repo_get_path(repo);
196 cde76477 2018-03-11 stsp if (repo_path == NULL) {
197 86c3caaf 2018-03-09 stsp err = got_error(GOT_ERR_NO_MEM);
200 cde76477 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY, repo_path);
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 cde76477 2018-03-11 stsp free(repo_path);
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 *formatstr = NULL;
233 056e7441 2018-03-11 stsp char *path_lock = NULL;
234 6d9d28c3 2018-03-11 stsp int version, fd = -1;
235 6d9d28c3 2018-03-11 stsp const char *errstr;
237 6d9d28c3 2018-03-11 stsp *worktree = NULL;
239 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
240 6d9d28c3 2018-03-11 stsp err = got_error(GOT_ERR_NO_MEM);
241 7ac97322 2018-03-11 stsp path_got = NULL;
245 7ac97322 2018-03-11 stsp if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
246 6d9d28c3 2018-03-11 stsp err = got_error(GOT_ERR_NO_MEM);
247 056e7441 2018-03-11 stsp path_lock = NULL;
251 056e7441 2018-03-11 stsp fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
252 6d9d28c3 2018-03-11 stsp if (fd == -1) {
253 73a5ef67 2018-03-11 stsp err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
254 73a5ef67 2018-03-11 stsp : got_error_from_errno());
258 7ac97322 2018-03-11 stsp err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
262 6d9d28c3 2018-03-11 stsp version = strtonum(formatstr, 1, INT_MAX, &errstr);
263 6d9d28c3 2018-03-11 stsp if (errstr) {
264 6d9d28c3 2018-03-11 stsp err = got_error(GOT_ERR_WORKTREE_META);
267 6d9d28c3 2018-03-11 stsp if (version != GOT_WORKTREE_FORMAT_VERSION) {
268 6d9d28c3 2018-03-11 stsp err = got_error(GOT_ERR_WORKTREE_VERS);
272 6d9d28c3 2018-03-11 stsp *worktree = calloc(1, sizeof(**worktree));
273 6d9d28c3 2018-03-11 stsp if (*worktree == NULL) {
274 6d9d28c3 2018-03-11 stsp err = got_error(GOT_ERR_NO_MEM);
277 056e7441 2018-03-11 stsp (*worktree)->lockfd = -1;
279 cde76477 2018-03-11 stsp (*worktree)->worktree_root = strdup(path);
280 cde76477 2018-03-11 stsp if ((*worktree)->worktree_root == NULL) {
281 6d9d28c3 2018-03-11 stsp err = got_error(GOT_ERR_NO_MEM);
284 cde76477 2018-03-11 stsp err = read_meta_file(&(*worktree)->repo_path, path_got,
285 6d9d28c3 2018-03-11 stsp GOT_WORKTREE_REPOSITORY);
288 7ac97322 2018-03-11 stsp err = read_meta_file(&(*worktree)->path_prefix, path_got,
289 6d9d28c3 2018-03-11 stsp GOT_WORKTREE_PATH_PREFIX);
293 f5baf295 2018-03-11 stsp err = read_meta_file(&(*worktree)->base_commit, path_got,
294 f5baf295 2018-03-11 stsp GOT_WORKTREE_BASE_COMMIT);
298 e8f36958 2018-03-11 stsp err = read_meta_file(&(*worktree)->head_ref, path_got,
299 e8f36958 2018-03-11 stsp GOT_WORKTREE_HEAD);
304 7ac97322 2018-03-11 stsp free(path_got);
305 056e7441 2018-03-11 stsp free(path_lock);
307 6d9d28c3 2018-03-11 stsp if (fd != -1)
309 6d9d28c3 2018-03-11 stsp if (*worktree != NULL)
310 6d9d28c3 2018-03-11 stsp got_worktree_close(*worktree);
311 6d9d28c3 2018-03-11 stsp *worktree = NULL;
313 056e7441 2018-03-11 stsp (*worktree)->lockfd = fd;
315 6d9d28c3 2018-03-11 stsp return err;
319 86c3caaf 2018-03-09 stsp got_worktree_close(struct got_worktree *worktree)
321 cde76477 2018-03-11 stsp free(worktree->worktree_root);
322 cde76477 2018-03-11 stsp free(worktree->repo_path);
323 6d9d28c3 2018-03-11 stsp free(worktree->path_prefix);
324 d6c38e0d 2018-03-11 stsp free(worktree->base_commit);
325 e8f36958 2018-03-11 stsp free(worktree->head_ref);
326 056e7441 2018-03-11 stsp if (worktree->lockfd != -1)
327 056e7441 2018-03-11 stsp close(worktree->lockfd);
328 6d9d28c3 2018-03-11 stsp free(worktree);
332 86c3caaf 2018-03-09 stsp got_worktree_get_repo_path(struct got_worktree *worktree)
334 cde76477 2018-03-11 stsp return strdup(worktree->repo_path);
337 86c3caaf 2018-03-09 stsp struct got_reference *
338 86c3caaf 2018-03-09 stsp got_worktree_get_head(struct got_worktree *worktree)
340 86c3caaf 2018-03-09 stsp return NULL;
343 86c3caaf 2018-03-09 stsp const struct got_error *
344 4d94df2d 2018-03-11 stsp got_worktree_change_head(struct got_worktree *worktree, struct got_reference *head,
345 86c3caaf 2018-03-09 stsp struct got_repository *repo)
347 86c3caaf 2018-03-09 stsp return NULL;
350 86c3caaf 2018-03-09 stsp const struct got_error *
351 86c3caaf 2018-03-09 stsp got_worktree_checkout_files(struct got_worktree *worktree,
352 86c3caaf 2018-03-09 stsp struct got_repository *repo)
354 86c3caaf 2018-03-09 stsp return NULL;