Blob


1 /*
2 * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 *
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.
7 *
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.
15 */
17 #include <sys/stat.h>
19 #include <string.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <fcntl.h>
23 #include <errno.h>
24 #include <unistd.h>
26 #include "got_error.h"
27 #include "got_repository.h"
28 #include "got_refs.h"
29 #include "got_worktree.h"
31 #include "got_worktree_priv.h"
32 #include "got_path_priv.h"
34 static const struct got_error *
35 create_meta_file(const char *gotpath, const char *name, const char *content)
36 {
37 const struct got_error *err = NULL;
38 char *path;
39 int fd = -1;
40 char buf[4];
41 ssize_t n;
43 if (asprintf(&path, "%s/%s", gotpath, name) == -1) {
44 err = got_error(GOT_ERR_NO_MEM);
45 path = NULL;
46 goto done;
47 }
49 fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_EXLOCK | O_NOFOLLOW,
50 GOT_DEFAULT_FILE_MODE);
51 if (fd == -1) {
52 err = got_error_from_errno();
53 goto done;
54 }
56 /* The file should be empty. */
57 n = read(fd, buf, sizeof(buf));
58 if (n != 0) {
59 err = (n == -1 ? got_error_from_errno() :
60 got_error(GOT_ERR_WORKTREE_EXISTS));
61 goto done;
62 }
64 if (content) {
65 int len = dprintf(fd, "%s\n", content);
66 if (len != strlen(content) + 1) {
67 err = got_error_from_errno();
68 goto done;
69 }
70 }
72 done:
73 if (fd != -1 && close(fd) == -1 && err == NULL)
74 err = got_error_from_errno();
75 free(path);
76 return err;
77 }
79 static const struct got_error *
80 read_meta_file(char **content, const char *gotpath, const char *name)
81 {
82 const struct got_error *err = NULL;
83 char *path;
84 int fd = -1;
85 ssize_t n;
86 struct stat sb;
88 *content = NULL;
90 if (asprintf(&path, "%s/%s", gotpath, name) == -1) {
91 err = got_error(GOT_ERR_NO_MEM);
92 path = NULL;
93 goto done;
94 }
96 fd = open(path, O_RDONLY | O_EXCL | O_EXLOCK | O_NOFOLLOW);
97 if (fd == -1) {
98 err = got_error_from_errno();
99 goto done;
102 stat(path, &sb);
103 *content = calloc(1, sb.st_size);
104 if (*content == NULL) {
105 err = got_error(GOT_ERR_NO_MEM);
106 goto done;
109 n = read(fd, *content, sb.st_size);
110 if (n != sb.st_size) {
111 err = got_error_from_errno();
112 goto done;
114 if ((*content)[sb.st_size - 1] != '\n') {
115 err = got_error(GOT_ERR_WORKTREE_META);
116 goto done;
118 (*content)[sb.st_size - 1] = '\0';
120 done:
121 if (fd != -1 && close(fd) == -1 && err == NULL)
122 err = got_error_from_errno();
123 free(path);
124 if (err) {
125 free(*content);
126 *content = NULL;
128 return err;
131 const struct got_error *
132 got_worktree_init(const char *path, struct got_reference *head_ref,
133 const char *prefix, struct got_repository *repo)
135 const struct got_error *err = NULL;
136 char *gotpath = NULL;
137 char *refstr = NULL;
138 char *path_repos = NULL;
139 char *formatstr = NULL;
141 if (!got_path_is_absolute(prefix))
142 return got_error(GOT_ERR_BAD_PATH);
144 /* Create top-level directory (may already exist). */
145 if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
146 err = got_error_from_errno();
147 goto done;
150 /* Create .got directory (may already exist). */
151 if (asprintf(&gotpath, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
152 err = got_error(GOT_ERR_NO_MEM);
153 goto done;
155 if (mkdir(gotpath, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
156 err = got_error_from_errno();
157 goto done;
160 /* Create an empty file index. */
161 err = create_meta_file(gotpath, GOT_WORKTREE_FILE_INDEX, NULL);
162 if (err)
163 goto done;
165 /* Write the HEAD reference. */
166 refstr = got_ref_to_str(head_ref);
167 if (refstr == NULL) {
168 err = got_error(GOT_ERR_NO_MEM);
169 goto done;
171 err = create_meta_file(gotpath, GOT_REF_HEAD, refstr);
172 if (err)
173 goto done;
175 /* Store path to repository. */
176 path_repos = got_repo_get_path(repo);
177 if (path_repos == NULL) {
178 err = got_error(GOT_ERR_NO_MEM);
179 goto done;
181 err = create_meta_file(gotpath, GOT_WORKTREE_REPOSITORY, path_repos);
182 if (err)
183 goto done;
185 /* Store in-repository path prefix. */
186 err = create_meta_file(gotpath, GOT_WORKTREE_PATH_PREFIX, prefix);
187 if (err)
188 goto done;
190 /* Stamp work tree with format file. */
191 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
192 err = got_error(GOT_ERR_NO_MEM);
193 goto done;
195 err = create_meta_file(gotpath, GOT_WORKTREE_FORMAT, formatstr);
196 if (err)
197 goto done;
199 done:
200 free(gotpath);
201 free(formatstr);
202 free(refstr);
203 free(path_repos);
204 return err;
207 const struct got_error *
208 got_worktree_open(struct got_worktree **worktree, const char *path)
210 return NULL;
213 void
214 got_worktree_close(struct got_worktree *worktree)
218 char *
219 got_worktree_get_repo_path(struct got_worktree *worktree)
221 return strdup(worktree->path_repo);
224 struct got_reference *
225 got_worktree_get_head(struct got_worktree *worktree)
227 return NULL;
230 const struct got_error *
231 got_worktree_change_head(struct got_worktree *worktree, struct got_reference *head,
232 struct got_repository *repo)
234 return NULL;
237 const struct got_error *
238 got_worktree_checkout_files(struct got_worktree *worktree,
239 struct got_repository *repo)
241 return NULL;