2 * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
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.
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.
18 #include <sys/limits.h>
27 #include "got_error.h"
28 #include "got_repository.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)
38 const struct got_error *err = NULL;
44 if (asprintf(&path, "%s/%s", gotpath, name) == -1) {
45 err = got_error(GOT_ERR_NO_MEM);
50 fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
51 GOT_DEFAULT_FILE_MODE);
53 err = got_error_from_errno();
57 /* The file should be empty. */
58 n = read(fd, buf, sizeof(buf));
60 err = (n == -1 ? got_error_from_errno() :
61 got_error(GOT_ERR_WORKTREE_EXISTS));
66 int len = dprintf(fd, "%s\n", content);
67 if (len != strlen(content) + 1) {
68 err = got_error_from_errno();
74 if (fd != -1 && close(fd) == -1 && err == NULL)
75 err = got_error_from_errno();
80 static const struct got_error *
81 read_meta_file(char **content, const char *gotpath, const char *name)
83 const struct got_error *err = NULL;
91 if (asprintf(&path, "%s/%s", gotpath, name) == -1) {
92 err = got_error(GOT_ERR_NO_MEM);
97 fd = open(path, O_RDONLY | O_NOFOLLOW);
99 err = got_error_from_errno();
102 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
103 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
104 : got_error_from_errno());
109 *content = calloc(1, sb.st_size);
110 if (*content == NULL) {
111 err = got_error(GOT_ERR_NO_MEM);
115 n = read(fd, *content, sb.st_size);
116 if (n != sb.st_size) {
117 err = got_error_from_errno();
120 if ((*content)[sb.st_size - 1] != '\n') {
121 err = got_error(GOT_ERR_WORKTREE_META);
124 (*content)[sb.st_size - 1] = '\0';
127 if (fd != -1 && close(fd) == -1 && err == NULL)
128 err = got_error_from_errno();
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;
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();
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);
161 if (mkdir(gotpath, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
162 err = got_error_from_errno();
166 /* Create an empty lock file. */
167 err = create_meta_file(gotpath, GOT_WORKTREE_LOCK, NULL);
171 /* Create an empty file index. */
172 err = create_meta_file(gotpath, GOT_WORKTREE_FILE_INDEX, NULL);
176 /* Set base commit to empty. */
177 err = create_meta_file(gotpath, GOT_WORKTREE_BASE_COMMIT, NULL);
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);
187 err = create_meta_file(gotpath, GOT_WORKTREE_HEAD, refstr);
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);
197 err = create_meta_file(gotpath, GOT_WORKTREE_REPOSITORY, path_repos);
201 /* Store in-repository path prefix. */
202 err = create_meta_file(gotpath, GOT_WORKTREE_PATH_PREFIX, prefix);
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);
211 err = create_meta_file(gotpath, GOT_WORKTREE_FORMAT, formatstr);
223 const struct got_error *
224 got_worktree_open(struct got_worktree **worktree, const char *path)
226 const struct got_error *err = NULL;
229 char *path_repos = NULL;
230 char *formatstr = NULL;
231 char *path_lock = NULL;
232 int version, fd = -1;
237 if (asprintf(&gotpath, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
238 err = got_error(GOT_ERR_NO_MEM);
243 if (asprintf(&path_lock, "%s/%s", gotpath, GOT_WORKTREE_LOCK) == -1) {
244 err = got_error(GOT_ERR_NO_MEM);
249 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
251 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
252 : got_error_from_errno());
256 err = read_meta_file(&formatstr, gotpath, GOT_WORKTREE_FORMAT);
260 version = strtonum(formatstr, 1, INT_MAX, &errstr);
262 err = got_error(GOT_ERR_WORKTREE_META);
265 if (version != GOT_WORKTREE_FORMAT_VERSION) {
266 err = got_error(GOT_ERR_WORKTREE_VERS);
270 *worktree = calloc(1, sizeof(**worktree));
271 if (*worktree == NULL) {
272 err = got_error(GOT_ERR_NO_MEM);
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);
282 err = read_meta_file(&(*worktree)->path_repo, gotpath,
283 GOT_WORKTREE_REPOSITORY);
286 err = read_meta_file(&(*worktree)->path_prefix, gotpath,
287 GOT_WORKTREE_PATH_PREFIX);
296 if (*worktree != NULL)
297 got_worktree_close(*worktree);
300 (*worktree)->lockfd = fd;
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);
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)
328 const struct got_error *
329 got_worktree_change_head(struct got_worktree *worktree, struct got_reference *head,
330 struct got_repository *repo)
335 const struct got_error *
336 got_worktree_checkout_files(struct got_worktree *worktree,
337 struct got_repository *repo)