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