Blame


1 f8a36e22 2021-08-26 stsp /*
2 f8a36e22 2021-08-26 stsp * Copyright (c) 2018, 2019 Ori Bernstein <ori@openbsd.org>
3 f8a36e22 2021-08-26 stsp * Copyright (c) 2021 Stefan Sperling <stsp@openbsd.org>
4 27555e8e 2023-07-05 jrick * Copyright (c) 2023 Josh Rickmar <jrick@zettaport.com>
5 f8a36e22 2021-08-26 stsp *
6 f8a36e22 2021-08-26 stsp * Permission to use, copy, modify, and distribute this software for any
7 f8a36e22 2021-08-26 stsp * purpose with or without fee is hereby granted, provided that the above
8 f8a36e22 2021-08-26 stsp * copyright notice and this permission notice appear in all copies.
9 f8a36e22 2021-08-26 stsp *
10 f8a36e22 2021-08-26 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 f8a36e22 2021-08-26 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 f8a36e22 2021-08-26 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 f8a36e22 2021-08-26 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 f8a36e22 2021-08-26 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 f8a36e22 2021-08-26 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 f8a36e22 2021-08-26 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 f8a36e22 2021-08-26 stsp */
18 f8a36e22 2021-08-26 stsp
19 f8a36e22 2021-08-26 stsp #include <sys/types.h>
20 f8a36e22 2021-08-26 stsp #include <sys/stat.h>
21 f8a36e22 2021-08-26 stsp #include <sys/queue.h>
22 f8b19efd 2021-10-13 stsp #include <sys/tree.h>
23 f8a36e22 2021-08-26 stsp #include <sys/uio.h>
24 f8a36e22 2021-08-26 stsp #include <sys/socket.h>
25 f8a36e22 2021-08-26 stsp #include <sys/wait.h>
26 f8a36e22 2021-08-26 stsp #include <sys/resource.h>
27 f8a36e22 2021-08-26 stsp #include <sys/socket.h>
28 f8a36e22 2021-08-26 stsp
29 f8a36e22 2021-08-26 stsp #include <endian.h>
30 f8a36e22 2021-08-26 stsp #include <errno.h>
31 f8a36e22 2021-08-26 stsp #include <err.h>
32 f8a36e22 2021-08-26 stsp #include <fcntl.h>
33 f8a36e22 2021-08-26 stsp #include <stdio.h>
34 f8a36e22 2021-08-26 stsp #include <stdlib.h>
35 f8a36e22 2021-08-26 stsp #include <string.h>
36 f8a36e22 2021-08-26 stsp #include <stdint.h>
37 f8a36e22 2021-08-26 stsp #include <sha1.h>
38 5822e79e 2023-02-23 op #include <sha2.h>
39 f8a36e22 2021-08-26 stsp #include <unistd.h>
40 f8a36e22 2021-08-26 stsp #include <zlib.h>
41 f8a36e22 2021-08-26 stsp #include <ctype.h>
42 f8a36e22 2021-08-26 stsp #include <limits.h>
43 f8a36e22 2021-08-26 stsp #include <imsg.h>
44 f8a36e22 2021-08-26 stsp #include <time.h>
45 f8a36e22 2021-08-26 stsp #include <uuid.h>
46 f8a36e22 2021-08-26 stsp
47 f8a36e22 2021-08-26 stsp #include "got_error.h"
48 f8a36e22 2021-08-26 stsp #include "got_reference.h"
49 f8a36e22 2021-08-26 stsp #include "got_repository.h"
50 f8a36e22 2021-08-26 stsp #include "got_path.h"
51 f8a36e22 2021-08-26 stsp #include "got_cancel.h"
52 f8a36e22 2021-08-26 stsp #include "got_worktree.h"
53 f8a36e22 2021-08-26 stsp #include "got_object.h"
54 f8a36e22 2021-08-26 stsp #include "got_opentemp.h"
55 f8a36e22 2021-08-26 stsp #include "got_send.h"
56 f8a36e22 2021-08-26 stsp #include "got_repository_admin.h"
57 f8a36e22 2021-08-26 stsp #include "got_commit_graph.h"
58 f8a36e22 2021-08-26 stsp
59 f8a36e22 2021-08-26 stsp #include "got_lib_delta.h"
60 f8a36e22 2021-08-26 stsp #include "got_lib_inflate.h"
61 f8a36e22 2021-08-26 stsp #include "got_lib_object.h"
62 f8a36e22 2021-08-26 stsp #include "got_lib_object_parse.h"
63 f8a36e22 2021-08-26 stsp #include "got_lib_object_create.h"
64 f8a36e22 2021-08-26 stsp #include "got_lib_pack.h"
65 53bf0b54 2023-02-23 op #include "got_lib_hash.h"
66 f8a36e22 2021-08-26 stsp #include "got_lib_privsep.h"
67 f8a36e22 2021-08-26 stsp #include "got_lib_object_cache.h"
68 f8a36e22 2021-08-26 stsp #include "got_lib_repository.h"
69 cae60ab8 2022-10-18 stsp #include "got_lib_ratelimit.h"
70 f8a36e22 2021-08-26 stsp #include "got_lib_pack_create.h"
71 d65a88a2 2021-09-05 stsp #include "got_lib_dial.h"
72 3d1b16d1 2023-07-08 jrick #include "got_lib_worktree_cvg.h"
73 01d78e9e 2023-09-15 stsp #include "got_lib_poll.h"
74 f8a36e22 2021-08-26 stsp
75 f8a36e22 2021-08-26 stsp #ifndef nitems
76 f8a36e22 2021-08-26 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
77 f8a36e22 2021-08-26 stsp #endif
78 f8a36e22 2021-08-26 stsp
79 f8a36e22 2021-08-26 stsp #ifndef ssizeof
80 f8a36e22 2021-08-26 stsp #define ssizeof(_x) ((ssize_t)(sizeof(_x)))
81 f8a36e22 2021-08-26 stsp #endif
82 f8a36e22 2021-08-26 stsp
83 f8a36e22 2021-08-26 stsp #ifndef MIN
84 f8a36e22 2021-08-26 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
85 f8a36e22 2021-08-26 stsp #endif
86 f8a36e22 2021-08-26 stsp
87 f8a36e22 2021-08-26 stsp const struct got_error *
88 f8a36e22 2021-08-26 stsp got_send_connect(pid_t *sendpid, int *sendfd, const char *proto,
89 f8a36e22 2021-08-26 stsp const char *host, const char *port, const char *server_path, int verbosity)
90 f8a36e22 2021-08-26 stsp {
91 f8a36e22 2021-08-26 stsp const struct got_error *err = NULL;
92 f8a36e22 2021-08-26 stsp
93 f8a36e22 2021-08-26 stsp *sendpid = -1;
94 f8a36e22 2021-08-26 stsp *sendfd = -1;
95 f8a36e22 2021-08-26 stsp
96 f8a36e22 2021-08-26 stsp if (strcmp(proto, "ssh") == 0 || strcmp(proto, "git+ssh") == 0)
97 d65a88a2 2021-09-05 stsp err = got_dial_ssh(sendpid, sendfd, host, port, server_path,
98 1eb38992 2023-04-14 stsp GOT_DIAL_CMD_SEND, verbosity);
99 f8a36e22 2021-08-26 stsp else if (strcmp(proto, "git") == 0)
100 d65a88a2 2021-09-05 stsp err = got_dial_git(sendfd, host, port, server_path,
101 1eb38992 2023-04-14 stsp GOT_DIAL_CMD_SEND);
102 f8a36e22 2021-08-26 stsp else if (strcmp(proto, "http") == 0 || strcmp(proto, "git+http") == 0)
103 f8a36e22 2021-08-26 stsp err = got_error_path(proto, GOT_ERR_NOT_IMPL);
104 f8a36e22 2021-08-26 stsp else
105 f8a36e22 2021-08-26 stsp err = got_error_path(proto, GOT_ERR_BAD_PROTO);
106 f8a36e22 2021-08-26 stsp return err;
107 f8a36e22 2021-08-26 stsp }
108 f8a36e22 2021-08-26 stsp
109 f8a36e22 2021-08-26 stsp struct pack_progress_arg {
110 f8a36e22 2021-08-26 stsp got_send_progress_cb progress_cb;
111 f8a36e22 2021-08-26 stsp void *progress_arg;
112 01d78e9e 2023-09-15 stsp int sendfd;
113 f8a36e22 2021-08-26 stsp
114 b8af7c06 2022-03-15 stsp int ncolored;
115 b8af7c06 2022-03-15 stsp int nfound;
116 b8af7c06 2022-03-15 stsp int ntrees;
117 f8a36e22 2021-08-26 stsp off_t packfile_size;
118 f8a36e22 2021-08-26 stsp int ncommits;
119 f8a36e22 2021-08-26 stsp int nobj_total;
120 f8a36e22 2021-08-26 stsp int nobj_deltify;
121 f8a36e22 2021-08-26 stsp int nobj_written;
122 f8a36e22 2021-08-26 stsp };
123 f8a36e22 2021-08-26 stsp
124 f8a36e22 2021-08-26 stsp static const struct got_error *
125 b8af7c06 2022-03-15 stsp pack_progress(void *arg, int ncolored, int nfound, int ntrees,
126 b8af7c06 2022-03-15 stsp off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
127 b8af7c06 2022-03-15 stsp int nobj_written)
128 f8a36e22 2021-08-26 stsp {
129 f8a36e22 2021-08-26 stsp const struct got_error *err;
130 f8a36e22 2021-08-26 stsp struct pack_progress_arg *a = arg;
131 f8a36e22 2021-08-26 stsp
132 b8af7c06 2022-03-15 stsp err = a->progress_cb(a->progress_arg, ncolored, nfound, ntrees,
133 b8af7c06 2022-03-15 stsp packfile_size, ncommits, nobj_total, nobj_deltify,
134 6242c45b 2022-11-14 op nobj_written, 0, NULL, NULL, 0);
135 f8a36e22 2021-08-26 stsp if (err)
136 f8a36e22 2021-08-26 stsp return err;
137 f8a36e22 2021-08-26 stsp
138 01d78e9e 2023-09-15 stsp /*
139 01d78e9e 2023-09-15 stsp * Detect the server closing our connection while we are
140 01d78e9e 2023-09-15 stsp * busy creating a pack file.
141 01d78e9e 2023-09-15 stsp *
142 01d78e9e 2023-09-15 stsp * XXX This should be a temporary workaround. A better fix would
143 01d78e9e 2023-09-15 stsp * be to avoid use of an on-disk tempfile for pack file data.
144 01d78e9e 2023-09-15 stsp * Instead we could stream pack file data to got-send-pack while
145 01d78e9e 2023-09-15 stsp * the pack file is being generated. Write errors in got-send-pack
146 01d78e9e 2023-09-15 stsp * would then automatically abort the creation of pack file data.
147 01d78e9e 2023-09-15 stsp */
148 01d78e9e 2023-09-15 stsp err = got_poll_fd(a->sendfd, 0, 0);
149 01d78e9e 2023-09-15 stsp if (err && err->code != GOT_ERR_TIMEOUT) {
150 01d78e9e 2023-09-15 stsp if (err->code == GOT_ERR_EOF) {
151 01d78e9e 2023-09-15 stsp err = got_error_msg(GOT_ERR_EOF,
152 01d78e9e 2023-09-15 stsp "server unexpectedly closed the connection");
153 01d78e9e 2023-09-15 stsp }
154 01d78e9e 2023-09-15 stsp return err;
155 01d78e9e 2023-09-15 stsp }
156 01d78e9e 2023-09-15 stsp
157 b8af7c06 2022-03-15 stsp a->ncolored= ncolored;
158 b8af7c06 2022-03-15 stsp a->nfound = nfound;
159 b8af7c06 2022-03-15 stsp a->ntrees = ntrees;
160 f8a36e22 2021-08-26 stsp a->packfile_size = packfile_size;
161 f8a36e22 2021-08-26 stsp a->ncommits = ncommits;
162 f8a36e22 2021-08-26 stsp a->nobj_total = nobj_total;
163 f8a36e22 2021-08-26 stsp a->nobj_deltify = nobj_deltify;
164 f8a36e22 2021-08-26 stsp a->nobj_written = nobj_written;
165 f8a36e22 2021-08-26 stsp return NULL;
166 f8a36e22 2021-08-26 stsp }
167 f8a36e22 2021-08-26 stsp
168 f8a36e22 2021-08-26 stsp static const struct got_error *
169 27555e8e 2023-07-05 jrick insert_sendable_ref(struct got_pathlist_head *refs, const char *refname,
170 3d1b16d1 2023-07-08 jrick const char *target_refname, struct got_repository *repo)
171 f8a36e22 2021-08-26 stsp {
172 f8a36e22 2021-08-26 stsp const struct got_error *err;
173 f8a36e22 2021-08-26 stsp struct got_reference *ref;
174 27555e8e 2023-07-05 jrick struct got_object_id *id = NULL;
175 27555e8e 2023-07-05 jrick int obj_type;
176 f8a36e22 2021-08-26 stsp
177 f8a36e22 2021-08-26 stsp err = got_ref_open(&ref, repo, refname, 0);
178 f8a36e22 2021-08-26 stsp if (err)
179 f8a36e22 2021-08-26 stsp return err;
180 f8a36e22 2021-08-26 stsp
181 27555e8e 2023-07-05 jrick if (got_ref_is_symbolic(ref)) {
182 27555e8e 2023-07-05 jrick err = got_error_fmt(GOT_ERR_BAD_REF_TYPE,
183 27555e8e 2023-07-05 jrick "cannot send symbolic reference %s", refname);
184 27555e8e 2023-07-05 jrick goto done;
185 27555e8e 2023-07-05 jrick }
186 f8a36e22 2021-08-26 stsp
187 27555e8e 2023-07-05 jrick err = got_ref_resolve(&id, repo, ref);
188 27555e8e 2023-07-05 jrick if (err)
189 27555e8e 2023-07-05 jrick goto done;
190 27555e8e 2023-07-05 jrick err = got_object_get_type(&obj_type, repo, id);
191 27555e8e 2023-07-05 jrick if (err)
192 27555e8e 2023-07-05 jrick goto done;
193 27555e8e 2023-07-05 jrick switch (obj_type) {
194 27555e8e 2023-07-05 jrick case GOT_OBJ_TYPE_COMMIT:
195 27555e8e 2023-07-05 jrick case GOT_OBJ_TYPE_TAG:
196 27555e8e 2023-07-05 jrick break;
197 27555e8e 2023-07-05 jrick default:
198 27555e8e 2023-07-05 jrick err = got_error_fmt(GOT_ERR_OBJ_TYPE," cannot send %s",
199 27555e8e 2023-07-05 jrick refname);
200 27555e8e 2023-07-05 jrick goto done;
201 27555e8e 2023-07-05 jrick }
202 27555e8e 2023-07-05 jrick
203 3d1b16d1 2023-07-08 jrick err = got_pathlist_insert(NULL, refs, target_refname, id);
204 27555e8e 2023-07-05 jrick done:
205 bafea5ba 2023-07-07 jrick if (ref)
206 bafea5ba 2023-07-07 jrick got_ref_close(ref);
207 bafea5ba 2023-07-07 jrick if (err)
208 27555e8e 2023-07-05 jrick free(id);
209 f8a36e22 2021-08-26 stsp return err;
210 f8a36e22 2021-08-26 stsp }
211 f8a36e22 2021-08-26 stsp
212 f8a36e22 2021-08-26 stsp static const struct got_error *
213 e166551f 2023-04-17 stsp check_common_ancestry(const char *refname, struct got_object_id *my_id,
214 f8a36e22 2021-08-26 stsp struct got_object_id *their_id, struct got_repository *repo,
215 f8a36e22 2021-08-26 stsp got_cancel_cb cancel_cb, void *cancel_arg)
216 f8a36e22 2021-08-26 stsp {
217 f8a36e22 2021-08-26 stsp const struct got_error *err = NULL;
218 f8a36e22 2021-08-26 stsp struct got_object_id *yca_id;
219 f8a36e22 2021-08-26 stsp int obj_type;
220 f8a36e22 2021-08-26 stsp
221 f8a36e22 2021-08-26 stsp err = got_object_get_type(&obj_type, repo, their_id);
222 f8a36e22 2021-08-26 stsp if (err)
223 f8a36e22 2021-08-26 stsp return err;
224 f8a36e22 2021-08-26 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
225 f8a36e22 2021-08-26 stsp return got_error_fmt(GOT_ERR_OBJ_TYPE,
226 f8a36e22 2021-08-26 stsp "bad object type on server for %s", refname);
227 f8a36e22 2021-08-26 stsp
228 f8a36e22 2021-08-26 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
229 e12cc036 2024-03-27 stsp my_id, their_id, 0, 0, repo, cancel_cb, cancel_arg);
230 f8a36e22 2021-08-26 stsp if (err)
231 f8a36e22 2021-08-26 stsp return err;
232 f8a36e22 2021-08-26 stsp if (yca_id == NULL)
233 f8a36e22 2021-08-26 stsp return got_error_fmt(GOT_ERR_SEND_ANCESTRY, "%s", refname);
234 f8a36e22 2021-08-26 stsp
235 f8a36e22 2021-08-26 stsp if (got_object_id_cmp(their_id, yca_id) != 0)
236 f8a36e22 2021-08-26 stsp err = got_error_fmt(GOT_ERR_SEND_ANCESTRY, "%s", refname);
237 f8a36e22 2021-08-26 stsp
238 f8a36e22 2021-08-26 stsp free(yca_id);
239 f8a36e22 2021-08-26 stsp return err;
240 f8a36e22 2021-08-26 stsp }
241 f8a36e22 2021-08-26 stsp
242 f8a36e22 2021-08-26 stsp static const struct got_error *
243 f8a36e22 2021-08-26 stsp realloc_ids(struct got_object_id ***ids, size_t *nalloc, size_t n)
244 f8a36e22 2021-08-26 stsp {
245 f8a36e22 2021-08-26 stsp struct got_object_id **new;
246 f8a36e22 2021-08-26 stsp const size_t alloc_chunksz = 256;
247 f8a36e22 2021-08-26 stsp
248 535e07c7 2021-08-29 naddy if (*nalloc >= n)
249 f8a36e22 2021-08-26 stsp return NULL;
250 f8a36e22 2021-08-26 stsp
251 f8a36e22 2021-08-26 stsp new = recallocarray(*ids, *nalloc, *nalloc + alloc_chunksz,
252 f8a36e22 2021-08-26 stsp sizeof(struct got_object_id));
253 f8a36e22 2021-08-26 stsp if (new == NULL)
254 f8a36e22 2021-08-26 stsp return got_error_from_errno("recallocarray");
255 f8a36e22 2021-08-26 stsp
256 f8a36e22 2021-08-26 stsp *ids = new;
257 f8a36e22 2021-08-26 stsp *nalloc += alloc_chunksz;
258 f8a36e22 2021-08-26 stsp return NULL;
259 f8a36e22 2021-08-26 stsp }
260 f8a36e22 2021-08-26 stsp
261 f8a36e22 2021-08-26 stsp static struct got_pathlist_entry *
262 27555e8e 2023-07-05 jrick find_ref(struct got_pathlist_head *refs, const char *refname)
263 f8a36e22 2021-08-26 stsp {
264 f8a36e22 2021-08-26 stsp struct got_pathlist_entry *pe;
265 f8a36e22 2021-08-26 stsp
266 27555e8e 2023-07-05 jrick TAILQ_FOREACH(pe, refs, entry) {
267 27555e8e 2023-07-05 jrick if (got_path_cmp(pe->path, refname, strlen(pe->path),
268 27555e8e 2023-07-05 jrick strlen(refname)) == 0) {
269 f8a36e22 2021-08-26 stsp return pe;
270 f8a36e22 2021-08-26 stsp }
271 f8a36e22 2021-08-26 stsp }
272 f8a36e22 2021-08-26 stsp
273 f8a36e22 2021-08-26 stsp return NULL;
274 f8a36e22 2021-08-26 stsp }
275 f8a36e22 2021-08-26 stsp
276 f8a36e22 2021-08-26 stsp static const struct got_error *
277 f8a36e22 2021-08-26 stsp get_remote_refname(char **remote_refname, const char *remote_name,
278 f8a36e22 2021-08-26 stsp const char *refname)
279 f8a36e22 2021-08-26 stsp {
280 f8a36e22 2021-08-26 stsp if (strncmp(refname, "refs/", 5) == 0)
281 f8a36e22 2021-08-26 stsp refname += 5;
282 f8a36e22 2021-08-26 stsp if (strncmp(refname, "heads/", 6) == 0)
283 f8a36e22 2021-08-26 stsp refname += 6;
284 f8a36e22 2021-08-26 stsp
285 f8a36e22 2021-08-26 stsp if (asprintf(remote_refname, "refs/remotes/%s/%s",
286 f8a36e22 2021-08-26 stsp remote_name, refname) == -1)
287 f8a36e22 2021-08-26 stsp return got_error_from_errno("asprintf");
288 f8a36e22 2021-08-26 stsp
289 f8a36e22 2021-08-26 stsp return NULL;
290 f8a36e22 2021-08-26 stsp }
291 f8a36e22 2021-08-26 stsp
292 f8a36e22 2021-08-26 stsp static const struct got_error *
293 27555e8e 2023-07-05 jrick update_remote_ref(struct got_pathlist_entry *my_ref, const char *remote_name,
294 f8a36e22 2021-08-26 stsp struct got_repository *repo)
295 f8a36e22 2021-08-26 stsp {
296 f8a36e22 2021-08-26 stsp const struct got_error *err, *unlock_err;
297 27555e8e 2023-07-05 jrick const char *refname = my_ref->path;
298 27555e8e 2023-07-05 jrick struct got_object_id *my_id = my_ref->data;
299 f8a36e22 2021-08-26 stsp struct got_reference *ref = NULL;
300 f8a36e22 2021-08-26 stsp char *remote_refname = NULL;
301 f8a36e22 2021-08-26 stsp int ref_locked = 0;
302 f8a36e22 2021-08-26 stsp
303 27555e8e 2023-07-05 jrick err = get_remote_refname(&remote_refname, remote_name, refname);
304 f8a36e22 2021-08-26 stsp if (err)
305 f8a36e22 2021-08-26 stsp goto done;
306 f8a36e22 2021-08-26 stsp
307 f8a36e22 2021-08-26 stsp err = got_ref_open(&ref, repo, remote_refname, 1 /* lock */);
308 f8a36e22 2021-08-26 stsp if (err) {
309 f8a36e22 2021-08-26 stsp if (err->code != GOT_ERR_NOT_REF)
310 f8a36e22 2021-08-26 stsp goto done;
311 f8a36e22 2021-08-26 stsp err = got_ref_alloc(&ref, remote_refname, my_id);
312 f8a36e22 2021-08-26 stsp if (err)
313 f8a36e22 2021-08-26 stsp goto done;
314 f8a36e22 2021-08-26 stsp } else {
315 f8a36e22 2021-08-26 stsp ref_locked = 1;
316 f8a36e22 2021-08-26 stsp err = got_ref_change_ref(ref, my_id);
317 f8a36e22 2021-08-26 stsp if (err)
318 f8a36e22 2021-08-26 stsp goto done;
319 f8a36e22 2021-08-26 stsp }
320 f8a36e22 2021-08-26 stsp
321 f8a36e22 2021-08-26 stsp err = got_ref_write(ref, repo);
322 f8a36e22 2021-08-26 stsp done:
323 f8a36e22 2021-08-26 stsp if (ref) {
324 f8a36e22 2021-08-26 stsp if (ref_locked) {
325 f8a36e22 2021-08-26 stsp unlock_err = got_ref_unlock(ref);
326 f8a36e22 2021-08-26 stsp if (unlock_err && err == NULL)
327 f8a36e22 2021-08-26 stsp err = unlock_err;
328 f8a36e22 2021-08-26 stsp }
329 f8a36e22 2021-08-26 stsp got_ref_close(ref);
330 f8a36e22 2021-08-26 stsp }
331 f8a36e22 2021-08-26 stsp free(remote_refname);
332 f8a36e22 2021-08-26 stsp return err;
333 f8a36e22 2021-08-26 stsp }
334 f8a36e22 2021-08-26 stsp
335 f8a36e22 2021-08-26 stsp const struct got_error*
336 f8a36e22 2021-08-26 stsp got_send_pack(const char *remote_name, struct got_pathlist_head *branch_names,
337 f8a36e22 2021-08-26 stsp struct got_pathlist_head *tag_names,
338 f8a36e22 2021-08-26 stsp struct got_pathlist_head *delete_branches,
339 f8a36e22 2021-08-26 stsp int verbosity, int overwrite_refs, int sendfd,
340 f8a36e22 2021-08-26 stsp struct got_repository *repo, got_send_progress_cb progress_cb,
341 f8a36e22 2021-08-26 stsp void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
342 f8a36e22 2021-08-26 stsp {
343 f8a36e22 2021-08-26 stsp int imsg_sendfds[2];
344 f8a36e22 2021-08-26 stsp int npackfd = -1, nsendfd = -1;
345 f8a36e22 2021-08-26 stsp int sendstatus, done = 0;
346 f8a36e22 2021-08-26 stsp const struct got_error *err;
347 f8a36e22 2021-08-26 stsp struct imsgbuf sendibuf;
348 f8a36e22 2021-08-26 stsp pid_t sendpid = -1;
349 f8a36e22 2021-08-26 stsp struct got_pathlist_head have_refs;
350 f8a36e22 2021-08-26 stsp struct got_pathlist_head their_refs;
351 f8a36e22 2021-08-26 stsp struct got_pathlist_entry *pe;
352 f8a36e22 2021-08-26 stsp struct got_object_id **our_ids = NULL;
353 f8a36e22 2021-08-26 stsp struct got_object_id **their_ids = NULL;
354 27555e8e 2023-07-05 jrick int nours = 0, ntheirs = 0;
355 f8a36e22 2021-08-26 stsp size_t nalloc_ours = 0, nalloc_theirs = 0;
356 27b75514 2021-08-28 stsp int refs_to_send = 0, refs_to_delete = 0;
357 c46eda88 2022-07-21 florian off_t bytes_sent = 0, bytes_sent_cur = 0;
358 f8a36e22 2021-08-26 stsp struct pack_progress_arg ppa;
359 f8a36e22 2021-08-26 stsp uint8_t packsha1[SHA1_DIGEST_LENGTH];
360 894e4711 2022-10-15 stsp int packfd = -1;
361 a32780aa 2022-10-15 stsp FILE *delta_cache = NULL;
362 27555e8e 2023-07-05 jrick char *s = NULL;
363 f8a36e22 2021-08-26 stsp
364 f8a36e22 2021-08-26 stsp TAILQ_INIT(&have_refs);
365 f8a36e22 2021-08-26 stsp TAILQ_INIT(&their_refs);
366 f8a36e22 2021-08-26 stsp
367 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(pe, branch_names, entry) {
368 f8a36e22 2021-08-26 stsp const char *branchname = pe->path;
369 3d1b16d1 2023-07-08 jrick const char *targetname = pe->data;
370 3d1b16d1 2023-07-08 jrick
371 3d1b16d1 2023-07-08 jrick if (targetname == NULL)
372 3d1b16d1 2023-07-08 jrick targetname = branchname;
373 3d1b16d1 2023-07-08 jrick
374 3d1b16d1 2023-07-08 jrick if (strncmp(targetname, "refs/heads/", 11) != 0) {
375 3d1b16d1 2023-07-08 jrick if (asprintf(&s, "refs/heads/%s", targetname) == -1) {
376 f8a36e22 2021-08-26 stsp err = got_error_from_errno("asprintf");
377 f8a36e22 2021-08-26 stsp goto done;
378 f8a36e22 2021-08-26 stsp }
379 f8a36e22 2021-08-26 stsp } else {
380 3d1b16d1 2023-07-08 jrick if ((s = strdup(targetname)) == NULL) {
381 27555e8e 2023-07-05 jrick err = got_error_from_errno("strdup");
382 27555e8e 2023-07-05 jrick goto done;
383 27555e8e 2023-07-05 jrick }
384 f8a36e22 2021-08-26 stsp }
385 3d1b16d1 2023-07-08 jrick err = insert_sendable_ref(&have_refs, branchname, s, repo);
386 f8a36e22 2021-08-26 stsp if (err)
387 f8a36e22 2021-08-26 stsp goto done;
388 27555e8e 2023-07-05 jrick s = NULL;
389 f8a36e22 2021-08-26 stsp }
390 f8a36e22 2021-08-26 stsp
391 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(pe, delete_branches, entry) {
392 f8a36e22 2021-08-26 stsp const char *branchname = pe->path;
393 27555e8e 2023-07-05 jrick struct got_pathlist_entry *ref;
394 f8a36e22 2021-08-26 stsp if (strncmp(branchname, "refs/heads/", 11) != 0) {
395 f8a36e22 2021-08-26 stsp err = got_error_fmt(GOT_ERR_SEND_DELETE_REF, "%s",
396 f8a36e22 2021-08-26 stsp branchname);
397 f8a36e22 2021-08-26 stsp goto done;
398 f8a36e22 2021-08-26 stsp }
399 27555e8e 2023-07-05 jrick ref = find_ref(&have_refs, branchname);
400 f8a36e22 2021-08-26 stsp if (ref) {
401 f8a36e22 2021-08-26 stsp err = got_error_fmt(GOT_ERR_SEND_DELETE_REF,
402 f8a36e22 2021-08-26 stsp "changes on %s will be sent to server",
403 f8a36e22 2021-08-26 stsp branchname);
404 f8a36e22 2021-08-26 stsp goto done;
405 f8a36e22 2021-08-26 stsp }
406 f8a36e22 2021-08-26 stsp }
407 f8a36e22 2021-08-26 stsp
408 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(pe, tag_names, entry) {
409 f8a36e22 2021-08-26 stsp const char *tagname = pe->path;
410 f8a36e22 2021-08-26 stsp if (strncmp(tagname, "refs/tags/", 10) != 0) {
411 f8a36e22 2021-08-26 stsp if (asprintf(&s, "refs/tags/%s", tagname) == -1) {
412 f8a36e22 2021-08-26 stsp err = got_error_from_errno("asprintf");
413 f8a36e22 2021-08-26 stsp goto done;
414 f8a36e22 2021-08-26 stsp }
415 f8a36e22 2021-08-26 stsp } else {
416 27555e8e 2023-07-05 jrick if ((s = strdup(pe->path)) == NULL) {
417 27555e8e 2023-07-05 jrick err = got_error_from_errno("strdup");
418 27555e8e 2023-07-05 jrick goto done;
419 27555e8e 2023-07-05 jrick }
420 f8a36e22 2021-08-26 stsp }
421 3d1b16d1 2023-07-08 jrick err = insert_sendable_ref(&have_refs, s, s, repo);
422 f8a36e22 2021-08-26 stsp if (err)
423 f8a36e22 2021-08-26 stsp goto done;
424 27555e8e 2023-07-05 jrick s = NULL;
425 f8a36e22 2021-08-26 stsp }
426 f8a36e22 2021-08-26 stsp
427 27555e8e 2023-07-05 jrick if (TAILQ_EMPTY(&have_refs) && TAILQ_EMPTY(delete_branches)) {
428 f8a36e22 2021-08-26 stsp err = got_error(GOT_ERR_SEND_EMPTY);
429 f8a36e22 2021-08-26 stsp goto done;
430 f8a36e22 2021-08-26 stsp }
431 f8a36e22 2021-08-26 stsp
432 894e4711 2022-10-15 stsp packfd = got_opentempfd();
433 894e4711 2022-10-15 stsp if (packfd == -1) {
434 894e4711 2022-10-15 stsp err = got_error_from_errno("got_opentempfd");
435 f8a36e22 2021-08-26 stsp goto done;
436 f8a36e22 2021-08-26 stsp }
437 f8a36e22 2021-08-26 stsp
438 a32780aa 2022-10-15 stsp delta_cache = got_opentemp();
439 a32780aa 2022-10-15 stsp if (delta_cache == NULL) {
440 a32780aa 2022-10-15 stsp err = got_error_from_errno("got_opentemp");
441 a32780aa 2022-10-15 stsp goto done;
442 a32780aa 2022-10-15 stsp }
443 a32780aa 2022-10-15 stsp
444 f8a36e22 2021-08-26 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_sendfds) == -1) {
445 f8a36e22 2021-08-26 stsp err = got_error_from_errno("socketpair");
446 f8a36e22 2021-08-26 stsp goto done;
447 f8a36e22 2021-08-26 stsp }
448 f8a36e22 2021-08-26 stsp
449 f8a36e22 2021-08-26 stsp sendpid = fork();
450 f8a36e22 2021-08-26 stsp if (sendpid == -1) {
451 f8a36e22 2021-08-26 stsp err = got_error_from_errno("fork");
452 f8a36e22 2021-08-26 stsp goto done;
453 f8a36e22 2021-08-26 stsp } else if (sendpid == 0){
454 f8a36e22 2021-08-26 stsp got_privsep_exec_child(imsg_sendfds,
455 f8a36e22 2021-08-26 stsp GOT_PATH_PROG_SEND_PACK, got_repo_get_path(repo));
456 f8a36e22 2021-08-26 stsp }
457 f8a36e22 2021-08-26 stsp
458 f8a36e22 2021-08-26 stsp if (close(imsg_sendfds[1]) == -1) {
459 f8a36e22 2021-08-26 stsp err = got_error_from_errno("close");
460 f8a36e22 2021-08-26 stsp goto done;
461 f8a36e22 2021-08-26 stsp }
462 f8a36e22 2021-08-26 stsp imsg_init(&sendibuf, imsg_sendfds[0]);
463 f8a36e22 2021-08-26 stsp nsendfd = dup(sendfd);
464 f8a36e22 2021-08-26 stsp if (nsendfd == -1) {
465 f8a36e22 2021-08-26 stsp err = got_error_from_errno("dup");
466 f8a36e22 2021-08-26 stsp goto done;
467 f8a36e22 2021-08-26 stsp }
468 f8a36e22 2021-08-26 stsp
469 f8a36e22 2021-08-26 stsp /*
470 bafea5ba 2023-07-07 jrick * Prepare the array of our object IDs which
471 27555e8e 2023-07-05 jrick * will be needed for generating a pack file.
472 f8a36e22 2021-08-26 stsp */
473 27555e8e 2023-07-05 jrick TAILQ_FOREACH(pe, &have_refs, entry) {
474 27555e8e 2023-07-05 jrick struct got_object_id *id = pe->data;
475 27555e8e 2023-07-05 jrick
476 535e07c7 2021-08-29 naddy err = realloc_ids(&our_ids, &nalloc_ours, nours + 1);
477 f8a36e22 2021-08-26 stsp if (err)
478 f8a36e22 2021-08-26 stsp goto done;
479 f8a36e22 2021-08-26 stsp our_ids[nours] = id;
480 f8a36e22 2021-08-26 stsp nours++;
481 f8a36e22 2021-08-26 stsp }
482 f8a36e22 2021-08-26 stsp
483 f8a36e22 2021-08-26 stsp err = got_privsep_send_send_req(&sendibuf, nsendfd, &have_refs,
484 f8a36e22 2021-08-26 stsp delete_branches, verbosity);
485 f8a36e22 2021-08-26 stsp if (err)
486 f8a36e22 2021-08-26 stsp goto done;
487 f8a36e22 2021-08-26 stsp nsendfd = -1;
488 f8a36e22 2021-08-26 stsp
489 f8a36e22 2021-08-26 stsp err = got_privsep_recv_send_remote_refs(&their_refs, &sendibuf);
490 f8a36e22 2021-08-26 stsp if (err)
491 f8a36e22 2021-08-26 stsp goto done;
492 f8a36e22 2021-08-26 stsp /*
493 f8a36e22 2021-08-26 stsp * Process references reported by the server.
494 f8a36e22 2021-08-26 stsp * Push appropriate object IDs onto the "their IDs" array.
495 f8a36e22 2021-08-26 stsp * This array will be used to exclude objects which already
496 f8a36e22 2021-08-26 stsp * exist on the server from our pack file.
497 f8a36e22 2021-08-26 stsp */
498 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(pe, &their_refs, entry) {
499 f8a36e22 2021-08-26 stsp const char *refname = pe->path;
500 f8a36e22 2021-08-26 stsp struct got_object_id *their_id = pe->data;
501 f8a36e22 2021-08-26 stsp int have_their_id;
502 f8a36e22 2021-08-26 stsp struct got_object *obj;
503 27555e8e 2023-07-05 jrick struct got_pathlist_entry *my_ref = NULL;
504 f8a36e22 2021-08-26 stsp int is_tag = 0;
505 f8a36e22 2021-08-26 stsp
506 f8a36e22 2021-08-26 stsp /* Don't blindly trust the server to send us valid names. */
507 f8a36e22 2021-08-26 stsp if (!got_ref_name_is_valid(refname))
508 f8a36e22 2021-08-26 stsp continue;
509 f8a36e22 2021-08-26 stsp
510 92410079 2021-10-16 stsp if (strncmp(refname, "refs/tags/", 10) == 0)
511 92410079 2021-10-16 stsp is_tag = 1;
512 f8a36e22 2021-08-26 stsp /*
513 f8a36e22 2021-08-26 stsp * Find out whether this is a reference we want to upload.
514 f8a36e22 2021-08-26 stsp * Otherwise we can still use this reference as a hint to
515 f8a36e22 2021-08-26 stsp * avoid uploading any objects the server already has.
516 f8a36e22 2021-08-26 stsp */
517 27555e8e 2023-07-05 jrick my_ref = find_ref(&have_refs, refname);
518 f8a36e22 2021-08-26 stsp if (my_ref) {
519 27555e8e 2023-07-05 jrick struct got_object_id *my_id = my_ref->data;
520 92410079 2021-10-16 stsp if (got_object_id_cmp(my_id, their_id) != 0) {
521 92410079 2021-10-16 stsp if (!overwrite_refs && is_tag) {
522 92410079 2021-10-16 stsp err = got_error_fmt(
523 92410079 2021-10-16 stsp GOT_ERR_SEND_TAG_EXISTS,
524 92410079 2021-10-16 stsp "%s", refname);
525 92410079 2021-10-16 stsp goto done;
526 92410079 2021-10-16 stsp }
527 c2105d00 2021-09-14 stsp refs_to_send++;
528 92410079 2021-10-16 stsp }
529 f8a36e22 2021-08-26 stsp }
530 f8a36e22 2021-08-26 stsp
531 f8a36e22 2021-08-26 stsp /* Check if their object exists locally. */
532 f8a36e22 2021-08-26 stsp err = got_object_open(&obj, repo, their_id);
533 f8a36e22 2021-08-26 stsp if (err) {
534 f8a36e22 2021-08-26 stsp if (err->code != GOT_ERR_NO_OBJ)
535 f8a36e22 2021-08-26 stsp goto done;
536 f8a36e22 2021-08-26 stsp if (!overwrite_refs && my_ref != NULL) {
537 f8a36e22 2021-08-26 stsp err = got_error_fmt(GOT_ERR_SEND_ANCESTRY,
538 f8a36e22 2021-08-26 stsp "%s", refname);
539 f8a36e22 2021-08-26 stsp goto done;
540 f8a36e22 2021-08-26 stsp }
541 f8a36e22 2021-08-26 stsp have_their_id = 0;
542 f8a36e22 2021-08-26 stsp } else {
543 f8a36e22 2021-08-26 stsp got_object_close(obj);
544 f8a36e22 2021-08-26 stsp have_their_id = 1;
545 f8a36e22 2021-08-26 stsp }
546 f8a36e22 2021-08-26 stsp
547 535e07c7 2021-08-29 naddy err = realloc_ids(&their_ids, &nalloc_theirs, ntheirs + 1);
548 f8a36e22 2021-08-26 stsp if (err)
549 f8a36e22 2021-08-26 stsp goto done;
550 f8a36e22 2021-08-26 stsp
551 f8a36e22 2021-08-26 stsp if (have_their_id) {
552 f8a36e22 2021-08-26 stsp /* Enforce linear ancestry if required. */
553 f8a36e22 2021-08-26 stsp if (!overwrite_refs && my_ref && !is_tag) {
554 27555e8e 2023-07-05 jrick struct got_object_id *my_id = my_ref->data;
555 e166551f 2023-04-17 stsp err = check_common_ancestry(refname, my_id,
556 f8a36e22 2021-08-26 stsp their_id, repo, cancel_cb, cancel_arg);
557 f8a36e22 2021-08-26 stsp if (err)
558 f8a36e22 2021-08-26 stsp goto done;
559 f8a36e22 2021-08-26 stsp }
560 f8a36e22 2021-08-26 stsp /* Exclude any objects reachable via their ID. */
561 27555e8e 2023-07-05 jrick their_ids[ntheirs] = their_id;
562 f8a36e22 2021-08-26 stsp ntheirs++;
563 f8a36e22 2021-08-26 stsp } else if (!is_tag) {
564 f8a36e22 2021-08-26 stsp char *remote_refname;
565 f8a36e22 2021-08-26 stsp struct got_reference *ref;
566 f8a36e22 2021-08-26 stsp /*
567 f8a36e22 2021-08-26 stsp * Exclude any objects which exist on the server
568 f8a36e22 2021-08-26 stsp * according to a locally cached remote reference.
569 f8a36e22 2021-08-26 stsp */
570 f8a36e22 2021-08-26 stsp err = get_remote_refname(&remote_refname,
571 f8a36e22 2021-08-26 stsp remote_name, refname);
572 f8a36e22 2021-08-26 stsp if (err)
573 f8a36e22 2021-08-26 stsp goto done;
574 f8a36e22 2021-08-26 stsp err = got_ref_open(&ref, repo, remote_refname, 0);
575 f8a36e22 2021-08-26 stsp free(remote_refname);
576 f8a36e22 2021-08-26 stsp if (err) {
577 f8a36e22 2021-08-26 stsp if (err->code != GOT_ERR_NOT_REF)
578 f8a36e22 2021-08-26 stsp goto done;
579 f8a36e22 2021-08-26 stsp } else {
580 f8a36e22 2021-08-26 stsp err = got_ref_resolve(&their_ids[ntheirs],
581 f8a36e22 2021-08-26 stsp repo, ref);
582 f8a36e22 2021-08-26 stsp got_ref_close(ref);
583 f8a36e22 2021-08-26 stsp if (err)
584 f8a36e22 2021-08-26 stsp goto done;
585 f8a36e22 2021-08-26 stsp ntheirs++;
586 f8a36e22 2021-08-26 stsp }
587 f8a36e22 2021-08-26 stsp }
588 f8a36e22 2021-08-26 stsp }
589 f8a36e22 2021-08-26 stsp
590 f8a36e22 2021-08-26 stsp /* Account for any new references we are going to upload. */
591 27555e8e 2023-07-05 jrick TAILQ_FOREACH(pe, &have_refs, entry) {
592 27555e8e 2023-07-05 jrick const char *refname = pe->path;
593 27555e8e 2023-07-05 jrick if (find_ref(&their_refs, refname) == NULL)
594 f8a36e22 2021-08-26 stsp refs_to_send++;
595 f8a36e22 2021-08-26 stsp }
596 f8a36e22 2021-08-26 stsp
597 1bd76734 2021-08-26 stsp /* Account for any existing references we are going to delete. */
598 1bd76734 2021-08-26 stsp TAILQ_FOREACH(pe, delete_branches, entry) {
599 1bd76734 2021-08-26 stsp const char *branchname = pe->path;
600 27555e8e 2023-07-05 jrick if (find_ref(&their_refs, branchname))
601 27b75514 2021-08-28 stsp refs_to_delete++;
602 1bd76734 2021-08-26 stsp }
603 1bd76734 2021-08-26 stsp
604 27b75514 2021-08-28 stsp if (refs_to_send == 0 && refs_to_delete == 0) {
605 f8a36e22 2021-08-26 stsp got_privsep_send_stop(imsg_sendfds[0]);
606 f8a36e22 2021-08-26 stsp goto done;
607 f8a36e22 2021-08-26 stsp }
608 f8a36e22 2021-08-26 stsp
609 27b75514 2021-08-28 stsp if (refs_to_send > 0) {
610 cae60ab8 2022-10-18 stsp struct got_ratelimit rl;
611 cae60ab8 2022-10-18 stsp got_ratelimit_init(&rl, 0, 500);
612 27b75514 2021-08-28 stsp memset(&ppa, 0, sizeof(ppa));
613 27b75514 2021-08-28 stsp ppa.progress_cb = progress_cb;
614 27b75514 2021-08-28 stsp ppa.progress_arg = progress_arg;
615 01d78e9e 2023-09-15 stsp ppa.sendfd = sendfd;
616 a32780aa 2022-10-15 stsp err = got_pack_create(packsha1, packfd, delta_cache,
617 bd56d7e1 2023-02-18 mark their_ids, ntheirs, our_ids, nours, repo, 0, 1, 0,
618 cae60ab8 2022-10-18 stsp pack_progress, &ppa, &rl, cancel_cb, cancel_arg);
619 27b75514 2021-08-28 stsp if (err)
620 27b75514 2021-08-28 stsp goto done;
621 f8a36e22 2021-08-26 stsp
622 894e4711 2022-10-15 stsp npackfd = dup(packfd);
623 27b75514 2021-08-28 stsp if (npackfd == -1) {
624 27b75514 2021-08-28 stsp err = got_error_from_errno("dup");
625 27b75514 2021-08-28 stsp goto done;
626 27b75514 2021-08-28 stsp }
627 27b75514 2021-08-28 stsp err = got_privsep_send_packfd(&sendibuf, npackfd);
628 27b75514 2021-08-28 stsp if (err != NULL)
629 27b75514 2021-08-28 stsp goto done;
630 27b75514 2021-08-28 stsp npackfd = -1;
631 27b75514 2021-08-28 stsp } else {
632 27b75514 2021-08-28 stsp err = got_privsep_send_packfd(&sendibuf, -1);
633 27b75514 2021-08-28 stsp if (err != NULL)
634 27b75514 2021-08-28 stsp goto done;
635 f8a36e22 2021-08-26 stsp }
636 f8a36e22 2021-08-26 stsp
637 f8a36e22 2021-08-26 stsp while (!done) {
638 f8a36e22 2021-08-26 stsp int success = 0;
639 f8a36e22 2021-08-26 stsp char *refname = NULL;
640 6242c45b 2022-11-14 op char *errmsg = NULL;
641 6242c45b 2022-11-14 op
642 f8a36e22 2021-08-26 stsp if (cancel_cb) {
643 f8a36e22 2021-08-26 stsp err = (*cancel_cb)(cancel_arg);
644 f8a36e22 2021-08-26 stsp if (err)
645 f8a36e22 2021-08-26 stsp goto done;
646 f8a36e22 2021-08-26 stsp }
647 f8a36e22 2021-08-26 stsp err = got_privsep_recv_send_progress(&done, &bytes_sent,
648 6242c45b 2022-11-14 op &success, &refname, &errmsg, &sendibuf);
649 f8a36e22 2021-08-26 stsp if (err)
650 f8a36e22 2021-08-26 stsp goto done;
651 f8a36e22 2021-08-26 stsp if (refname && got_ref_name_is_valid(refname) && success &&
652 f8a36e22 2021-08-26 stsp strncmp(refname, "refs/tags/", 10) != 0) {
653 27555e8e 2023-07-05 jrick struct got_pathlist_entry *my_ref;
654 f8a36e22 2021-08-26 stsp /*
655 f8a36e22 2021-08-26 stsp * The server has accepted our changes.
656 f8a36e22 2021-08-26 stsp * Update our reference in refs/remotes/ accordingly.
657 f8a36e22 2021-08-26 stsp */
658 27555e8e 2023-07-05 jrick my_ref = find_ref(&have_refs, refname);
659 f8a36e22 2021-08-26 stsp if (my_ref) {
660 f8a36e22 2021-08-26 stsp err = update_remote_ref(my_ref, remote_name,
661 f8a36e22 2021-08-26 stsp repo);
662 f8a36e22 2021-08-26 stsp if (err)
663 f8a36e22 2021-08-26 stsp goto done;
664 f8a36e22 2021-08-26 stsp }
665 f8a36e22 2021-08-26 stsp }
666 f8a36e22 2021-08-26 stsp if (refname != NULL ||
667 f8a36e22 2021-08-26 stsp bytes_sent_cur != bytes_sent) {
668 b8af7c06 2022-03-15 stsp err = progress_cb(progress_arg, ppa.ncolored,
669 b8af7c06 2022-03-15 stsp ppa.nfound, ppa.ntrees, ppa.packfile_size,
670 f8a36e22 2021-08-26 stsp ppa.ncommits, ppa.nobj_total, ppa.nobj_deltify,
671 f8a36e22 2021-08-26 stsp ppa.nobj_written, bytes_sent,
672 6242c45b 2022-11-14 op refname, errmsg, success);
673 f8a36e22 2021-08-26 stsp if (err) {
674 f8a36e22 2021-08-26 stsp free(refname);
675 6242c45b 2022-11-14 op free(errmsg);
676 f8a36e22 2021-08-26 stsp goto done;
677 f8a36e22 2021-08-26 stsp }
678 f8a36e22 2021-08-26 stsp bytes_sent_cur = bytes_sent;
679 f8a36e22 2021-08-26 stsp }
680 f8a36e22 2021-08-26 stsp free(refname);
681 6242c45b 2022-11-14 op free(errmsg);
682 f8a36e22 2021-08-26 stsp }
683 f8a36e22 2021-08-26 stsp done:
684 f8a36e22 2021-08-26 stsp if (sendpid != -1) {
685 f8a36e22 2021-08-26 stsp if (err)
686 f8a36e22 2021-08-26 stsp got_privsep_send_stop(imsg_sendfds[0]);
687 f8a36e22 2021-08-26 stsp if (waitpid(sendpid, &sendstatus, 0) == -1 && err == NULL)
688 f8a36e22 2021-08-26 stsp err = got_error_from_errno("waitpid");
689 f8a36e22 2021-08-26 stsp }
690 894e4711 2022-10-15 stsp if (packfd != -1 && close(packfd) == -1 && err == NULL)
691 894e4711 2022-10-15 stsp err = got_error_from_errno("close");
692 a32780aa 2022-10-15 stsp if (delta_cache && fclose(delta_cache) == EOF && err == NULL)
693 a32780aa 2022-10-15 stsp err = got_error_from_errno("fclose");
694 f8a36e22 2021-08-26 stsp if (nsendfd != -1 && close(nsendfd) == -1 && err == NULL)
695 f8a36e22 2021-08-26 stsp err = got_error_from_errno("close");
696 f8a36e22 2021-08-26 stsp if (npackfd != -1 && close(npackfd) == -1 && err == NULL)
697 f8a36e22 2021-08-26 stsp err = got_error_from_errno("close");
698 f8a36e22 2021-08-26 stsp
699 27555e8e 2023-07-05 jrick got_pathlist_free(&have_refs, GOT_PATHLIST_FREE_ALL);
700 27555e8e 2023-07-05 jrick got_pathlist_free(&their_refs, GOT_PATHLIST_FREE_ALL);
701 27555e8e 2023-07-05 jrick /*
702 27555e8e 2023-07-05 jrick * Object ids are owned by have_refs/their_refs and are already freed;
703 27555e8e 2023-07-05 jrick * Only the arrays must be freed.
704 27555e8e 2023-07-05 jrick */
705 f8a36e22 2021-08-26 stsp free(our_ids);
706 f8a36e22 2021-08-26 stsp free(their_ids);
707 27555e8e 2023-07-05 jrick free(s);
708 f8a36e22 2021-08-26 stsp return err;
709 f8a36e22 2021-08-26 stsp }