2 * Copyright (c) 2018, 2019 Ori Bernstein <ori@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/types.h>
20 #include <sys/socket.h>
22 #include <sys/resource.h>
23 #include <sys/socket.h>
39 #include "got_error.h"
40 #include "got_reference.h"
41 #include "got_repository.h"
43 #include "got_cancel.h"
44 #include "got_worktree.h"
45 #include "got_object.h"
46 #include "got_opentemp.h"
47 #include "got_fetch.h"
49 #include "got_lib_delta.h"
50 #include "got_lib_inflate.h"
51 #include "got_lib_object.h"
52 #include "got_lib_object_parse.h"
53 #include "got_lib_object_create.h"
54 #include "got_lib_pack.h"
55 #include "got_lib_sha1.h"
56 #include "got_lib_privsep.h"
57 #include "got_lib_object_cache.h"
58 #include "got_lib_repository.h"
59 #include "got_lib_dial.h"
60 #include "got_lib_pkt.h"
63 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
67 #define ssizeof(_x) ((ssize_t)(sizeof(_x)))
71 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
74 const struct got_error
*
75 got_fetch_connect(pid_t
*fetchpid
, int *fetchfd
, const char *proto
,
76 const char *host
, const char *port
, const char *server_path
, int verbosity
)
78 const struct got_error
*err
= NULL
;
83 if (strcmp(proto
, "ssh") == 0 || strcmp(proto
, "git+ssh") == 0)
84 err
= got_dial_ssh(fetchpid
, fetchfd
, host
, port
,
85 server_path
, GOT_DIAL_DIRECTION_FETCH
, verbosity
);
86 else if (strcmp(proto
, "git") == 0)
87 err
= got_dial_git(fetchfd
, host
, port
, server_path
,
88 GOT_DIAL_DIRECTION_FETCH
);
89 else if (strcmp(proto
, "http") == 0 || strcmp(proto
, "git+http") == 0)
90 err
= got_error_path(proto
, GOT_ERR_NOT_IMPL
);
92 err
= got_error_path(proto
, GOT_ERR_BAD_PROTO
);
96 const struct got_error
*
97 got_fetch_pack(struct got_object_id
**pack_hash
, struct got_pathlist_head
*refs
,
98 struct got_pathlist_head
*symrefs
, const char *remote_name
,
99 int mirror_references
, int fetch_all_branches
,
100 struct got_pathlist_head
*wanted_branches
,
101 struct got_pathlist_head
*wanted_refs
, int list_refs_only
, int verbosity
,
102 int fetchfd
, struct got_repository
*repo
,
103 got_fetch_progress_cb progress_cb
, void *progress_arg
)
106 int imsg_fetchfds
[2], imsg_idxfds
[2];
107 int packfd
= -1, npackfd
= -1, idxfd
= -1, nidxfd
= -1, nfetchfd
= -1;
109 int fetchstatus
, idxstatus
, done
= 0;
110 const struct got_error
*err
;
111 struct imsgbuf fetchibuf
, idxibuf
;
112 pid_t fetchpid
, idxpid
;
113 char *tmppackpath
= NULL
, *tmpidxpath
= NULL
;
114 char *packpath
= NULL
, *idxpath
= NULL
, *id_str
= NULL
;
115 const char *repo_path
= NULL
;
116 struct got_pathlist_head have_refs
;
117 struct got_pathlist_entry
*pe
;
118 struct got_reflist_head my_refs
;
119 struct got_reflist_entry
*re
;
120 off_t packfile_size
= 0;
121 struct got_packfile_hdr pack_hdr
;
124 char *progress
= NULL
;
129 * Prevent fetching of references that won't make any
130 * sense outside of the remote repository's context.
132 TAILQ_FOREACH(pe
, wanted_refs
, entry
) {
133 const char *refname
= pe
->path
;
134 if (strncmp(refname
, "refs/got/", 9) == 0 ||
135 strncmp(refname
, "got/", 4) == 0 ||
136 strncmp(refname
, "refs/remotes/", 13) == 0 ||
137 strncmp(refname
, "remotes/", 8) == 0)
138 return got_error_path(refname
, GOT_ERR_FETCH_BAD_REF
);
142 repo_path
= got_repo_get_path_git_dir(repo
);
144 for (i
= 0; i
< nitems(tmpfds
); i
++)
147 TAILQ_INIT(&have_refs
);
148 TAILQ_INIT(&my_refs
);
150 if (!list_refs_only
) {
151 err
= got_ref_list(&my_refs
, repo
, NULL
,
152 got_ref_cmp_by_name
, NULL
);
157 TAILQ_FOREACH(re
, &my_refs
, entry
) {
158 struct got_object_id
*id
;
161 if (got_ref_is_symbolic(re
->ref
))
164 err
= got_ref_resolve(&id
, repo
, re
->ref
);
167 refname
= strdup(got_ref_get_name(re
->ref
));
168 if (refname
== NULL
) {
169 err
= got_error_from_errno("strdup");
172 err
= got_pathlist_append(&have_refs
, refname
, id
);
177 if (list_refs_only
) {
178 packfd
= got_opentempfd();
180 err
= got_error_from_errno("got_opentempfd");
184 if (asprintf(&path
, "%s/%s/fetching.pack",
185 repo_path
, GOT_OBJECTS_PACK_DIR
) == -1) {
186 err
= got_error_from_errno("asprintf");
189 err
= got_opentemp_named_fd(&tmppackpath
, &packfd
, path
);
193 if (fchmod(packfd
, GOT_DEFAULT_FILE_MODE
) != 0) {
194 err
= got_error_from_errno2("fchmod", tmppackpath
);
198 if (list_refs_only
) {
199 idxfd
= got_opentempfd();
201 err
= got_error_from_errno("got_opentempfd");
205 if (asprintf(&path
, "%s/%s/fetching.idx",
206 repo_path
, GOT_OBJECTS_PACK_DIR
) == -1) {
207 err
= got_error_from_errno("asprintf");
210 err
= got_opentemp_named_fd(&tmpidxpath
, &idxfd
, path
);
214 if (fchmod(idxfd
, GOT_DEFAULT_FILE_MODE
) != 0) {
215 err
= got_error_from_errno2("fchmod", tmpidxpath
);
221 err
= got_error_from_errno("dup");
225 for (i
= 0; i
< nitems(tmpfds
); i
++) {
226 tmpfds
[i
] = got_opentempfd();
227 if (tmpfds
[i
] == -1) {
228 err
= got_error_from_errno("got_opentempfd");
233 if (socketpair(AF_UNIX
, SOCK_STREAM
, PF_UNSPEC
, imsg_fetchfds
) == -1) {
234 err
= got_error_from_errno("socketpair");
239 if (fetchpid
== -1) {
240 err
= got_error_from_errno("fork");
242 } else if (fetchpid
== 0){
243 got_privsep_exec_child(imsg_fetchfds
,
244 GOT_PATH_PROG_FETCH_PACK
, tmppackpath
);
247 if (close(imsg_fetchfds
[1]) == -1) {
248 err
= got_error_from_errno("close");
251 imsg_init(&fetchibuf
, imsg_fetchfds
[0]);
252 nfetchfd
= dup(fetchfd
);
253 if (nfetchfd
== -1) {
254 err
= got_error_from_errno("dup");
257 err
= got_privsep_send_fetch_req(&fetchibuf
, nfetchfd
, &have_refs
,
258 fetch_all_branches
, wanted_branches
, wanted_refs
,
259 list_refs_only
, verbosity
);
263 npackfd
= dup(packfd
);
265 err
= got_error_from_errno("dup");
268 err
= got_privsep_send_fetch_outfd(&fetchibuf
, npackfd
);
274 progress
= calloc(GOT_PKT_MAX
, 1);
275 if (progress
== NULL
) {
276 err
= got_error_from_errno("calloc");
280 *pack_hash
= calloc(1, sizeof(**pack_hash
));
281 if (*pack_hash
== NULL
) {
282 err
= got_error_from_errno("calloc");
287 struct got_object_id
*id
= NULL
;
288 char *refname
= NULL
;
289 char *server_progress
= NULL
;
290 off_t packfile_size_cur
= 0;
292 err
= got_privsep_recv_fetch_progress(&done
,
293 &id
, &refname
, symrefs
, &server_progress
,
294 &packfile_size_cur
, (*pack_hash
)->sha1
, &fetchibuf
);
297 if (!done
&& refname
&& id
) {
298 err
= got_pathlist_insert(NULL
, refs
, refname
, id
);
301 } else if (!done
&& server_progress
) {
304 * XXX git-daemon tends to send batched output with
305 * lines spanning separate packets. Buffer progress
306 * output until we see a CR or LF to avoid giving
307 * partial lines of progress output to the callback.
309 if (strlcat(progress
, server_progress
,
310 GOT_PKT_MAX
) >= GOT_PKT_MAX
) {
311 progress
[0] = '\0'; /* discard */
314 while ((p
= strchr(progress
, '\r')) != NULL
||
315 (p
= strchr(progress
, '\n')) != NULL
) {
320 if (asprintf(&s
, "%s%s", progress
,
321 c
== '\n' ? "\n" : "") == -1) {
322 err
= got_error_from_errno("asprintf");
325 err
= progress_cb(progress_arg
, s
,
326 packfile_size_cur
, 0, 0, 0, 0);
330 n
= strlen(progress
);
331 if (n
< GOT_PKT_MAX
- 1) {
332 memmove(progress
, &progress
[n
+ 1],
333 GOT_PKT_MAX
- n
- 1);
337 free(server_progress
);
340 } else if (!done
&& packfile_size_cur
!= packfile_size
) {
341 err
= progress_cb(progress_arg
, NULL
,
342 packfile_size_cur
, 0, 0, 0, 0);
345 packfile_size
= packfile_size_cur
;
348 if (waitpid(fetchpid
, &fetchstatus
, 0) == -1) {
349 err
= got_error_from_errno("waitpid");
353 if (lseek(packfd
, 0, SEEK_SET
) == -1) {
354 err
= got_error_from_errno("lseek");
358 /* If zero data was fetched without error we are already up-to-date. */
359 if (packfile_size
== 0) {
363 } else if (packfile_size
< ssizeof(pack_hdr
) + SHA1_DIGEST_LENGTH
) {
364 err
= got_error_msg(GOT_ERR_BAD_PACKFILE
, "short pack file");
369 n
= read(packfd
, &pack_hdr
, ssizeof(pack_hdr
));
371 err
= got_error_from_errno("read");
374 if (n
!= ssizeof(pack_hdr
)) {
375 err
= got_error(GOT_ERR_IO
);
378 if (pack_hdr
.signature
!= htobe32(GOT_PACKFILE_SIGNATURE
)) {
379 err
= got_error_msg(GOT_ERR_BAD_PACKFILE
,
380 "bad pack file signature");
383 if (pack_hdr
.version
!= htobe32(GOT_PACKFILE_VERSION
)) {
384 err
= got_error_msg(GOT_ERR_BAD_PACKFILE
,
385 "bad pack file version");
388 nobj
= be32toh(pack_hdr
.nobjects
);
390 packfile_size
> ssizeof(pack_hdr
) + SHA1_DIGEST_LENGTH
)
391 return got_error_msg(GOT_ERR_BAD_PACKFILE
,
392 "bad pack file with zero objects");
394 packfile_size
<= ssizeof(pack_hdr
) + SHA1_DIGEST_LENGTH
)
395 return got_error_msg(GOT_ERR_BAD_PACKFILE
,
396 "empty pack file with non-zero object count");
400 * If the pack file contains no objects, we may only need to update
401 * references in our repository. The caller will take care of that.
406 if (lseek(packfd
, 0, SEEK_SET
) == -1) {
407 err
= got_error_from_errno("lseek");
411 if (socketpair(AF_UNIX
, SOCK_STREAM
, PF_UNSPEC
, imsg_idxfds
) == -1) {
412 err
= got_error_from_errno("socketpair");
417 err
= got_error_from_errno("fork");
419 } else if (idxpid
== 0)
420 got_privsep_exec_child(imsg_idxfds
,
421 GOT_PATH_PROG_INDEX_PACK
, tmppackpath
);
422 if (close(imsg_idxfds
[1]) == -1) {
423 err
= got_error_from_errno("close");
426 imsg_init(&idxibuf
, imsg_idxfds
[0]);
428 npackfd
= dup(packfd
);
430 err
= got_error_from_errno("dup");
433 err
= got_privsep_send_index_pack_req(&idxibuf
, (*pack_hash
)->sha1
,
438 err
= got_privsep_send_index_pack_outfd(&idxibuf
, nidxfd
);
442 for (i
= 0; i
< nitems(tmpfds
); i
++) {
443 err
= got_privsep_send_tmpfd(&idxibuf
, tmpfds
[i
]);
450 int nobj_total
, nobj_indexed
, nobj_loose
, nobj_resolved
;
452 err
= got_privsep_recv_index_progress(&done
, &nobj_total
,
453 &nobj_indexed
, &nobj_loose
, &nobj_resolved
,
457 if (nobj_indexed
!= 0) {
458 err
= progress_cb(progress_arg
, NULL
,
459 packfile_size
, nobj_total
,
460 nobj_indexed
, nobj_loose
, nobj_resolved
);
464 imsg_clear(&idxibuf
);
466 if (close(imsg_idxfds
[0]) == -1) {
467 err
= got_error_from_errno("close");
470 if (waitpid(idxpid
, &idxstatus
, 0) == -1) {
471 err
= got_error_from_errno("waitpid");
475 err
= got_object_id_str(&id_str
, *pack_hash
);
478 if (asprintf(&packpath
, "%s/%s/pack-%s.pack",
479 repo_path
, GOT_OBJECTS_PACK_DIR
, id_str
) == -1) {
480 err
= got_error_from_errno("asprintf");
484 if (asprintf(&idxpath
, "%s/%s/pack-%s.idx",
485 repo_path
, GOT_OBJECTS_PACK_DIR
, id_str
) == -1) {
486 err
= got_error_from_errno("asprintf");
490 if (rename(tmppackpath
, packpath
) == -1) {
491 err
= got_error_from_errno3("rename", tmppackpath
, packpath
);
496 if (rename(tmpidxpath
, idxpath
) == -1) {
497 err
= got_error_from_errno3("rename", tmpidxpath
, idxpath
);
504 if (tmppackpath
&& unlink(tmppackpath
) == -1 && err
== NULL
)
505 err
= got_error_from_errno2("unlink", tmppackpath
);
506 if (tmpidxpath
&& unlink(tmpidxpath
) == -1 && err
== NULL
)
507 err
= got_error_from_errno2("unlink", tmpidxpath
);
508 if (nfetchfd
!= -1 && close(nfetchfd
) == -1 && err
== NULL
)
509 err
= got_error_from_errno("close");
510 if (npackfd
!= -1 && close(npackfd
) == -1 && err
== NULL
)
511 err
= got_error_from_errno("close");
512 if (packfd
!= -1 && close(packfd
) == -1 && err
== NULL
)
513 err
= got_error_from_errno("close");
514 if (idxfd
!= -1 && close(idxfd
) == -1 && err
== NULL
)
515 err
= got_error_from_errno("close");
516 for (i
= 0; i
< nitems(tmpfds
); i
++) {
517 if (tmpfds
[i
] != -1 && close(tmpfds
[i
]) == -1 && err
== NULL
)
518 err
= got_error_from_errno("close");
526 TAILQ_FOREACH(pe
, &have_refs
, entry
) {
527 free((char *)pe
->path
);
530 got_pathlist_free(&have_refs
);
531 got_ref_list_free(&my_refs
);
535 TAILQ_FOREACH(pe
, refs
, entry
) {
536 free((void *)pe
->path
);
539 got_pathlist_free(refs
);
540 TAILQ_FOREACH(pe
, symrefs
, entry
) {
541 free((void *)pe
->path
);
544 got_pathlist_free(symrefs
);