Artifact 01a00b4beef68839701c7514ce82e3933e7df6159899c892008c863478dc8a14:
- File
src/xfer.c
— part of check-in
[9dabe49948]
at
2026-11-21 19:00:33
on branch amalgam
— Amalgamation: pic as one pic.c beside the vendored sources, built by one cc line
make amalgamation writes pic-amalgamation-VERSION/ and its tarball; make test-amal builds it from the README's own line and runs crash, print and tools against that binary, and is part of make test-pic. About 60 file-local names that collided in one translation unit take their file's prefix; test/pic/amalgam.sh fails on a new collision. (user: dmestas, size: 4228) [annotate] [blame] [check-ins using] [more...]
/* pic's Fossil sync transport: libfossil's stream channel (it writes the * HTTP request and parses the reply) over a pic_stream, net.c's by default. */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include "net.h" #include "xfer.h" /* One round trip's connection, however slow, ends by then. */ #define XFER_DEADLINE_SECONDS 1800 struct pic_xfer { fsl_sc ch; fsl_sc_stream_ops ops; pic_stream stream; pic_net *net; }; static int net_connect(void *state, const char *host, int port, int tls, int idle_seconds) { return pic_net_connect(state, host, port, tls, idle_seconds, XFER_DEADLINE_SECONDS); } static long net_read(void *state, void *buf, size_t cap) { long n = pic_net_read(state, buf, cap); return n < 0 && pic_net_unclean_close(state) ? PIC_STREAM_UNCLEAN : n; } static int net_write(void *state, const void *buf, size_t len) { return pic_net_write(state, buf, len); } static void net_close(void *state) { pic_net_close(state); } static const char *net_error(void *state) { return pic_net_error(state); } static int xf_fail(pic_xfer *x, fsl_xfer *xf, const char *what) { const char *why = x->stream.error ? x->stream.error(x->stream.state) : NULL; return fsl_xfer_errorf(xf, FSL_RC_IO, "%s: %s", what, why && *why ? why : "failed"); } static int ch_connect(void *state, fsl_url const *url, fsl_xfer *xf) { pic_xfer *x = state; int tls = url->scheme && strcmp(url->scheme, "https") == 0; int port = url->port ? url->port : tls ? 443 : 80; if (!url->host || !*url->host) { return fsl_xfer_errorf(xf, FSL_RC_MISUSE, "the sync URL has no host"); } if (x->stream.connect(x->stream.state, url->host, port, tls, fsl_xfer_socket_timeout(xf)) != 0) { return xf_fail(x, xf, "connect"); } return 0; } static fsl_int_t ch_read(void *state, void *dest, fsl_size_t n, fsl_xfer *xf) { pic_xfer *x = state; long got = x->stream.read(x->stream.state, dest, (size_t)n); if (got >= 0) { return (fsl_int_t)got; } if (got == PIC_STREAM_UNCLEAN) { return FSL_SC_STREAM_UNCLEAN; } (void)xf_fail(x, xf, "read the reply"); return -1; } static fsl_int_t ch_write(void *state, void const *src, fsl_size_t n, fsl_xfer *xf) { pic_xfer *x = state; if (x->stream.write(x->stream.state, src, (size_t)n) != 0) { (void)xf_fail(x, xf, "send the request"); return -1; } return (fsl_int_t)n; } static void ch_close(void *state) { pic_xfer *x = state; x->stream.close(x->stream.state); } int pic_xfer_open(const char *url, const pic_stream *stream, fsl_xfer_config *cfg, pic_xfer **out, char *err, size_t errcap) { pic_xfer *x; *out = NULL; if (strncmp(url, "zit:", 4) == 0) { (void)snprintf(err, errcap, "zit: projects are not supported yet; clone over https:// or http://"); return -1; } if (strncmp(url, "https://", 8) != 0 && strncmp(url, "http://", 7) != 0) { const char *colon = strstr(url, "://"); (void)snprintf(err, errcap, "cannot sync with %.*s%s: only https:// and http:// (loopback) are served", colon ? (int)(colon - url + 3) : 0, url, colon ? "" : "a URL without a scheme"); return -1; } if (!(x = calloc(1, sizeof *x))) { (void)snprintf(err, errcap, "out of memory"); return -1; } if (stream) { x->stream = *stream; } else { if (!(x->net = pic_net_new(err, errcap))) { free(x); return -1; } x->stream = (pic_stream){net_connect, net_read, net_write, net_close, net_error, x->net}; } x->ops = (fsl_sc_stream_ops){ch_connect, ch_read, ch_write, ch_close, x}; if (fsl_sc_stream_init(&x->ch, &x->ops) != 0 || fsl_b_append(&cfg->url, url, -1) != 0) { (void)snprintf(err, errcap, "cannot set up the sync channel"); pic_xfer_close(x); return -1; } x->ch.name = "pic"; cfg->channel = &x->ch; cfg->password.save = false; cfg->password.ignoreSaved = true; *out = x; return 0; } void pic_xfer_close(pic_xfer *x) { if (x) { pic_net_free(x->net); free(x); } }