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 /* Write the HEAD reference. */
177 refstr = got_ref_to_str(head_ref);
178 if (refstr == NULL) {
179 err = got_error(GOT_ERR_NO_MEM);
182 err = create_meta_file(gotpath, GOT_REF_HEAD, refstr);
186 /* Store path to repository. */
187 path_repos = got_repo_get_path(repo);
188 if (path_repos == NULL) {
189 err = got_error(GOT_ERR_NO_MEM);
192 err = create_meta_file(gotpath, GOT_WORKTREE_REPOSITORY, path_repos);
196 /* Store in-repository path prefix. */
197 err = create_meta_file(gotpath, GOT_WORKTREE_PATH_PREFIX, prefix);
201 /* Stamp work tree with format file. */
202 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
203 err = got_error(GOT_ERR_NO_MEM);
206 err = create_meta_file(gotpath, GOT_WORKTREE_FORMAT, formatstr);
218 const struct got_error *
219 got_worktree_open(struct got_worktree **worktree, const char *path)
221 const struct got_error *err = NULL;
224 char *path_repos = NULL;
225 char *formatstr = NULL;
226 char *path_lock = NULL;
227 int version, fd = -1;
232 if (asprintf(&gotpath, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
233 err = got_error(GOT_ERR_NO_MEM);
238 if (asprintf(&path_lock, "%s/%s", gotpath, GOT_WORKTREE_LOCK) == -1) {
239 err = got_error(GOT_ERR_NO_MEM);
244 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
246 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
247 : got_error_from_errno());
251 err = read_meta_file(&formatstr, gotpath, GOT_WORKTREE_FORMAT);
255 version = strtonum(formatstr, 1, INT_MAX, &errstr);
257 err = got_error(GOT_ERR_WORKTREE_META);
260 if (version != GOT_WORKTREE_FORMAT_VERSION) {
261 err = got_error(GOT_ERR_WORKTREE_VERS);
265 *worktree = calloc(1, sizeof(**worktree));
266 if (*worktree == NULL) {
267 err = got_error(GOT_ERR_NO_MEM);
270 (*worktree)->lockfd = -1;
272 (*worktree)->path_worktree_root = strdup(path);
273 if ((*worktree)->path_worktree_root == NULL) {
274 err = got_error(GOT_ERR_NO_MEM);
277 err = read_meta_file(&(*worktree)->path_repo, gotpath,
278 GOT_WORKTREE_REPOSITORY);
281 err = read_meta_file(&(*worktree)->path_prefix, gotpath,
282 GOT_WORKTREE_PATH_PREFIX);
291 if (*worktree != NULL)
292 got_worktree_close(*worktree);
295 (*worktree)->lockfd = fd;
301 got_worktree_close(struct got_worktree *worktree)
303 free(worktree->path_worktree_root);
304 free(worktree->path_repo);
305 free(worktree->path_prefix);
306 if (worktree->lockfd != -1)
307 close(worktree->lockfd);
312 got_worktree_get_repo_path(struct got_worktree *worktree)
314 return strdup(worktree->path_repo);
317 struct got_reference *
318 got_worktree_get_head(struct got_worktree *worktree)
323 const struct got_error *
324 got_worktree_change_head(struct got_worktree *worktree, struct got_reference *head,
325 struct got_repository *repo)
330 const struct got_error *
331 got_worktree_checkout_files(struct got_worktree *worktree,
332 struct got_repository *repo)