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>
18 #include <sys/limits.h>
20 #include <string.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <fcntl.h>
24 #include <errno.h>
25 #include <unistd.h>
27 #include "got_error.h"
28 #include "got_repository.h"
29 #include "got_refs.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)
37 {
38 const struct got_error *err = NULL;
39 char *path;
40 int fd = -1;
41 char buf[4];
42 ssize_t n;
44 if (asprintf(&path, "%s/%s", gotpath, name) == -1) {
45 err = got_error(GOT_ERR_NO_MEM);
46 path = NULL;
47 goto done;
48 }
50 fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
51 GOT_DEFAULT_FILE_MODE);
52 if (fd == -1) {
53 err = got_error_from_errno();
54 goto done;
55 }
57 /* The file should be empty. */
58 n = read(fd, buf, sizeof(buf));
59 if (n != 0) {
60 err = (n == -1 ? got_error_from_errno() :
61 got_error(GOT_ERR_WORKTREE_EXISTS));
62 goto done;
63 }
65 if (content) {
66 int len = dprintf(fd, "%s\n", content);
67 if (len != strlen(content) + 1) {
68 err = got_error_from_errno();
69 goto done;
70 }
71 }
73 done:
74 if (fd != -1 && close(fd) == -1 && err == NULL)
75 err = got_error_from_errno();
76 free(path);
77 return err;
78 }
80 static const struct got_error *
81 read_meta_file(char **content, const char *gotpath, const char *name)
82 {
83 const struct got_error *err = NULL;
84 char *path;
85 int fd = -1;
86 ssize_t n;
87 struct stat sb;
89 *content = NULL;
91 if (asprintf(&path, "%s/%s", gotpath, name) == -1) {
92 err = got_error(GOT_ERR_NO_MEM);
93 path = NULL;
94 goto done;
95 }
97 fd = open(path, O_RDONLY | O_NOFOLLOW);
98 if (fd == -1) {
99 err = got_error_from_errno();
100 goto done;
102 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
103 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
104 : got_error_from_errno());
105 goto done;
108 stat(path, &sb);
109 *content = calloc(1, sb.st_size);
110 if (*content == NULL) {
111 err = got_error(GOT_ERR_NO_MEM);
112 goto done;
115 n = read(fd, *content, sb.st_size);
116 if (n != sb.st_size) {
117 err = got_error_from_errno();
118 goto done;
120 if ((*content)[sb.st_size - 1] != '\n') {
121 err = got_error(GOT_ERR_WORKTREE_META);
122 goto done;
124 (*content)[sb.st_size - 1] = '\0';
126 done:
127 if (fd != -1 && close(fd) == -1 && err == NULL)
128 err = got_error_from_errno();
129 free(path);
130 if (err) {
131 free(*content);
132 *content = NULL;
134 return err;
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;
143 char *refstr = 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();
153 goto done;
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);
159 goto done;
161 if (mkdir(gotpath, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
162 err = got_error_from_errno();
163 goto done;
166 /* Create an empty file index. */
167 err = create_meta_file(gotpath, GOT_WORKTREE_FILE_INDEX, NULL);
168 if (err)
169 goto done;
171 /* Write the HEAD reference. */
172 refstr = got_ref_to_str(head_ref);
173 if (refstr == NULL) {
174 err = got_error(GOT_ERR_NO_MEM);
175 goto done;
177 err = create_meta_file(gotpath, GOT_REF_HEAD, refstr);
178 if (err)
179 goto done;
181 /* Store path to repository. */
182 path_repos = got_repo_get_path(repo);
183 if (path_repos == NULL) {
184 err = got_error(GOT_ERR_NO_MEM);
185 goto done;
187 err = create_meta_file(gotpath, GOT_WORKTREE_REPOSITORY, path_repos);
188 if (err)
189 goto done;
191 /* Store in-repository path prefix. */
192 err = create_meta_file(gotpath, GOT_WORKTREE_PATH_PREFIX, prefix);
193 if (err)
194 goto done;
196 /* Stamp work tree with format file. */
197 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
198 err = got_error(GOT_ERR_NO_MEM);
199 goto done;
201 err = create_meta_file(gotpath, GOT_WORKTREE_FORMAT, formatstr);
202 if (err)
203 goto done;
205 done:
206 free(gotpath);
207 free(formatstr);
208 free(refstr);
209 free(path_repos);
210 return err;
213 const struct got_error *
214 got_worktree_open(struct got_worktree **worktree, const char *path)
216 const struct got_error *err = NULL;
217 char *gotpath;
218 char *refstr = NULL;
219 char *path_repos = NULL;
220 char *formatstr = NULL;
221 char *path_fileindex = NULL;
222 int version, fd = -1;
223 const char *errstr;
225 *worktree = NULL;
227 if (asprintf(&gotpath, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
228 err = got_error(GOT_ERR_NO_MEM);
229 gotpath = NULL;
230 goto done;
233 if (asprintf(&path_fileindex, "%s/%s", gotpath,
234 GOT_WORKTREE_FILE_INDEX) == -1) {
235 err = got_error(GOT_ERR_NO_MEM);
236 path_fileindex = NULL;
237 goto done;
240 fd = open(path_fileindex, O_RDWR | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
241 if (fd == -1) {
242 err = got_error_from_errno();
243 goto done;
245 if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
246 err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
247 : got_error_from_errno());
248 goto done;
251 err = read_meta_file(&formatstr, gotpath, GOT_WORKTREE_FORMAT);
252 if (err)
253 goto done;
255 version = strtonum(formatstr, 1, INT_MAX, &errstr);
256 if (errstr) {
257 err = got_error(GOT_ERR_WORKTREE_META);
258 goto done;
260 if (version != GOT_WORKTREE_FORMAT_VERSION) {
261 err = got_error(GOT_ERR_WORKTREE_VERS);
262 goto done;
265 *worktree = calloc(1, sizeof(**worktree));
266 if (*worktree == NULL) {
267 err = got_error(GOT_ERR_NO_MEM);
268 goto done;
270 (*worktree)->fd_fileindex = -1;
272 (*worktree)->path_worktree_root = strdup(path);
273 if ((*worktree)->path_worktree_root == NULL) {
274 err = got_error(GOT_ERR_NO_MEM);
275 goto done;
277 err = read_meta_file(&(*worktree)->path_repo, gotpath,
278 GOT_WORKTREE_REPOSITORY);
279 if (err)
280 goto done;
281 err = read_meta_file(&(*worktree)->path_prefix, gotpath,
282 GOT_WORKTREE_PATH_PREFIX);
283 goto done;
285 done:
286 free(gotpath);
287 free(path_fileindex);
288 if (err) {
289 if (fd != -1)
290 close(fd);
291 if (*worktree != NULL)
292 got_worktree_close(*worktree);
293 *worktree = NULL;
294 } else
295 (*worktree)->fd_fileindex = fd;
297 return err;
300 void
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->fd_fileindex != -1)
307 close(worktree->fd_fileindex);
308 free(worktree);
311 char *
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)
320 return NULL;
323 const struct got_error *
324 got_worktree_change_head(struct got_worktree *worktree, struct got_reference *head,
325 struct got_repository *repo)
327 return NULL;
330 const struct got_error *
331 got_worktree_checkout_files(struct got_worktree *worktree,
332 struct got_repository *repo)
334 return NULL;