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 const struct got_error *
80 got_worktree_init(const char *path, struct got_reference *head_ref,
81 struct got_repository *repo)
82 {
83 const struct got_error *err = NULL;
84 char *abspath = NULL;
85 char *normpath = NULL;
86 char *gotpath = NULL;
87 char *refstr = NULL;
88 char *path_repos = NULL;
89 char *formatstr = NULL;
91 if (got_path_is_absolute(path)) {
92 abspath = strdup(path);
93 if (abspath == NULL)
94 return got_error(GOT_ERR_NO_MEM);
95 } else {
96 abspath = got_path_get_absolute(path);
97 if (abspath == NULL)
98 return got_error(GOT_ERR_BAD_PATH);
99 }
101 /* Create top-level directory (may already exist). */
102 normpath = got_path_normalize(abspath);
103 if (normpath == NULL) {
104 err = got_error(GOT_ERR_BAD_PATH);
105 goto done;
107 if (mkdir(normpath, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
108 err = got_error_from_errno();
109 goto done;
112 /* Create .got directory (may already exist). */
113 if (asprintf(&gotpath, "%s/%s", normpath, GOT_WORKTREE_GOT_DIR) == -1) {
114 err = got_error(GOT_ERR_NO_MEM);
115 goto done;
117 if (mkdir(gotpath, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
118 err = got_error_from_errno();
119 goto done;
122 /* Create an empty file index. */
123 err = create_meta_file(gotpath, GOT_WORKTREE_FILE_INDEX, NULL);
124 if (err)
125 goto done;
127 /* Write the HEAD reference. */
128 refstr = got_ref_to_str(head_ref);
129 if (refstr == NULL) {
130 err = got_error(GOT_ERR_NO_MEM);
131 goto done;
133 err = create_meta_file(gotpath, GOT_REF_HEAD, refstr);
134 if (err)
135 goto done;
137 /* Store path to repository. */
138 path_repos = got_repo_get_path(repo);
139 if (path_repos == NULL) {
140 err = got_error(GOT_ERR_NO_MEM);
141 goto done;
143 err = create_meta_file(gotpath, GOT_WORKTREE_REPOSITORY, path_repos);
144 if (err)
145 goto done;
147 /* Stamp repository with format file. */
148 if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
149 err = got_error(GOT_ERR_NO_MEM);
150 goto done;
152 err = create_meta_file(gotpath, GOT_WORKTREE_FORMAT, formatstr);
153 if (err)
154 goto done;
156 done:
157 free(abspath);
158 free(normpath);
159 free(gotpath);
160 free(formatstr);
161 free(refstr);
162 free(path_repos);
163 return err;
166 const struct got_error *
167 got_worktree_open(struct got_worktree **worktree, const char *path)
169 return NULL;
172 void
173 got_worktree_close(struct got_worktree *worktree)
177 char *
178 got_worktree_get_repo_path(struct got_worktree *worktree)
180 return strdup(worktree->path_repo);
183 struct got_reference *
184 got_worktree_get_head(struct got_worktree *worktree)
186 return NULL;
189 const struct got_error *
190 got_worktree_set_head(struct got_worktree *worktree, struct got_reference *head,
191 struct got_repository *repo)
193 return NULL;
196 const struct got_error *
197 got_worktree_update_fileindex(struct got_worktree *worktree,
198 struct got_repository *repo)
200 return NULL;
203 const struct got_error *
204 got_worktree_checkout_files(struct got_worktree *worktree,
205 struct got_repository *repo)
207 return NULL;