Blame


1 86c3caaf 2018-03-09 stsp /*
2 86c3caaf 2018-03-09 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 86c3caaf 2018-03-09 stsp *
4 86c3caaf 2018-03-09 stsp * Permission to use, copy, modify, and distribute this software for any
5 86c3caaf 2018-03-09 stsp * purpose with or without fee is hereby granted, provided that the above
6 86c3caaf 2018-03-09 stsp * copyright notice and this permission notice appear in all copies.
7 86c3caaf 2018-03-09 stsp *
8 86c3caaf 2018-03-09 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 86c3caaf 2018-03-09 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 86c3caaf 2018-03-09 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 86c3caaf 2018-03-09 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 86c3caaf 2018-03-09 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 86c3caaf 2018-03-09 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 86c3caaf 2018-03-09 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 86c3caaf 2018-03-09 stsp */
16 86c3caaf 2018-03-09 stsp
17 86c3caaf 2018-03-09 stsp #include <sys/stat.h>
18 86c3caaf 2018-03-09 stsp
19 86c3caaf 2018-03-09 stsp #include <string.h>
20 86c3caaf 2018-03-09 stsp #include <stdio.h>
21 86c3caaf 2018-03-09 stsp #include <stdlib.h>
22 86c3caaf 2018-03-09 stsp #include <fcntl.h>
23 86c3caaf 2018-03-09 stsp #include <errno.h>
24 86c3caaf 2018-03-09 stsp #include <unistd.h>
25 86c3caaf 2018-03-09 stsp
26 86c3caaf 2018-03-09 stsp #include "got_error.h"
27 86c3caaf 2018-03-09 stsp #include "got_repository.h"
28 86c3caaf 2018-03-09 stsp #include "got_refs.h"
29 86c3caaf 2018-03-09 stsp #include "got_worktree.h"
30 86c3caaf 2018-03-09 stsp
31 86c3caaf 2018-03-09 stsp #include "got_worktree_priv.h"
32 86c3caaf 2018-03-09 stsp #include "got_path_priv.h"
33 86c3caaf 2018-03-09 stsp
34 99724ed4 2018-03-10 stsp static const struct got_error *
35 99724ed4 2018-03-10 stsp create_meta_file(const char *gotpath, const char *name, const char *content)
36 99724ed4 2018-03-10 stsp {
37 99724ed4 2018-03-10 stsp const struct got_error *err = NULL;
38 99724ed4 2018-03-10 stsp char *path;
39 99724ed4 2018-03-10 stsp int fd = -1;
40 99724ed4 2018-03-10 stsp char buf[4];
41 99724ed4 2018-03-10 stsp ssize_t n;
42 99724ed4 2018-03-10 stsp
43 99724ed4 2018-03-10 stsp if (asprintf(&path, "%s/%s", gotpath, name) == -1) {
44 99724ed4 2018-03-10 stsp err = got_error(GOT_ERR_NO_MEM);
45 99724ed4 2018-03-10 stsp path = NULL;
46 99724ed4 2018-03-10 stsp goto done;
47 99724ed4 2018-03-10 stsp }
48 99724ed4 2018-03-10 stsp
49 99724ed4 2018-03-10 stsp fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_EXLOCK | O_NOFOLLOW,
50 99724ed4 2018-03-10 stsp GOT_DEFAULT_FILE_MODE);
51 99724ed4 2018-03-10 stsp if (fd == -1) {
52 99724ed4 2018-03-10 stsp err = got_error_from_errno();
53 99724ed4 2018-03-10 stsp goto done;
54 99724ed4 2018-03-10 stsp }
55 99724ed4 2018-03-10 stsp
56 99724ed4 2018-03-10 stsp /* The file should be empty. */
57 99724ed4 2018-03-10 stsp n = read(fd, buf, sizeof(buf));
58 99724ed4 2018-03-10 stsp if (n != 0) {
59 99724ed4 2018-03-10 stsp err = (n == -1 ? got_error_from_errno() :
60 99724ed4 2018-03-10 stsp got_error(GOT_ERR_WORKTREE_EXISTS));
61 99724ed4 2018-03-10 stsp goto done;
62 99724ed4 2018-03-10 stsp }
63 99724ed4 2018-03-10 stsp
64 99724ed4 2018-03-10 stsp if (content) {
65 99724ed4 2018-03-10 stsp int len = dprintf(fd, "%s\n", content);
66 99724ed4 2018-03-10 stsp if (len != strlen(content) + 1) {
67 99724ed4 2018-03-10 stsp err = got_error_from_errno();
68 99724ed4 2018-03-10 stsp goto done;
69 99724ed4 2018-03-10 stsp }
70 99724ed4 2018-03-10 stsp }
71 99724ed4 2018-03-10 stsp
72 99724ed4 2018-03-10 stsp done:
73 99724ed4 2018-03-10 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
74 99724ed4 2018-03-10 stsp err = got_error_from_errno();
75 99724ed4 2018-03-10 stsp free(path);
76 99724ed4 2018-03-10 stsp return err;
77 99724ed4 2018-03-10 stsp }
78 99724ed4 2018-03-10 stsp
79 86c3caaf 2018-03-09 stsp const struct got_error *
80 86c3caaf 2018-03-09 stsp got_worktree_init(const char *path, struct got_reference *head_ref,
81 86c3caaf 2018-03-09 stsp struct got_repository *repo)
82 86c3caaf 2018-03-09 stsp {
83 86c3caaf 2018-03-09 stsp const struct got_error *err = NULL;
84 86c3caaf 2018-03-09 stsp char *abspath = NULL;
85 86c3caaf 2018-03-09 stsp char *normpath = NULL;
86 86c3caaf 2018-03-09 stsp char *gotpath = NULL;
87 86c3caaf 2018-03-09 stsp char *refstr = NULL;
88 86c3caaf 2018-03-09 stsp char *path_repos = NULL;
89 1451e70d 2018-03-10 stsp char *formatstr = NULL;
90 86c3caaf 2018-03-09 stsp
91 86c3caaf 2018-03-09 stsp if (got_path_is_absolute(path)) {
92 86c3caaf 2018-03-09 stsp abspath = strdup(path);
93 86c3caaf 2018-03-09 stsp if (abspath == NULL)
94 86c3caaf 2018-03-09 stsp return got_error(GOT_ERR_NO_MEM);
95 86c3caaf 2018-03-09 stsp } else {
96 86c3caaf 2018-03-09 stsp abspath = got_path_get_absolute(path);
97 86c3caaf 2018-03-09 stsp if (abspath == NULL)
98 86c3caaf 2018-03-09 stsp return got_error(GOT_ERR_BAD_PATH);
99 86c3caaf 2018-03-09 stsp }
100 86c3caaf 2018-03-09 stsp
101 86c3caaf 2018-03-09 stsp /* Create top-level directory (may already exist). */
102 86c3caaf 2018-03-09 stsp normpath = got_path_normalize(abspath);
103 86c3caaf 2018-03-09 stsp if (normpath == NULL) {
104 86c3caaf 2018-03-09 stsp err = got_error(GOT_ERR_BAD_PATH);
105 86c3caaf 2018-03-09 stsp goto done;
106 86c3caaf 2018-03-09 stsp }
107 86c3caaf 2018-03-09 stsp if (mkdir(normpath, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
108 86c3caaf 2018-03-09 stsp err = got_error_from_errno();
109 86c3caaf 2018-03-09 stsp goto done;
110 86c3caaf 2018-03-09 stsp }
111 86c3caaf 2018-03-09 stsp
112 86c3caaf 2018-03-09 stsp /* Create .got directory (may already exist). */
113 86c3caaf 2018-03-09 stsp if (asprintf(&gotpath, "%s/%s", normpath, GOT_WORKTREE_GOT_DIR) == -1) {
114 86c3caaf 2018-03-09 stsp err = got_error(GOT_ERR_NO_MEM);
115 86c3caaf 2018-03-09 stsp goto done;
116 86c3caaf 2018-03-09 stsp }
117 86c3caaf 2018-03-09 stsp if (mkdir(gotpath, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
118 86c3caaf 2018-03-09 stsp err = got_error_from_errno();
119 86c3caaf 2018-03-09 stsp goto done;
120 86c3caaf 2018-03-09 stsp }
121 86c3caaf 2018-03-09 stsp
122 86c3caaf 2018-03-09 stsp /* Create an empty file index. */
123 99724ed4 2018-03-10 stsp err = create_meta_file(gotpath, GOT_WORKTREE_FILE_INDEX, NULL);
124 99724ed4 2018-03-10 stsp if (err)
125 86c3caaf 2018-03-09 stsp goto done;
126 86c3caaf 2018-03-09 stsp
127 86c3caaf 2018-03-09 stsp /* Write the HEAD reference. */
128 86c3caaf 2018-03-09 stsp refstr = got_ref_to_str(head_ref);
129 86c3caaf 2018-03-09 stsp if (refstr == NULL) {
130 86c3caaf 2018-03-09 stsp err = got_error(GOT_ERR_NO_MEM);
131 86c3caaf 2018-03-09 stsp goto done;
132 86c3caaf 2018-03-09 stsp }
133 99724ed4 2018-03-10 stsp err = create_meta_file(gotpath, GOT_REF_HEAD, refstr);
134 99724ed4 2018-03-10 stsp if (err)
135 86c3caaf 2018-03-09 stsp goto done;
136 86c3caaf 2018-03-09 stsp
137 1451e70d 2018-03-10 stsp /* Store path to repository. */
138 86c3caaf 2018-03-09 stsp path_repos = got_repo_get_path(repo);
139 86c3caaf 2018-03-09 stsp if (path_repos == NULL) {
140 86c3caaf 2018-03-09 stsp err = got_error(GOT_ERR_NO_MEM);
141 86c3caaf 2018-03-09 stsp goto done;
142 86c3caaf 2018-03-09 stsp }
143 99724ed4 2018-03-10 stsp err = create_meta_file(gotpath, GOT_WORKTREE_REPOSITORY, path_repos);
144 99724ed4 2018-03-10 stsp if (err)
145 86c3caaf 2018-03-09 stsp goto done;
146 86c3caaf 2018-03-09 stsp
147 1451e70d 2018-03-10 stsp /* Stamp repository with format file. */
148 1451e70d 2018-03-10 stsp if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
149 1451e70d 2018-03-10 stsp err = got_error(GOT_ERR_NO_MEM);
150 1451e70d 2018-03-10 stsp goto done;
151 1451e70d 2018-03-10 stsp }
152 99724ed4 2018-03-10 stsp err = create_meta_file(gotpath, GOT_WORKTREE_FORMAT, formatstr);
153 99724ed4 2018-03-10 stsp if (err)
154 1451e70d 2018-03-10 stsp goto done;
155 1451e70d 2018-03-10 stsp
156 86c3caaf 2018-03-09 stsp done:
157 86c3caaf 2018-03-09 stsp free(abspath);
158 86c3caaf 2018-03-09 stsp free(normpath);
159 86c3caaf 2018-03-09 stsp free(gotpath);
160 1451e70d 2018-03-10 stsp free(formatstr);
161 86c3caaf 2018-03-09 stsp free(refstr);
162 86c3caaf 2018-03-09 stsp free(path_repos);
163 86c3caaf 2018-03-09 stsp return err;
164 86c3caaf 2018-03-09 stsp }
165 86c3caaf 2018-03-09 stsp
166 86c3caaf 2018-03-09 stsp const struct got_error *
167 86c3caaf 2018-03-09 stsp got_worktree_open(struct got_worktree **worktree, const char *path)
168 86c3caaf 2018-03-09 stsp {
169 86c3caaf 2018-03-09 stsp return NULL;
170 86c3caaf 2018-03-09 stsp }
171 86c3caaf 2018-03-09 stsp
172 86c3caaf 2018-03-09 stsp void
173 86c3caaf 2018-03-09 stsp got_worktree_close(struct got_worktree *worktree)
174 86c3caaf 2018-03-09 stsp {
175 86c3caaf 2018-03-09 stsp }
176 86c3caaf 2018-03-09 stsp
177 86c3caaf 2018-03-09 stsp char *
178 86c3caaf 2018-03-09 stsp got_worktree_get_repo_path(struct got_worktree *worktree)
179 86c3caaf 2018-03-09 stsp {
180 86c3caaf 2018-03-09 stsp return strdup(worktree->path_repo);
181 86c3caaf 2018-03-09 stsp }
182 86c3caaf 2018-03-09 stsp
183 86c3caaf 2018-03-09 stsp struct got_reference *
184 86c3caaf 2018-03-09 stsp got_worktree_get_head(struct got_worktree *worktree)
185 86c3caaf 2018-03-09 stsp {
186 86c3caaf 2018-03-09 stsp return NULL;
187 86c3caaf 2018-03-09 stsp }
188 86c3caaf 2018-03-09 stsp
189 86c3caaf 2018-03-09 stsp const struct got_error *
190 86c3caaf 2018-03-09 stsp got_worktree_set_head(struct got_worktree *worktree, struct got_reference *head,
191 86c3caaf 2018-03-09 stsp struct got_repository *repo)
192 86c3caaf 2018-03-09 stsp {
193 86c3caaf 2018-03-09 stsp return NULL;
194 86c3caaf 2018-03-09 stsp }
195 86c3caaf 2018-03-09 stsp
196 86c3caaf 2018-03-09 stsp const struct got_error *
197 281294a5 2018-03-10 stsp got_worktree_update_fileindex(struct got_worktree *worktree,
198 281294a5 2018-03-10 stsp struct got_repository *repo)
199 281294a5 2018-03-10 stsp {
200 281294a5 2018-03-10 stsp return NULL;
201 281294a5 2018-03-10 stsp }
202 ed7464bb 2018-03-10 stsp
203 281294a5 2018-03-10 stsp const struct got_error *
204 86c3caaf 2018-03-09 stsp got_worktree_checkout_files(struct got_worktree *worktree,
205 86c3caaf 2018-03-09 stsp struct got_repository *repo)
206 86c3caaf 2018-03-09 stsp {
207 86c3caaf 2018-03-09 stsp return NULL;
208 86c3caaf 2018-03-09 stsp }