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 6d9d28c3 2018-03-11 stsp #include <sys/limits.h>
19 9d31a1d8 2018-03-11 stsp #include <sys/queue.h>
20 86c3caaf 2018-03-09 stsp
21 86c3caaf 2018-03-09 stsp #include <string.h>
22 86c3caaf 2018-03-09 stsp #include <stdio.h>
23 86c3caaf 2018-03-09 stsp #include <stdlib.h>
24 86c3caaf 2018-03-09 stsp #include <fcntl.h>
25 86c3caaf 2018-03-09 stsp #include <errno.h>
26 86c3caaf 2018-03-09 stsp #include <unistd.h>
27 9d31a1d8 2018-03-11 stsp #include <sha1.h>
28 9d31a1d8 2018-03-11 stsp #include <zlib.h>
29 9d31a1d8 2018-03-11 stsp #include <fnmatch.h>
30 86c3caaf 2018-03-09 stsp
31 86c3caaf 2018-03-09 stsp #include "got_error.h"
32 86c3caaf 2018-03-09 stsp #include "got_repository.h"
33 5261c201 2018-04-01 stsp #include "got_reference.h"
34 9d31a1d8 2018-03-11 stsp #include "got_object.h"
35 86c3caaf 2018-03-09 stsp #include "got_worktree.h"
36 86c3caaf 2018-03-09 stsp
37 718b3ab0 2018-03-17 stsp #include "got_lib_worktree.h"
38 718b3ab0 2018-03-17 stsp #include "got_lib_path.h"
39 718b3ab0 2018-03-17 stsp #include "got_lib_sha1.h"
40 718b3ab0 2018-03-17 stsp #include "got_lib_fileindex.h"
41 718b3ab0 2018-03-17 stsp #include "got_lib_zbuf.h"
42 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
43 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
44 9d31a1d8 2018-03-11 stsp
45 9d31a1d8 2018-03-11 stsp #ifndef MIN
46 9d31a1d8 2018-03-11 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
47 9d31a1d8 2018-03-11 stsp #endif
48 86c3caaf 2018-03-09 stsp
49 99724ed4 2018-03-10 stsp static const struct got_error *
50 7ac97322 2018-03-11 stsp create_meta_file(const char *path_got, const char *name, const char *content)
51 99724ed4 2018-03-10 stsp {
52 99724ed4 2018-03-10 stsp const struct got_error *err = NULL;
53 99724ed4 2018-03-10 stsp char *path;
54 99724ed4 2018-03-10 stsp int fd = -1;
55 99724ed4 2018-03-10 stsp char buf[4];
56 99724ed4 2018-03-10 stsp ssize_t n;
57 99724ed4 2018-03-10 stsp
58 7ac97322 2018-03-11 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
59 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
60 99724ed4 2018-03-10 stsp path = NULL;
61 99724ed4 2018-03-10 stsp goto done;
62 99724ed4 2018-03-10 stsp }
63 99724ed4 2018-03-10 stsp
64 73a5ef67 2018-03-11 stsp fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
65 99724ed4 2018-03-10 stsp GOT_DEFAULT_FILE_MODE);
66 99724ed4 2018-03-10 stsp if (fd == -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 /* The file should be empty. */
72 99724ed4 2018-03-10 stsp n = read(fd, buf, sizeof(buf));
73 99724ed4 2018-03-10 stsp if (n != 0) {
74 99724ed4 2018-03-10 stsp err = (n == -1 ? got_error_from_errno() :
75 99724ed4 2018-03-10 stsp got_error(GOT_ERR_WORKTREE_EXISTS));
76 99724ed4 2018-03-10 stsp goto done;
77 99724ed4 2018-03-10 stsp }
78 99724ed4 2018-03-10 stsp
79 99724ed4 2018-03-10 stsp if (content) {
80 99724ed4 2018-03-10 stsp int len = dprintf(fd, "%s\n", content);
81 99724ed4 2018-03-10 stsp if (len != strlen(content) + 1) {
82 99724ed4 2018-03-10 stsp err = got_error_from_errno();
83 99724ed4 2018-03-10 stsp goto done;
84 99724ed4 2018-03-10 stsp }
85 99724ed4 2018-03-10 stsp }
86 99724ed4 2018-03-10 stsp
87 99724ed4 2018-03-10 stsp done:
88 99724ed4 2018-03-10 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
89 99724ed4 2018-03-10 stsp err = got_error_from_errno();
90 99724ed4 2018-03-10 stsp free(path);
91 99724ed4 2018-03-10 stsp return err;
92 99724ed4 2018-03-10 stsp }
93 99724ed4 2018-03-10 stsp
94 09fe317a 2018-03-11 stsp static const struct got_error *
95 7ac97322 2018-03-11 stsp read_meta_file(char **content, const char *path_got, const char *name)
96 09fe317a 2018-03-11 stsp {
97 09fe317a 2018-03-11 stsp const struct got_error *err = NULL;
98 09fe317a 2018-03-11 stsp char *path;
99 09fe317a 2018-03-11 stsp int fd = -1;
100 09fe317a 2018-03-11 stsp ssize_t n;
101 09fe317a 2018-03-11 stsp struct stat sb;
102 09fe317a 2018-03-11 stsp
103 09fe317a 2018-03-11 stsp *content = NULL;
104 09fe317a 2018-03-11 stsp
105 7ac97322 2018-03-11 stsp if (asprintf(&path, "%s/%s", path_got, name) == -1) {
106 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
107 09fe317a 2018-03-11 stsp path = NULL;
108 09fe317a 2018-03-11 stsp goto done;
109 09fe317a 2018-03-11 stsp }
110 09fe317a 2018-03-11 stsp
111 ef99fdb1 2018-03-11 stsp fd = open(path, O_RDONLY | O_NOFOLLOW);
112 09fe317a 2018-03-11 stsp if (fd == -1) {
113 ef99fdb1 2018-03-11 stsp err = got_error_from_errno();
114 ef99fdb1 2018-03-11 stsp goto done;
115 ef99fdb1 2018-03-11 stsp }
116 ef99fdb1 2018-03-11 stsp if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
117 73a5ef67 2018-03-11 stsp err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
118 73a5ef67 2018-03-11 stsp : got_error_from_errno());
119 09fe317a 2018-03-11 stsp goto done;
120 09fe317a 2018-03-11 stsp }
121 09fe317a 2018-03-11 stsp
122 09fe317a 2018-03-11 stsp stat(path, &sb);
123 09fe317a 2018-03-11 stsp *content = calloc(1, sb.st_size);
124 09fe317a 2018-03-11 stsp if (*content == NULL) {
125 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
126 09fe317a 2018-03-11 stsp goto done;
127 09fe317a 2018-03-11 stsp }
128 09fe317a 2018-03-11 stsp
129 09fe317a 2018-03-11 stsp n = read(fd, *content, sb.st_size);
130 09fe317a 2018-03-11 stsp if (n != sb.st_size) {
131 0605801d 2018-03-11 stsp err = (n == -1 ? got_error_from_errno() :
132 0605801d 2018-03-11 stsp got_error(GOT_ERR_WORKTREE_META));
133 09fe317a 2018-03-11 stsp goto done;
134 09fe317a 2018-03-11 stsp }
135 09fe317a 2018-03-11 stsp if ((*content)[sb.st_size - 1] != '\n') {
136 09fe317a 2018-03-11 stsp err = got_error(GOT_ERR_WORKTREE_META);
137 09fe317a 2018-03-11 stsp goto done;
138 09fe317a 2018-03-11 stsp }
139 09fe317a 2018-03-11 stsp (*content)[sb.st_size - 1] = '\0';
140 09fe317a 2018-03-11 stsp
141 09fe317a 2018-03-11 stsp done:
142 09fe317a 2018-03-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
143 09fe317a 2018-03-11 stsp err = got_error_from_errno();
144 09fe317a 2018-03-11 stsp free(path);
145 09fe317a 2018-03-11 stsp if (err) {
146 09fe317a 2018-03-11 stsp free(*content);
147 09fe317a 2018-03-11 stsp *content = NULL;
148 09fe317a 2018-03-11 stsp }
149 09fe317a 2018-03-11 stsp return err;
150 09fe317a 2018-03-11 stsp }
151 09fe317a 2018-03-11 stsp
152 86c3caaf 2018-03-09 stsp const struct got_error *
153 86c3caaf 2018-03-09 stsp got_worktree_init(const char *path, struct got_reference *head_ref,
154 577ec78f 2018-03-11 stsp const char *prefix, struct got_repository *repo)
155 86c3caaf 2018-03-09 stsp {
156 86c3caaf 2018-03-09 stsp const struct got_error *err = NULL;
157 7ac97322 2018-03-11 stsp char *path_got = NULL;
158 86c3caaf 2018-03-09 stsp char *refstr = NULL;
159 cde76477 2018-03-11 stsp char *repo_path = NULL;
160 1451e70d 2018-03-10 stsp char *formatstr = NULL;
161 0bb8a95e 2018-03-12 stsp char *absprefix = NULL;
162 86c3caaf 2018-03-09 stsp
163 0bb8a95e 2018-03-12 stsp if (!got_path_is_absolute(prefix)) {
164 0bb8a95e 2018-03-12 stsp if (asprintf(&absprefix, "/%s", prefix) == -1)
165 0a585a0d 2018-03-17 stsp return got_error_from_errno();
166 0bb8a95e 2018-03-12 stsp }
167 577ec78f 2018-03-11 stsp
168 86c3caaf 2018-03-09 stsp /* Create top-level directory (may already exist). */
169 2cb4bacb 2018-03-11 stsp if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
170 86c3caaf 2018-03-09 stsp err = got_error_from_errno();
171 86c3caaf 2018-03-09 stsp goto done;
172 86c3caaf 2018-03-09 stsp }
173 86c3caaf 2018-03-09 stsp
174 86c3caaf 2018-03-09 stsp /* Create .got directory (may already exist). */
175 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
176 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
177 86c3caaf 2018-03-09 stsp goto done;
178 86c3caaf 2018-03-09 stsp }
179 7ac97322 2018-03-11 stsp if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
180 86c3caaf 2018-03-09 stsp err = got_error_from_errno();
181 86c3caaf 2018-03-09 stsp goto done;
182 86c3caaf 2018-03-09 stsp }
183 86c3caaf 2018-03-09 stsp
184 056e7441 2018-03-11 stsp /* Create an empty lock file. */
185 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_LOCK, NULL);
186 056e7441 2018-03-11 stsp if (err)
187 056e7441 2018-03-11 stsp goto done;
188 056e7441 2018-03-11 stsp
189 86c3caaf 2018-03-09 stsp /* Create an empty file index. */
190 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FILE_INDEX, NULL);
191 99724ed4 2018-03-10 stsp if (err)
192 86c3caaf 2018-03-09 stsp goto done;
193 86c3caaf 2018-03-09 stsp
194 86c3caaf 2018-03-09 stsp /* Write the HEAD reference. */
195 86c3caaf 2018-03-09 stsp refstr = got_ref_to_str(head_ref);
196 86c3caaf 2018-03-09 stsp if (refstr == NULL) {
197 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
198 86c3caaf 2018-03-09 stsp goto done;
199 86c3caaf 2018-03-09 stsp }
200 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_HEAD, refstr);
201 99724ed4 2018-03-10 stsp if (err)
202 86c3caaf 2018-03-09 stsp goto done;
203 86c3caaf 2018-03-09 stsp
204 1451e70d 2018-03-10 stsp /* Store path to repository. */
205 cde76477 2018-03-11 stsp repo_path = got_repo_get_path(repo);
206 cde76477 2018-03-11 stsp if (repo_path == NULL) {
207 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
208 86c3caaf 2018-03-09 stsp goto done;
209 86c3caaf 2018-03-09 stsp }
210 cde76477 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY, repo_path);
211 99724ed4 2018-03-10 stsp if (err)
212 86c3caaf 2018-03-09 stsp goto done;
213 86c3caaf 2018-03-09 stsp
214 577ec78f 2018-03-11 stsp /* Store in-repository path prefix. */
215 0bb8a95e 2018-03-12 stsp err = create_meta_file(path_got, GOT_WORKTREE_PATH_PREFIX,
216 0bb8a95e 2018-03-12 stsp absprefix ? absprefix : prefix);
217 577ec78f 2018-03-11 stsp if (err)
218 577ec78f 2018-03-11 stsp goto done;
219 577ec78f 2018-03-11 stsp
220 9dce68ed 2018-03-10 stsp /* Stamp work tree with format file. */
221 1451e70d 2018-03-10 stsp if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
222 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
223 1451e70d 2018-03-10 stsp goto done;
224 1451e70d 2018-03-10 stsp }
225 7ac97322 2018-03-11 stsp err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
226 99724ed4 2018-03-10 stsp if (err)
227 1451e70d 2018-03-10 stsp goto done;
228 1451e70d 2018-03-10 stsp
229 86c3caaf 2018-03-09 stsp done:
230 7ac97322 2018-03-11 stsp free(path_got);
231 1451e70d 2018-03-10 stsp free(formatstr);
232 86c3caaf 2018-03-09 stsp free(refstr);
233 cde76477 2018-03-11 stsp free(repo_path);
234 0bb8a95e 2018-03-12 stsp free(absprefix);
235 86c3caaf 2018-03-09 stsp return err;
236 86c3caaf 2018-03-09 stsp }
237 86c3caaf 2018-03-09 stsp
238 86c3caaf 2018-03-09 stsp const struct got_error *
239 86c3caaf 2018-03-09 stsp got_worktree_open(struct got_worktree **worktree, const char *path)
240 86c3caaf 2018-03-09 stsp {
241 6d9d28c3 2018-03-11 stsp const struct got_error *err = NULL;
242 7ac97322 2018-03-11 stsp char *path_got;
243 6d9d28c3 2018-03-11 stsp char *formatstr = NULL;
244 056e7441 2018-03-11 stsp char *path_lock = NULL;
245 6d9d28c3 2018-03-11 stsp int version, fd = -1;
246 6d9d28c3 2018-03-11 stsp const char *errstr;
247 6d9d28c3 2018-03-11 stsp
248 6d9d28c3 2018-03-11 stsp *worktree = NULL;
249 6d9d28c3 2018-03-11 stsp
250 7ac97322 2018-03-11 stsp if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
251 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
252 7ac97322 2018-03-11 stsp path_got = NULL;
253 6d9d28c3 2018-03-11 stsp goto done;
254 6d9d28c3 2018-03-11 stsp }
255 6d9d28c3 2018-03-11 stsp
256 7ac97322 2018-03-11 stsp if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
257 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
258 056e7441 2018-03-11 stsp path_lock = NULL;
259 6d9d28c3 2018-03-11 stsp goto done;
260 6d9d28c3 2018-03-11 stsp }
261 6d9d28c3 2018-03-11 stsp
262 056e7441 2018-03-11 stsp fd = open(path_lock, O_RDWR | O_EXLOCK | O_NONBLOCK);
263 6d9d28c3 2018-03-11 stsp if (fd == -1) {
264 73a5ef67 2018-03-11 stsp err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
265 73a5ef67 2018-03-11 stsp : got_error_from_errno());
266 6d9d28c3 2018-03-11 stsp goto done;
267 6d9d28c3 2018-03-11 stsp }
268 6d9d28c3 2018-03-11 stsp
269 7ac97322 2018-03-11 stsp err = read_meta_file(&formatstr, path_got, GOT_WORKTREE_FORMAT);
270 6d9d28c3 2018-03-11 stsp if (err)
271 6d9d28c3 2018-03-11 stsp goto done;
272 6d9d28c3 2018-03-11 stsp
273 6d9d28c3 2018-03-11 stsp version = strtonum(formatstr, 1, INT_MAX, &errstr);
274 6d9d28c3 2018-03-11 stsp if (errstr) {
275 6d9d28c3 2018-03-11 stsp err = got_error(GOT_ERR_WORKTREE_META);
276 6d9d28c3 2018-03-11 stsp goto done;
277 6d9d28c3 2018-03-11 stsp }
278 6d9d28c3 2018-03-11 stsp if (version != GOT_WORKTREE_FORMAT_VERSION) {
279 6d9d28c3 2018-03-11 stsp err = got_error(GOT_ERR_WORKTREE_VERS);
280 6d9d28c3 2018-03-11 stsp goto done;
281 6d9d28c3 2018-03-11 stsp }
282 6d9d28c3 2018-03-11 stsp
283 6d9d28c3 2018-03-11 stsp *worktree = calloc(1, sizeof(**worktree));
284 6d9d28c3 2018-03-11 stsp if (*worktree == NULL) {
285 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
286 6d9d28c3 2018-03-11 stsp goto done;
287 6d9d28c3 2018-03-11 stsp }
288 056e7441 2018-03-11 stsp (*worktree)->lockfd = -1;
289 6d9d28c3 2018-03-11 stsp
290 c88eb298 2018-03-11 stsp (*worktree)->root_path = strdup(path);
291 c88eb298 2018-03-11 stsp if ((*worktree)->root_path == NULL) {
292 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
293 6d9d28c3 2018-03-11 stsp goto done;
294 6d9d28c3 2018-03-11 stsp }
295 cde76477 2018-03-11 stsp err = read_meta_file(&(*worktree)->repo_path, path_got,
296 6d9d28c3 2018-03-11 stsp GOT_WORKTREE_REPOSITORY);
297 6d9d28c3 2018-03-11 stsp if (err)
298 6d9d28c3 2018-03-11 stsp goto done;
299 7ac97322 2018-03-11 stsp err = read_meta_file(&(*worktree)->path_prefix, path_got,
300 6d9d28c3 2018-03-11 stsp GOT_WORKTREE_PATH_PREFIX);
301 f5baf295 2018-03-11 stsp if (err)
302 f5baf295 2018-03-11 stsp goto done;
303 f5baf295 2018-03-11 stsp
304 e8f36958 2018-03-11 stsp err = read_meta_file(&(*worktree)->head_ref, path_got,
305 e8f36958 2018-03-11 stsp GOT_WORKTREE_HEAD);
306 f5baf295 2018-03-11 stsp if (err)
307 6d9d28c3 2018-03-11 stsp goto done;
308 6d9d28c3 2018-03-11 stsp
309 6d9d28c3 2018-03-11 stsp done:
310 7ac97322 2018-03-11 stsp free(path_got);
311 056e7441 2018-03-11 stsp free(path_lock);
312 6d9d28c3 2018-03-11 stsp if (err) {
313 6d9d28c3 2018-03-11 stsp if (fd != -1)
314 6d9d28c3 2018-03-11 stsp close(fd);
315 6d9d28c3 2018-03-11 stsp if (*worktree != NULL)
316 6d9d28c3 2018-03-11 stsp got_worktree_close(*worktree);
317 6d9d28c3 2018-03-11 stsp *worktree = NULL;
318 6d9d28c3 2018-03-11 stsp } else
319 056e7441 2018-03-11 stsp (*worktree)->lockfd = fd;
320 6d9d28c3 2018-03-11 stsp
321 6d9d28c3 2018-03-11 stsp return err;
322 86c3caaf 2018-03-09 stsp }
323 86c3caaf 2018-03-09 stsp
324 86c3caaf 2018-03-09 stsp void
325 86c3caaf 2018-03-09 stsp got_worktree_close(struct got_worktree *worktree)
326 86c3caaf 2018-03-09 stsp {
327 c88eb298 2018-03-11 stsp free(worktree->root_path);
328 cde76477 2018-03-11 stsp free(worktree->repo_path);
329 6d9d28c3 2018-03-11 stsp free(worktree->path_prefix);
330 d6c38e0d 2018-03-11 stsp free(worktree->base_commit);
331 e8f36958 2018-03-11 stsp free(worktree->head_ref);
332 056e7441 2018-03-11 stsp if (worktree->lockfd != -1)
333 056e7441 2018-03-11 stsp close(worktree->lockfd);
334 6d9d28c3 2018-03-11 stsp free(worktree);
335 86c3caaf 2018-03-09 stsp }
336 86c3caaf 2018-03-09 stsp
337 86c3caaf 2018-03-09 stsp char *
338 86c3caaf 2018-03-09 stsp got_worktree_get_repo_path(struct got_worktree *worktree)
339 86c3caaf 2018-03-09 stsp {
340 cde76477 2018-03-11 stsp return strdup(worktree->repo_path);
341 86c3caaf 2018-03-09 stsp }
342 86c3caaf 2018-03-09 stsp
343 35be1456 2018-03-11 stsp char *
344 35be1456 2018-03-11 stsp got_worktree_get_head_ref_name(struct got_worktree *worktree)
345 86c3caaf 2018-03-09 stsp {
346 35be1456 2018-03-11 stsp return strdup(worktree->head_ref);
347 86c3caaf 2018-03-09 stsp }
348 86c3caaf 2018-03-09 stsp
349 9d31a1d8 2018-03-11 stsp static const struct got_error *
350 9d31a1d8 2018-03-11 stsp lock_worktree(struct got_worktree *worktree, int operation)
351 86c3caaf 2018-03-09 stsp {
352 9d31a1d8 2018-03-11 stsp if (flock(worktree->lockfd, operation | LOCK_NB) == -1)
353 9d31a1d8 2018-03-11 stsp return (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
354 9d31a1d8 2018-03-11 stsp : got_error_from_errno());
355 86c3caaf 2018-03-09 stsp return NULL;
356 86c3caaf 2018-03-09 stsp }
357 9d31a1d8 2018-03-11 stsp
358 9d31a1d8 2018-03-11 stsp static const char *
359 9d31a1d8 2018-03-11 stsp apply_path_prefix(struct got_worktree *worktree, const char *path)
360 9d31a1d8 2018-03-11 stsp {
361 9d31a1d8 2018-03-11 stsp const char *p = path;
362 9d31a1d8 2018-03-11 stsp p += strlen(worktree->path_prefix);
363 9d31a1d8 2018-03-11 stsp if (*p == '/')
364 9d31a1d8 2018-03-11 stsp p++;
365 9d31a1d8 2018-03-11 stsp return p;
366 9d31a1d8 2018-03-11 stsp }
367 9d31a1d8 2018-03-11 stsp
368 9d31a1d8 2018-03-11 stsp static const struct got_error *
369 9d31a1d8 2018-03-11 stsp add_file_on_disk(struct got_worktree *worktree, struct got_fileindex *fileindex,
370 9d31a1d8 2018-03-11 stsp const char *path, struct got_blob_object *blob, struct got_repository *repo)
371 9d31a1d8 2018-03-11 stsp {
372 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
373 c34b20a2 2018-03-12 stsp char *ondisk_path;
374 9d31a1d8 2018-03-11 stsp int fd;
375 9d31a1d8 2018-03-11 stsp size_t len, hdrlen;
376 9d31a1d8 2018-03-11 stsp struct got_fileindex_entry *entry;
377 9d31a1d8 2018-03-11 stsp
378 c34b20a2 2018-03-12 stsp if (asprintf(&ondisk_path, "%s/%s", worktree->root_path,
379 9d31a1d8 2018-03-11 stsp apply_path_prefix(worktree, path)) == -1)
380 0a585a0d 2018-03-17 stsp return got_error_from_errno();
381 9d31a1d8 2018-03-11 stsp
382 c34b20a2 2018-03-12 stsp fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
383 9d31a1d8 2018-03-11 stsp GOT_DEFAULT_FILE_MODE);
384 9d31a1d8 2018-03-11 stsp if (fd == -1) {
385 9d31a1d8 2018-03-11 stsp err = got_error_from_errno();
386 9d31a1d8 2018-03-11 stsp if (errno == EEXIST) {
387 9d31a1d8 2018-03-11 stsp struct stat sb;
388 c34b20a2 2018-03-12 stsp if (lstat(ondisk_path, &sb) == -1) {
389 9d31a1d8 2018-03-11 stsp err = got_error_from_errno();
390 9d31a1d8 2018-03-11 stsp } else if (!S_ISREG(sb.st_mode)) {
391 9d31a1d8 2018-03-11 stsp /* TODO file is obstructed; do something */
392 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_FILE_OBSTRUCTED);
393 9d31a1d8 2018-03-11 stsp }
394 9d31a1d8 2018-03-11 stsp }
395 9d31a1d8 2018-03-11 stsp return err;
396 9d31a1d8 2018-03-11 stsp }
397 9d31a1d8 2018-03-11 stsp
398 9d31a1d8 2018-03-11 stsp hdrlen = got_object_blob_get_hdrlen(blob);
399 9d31a1d8 2018-03-11 stsp do {
400 9d31a1d8 2018-03-11 stsp const uint8_t *buf = got_object_blob_get_read_buf(blob);
401 9d31a1d8 2018-03-11 stsp err = got_object_blob_read_block(&len, blob);
402 9d31a1d8 2018-03-11 stsp if (err)
403 9d31a1d8 2018-03-11 stsp break;
404 9d31a1d8 2018-03-11 stsp if (len > 0) {
405 9d31a1d8 2018-03-11 stsp /* Skip blob object header first time around. */
406 9d31a1d8 2018-03-11 stsp ssize_t outlen = write(fd, buf + hdrlen, len - hdrlen);
407 9d31a1d8 2018-03-11 stsp hdrlen = 0;
408 9d31a1d8 2018-03-11 stsp if (outlen == -1) {
409 9d31a1d8 2018-03-11 stsp err = got_error_from_errno();
410 9d31a1d8 2018-03-11 stsp break;
411 9d31a1d8 2018-03-11 stsp } else if (outlen != len) {
412 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_IO);
413 9d31a1d8 2018-03-11 stsp break;
414 9d31a1d8 2018-03-11 stsp }
415 9d31a1d8 2018-03-11 stsp }
416 9d31a1d8 2018-03-11 stsp } while (len != 0);
417 9d31a1d8 2018-03-11 stsp
418 9d31a1d8 2018-03-11 stsp fsync(fd);
419 9d31a1d8 2018-03-11 stsp
420 c34b20a2 2018-03-12 stsp err = got_fileindex_entry_open(&entry, ondisk_path,
421 c34b20a2 2018-03-12 stsp apply_path_prefix(worktree, path), blob->id.sha1);
422 9d31a1d8 2018-03-11 stsp if (err)
423 9d31a1d8 2018-03-11 stsp goto done;
424 9d31a1d8 2018-03-11 stsp
425 9d31a1d8 2018-03-11 stsp err = got_fileindex_entry_add(fileindex, entry);
426 9d31a1d8 2018-03-11 stsp if (err)
427 9d31a1d8 2018-03-11 stsp goto done;
428 9d31a1d8 2018-03-11 stsp done:
429 9d31a1d8 2018-03-11 stsp close(fd);
430 c34b20a2 2018-03-12 stsp free(ondisk_path);
431 9d31a1d8 2018-03-11 stsp return err;
432 9d31a1d8 2018-03-11 stsp }
433 9d31a1d8 2018-03-11 stsp
434 9d31a1d8 2018-03-11 stsp static const struct got_error *
435 9d31a1d8 2018-03-11 stsp add_dir_on_disk(struct got_worktree *worktree, const char *path)
436 9d31a1d8 2018-03-11 stsp {
437 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
438 9d31a1d8 2018-03-11 stsp char *abspath;
439 9d31a1d8 2018-03-11 stsp
440 9d31a1d8 2018-03-11 stsp if (asprintf(&abspath, "%s/%s", worktree->root_path,
441 9d31a1d8 2018-03-11 stsp apply_path_prefix(worktree, path)) == -1)
442 0a585a0d 2018-03-17 stsp return got_error_from_errno();
443 9d31a1d8 2018-03-11 stsp
444 9d31a1d8 2018-03-11 stsp /* XXX queue work rather than editing disk directly? */
445 9d31a1d8 2018-03-11 stsp if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1) {
446 9d31a1d8 2018-03-11 stsp struct stat sb;
447 9d31a1d8 2018-03-11 stsp
448 9d31a1d8 2018-03-11 stsp if (errno != EEXIST) {
449 9d31a1d8 2018-03-11 stsp err = got_error_from_errno();
450 9d31a1d8 2018-03-11 stsp goto done;
451 9d31a1d8 2018-03-11 stsp }
452 9d31a1d8 2018-03-11 stsp
453 9d31a1d8 2018-03-11 stsp if (lstat(abspath, &sb) == -1) {
454 9d31a1d8 2018-03-11 stsp err = got_error_from_errno();
455 9d31a1d8 2018-03-11 stsp goto done;
456 9d31a1d8 2018-03-11 stsp }
457 9d31a1d8 2018-03-11 stsp
458 9d31a1d8 2018-03-11 stsp if (!S_ISDIR(sb.st_mode)) {
459 9d31a1d8 2018-03-11 stsp /* TODO directory is obstructed; do something */
460 9d31a1d8 2018-03-11 stsp return got_error(GOT_ERR_FILE_OBSTRUCTED);
461 9d31a1d8 2018-03-11 stsp }
462 9d31a1d8 2018-03-11 stsp }
463 9d31a1d8 2018-03-11 stsp
464 9d31a1d8 2018-03-11 stsp done:
465 9d31a1d8 2018-03-11 stsp free(abspath);
466 9d31a1d8 2018-03-11 stsp return err;
467 9d31a1d8 2018-03-11 stsp }
468 9d31a1d8 2018-03-11 stsp
469 9d31a1d8 2018-03-11 stsp static const struct got_error *
470 9d31a1d8 2018-03-11 stsp tree_checkout(struct got_worktree *, struct got_fileindex *,
471 92a684f4 2018-03-12 stsp struct got_tree_object *, const char *, struct got_repository *,
472 92a684f4 2018-03-12 stsp got_worktree_checkout_cb progress_cb, void *progress_arg);
473 9d31a1d8 2018-03-11 stsp
474 9d31a1d8 2018-03-11 stsp static const struct got_error *
475 9d31a1d8 2018-03-11 stsp tree_checkout_entry(struct got_worktree *worktree,
476 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex, struct got_tree_entry *te,
477 92a684f4 2018-03-12 stsp const char *parent, struct got_repository *repo,
478 92a684f4 2018-03-12 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
479 9d31a1d8 2018-03-11 stsp {
480 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
481 9d31a1d8 2018-03-11 stsp struct got_object *obj = NULL;
482 9d31a1d8 2018-03-11 stsp struct got_blob_object *blob = NULL;
483 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
484 9d31a1d8 2018-03-11 stsp char *path = NULL;
485 f7b38925 2018-04-01 stsp char *progress_path = NULL;
486 9d31a1d8 2018-03-11 stsp size_t len;
487 9d31a1d8 2018-03-11 stsp
488 9d31a1d8 2018-03-11 stsp if (parent[0] == '/' && parent[1] == '\0')
489 9d31a1d8 2018-03-11 stsp parent = "";
490 9d31a1d8 2018-03-11 stsp if (asprintf(&path, "%s/%s", parent, te->name) == -1)
491 0a585a0d 2018-03-17 stsp return got_error_from_errno();
492 9d31a1d8 2018-03-11 stsp
493 9d31a1d8 2018-03-11 stsp /* Skip this entry if it is outside of our path prefix. */
494 9d31a1d8 2018-03-11 stsp len = MIN(strlen(worktree->path_prefix), strlen(path));
495 9d31a1d8 2018-03-11 stsp if (strncmp(path, worktree->path_prefix, len) != 0) {
496 9d31a1d8 2018-03-11 stsp free(path);
497 9d31a1d8 2018-03-11 stsp return NULL;
498 9d31a1d8 2018-03-11 stsp }
499 9d31a1d8 2018-03-11 stsp
500 9d31a1d8 2018-03-11 stsp err = got_object_open(&obj, repo, te->id);
501 9d31a1d8 2018-03-11 stsp if (err)
502 9d31a1d8 2018-03-11 stsp goto done;
503 9d31a1d8 2018-03-11 stsp
504 f7b38925 2018-04-01 stsp progress_path = path;
505 f7b38925 2018-04-01 stsp if (strncmp(progress_path, worktree->path_prefix, len) == 0)
506 f7b38925 2018-04-01 stsp progress_path += len;
507 f7b38925 2018-04-01 stsp (*progress_cb)(progress_arg, progress_path);
508 92a684f4 2018-03-12 stsp
509 9d31a1d8 2018-03-11 stsp switch (got_object_get_type(obj)) {
510 9d31a1d8 2018-03-11 stsp case GOT_OBJ_TYPE_BLOB:
511 9d31a1d8 2018-03-11 stsp if (strlen(worktree->path_prefix) >= strlen(path))
512 9d31a1d8 2018-03-11 stsp break;
513 9d31a1d8 2018-03-11 stsp err = got_object_blob_open(&blob, repo, obj, 8192);
514 9d31a1d8 2018-03-11 stsp if (err)
515 9d31a1d8 2018-03-11 stsp goto done;
516 9d31a1d8 2018-03-11 stsp err = add_file_on_disk(worktree, fileindex, path, blob, repo);
517 9d31a1d8 2018-03-11 stsp break;
518 9d31a1d8 2018-03-11 stsp case GOT_OBJ_TYPE_TREE:
519 9d31a1d8 2018-03-11 stsp err = got_object_tree_open(&tree, repo, obj);
520 9d31a1d8 2018-03-11 stsp if (err)
521 9d31a1d8 2018-03-11 stsp goto done;
522 9d31a1d8 2018-03-11 stsp if (strlen(worktree->path_prefix) < strlen(path)) {
523 9d31a1d8 2018-03-11 stsp err = add_dir_on_disk(worktree, path);
524 9d31a1d8 2018-03-11 stsp if (err)
525 9d31a1d8 2018-03-11 stsp break;
526 9d31a1d8 2018-03-11 stsp }
527 92a684f4 2018-03-12 stsp err = tree_checkout(worktree, fileindex, tree, path, repo,
528 92a684f4 2018-03-12 stsp progress_cb, progress_arg);
529 9d31a1d8 2018-03-11 stsp break;
530 9d31a1d8 2018-03-11 stsp default:
531 9d31a1d8 2018-03-11 stsp break;
532 9d31a1d8 2018-03-11 stsp }
533 9d31a1d8 2018-03-11 stsp
534 9d31a1d8 2018-03-11 stsp done:
535 9d31a1d8 2018-03-11 stsp if (blob)
536 9d31a1d8 2018-03-11 stsp got_object_blob_close(blob);
537 9d31a1d8 2018-03-11 stsp if (tree)
538 9d31a1d8 2018-03-11 stsp got_object_tree_close(tree);
539 9d31a1d8 2018-03-11 stsp if (obj)
540 9d31a1d8 2018-03-11 stsp got_object_close(obj);
541 9d31a1d8 2018-03-11 stsp free(path);
542 9d31a1d8 2018-03-11 stsp return err;
543 9d31a1d8 2018-03-11 stsp }
544 9d31a1d8 2018-03-11 stsp
545 9d31a1d8 2018-03-11 stsp static const struct got_error *
546 9d31a1d8 2018-03-11 stsp tree_checkout(struct got_worktree *worktree,
547 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex, struct got_tree_object *tree,
548 92a684f4 2018-03-12 stsp const char *path, struct got_repository *repo,
549 92a684f4 2018-03-12 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
550 9d31a1d8 2018-03-11 stsp {
551 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL;
552 9d31a1d8 2018-03-11 stsp struct got_tree_entry *te;
553 9d31a1d8 2018-03-11 stsp size_t len;
554 9d31a1d8 2018-03-11 stsp
555 9d31a1d8 2018-03-11 stsp /* Skip this tree if it is outside of our path prefix. */
556 9d31a1d8 2018-03-11 stsp len = MIN(strlen(worktree->path_prefix), strlen(path));
557 9d31a1d8 2018-03-11 stsp if (strncmp(path, worktree->path_prefix, len) != 0)
558 9d31a1d8 2018-03-11 stsp return NULL;
559 9d31a1d8 2018-03-11 stsp
560 9d31a1d8 2018-03-11 stsp SIMPLEQ_FOREACH(te, &tree->entries, entry) {
561 92a684f4 2018-03-12 stsp err = tree_checkout_entry(worktree, fileindex, te, path, repo,
562 92a684f4 2018-03-12 stsp progress_cb, progress_arg);
563 9d31a1d8 2018-03-11 stsp if (err)
564 9d31a1d8 2018-03-11 stsp break;
565 9d31a1d8 2018-03-11 stsp }
566 9d31a1d8 2018-03-11 stsp
567 9d31a1d8 2018-03-11 stsp return err;
568 9d31a1d8 2018-03-11 stsp }
569 9d31a1d8 2018-03-11 stsp
570 9d31a1d8 2018-03-11 stsp const struct got_error *
571 9d31a1d8 2018-03-11 stsp got_worktree_checkout_files(struct got_worktree *worktree,
572 92a684f4 2018-03-12 stsp struct got_reference *head_ref, struct got_repository *repo,
573 92a684f4 2018-03-12 stsp got_worktree_checkout_cb progress_cb, void *progress_arg)
574 9d31a1d8 2018-03-11 stsp {
575 9d31a1d8 2018-03-11 stsp const struct got_error *err = NULL, *unlockerr;
576 9d31a1d8 2018-03-11 stsp struct got_object_id *commit_id = NULL;
577 9d31a1d8 2018-03-11 stsp struct got_object *obj = NULL;
578 9d31a1d8 2018-03-11 stsp struct got_commit_object *commit = NULL;
579 9d31a1d8 2018-03-11 stsp struct got_tree_object *tree = NULL;
580 9d31a1d8 2018-03-11 stsp char *fileindex_path = NULL, *new_fileindex_path = NULL;
581 9d31a1d8 2018-03-11 stsp struct got_fileindex *fileindex = NULL;
582 9d31a1d8 2018-03-11 stsp FILE *findex = NULL;
583 9d31a1d8 2018-03-11 stsp
584 9d31a1d8 2018-03-11 stsp err = lock_worktree(worktree, LOCK_EX);
585 9d31a1d8 2018-03-11 stsp if (err)
586 9d31a1d8 2018-03-11 stsp return err;
587 9d31a1d8 2018-03-11 stsp
588 9d31a1d8 2018-03-11 stsp fileindex = got_fileindex_open();
589 9d31a1d8 2018-03-11 stsp if (fileindex == NULL) {
590 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
591 9d31a1d8 2018-03-11 stsp goto done;
592 9d31a1d8 2018-03-11 stsp }
593 9d31a1d8 2018-03-11 stsp
594 9d31a1d8 2018-03-11 stsp if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
595 9d31a1d8 2018-03-11 stsp GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
596 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
597 9d31a1d8 2018-03-11 stsp fileindex_path = NULL;
598 9d31a1d8 2018-03-11 stsp goto done;
599 9d31a1d8 2018-03-11 stsp }
600 9d31a1d8 2018-03-11 stsp
601 51664889 2018-03-12 stsp err = got_opentemp_named(&new_fileindex_path, &findex, fileindex_path);
602 51664889 2018-03-12 stsp if (err)
603 51664889 2018-03-12 stsp goto done;
604 51664889 2018-03-12 stsp
605 9d31a1d8 2018-03-11 stsp err = got_ref_resolve(&commit_id, repo, head_ref);
606 9d31a1d8 2018-03-11 stsp if (err)
607 9d31a1d8 2018-03-11 stsp goto done;
608 9d31a1d8 2018-03-11 stsp
609 9d31a1d8 2018-03-11 stsp err = got_object_open(&obj, repo, commit_id);
610 9d31a1d8 2018-03-11 stsp if (err)
611 9d31a1d8 2018-03-11 stsp goto done;
612 9d31a1d8 2018-03-11 stsp
613 9d31a1d8 2018-03-11 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
614 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_OBJ_TYPE);
615 9d31a1d8 2018-03-11 stsp goto done;
616 9d31a1d8 2018-03-11 stsp }
617 9d31a1d8 2018-03-11 stsp
618 9d31a1d8 2018-03-11 stsp err = got_object_commit_open(&commit, repo, obj);
619 9d31a1d8 2018-03-11 stsp if (err)
620 9d31a1d8 2018-03-11 stsp goto done;
621 9d31a1d8 2018-03-11 stsp
622 9d31a1d8 2018-03-11 stsp got_object_close(obj);
623 9d31a1d8 2018-03-11 stsp err = got_object_open(&obj, repo, commit->tree_id);
624 9d31a1d8 2018-03-11 stsp if (err)
625 9d31a1d8 2018-03-11 stsp goto done;
626 9d31a1d8 2018-03-11 stsp
627 9d31a1d8 2018-03-11 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_TREE) {
628 9d31a1d8 2018-03-11 stsp err = got_error(GOT_ERR_OBJ_TYPE);
629 9d31a1d8 2018-03-11 stsp goto done;
630 9d31a1d8 2018-03-11 stsp }
631 9d31a1d8 2018-03-11 stsp
632 9d31a1d8 2018-03-11 stsp err = got_object_tree_open(&tree, repo, obj);
633 9d31a1d8 2018-03-11 stsp if (err)
634 9d31a1d8 2018-03-11 stsp goto done;
635 9d31a1d8 2018-03-11 stsp
636 92a684f4 2018-03-12 stsp err = tree_checkout(worktree, fileindex, tree, "/", repo,
637 92a684f4 2018-03-12 stsp progress_cb, progress_arg);
638 9d31a1d8 2018-03-11 stsp if (err)
639 9d31a1d8 2018-03-11 stsp goto done;
640 9d31a1d8 2018-03-11 stsp
641 9d31a1d8 2018-03-11 stsp err = got_fileindex_write(fileindex, findex);
642 9d31a1d8 2018-03-11 stsp if (err)
643 9d31a1d8 2018-03-11 stsp goto done;
644 9d31a1d8 2018-03-11 stsp
645 9d31a1d8 2018-03-11 stsp if (rename(new_fileindex_path, fileindex_path) != 0) {
646 9d31a1d8 2018-03-11 stsp err = got_error_from_errno();
647 9d31a1d8 2018-03-11 stsp goto done;
648 9d31a1d8 2018-03-11 stsp }
649 9d31a1d8 2018-03-11 stsp
650 9d31a1d8 2018-03-11 stsp free(new_fileindex_path);
651 9d31a1d8 2018-03-11 stsp new_fileindex_path = NULL;
652 9d31a1d8 2018-03-11 stsp
653 9d31a1d8 2018-03-11 stsp done:
654 9d31a1d8 2018-03-11 stsp if (commit)
655 9d31a1d8 2018-03-11 stsp got_object_commit_close(commit);
656 9d31a1d8 2018-03-11 stsp if (obj)
657 9d31a1d8 2018-03-11 stsp got_object_close(obj);
658 9d31a1d8 2018-03-11 stsp free(commit_id);
659 9d31a1d8 2018-03-11 stsp if (new_fileindex_path)
660 9d31a1d8 2018-03-11 stsp unlink(new_fileindex_path);
661 9d31a1d8 2018-03-11 stsp if (findex)
662 9d31a1d8 2018-03-11 stsp fclose(findex);
663 9d31a1d8 2018-03-11 stsp free(new_fileindex_path);
664 9d31a1d8 2018-03-11 stsp free(fileindex_path);
665 9d31a1d8 2018-03-11 stsp got_fileindex_close(fileindex);
666 9d31a1d8 2018-03-11 stsp unlockerr = lock_worktree(worktree, LOCK_SH);
667 9d31a1d8 2018-03-11 stsp if (unlockerr && err == NULL)
668 9d31a1d8 2018-03-11 stsp err = unlockerr;
669 9d31a1d8 2018-03-11 stsp return err;
670 9d31a1d8 2018-03-11 stsp }