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"
35 99724ed4 2018-03-10 stsp static const struct got_error *
36 99724ed4 2018-03-10 stsp create_meta_file(const char *gotpath, const char *name, const char *content)
38 99724ed4 2018-03-10 stsp const struct got_error *err = NULL;
40 99724ed4 2018-03-10 stsp int fd = -1;
41 99724ed4 2018-03-10 stsp char buf[4];
44 99724ed4 2018-03-10 stsp if (asprintf(&path, "%s/%s", gotpath, name) == -1) {
45 99724ed4 2018-03-10 stsp err = got_error(GOT_ERR_NO_MEM);
46 99724ed4 2018-03-10 stsp path = NULL;
50 73a5ef67 2018-03-11 stsp fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
51 99724ed4 2018-03-10 stsp GOT_DEFAULT_FILE_MODE);
52 99724ed4 2018-03-10 stsp if (fd == -1) {
53 99724ed4 2018-03-10 stsp err = got_error_from_errno();
57 99724ed4 2018-03-10 stsp /* The file should be empty. */
58 99724ed4 2018-03-10 stsp n = read(fd, buf, sizeof(buf));
59 99724ed4 2018-03-10 stsp if (n != 0) {
60 99724ed4 2018-03-10 stsp err = (n == -1 ? got_error_from_errno() :
61 99724ed4 2018-03-10 stsp got_error(GOT_ERR_WORKTREE_EXISTS));
65 99724ed4 2018-03-10 stsp if (content) {
66 99724ed4 2018-03-10 stsp int len = dprintf(fd, "%s\n", content);
67 99724ed4 2018-03-10 stsp if (len != strlen(content) + 1) {
68 99724ed4 2018-03-10 stsp err = got_error_from_errno();
74 99724ed4 2018-03-10 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
75 99724ed4 2018-03-10 stsp err = got_error_from_errno();
80 09fe317a 2018-03-11 stsp static const struct got_error *
81 09fe317a 2018-03-11 stsp read_meta_file(char **content, const char *gotpath, const char *name)
83 09fe317a 2018-03-11 stsp const struct got_error *err = NULL;
85 09fe317a 2018-03-11 stsp int fd = -1;
87 09fe317a 2018-03-11 stsp struct stat sb;
89 09fe317a 2018-03-11 stsp *content = NULL;
91 09fe317a 2018-03-11 stsp if (asprintf(&path, "%s/%s", gotpath, name) == -1) {
92 09fe317a 2018-03-11 stsp err = got_error(GOT_ERR_NO_MEM);
93 09fe317a 2018-03-11 stsp path = NULL;
97 ef99fdb1 2018-03-11 stsp fd = open(path, O_RDONLY | O_NOFOLLOW);
98 09fe317a 2018-03-11 stsp if (fd == -1) {
99 ef99fdb1 2018-03-11 stsp err = got_error_from_errno();
102 ef99fdb1 2018-03-11 stsp if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
103 73a5ef67 2018-03-11 stsp err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
104 73a5ef67 2018-03-11 stsp : got_error_from_errno());
108 09fe317a 2018-03-11 stsp stat(path, &sb);
109 09fe317a 2018-03-11 stsp *content = calloc(1, sb.st_size);
110 09fe317a 2018-03-11 stsp if (*content == NULL) {
111 09fe317a 2018-03-11 stsp err = got_error(GOT_ERR_NO_MEM);
115 09fe317a 2018-03-11 stsp n = read(fd, *content, sb.st_size);
116 09fe317a 2018-03-11 stsp if (n != sb.st_size) {
117 09fe317a 2018-03-11 stsp err = got_error_from_errno();
120 09fe317a 2018-03-11 stsp if ((*content)[sb.st_size - 1] != '\n') {
121 09fe317a 2018-03-11 stsp err = got_error(GOT_ERR_WORKTREE_META);
124 09fe317a 2018-03-11 stsp (*content)[sb.st_size - 1] = '\0';
127 09fe317a 2018-03-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
128 09fe317a 2018-03-11 stsp err = got_error_from_errno();
129 09fe317a 2018-03-11 stsp free(path);
131 09fe317a 2018-03-11 stsp free(*content);
132 09fe317a 2018-03-11 stsp *content = NULL;
134 09fe317a 2018-03-11 stsp return err;
137 86c3caaf 2018-03-09 stsp const struct got_error *
138 86c3caaf 2018-03-09 stsp got_worktree_init(const char *path, struct got_reference *head_ref,
139 577ec78f 2018-03-11 stsp const char *prefix, struct got_repository *repo)
141 86c3caaf 2018-03-09 stsp const struct got_error *err = NULL;
142 86c3caaf 2018-03-09 stsp char *gotpath = NULL;
143 86c3caaf 2018-03-09 stsp char *refstr = NULL;
144 86c3caaf 2018-03-09 stsp char *path_repos = NULL;
145 1451e70d 2018-03-10 stsp char *formatstr = NULL;
147 577ec78f 2018-03-11 stsp if (!got_path_is_absolute(prefix))
148 577ec78f 2018-03-11 stsp return got_error(GOT_ERR_BAD_PATH);
150 86c3caaf 2018-03-09 stsp /* Create top-level directory (may already exist). */
151 2cb4bacb 2018-03-11 stsp if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
152 86c3caaf 2018-03-09 stsp err = got_error_from_errno();
156 86c3caaf 2018-03-09 stsp /* Create .got directory (may already exist). */
157 2cb4bacb 2018-03-11 stsp if (asprintf(&gotpath, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
158 86c3caaf 2018-03-09 stsp err = got_error(GOT_ERR_NO_MEM);
161 86c3caaf 2018-03-09 stsp if (mkdir(gotpath, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
162 86c3caaf 2018-03-09 stsp err = got_error_from_errno();
166 056e7441 2018-03-11 stsp /* Create an empty lock file. */
167 056e7441 2018-03-11 stsp err = create_meta_file(gotpath, GOT_WORKTREE_LOCK, NULL);
171 86c3caaf 2018-03-09 stsp /* Create an empty file index. */
172 99724ed4 2018-03-10 stsp err = create_meta_file(gotpath, GOT_WORKTREE_FILE_INDEX, NULL);
176 fdf001a7 2018-03-11 stsp /* Set base commit to empty. */
177 e350ead3 2018-03-11 stsp err = create_meta_file(gotpath, GOT_WORKTREE_BASE_COMMIT, NULL);
181 86c3caaf 2018-03-09 stsp /* Write the HEAD reference. */
182 86c3caaf 2018-03-09 stsp refstr = got_ref_to_str(head_ref);
183 86c3caaf 2018-03-09 stsp if (refstr == NULL) {
184 86c3caaf 2018-03-09 stsp err = got_error(GOT_ERR_NO_MEM);
187 fdf001a7 2018-03-11 stsp err = create_meta_file(gotpath, GOT_WORKTREE_HEAD, refstr);
191 1451e70d 2018-03-10 stsp /* Store path to repository. */
192 86c3caaf 2018-03-09 stsp path_repos = got_repo_get_path(repo);
193 86c3caaf 2018-03-09 stsp if (path_repos == NULL) {
194 86c3caaf 2018-03-09 stsp err = got_error(GOT_ERR_NO_MEM);
197 99724ed4 2018-03-10 stsp err = create_meta_file(gotpath, GOT_WORKTREE_REPOSITORY, path_repos);
201 577ec78f 2018-03-11 stsp /* Store in-repository path prefix. */
202 577ec78f 2018-03-11 stsp err = create_meta_file(gotpath, GOT_WORKTREE_PATH_PREFIX, prefix);
206 9dce68ed 2018-03-10 stsp /* Stamp work tree with format file. */
207 1451e70d 2018-03-10 stsp if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
208 1451e70d 2018-03-10 stsp err = got_error(GOT_ERR_NO_MEM);
211 99724ed4 2018-03-10 stsp err = create_meta_file(gotpath, GOT_WORKTREE_FORMAT, formatstr);
216 86c3caaf 2018-03-09 stsp free(gotpath);
217 1451e70d 2018-03-10 stsp free(formatstr);
218 86c3caaf 2018-03-09 stsp free(refstr);
219 86c3caaf 2018-03-09 stsp free(path_repos);
220 86c3caaf 2018-03-09 stsp return err;
223 86c3caaf 2018-03-09 stsp const struct got_error *
224 86c3caaf 2018-03-09 stsp got_worktree_open(struct got_worktree **worktree, const char *path)
226 6d9d28c3 2018-03-11 stsp const struct got_error *err = NULL;
227 6d9d28c3 2018-03-11 stsp char *gotpath;
228 6d9d28c3 2018-03-11 stsp char *refstr = NULL;
229 6d9d28c3 2018-03-11 stsp char *path_repos = NULL;
230 6d9d28c3 2018-03-11 stsp char *formatstr = NULL;
231 056e7441 2018-03-11 stsp char *path_lock = NULL;
232 6d9d28c3 2018-03-11 stsp int version, fd = -1;
233 6d9d28c3 2018-03-11 stsp const char *errstr;
235 6d9d28c3 2018-03-11 stsp *worktree = NULL;
237 6d9d28c3 2018-03-11 stsp if (asprintf(&gotpath, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
238 6d9d28c3 2018-03-11 stsp err = got_error(GOT_ERR_NO_MEM);
239 6d9d28c3 2018-03-11 stsp gotpath = NULL;
243 056e7441 2018-03-11 stsp if (asprintf(&path_lock, "%s/%s", gotpath, GOT_WORKTREE_LOCK) == -1) {
244 6d9d28c3 2018-03-11 stsp err = got_error(GOT_ERR_NO_MEM);
245 056e7441 2018-03-11 stsp path_lock = NULL;
249 056e7441 2018-03-11 stsp fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
250 6d9d28c3 2018-03-11 stsp if (fd == -1) {
251 73a5ef67 2018-03-11 stsp err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
252 73a5ef67 2018-03-11 stsp : got_error_from_errno());
256 6d9d28c3 2018-03-11 stsp err = read_meta_file(&formatstr, gotpath, GOT_WORKTREE_FORMAT);
260 6d9d28c3 2018-03-11 stsp version = strtonum(formatstr, 1, INT_MAX, &errstr);
261 6d9d28c3 2018-03-11 stsp if (errstr) {
262 6d9d28c3 2018-03-11 stsp err = got_error(GOT_ERR_WORKTREE_META);
265 6d9d28c3 2018-03-11 stsp if (version != GOT_WORKTREE_FORMAT_VERSION) {
266 6d9d28c3 2018-03-11 stsp err = got_error(GOT_ERR_WORKTREE_VERS);
270 6d9d28c3 2018-03-11 stsp *worktree = calloc(1, sizeof(**worktree));
271 6d9d28c3 2018-03-11 stsp if (*worktree == NULL) {
272 6d9d28c3 2018-03-11 stsp err = got_error(GOT_ERR_NO_MEM);
275 056e7441 2018-03-11 stsp (*worktree)->lockfd = -1;
277 6d9d28c3 2018-03-11 stsp (*worktree)->path_worktree_root = strdup(path);
278 6d9d28c3 2018-03-11 stsp if ((*worktree)->path_worktree_root == NULL) {
279 6d9d28c3 2018-03-11 stsp err = got_error(GOT_ERR_NO_MEM);
282 6d9d28c3 2018-03-11 stsp err = read_meta_file(&(*worktree)->path_repo, gotpath,
283 6d9d28c3 2018-03-11 stsp GOT_WORKTREE_REPOSITORY);
286 6d9d28c3 2018-03-11 stsp err = read_meta_file(&(*worktree)->path_prefix, gotpath,
287 6d9d28c3 2018-03-11 stsp GOT_WORKTREE_PATH_PREFIX);
291 6d9d28c3 2018-03-11 stsp free(gotpath);
292 056e7441 2018-03-11 stsp free(path_lock);
294 6d9d28c3 2018-03-11 stsp if (fd != -1)
296 6d9d28c3 2018-03-11 stsp if (*worktree != NULL)
297 6d9d28c3 2018-03-11 stsp got_worktree_close(*worktree);
298 6d9d28c3 2018-03-11 stsp *worktree = NULL;
300 056e7441 2018-03-11 stsp (*worktree)->lockfd = fd;
302 6d9d28c3 2018-03-11 stsp return err;
306 86c3caaf 2018-03-09 stsp got_worktree_close(struct got_worktree *worktree)
308 6d9d28c3 2018-03-11 stsp free(worktree->path_worktree_root);
309 6d9d28c3 2018-03-11 stsp free(worktree->path_repo);
310 6d9d28c3 2018-03-11 stsp free(worktree->path_prefix);
311 056e7441 2018-03-11 stsp if (worktree->lockfd != -1)
312 056e7441 2018-03-11 stsp close(worktree->lockfd);
313 6d9d28c3 2018-03-11 stsp free(worktree);
317 86c3caaf 2018-03-09 stsp got_worktree_get_repo_path(struct got_worktree *worktree)
319 86c3caaf 2018-03-09 stsp return strdup(worktree->path_repo);
322 86c3caaf 2018-03-09 stsp struct got_reference *
323 86c3caaf 2018-03-09 stsp got_worktree_get_head(struct got_worktree *worktree)
325 86c3caaf 2018-03-09 stsp return NULL;
328 86c3caaf 2018-03-09 stsp const struct got_error *
329 4d94df2d 2018-03-11 stsp got_worktree_change_head(struct got_worktree *worktree, struct got_reference *head,
330 86c3caaf 2018-03-09 stsp struct got_repository *repo)
332 86c3caaf 2018-03-09 stsp return NULL;
335 86c3caaf 2018-03-09 stsp const struct got_error *
336 86c3caaf 2018-03-09 stsp got_worktree_checkout_files(struct got_worktree *worktree,
337 86c3caaf 2018-03-09 stsp struct got_repository *repo)
339 86c3caaf 2018-03-09 stsp return NULL;