]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/gcf2_libgit2.h
ds: inline set_cloexec
[public-inbox.git] / lib / PublicInbox / gcf2_libgit2.h
1 /*
2  * Copyright (C) 2020-2021 all contributors <meta@public-inbox.org>
3  * License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4  *
5  * libgit2 for Inline::C
6  * Avoiding Git::Raw since it doesn't guarantee a stable API,
7  * while libgit2 itself seems reasonably stable.
8  */
9 #include <git2.h>
10 #include <sys/uio.h>
11 #include <errno.h>
12 #include <poll.h>
13
14 static void croak_if_err(int rc, const char *msg)
15 {
16         if (rc != GIT_OK) {
17                 const git_error *e = giterr_last();
18
19                 croak("%d %s (%s)", rc, msg, e ? e->message : "unknown");
20         }
21 }
22
23 SV *new()
24 {
25         git_odb *odb;
26         SV *ref, *self;
27         int rc = git_odb_new(&odb);
28         croak_if_err(rc, "git_odb_new");
29
30         ref = newSViv((IV)odb);
31         self = newRV_noinc(ref);
32         sv_bless(self, gv_stashpv("PublicInbox::Gcf2", GV_ADD));
33         SvREADONLY_on(ref);
34
35         return self;
36 }
37
38 static git_odb *odb_ptr(SV *self)
39 {
40         return (git_odb *)SvIV(SvRV(self));
41 }
42
43 void DESTROY(SV *self)
44 {
45         git_odb_free(odb_ptr(self));
46 }
47
48 /* needs "$GIT_DIR/objects", not $GIT_DIR */
49 void add_alternate(SV *self, const char *objects_path)
50 {
51         int rc = git_odb_add_disk_alternate(odb_ptr(self), objects_path);
52         croak_if_err(rc, "git_odb_add_disk_alternate");
53 }
54
55 #define CAPA(v) (sizeof(v) / sizeof((v)[0]))
56
57 /*
58  * returns true on success, false on failure
59  * this requires an unabbreviated git OID
60  */
61 int cat_oid(SV *self, int fd, SV *oidsv)
62 {
63         /*
64          * adjust when libgit2 gets SHA-256 support, we return the
65          * same header as git-cat-file --batch "$OID $TYPE $SIZE\n"
66          */
67         char hdr[GIT_OID_HEXSZ + sizeof(" commit 18446744073709551615")];
68         struct iovec vec[3];
69         size_t nvec = CAPA(vec);
70         git_oid oid;
71         git_odb_object *object = NULL;
72         int rc, err = 0;
73         STRLEN oidlen;
74         char *oidptr = SvPV(oidsv, oidlen);
75
76         /* same trailer as git-cat-file --batch */
77         vec[2].iov_len = 1;
78         vec[2].iov_base = "\n";
79
80         rc = git_oid_fromstrn(&oid, oidptr, oidlen);
81         if (rc == GIT_OK)
82                 rc = git_odb_read(&object, odb_ptr(self), &oid);
83         if (rc == GIT_OK) {
84                 vec[0].iov_base = hdr;
85                 vec[1].iov_base = (void *)git_odb_object_data(object);
86                 vec[1].iov_len = git_odb_object_size(object);
87
88                 git_oid_nfmt(hdr, GIT_OID_HEXSZ, git_odb_object_id(object));
89                 vec[0].iov_len = GIT_OID_HEXSZ +
90                                 snprintf(hdr + GIT_OID_HEXSZ,
91                                         sizeof(hdr) - GIT_OID_HEXSZ,
92                                         " %s %zu\n",
93                                         git_object_type2string(
94                                                 git_odb_object_type(object)),
95                                         vec[1].iov_len);
96         } else { /* caller retries */
97                 nvec = 0;
98         }
99         while (nvec && !err) {
100                 ssize_t w = writev(fd, vec + CAPA(vec) - nvec, nvec);
101
102                 if (w > 0) {
103                         size_t done = 0;
104                         size_t i;
105
106                         for (i = CAPA(vec) - nvec; i < CAPA(vec); i++) {
107                                 if (w >= vec[i].iov_len) {
108                                         /* fully written vec */
109                                         w -= vec[i].iov_len;
110                                         done++;
111                                 } else { /* partially written vec */
112                                         char *p = vec[i].iov_base;
113                                         vec[i].iov_base = p + w;
114                                         vec[i].iov_len -= w;
115                                         break;
116                                 }
117                         }
118                         nvec -= done;
119                 } else if (w < 0) {
120                         err = errno;
121                         switch (err) {
122                         case EAGAIN: {
123                                 struct pollfd pfd;
124                                 pfd.events = POLLOUT;
125                                 pfd.fd = fd;
126                                 poll(&pfd, 1, -1);
127                         }
128                                 /* fall-through */
129                         case EINTR:
130                                 err = 0;
131                         }
132                 } else { /* w == 0 */
133                         err = ENOSPC;
134                 }
135         }
136         if (object)
137                 git_odb_object_free(object);
138         if (err)
139                 croak("writev error: %s", strerror(err));
140
141         return rc == GIT_OK;
142 }