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"
34 #include "got_sha1_priv.h"
36 static const struct got_error *
37 create_meta_file(const char *path_got, const char *name, const char *content)
39 const struct got_error *err = NULL;
45 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
46 err = got_error(GOT_ERR_NO_MEM);
51 fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
52 GOT_DEFAULT_FILE_MODE);
54 err = got_error_from_errno();
58 /* The file should be empty. */
59 n = read(fd, buf, sizeof(buf));
61 err = (n == -1 ? got_error_from_errno() :
62 got_error(GOT_ERR_WORKTREE_EXISTS));
67 int len = dprintf(fd, "%s\n", content);
68 if (len != strlen(content) + 1) {
69 err = got_error_from_errno();
75 if (fd != -1 && close(fd) == -1 && err == NULL)
76 err = got_error_from_errno();
81 static const struct got_error *
82 read_meta_file(char **content, const char *path_got, const char *name)
84 const struct got_error *err = NULL;
92 if (asprintf(&path, "%s/%s", path_got, name) == -1) {
93 err = got_error(GOT_ERR_NO_MEM);
98 fd = open(path, O_RDONLY | O_NOFOLLOW);
100 err = got_error_from_errno();
103 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
104 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
105 : got_error_from_errno());
110 *content = calloc(1, sb.st_size);
111 if (*content == NULL) {
112 err = got_error(GOT_ERR_NO_MEM);
116 n = read(fd, *content, sb.st_size);
117 if (n != sb.st_size) {
118 err = (n == -1 ? got_error_from_errno() :
119 got_error(GOT_ERR_WORKTREE_META));
122 if ((*content)[sb.st_size - 1] != '\n') {
123 err = got_error(GOT_ERR_WORKTREE_META);
126 (*content)[sb.st_size - 1] = '\0';
129 if (fd != -1 && close(fd) == -1 && err == NULL)
130 err = got_error_from_errno();
139 const struct got_error *
140 got_worktree_init(const char *path, struct got_reference *head_ref,
141 const char *prefix, struct got_repository *repo)
143 const struct got_error *err = NULL;
144 char *path_got = NULL;
146 char *repo_path = NULL;
147 char *formatstr = NULL;
149 if (!got_path_is_absolute(prefix))
150 return got_error(GOT_ERR_BAD_PATH);
152 /* Create top-level directory (may already exist). */
153 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
154 err = got_error_from_errno();
158 /* Create .got directory (may already exist). */
159 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
160 err = got_error(GOT_ERR_NO_MEM);
163 if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
164 err = got_error_from_errno();
168 /* Create an empty lock file. */
169 err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
173 /* Create an empty file index. */
174 err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
178 /* Save an invalid base commit hash. */
179 err = create_meta_file(path_got, GOT_WORKTREE_BASE_COMMIT,
180 GOT_WORKTREE_INVALID_COMMIT_ID);
184 /* Write the HEAD reference. */
185 refstr = got_ref_to_str(head_ref);
186 if (refstr == NULL) {
187 err = got_error(GOT_ERR_NO_MEM);
190 err = create_meta_file(path_got, GOT_WORKTREE_HEAD, refstr);
194 /* Store path to repository. */
195 repo_path = got_repo_get_path(repo);
196 if (repo_path == NULL) {
197 err = got_error(GOT_ERR_NO_MEM);
200 err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY, repo_path);
204 /* Store in-repository path prefix. */
205 err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX, prefix);
209 /* Stamp work tree with format file. */
210 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
211 err = got_error(GOT_ERR_NO_MEM);
214 err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
226 const struct got_error *
227 got_worktree_open(struct got_worktree **worktree, const char *path)
229 const struct got_error *err = NULL;
232 char *formatstr = NULL;
233 char *path_lock = NULL;
234 int version, fd = -1;
239 if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
240 err = got_error(GOT_ERR_NO_MEM);
245 if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
246 err = got_error(GOT_ERR_NO_MEM);
251 fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
253 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
254 : got_error_from_errno());
258 err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
262 version = strtonum(formatstr, 1, INT_MAX, &errstr);
264 err = got_error(GOT_ERR_WORKTREE_META);
267 if (version != GOT_WORKTREE_FORMAT_VERSION) {
268 err = got_error(GOT_ERR_WORKTREE_VERS);
272 *worktree = calloc(1, sizeof(**worktree));
273 if (*worktree == NULL) {
274 err = got_error(GOT_ERR_NO_MEM);
277 (*worktree)->lockfd = -1;
279 (*worktree)->root_path = strdup(path);
280 if ((*worktree)->root_path == NULL) {
281 err = got_error(GOT_ERR_NO_MEM);
284 err = read_meta_file(&(*worktree)->repo_path, path_got,
285 GOT_WORKTREE_REPOSITORY);
288 err = read_meta_file(&(*worktree)->path_prefix, path_got,
289 GOT_WORKTREE_PATH_PREFIX);
293 err = read_meta_file(&(*worktree)->base_commit, path_got,
294 GOT_WORKTREE_BASE_COMMIT);
298 err = read_meta_file(&(*worktree)->head_ref, path_got,
309 if (*worktree != NULL)
310 got_worktree_close(*worktree);
313 (*worktree)->lockfd = fd;
319 got_worktree_close(struct got_worktree *worktree)
321 free(worktree->root_path);
322 free(worktree->repo_path);
323 free(worktree->path_prefix);
324 free(worktree->base_commit);
325 free(worktree->head_ref);
326 if (worktree->lockfd != -1)
327 close(worktree->lockfd);
332 got_worktree_get_repo_path(struct got_worktree *worktree)
334 return strdup(worktree->repo_path);
337 struct got_reference *
338 got_worktree_get_head(struct got_worktree *worktree)
343 const struct got_error *
344 got_worktree_change_head(struct got_worktree *worktree, struct got_reference *head,
345 struct got_repository *repo)
350 const struct got_error *
351 got_worktree_checkout_files(struct got_worktree *worktree,
352 struct got_repository *repo)