LCOV - code coverage report
Current view: top level - src - gittransport.cpp (source / functions) Coverage Total Hit
Test: jami-coverage-filtered.info Lines: 81.9 % 155 127
Test Date: 2026-06-13 09:18:46 Functions: 52.0 % 25 13

            Line data    Source code
       1              : /*
       2              :  *  Copyright (C) 2004-2026 Savoir-faire Linux Inc.
       3              :  *
       4              :  *  This program is free software: you can redistribute it and/or modify
       5              :  *  it under the terms of the GNU General Public License as published by
       6              :  *  the Free Software Foundation, either version 3 of the License, or
       7              :  *  (at your option) any later version.
       8              :  *
       9              :  *  This program is distributed in the hope that it will be useful,
      10              :  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
      11              :  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
      12              :  *  GNU General Public License for more details.
      13              :  *
      14              :  *  You should have received a copy of the GNU General Public License
      15              :  *  along with this program. If not, see <https://www.gnu.org/licenses/>.
      16              :  */
      17              : #include "gittransport.h"
      18              : 
      19              : #include "manager.h"
      20              : 
      21              : #include <dhtnet/multiplexed_socket.h>
      22              : #include <dhtnet/connectionmanager.h>
      23              : 
      24              : using namespace std::string_view_literals;
      25              : 
      26              : // NOTE: THIS MUST BE IN THE ROOT NAMESPACE FOR LIBGIT2
      27              : 
      28              : /*
      29              :  * Create a git protocol request.
      30              :  *
      31              :  * For example: 0029git-upload-pack conversation\0host=device\0
      32              :  * @param buf       The buffer to fill
      33              :  * @param cmd       The wanted command
      34              :  * @param url       The repository's URL
      35              :  * @return 0 on success, - 1 on error
      36              :  */
      37              : static int
      38         2162 : generateRequest(git_buf* request, const std::string& cmd, std::string_view url)
      39              : {
      40         2162 :     if (cmd.empty()) {
      41            0 :         giterr_set_str(GITERR_NET, "empty command");
      42            0 :         return -1;
      43              :     }
      44              :     // url format = deviceId/conversationId
      45         2162 :     auto delim = url.find('/');
      46         2162 :     if (delim == std::string::npos) {
      47            0 :         giterr_set_str(GITERR_NET, "malformed URL");
      48            0 :         return -1;
      49              :     }
      50              : 
      51         2162 :     auto deviceId = url.substr(0, delim);
      52         2162 :     auto conversationId = url.substr(delim, url.size());
      53              : 
      54         2162 :     constexpr auto nullSeparator = "\0"sv;
      55              :     auto total = 4                                   /* 4 bytes for the len len */
      56         2162 :                  + cmd.size()                        /* followed by the command */
      57              :                  + 1                                 /* space */
      58         2162 :                  + conversationId.size()             /* conversation */
      59              :                  + 1                                 /* \0 */
      60         2162 :                  + HOST_TAG.size() + deviceId.size() /* device */
      61         2162 :                  + nullSeparator.size() /* \0 */;
      62              : 
      63         2162 :     std::string str;
      64         2162 :     str.reserve(total);
      65         2162 :     str.append("0000"sv);
      66         2161 :     fmt::format_to_n(str.begin(), 4, "{:04x}", total);
      67         2161 :     str.append(cmd)
      68         2160 :         .append(" "sv)
      69         2161 :         .append(conversationId)
      70         2162 :         .append(nullSeparator)
      71         2161 :         .append(HOST_TAG)
      72         2162 :         .append(deviceId)
      73         2162 :         .append(nullSeparator);
      74              : 
      75         2162 :     git_buf_set(request, str.data(), str.size());
      76         2162 :     return 0;
      77         2162 : }
      78              : 
      79              : int
      80         2162 : sendCmd(P2PStream* s)
      81              : {
      82         2162 :     auto res = 0;
      83         2162 :     git_buf request = {};
      84         2162 :     if ((res = generateRequest(&request, s->cmd, s->url)) < 0) {
      85            0 :         git_buf_dispose(&request);
      86            0 :         return res;
      87              :     }
      88              : 
      89         2161 :     std::error_code ec;
      90         2161 :     auto sock = s->socket.lock();
      91         2162 :     if (!sock) {
      92            0 :         git_buf_dispose(&request);
      93            0 :         return -1;
      94              :     }
      95         2161 :     if ((res = static_cast<int>(sock->write(reinterpret_cast<const unsigned char*>(request.ptr), request.size, ec)))) {
      96         2162 :         s->sent_command = 1;
      97         2162 :         git_buf_dispose(&request);
      98         2162 :         return res;
      99              :     }
     100              : 
     101            0 :     s->sent_command = 1;
     102            0 :     git_buf_dispose(&request);
     103            0 :     return res;
     104         2162 : }
     105              : 
     106              : int
     107         5606 : P2PStreamRead(git_smart_subtransport_stream* stream, char* buffer, size_t buflen, size_t* read)
     108              : {
     109         5606 :     *read = 0;
     110         5606 :     auto* fs = reinterpret_cast<P2PStream*>(stream);
     111         5606 :     auto sock = fs->socket.lock();
     112         5607 :     if (!sock) {
     113            1 :         giterr_set_str(GITERR_NET, "unavailable socket");
     114            1 :         return -1;
     115              :     }
     116              : 
     117         5606 :     int res = 0;
     118              :     // If it's the first read, we need to send
     119              :     // the upload-pack command
     120         5606 :     if (!fs->sent_command && (res = sendCmd(fs)) < 0)
     121            3 :         return res;
     122              : 
     123         5604 :     std::error_code ec;
     124              :     // TODO ChannelSocket needs a blocking read operation
     125         5604 :     size_t datalen = sock->waitForData(std::chrono::milliseconds(3600 * 1000 * 24), ec);
     126         5604 :     if (datalen > 0)
     127         5604 :         *read = sock->read(reinterpret_cast<unsigned char*>(buffer), std::min(datalen, buflen), ec);
     128              : 
     129         5603 :     return res;
     130         5607 : }
     131              : 
     132              : int
     133         3829 : P2PStreamWrite(git_smart_subtransport_stream* stream, const char* buffer, size_t len)
     134              : {
     135         3829 :     auto* fs = reinterpret_cast<P2PStream*>(stream);
     136         3829 :     auto sock = fs->socket.lock();
     137         3829 :     if (!sock) {
     138            1 :         giterr_set_str(GITERR_NET, "unavailable socket");
     139            1 :         return -1;
     140              :     }
     141         3828 :     std::error_code ec;
     142         3828 :     sock->write(reinterpret_cast<const unsigned char*>(buffer), len, ec);
     143         3828 :     if (ec) {
     144            0 :         giterr_set_str(GITERR_NET, ec.message().c_str());
     145            0 :         return -1;
     146              :     }
     147         3828 :     return 0;
     148         3829 : }
     149              : 
     150              : void
     151         2162 : P2PStreamFree(git_smart_subtransport_stream*)
     152         2162 : {}
     153              : 
     154              : int
     155         6012 : P2PSubTransportAction(git_smart_subtransport_stream** out,
     156              :                       git_smart_subtransport* transport,
     157              :                       const char* url,
     158              :                       git_smart_service_t action)
     159              : {
     160         6012 :     auto* sub = reinterpret_cast<P2PSubTransport*>(transport);
     161         6012 :     if (!sub || !sub->remote) {
     162            0 :         JAMI_ERROR("Invalid subtransport");
     163            0 :         return -1;
     164              :     }
     165              : 
     166         6012 :     auto* repo = git_remote_owner(sub->remote);
     167         6012 :     if (!repo) {
     168            0 :         JAMI_ERROR("No repository linked to the transport");
     169            0 :         return -1;
     170              :     }
     171              : 
     172         6012 :     const auto* workdir = git_repository_workdir(repo);
     173         6012 :     if (!workdir) {
     174            0 :         JAMI_ERROR("No working linked to the repository");
     175            0 :         return -1;
     176              :     }
     177         6012 :     std::string_view path = workdir;
     178         6011 :     auto delimConv = path.rfind("/conversations");
     179         6012 :     if (delimConv == std::string::npos) {
     180            0 :         JAMI_ERROR("No conversation id found");
     181            0 :         return -1;
     182              :     }
     183         6012 :     auto delimAccount = path.rfind('/', delimConv - 1);
     184         6012 :     if (delimAccount == std::string::npos && delimConv - 1 - delimAccount == 16) {
     185            0 :         JAMI_ERROR("No account id found");
     186            0 :         return -1;
     187              :     }
     188         6012 :     auto accountId = path.substr(delimAccount + 1, delimConv - 1 - delimAccount);
     189         6011 :     std::string_view gitUrl = url + ("git://"sv).size();
     190         6011 :     auto delim = gitUrl.find('/');
     191         6011 :     if (delim == std::string::npos) {
     192            0 :         JAMI_ERROR("Incorrect url {:s}", gitUrl);
     193            0 :         return -1;
     194              :     }
     195         6011 :     auto deviceId = gitUrl.substr(0, delim);
     196         6012 :     auto conversationId = gitUrl.substr(delim + 1, gitUrl.size());
     197              : 
     198         6012 :     if (action == GIT_SERVICE_UPLOADPACK_LS) {
     199         2183 :         auto gitSocket = jami::Manager::instance().gitSocket(accountId, deviceId, conversationId);
     200         2183 :         if (!gitSocket) {
     201           84 :             JAMI_ERROR("Unable to find related socket for {:s}, {:s}, {:s}", accountId, deviceId, conversationId);
     202           21 :             return -1;
     203              :         }
     204         2162 :         auto stream = std::make_unique<P2PStream>();
     205         2162 :         stream->socket = gitSocket;
     206         2162 :         stream->base.read = P2PStreamRead;
     207         2161 :         stream->base.write = P2PStreamWrite;
     208         2162 :         stream->base.free = P2PStreamFree;
     209         2162 :         stream->cmd = UPLOAD_PACK_CMD;
     210         2162 :         stream->url = gitUrl;
     211         2161 :         sub->stream = std::move(stream);
     212         2162 :         *out = &sub->stream->base;
     213         2162 :         return 0;
     214         6012 :     } else if (action == GIT_SERVICE_UPLOADPACK) {
     215         3829 :         if (sub->stream) {
     216         3829 :             *out = &sub->stream->base;
     217         3829 :             return 0;
     218              :         }
     219            0 :         return -1;
     220              :     }
     221            0 :     return 0;
     222              : }
     223              : 
     224              : int
     225         6525 : P2PSubTransportClose(git_smart_subtransport*)
     226              : {
     227         6525 :     return 0;
     228              : }
     229              : 
     230              : void
     231         2183 : P2PSubTransportFree(git_smart_subtransport* transport)
     232              : {
     233         2183 :     jami::Manager::instance().eraseGitTransport(transport);
     234         2183 : }
     235              : 
     236              : int
     237         2183 : P2PSubTransportNew(P2PSubTransport** out, git_transport*, void* payload)
     238              : {
     239         2183 :     auto sub = std::make_unique<P2PSubTransport>();
     240         2183 :     sub->remote = reinterpret_cast<git_remote*>(payload);
     241         2182 :     auto* base = &sub->base;
     242         2182 :     base->action = P2PSubTransportAction;
     243         2182 :     base->close = P2PSubTransportClose;
     244         2182 :     base->free = P2PSubTransportFree;
     245         2182 :     *out = sub.get();
     246         2183 :     jami::Manager::instance().insertGitTransport(base, std::move(sub));
     247         2183 :     return 0;
     248         2183 : }
     249              : 
     250              : int
     251         2183 : p2p_subtransport_cb(git_smart_subtransport** out, git_transport* owner, void* payload)
     252              : {
     253              :     P2PSubTransport* sub;
     254              : 
     255         2183 :     if (P2PSubTransportNew(&sub, owner, payload) < 0)
     256            0 :         return -1;
     257              : 
     258         2183 :     *out = &sub->base;
     259         2183 :     return 0;
     260              : }
     261              : 
     262              : int
     263         2183 : p2p_transport_cb(git_transport** out, git_remote* owner, void*)
     264              : {
     265         2183 :     git_smart_subtransport_definition def
     266              :         = {p2p_subtransport_cb,
     267              :            0, /* Because we use an already existing channel socket, we use a permanent transport */
     268         2183 :            reinterpret_cast<void*>(owner)};
     269         4366 :     return git_transport_smart(out, owner, &def);
     270              : }
        

Generated by: LCOV version 2.0-1