ssh-fetch.con commit short i/o: fix calls to read to use xread or read_in_full (93d26e4)
   1#ifndef COUNTERPART_ENV_NAME
   2#define COUNTERPART_ENV_NAME "GIT_SSH_UPLOAD"
   3#endif
   4#ifndef COUNTERPART_PROGRAM_NAME
   5#define COUNTERPART_PROGRAM_NAME "git-ssh-upload"
   6#endif
   7#ifndef MY_PROGRAM_NAME
   8#define MY_PROGRAM_NAME "git-ssh-fetch"
   9#endif
  10
  11#include "cache.h"
  12#include "commit.h"
  13#include "rsh.h"
  14#include "fetch.h"
  15#include "refs.h"
  16
  17static int fd_in;
  18static int fd_out;
  19
  20static unsigned char remote_version;
  21static unsigned char local_version = 1;
  22
  23static ssize_t force_write(int fd, void *buffer, size_t length)
  24{
  25        ssize_t ret = 0;
  26        while (ret < length) {
  27                ssize_t size = write(fd, (char *) buffer + ret, length - ret);
  28                if (size < 0) {
  29                        return size;
  30                }
  31                if (size == 0) {
  32                        return ret;
  33                }
  34                ret += size;
  35        }
  36        return ret;
  37}
  38
  39static int prefetches;
  40
  41static struct object_list *in_transit;
  42static struct object_list **end_of_transit = &in_transit;
  43
  44void prefetch(unsigned char *sha1)
  45{
  46        char type = 'o';
  47        struct object_list *node;
  48        if (prefetches > 100) {
  49                fetch(in_transit->item->sha1);
  50        }
  51        node = xmalloc(sizeof(struct object_list));
  52        node->next = NULL;
  53        node->item = lookup_unknown_object(sha1);
  54        *end_of_transit = node;
  55        end_of_transit = &node->next;
  56        force_write(fd_out, &type, 1);
  57        force_write(fd_out, sha1, 20);
  58        prefetches++;
  59}
  60
  61static char conn_buf[4096];
  62static size_t conn_buf_posn;
  63
  64int fetch(unsigned char *sha1)
  65{
  66        int ret;
  67        signed char remote;
  68        struct object_list *temp;
  69
  70        if (hashcmp(sha1, in_transit->item->sha1)) {
  71                /* we must have already fetched it to clean the queue */
  72                return has_sha1_file(sha1) ? 0 : -1;
  73        }
  74        prefetches--;
  75        temp = in_transit;
  76        in_transit = in_transit->next;
  77        if (!in_transit)
  78                end_of_transit = &in_transit;
  79        free(temp);
  80
  81        if (conn_buf_posn) {
  82                remote = conn_buf[0];
  83                memmove(conn_buf, conn_buf + 1, --conn_buf_posn);
  84        } else {
  85                if (xread(fd_in, &remote, 1) < 1)
  86                        return -1;
  87        }
  88        /* fprintf(stderr, "Got %d\n", remote); */
  89        if (remote < 0)
  90                return remote;
  91        ret = write_sha1_from_fd(sha1, fd_in, conn_buf, 4096, &conn_buf_posn);
  92        if (!ret)
  93                pull_say("got %s\n", sha1_to_hex(sha1));
  94        return ret;
  95}
  96
  97static int get_version(void)
  98{
  99        char type = 'v';
 100        write(fd_out, &type, 1);
 101        write(fd_out, &local_version, 1);
 102        if (xread(fd_in, &remote_version, 1) < 1) {
 103                return error("Couldn't read version from remote end");
 104        }
 105        return 0;
 106}
 107
 108int fetch_ref(char *ref, unsigned char *sha1)
 109{
 110        signed char remote;
 111        char type = 'r';
 112        write(fd_out, &type, 1);
 113        write(fd_out, ref, strlen(ref) + 1);
 114
 115        if (read_in_full(fd_in, &remote, 1) != 1)
 116                return -1;
 117        if (remote < 0)
 118                return remote;
 119        if (read_in_full(fd_in, sha1, 20) != 20)
 120                return -1;
 121        return 0;
 122}
 123
 124static const char ssh_fetch_usage[] =
 125  MY_PROGRAM_NAME
 126  " [-c] [-t] [-a] [-v] [--recover] [-w ref] commit-id url";
 127int main(int argc, char **argv)
 128{
 129        const char *write_ref = NULL;
 130        char *commit_id;
 131        char *url;
 132        int arg = 1;
 133        const char *prog;
 134
 135        prog = getenv("GIT_SSH_PUSH");
 136        if (!prog) prog = "git-ssh-upload";
 137
 138        setup_ident();
 139        setup_git_directory();
 140        git_config(git_default_config);
 141
 142        while (arg < argc && argv[arg][0] == '-') {
 143                if (argv[arg][1] == 't') {
 144                        get_tree = 1;
 145                } else if (argv[arg][1] == 'c') {
 146                        get_history = 1;
 147                } else if (argv[arg][1] == 'a') {
 148                        get_all = 1;
 149                        get_tree = 1;
 150                        get_history = 1;
 151                } else if (argv[arg][1] == 'v') {
 152                        get_verbosely = 1;
 153                } else if (argv[arg][1] == 'w') {
 154                        write_ref = argv[arg + 1];
 155                        arg++;
 156                } else if (!strcmp(argv[arg], "--recover")) {
 157                        get_recover = 1;
 158                }
 159                arg++;
 160        }
 161        if (argc < arg + 2) {
 162                usage(ssh_fetch_usage);
 163                return 1;
 164        }
 165        commit_id = argv[arg];
 166        url = argv[arg + 1];
 167
 168        if (setup_connection(&fd_in, &fd_out, prog, url, arg, argv + 1))
 169                return 1;
 170
 171        if (get_version())
 172                return 1;
 173
 174        if (pull(1, &commit_id, &write_ref, url))
 175                return 1;
 176
 177        return 0;
 178}