completion: Get rid of tabbed indentation in comments. Replace with spaces.
[gitweb.git] / builtin-upload-archive.c
index 42cb9f8876b5c1ad870b9438e5525d52c6929cf0..a9b02fa32f372a6810867c10560a20d58b5b2a91 100644 (file)
@@ -1,36 +1,35 @@
 /*
  * Copyright (c) 2006 Franck Bui-Huu
  */
-#include <time.h>
 #include "cache.h"
 #include "builtin.h"
 #include "archive.h"
 #include "pkt-line.h"
 #include "sideband.h"
-#include <sys/wait.h>
-#include <sys/poll.h>
 
 static const char upload_archive_usage[] =
-       "git-upload-archive <repo>";
+       "git upload-archive <repo>";
 
 static const char deadchild[] =
-"git-upload-archive: archiver died with error";
+"git upload-archive: archiver died with error";
 
+static const char lostchild[] =
+"git upload-archive: archiver process was lost";
+
+#define MAX_ARGS (64)
 
 static int run_upload_archive(int argc, const char **argv, const char *prefix)
 {
-       struct archiver ar;
        const char *sent_argv[MAX_ARGS];
        const char *arg_cmd = "argument ";
        char *p, buf[4096];
-       int treeish_idx;
        int sent_argc;
        int len;
 
        if (argc != 2)
                usage(upload_archive_usage);
 
-       if (strlen(argv[1]) > sizeof(buf))
+       if (strlen(argv[1]) + 1 > sizeof(buf))
                die("insanely long repository name");
 
        strcpy(buf, argv[1]); /* enter-repo smudges its argument */
@@ -47,7 +46,7 @@ static int run_upload_archive(int argc, const char **argv, const char *prefix)
                if (len == 0)
                        break;  /* got a flush */
                if (sent_argc > MAX_ARGS - 2)
-                       die("Too many options (>29)");
+                       die("Too many options (>%d)", MAX_ARGS - 2);
 
                if (p[len-1] == '\n') {
                        p[--len] = 0;
@@ -65,12 +64,32 @@ static int run_upload_archive(int argc, const char **argv, const char *prefix)
        sent_argv[sent_argc] = NULL;
 
        /* parse all options sent by the client */
-       treeish_idx = parse_archive_args(sent_argc, sent_argv, &ar);
+       return write_archive(sent_argc, sent_argv, prefix, 0);
+}
 
-       parse_treeish_arg(sent_argv + treeish_idx, &ar.args, prefix);
-       parse_pathspec_arg(sent_argv + treeish_idx + 1, &ar.args);
+static void error_clnt(const char *fmt, ...)
+{
+       char buf[1024];
+       va_list params;
+       int len;
 
-       return ar.write_archive(&ar.args);
+       va_start(params, fmt);
+       len = vsprintf(buf, fmt, params);
+       va_end(params);
+       send_sideband(1, 3, buf, len, LARGE_PACKET_MAX);
+       die("sent error to the client: %s", buf);
+}
+
+static void process_input(int child_fd, int band)
+{
+       char buf[16384];
+       ssize_t sz = read(child_fd, buf, sizeof(buf));
+       if (sz < 0) {
+               if (errno != EAGAIN && errno != EINTR)
+                       error_clnt("read error: %s\n", strerror(errno));
+               return;
+       }
+       send_sideband(1, band, buf, sz, LARGE_PACKET_MAX);
 }
 
 int cmd_upload_archive(int argc, const char **argv, const char *prefix)
@@ -112,9 +131,6 @@ int cmd_upload_archive(int argc, const char **argv, const char *prefix)
 
        while (1) {
                struct pollfd pfd[2];
-               char buf[16384];
-               ssize_t sz;
-               pid_t pid;
                int status;
 
                pfd[0].fd = fd1[0];
@@ -129,28 +145,20 @@ int cmd_upload_archive(int argc, const char **argv, const char *prefix)
                        }
                        continue;
                }
-               if (pfd[0].revents & (POLLIN|POLLHUP)) {
+               if (pfd[0].revents & POLLIN)
                        /* Data stream ready */
-                       sz = read(pfd[0].fd, buf, sizeof(buf));
-                       send_sideband(1, 1, buf, sz, LARGE_PACKET_MAX);
-               }
-               if (pfd[1].revents & (POLLIN|POLLHUP)) {
+                       process_input(pfd[0].fd, 1);
+               if (pfd[1].revents & POLLIN)
                        /* Status stream ready */
-                       sz = read(pfd[1].fd, buf, sizeof(buf));
-                       send_sideband(1, 2, buf, sz, LARGE_PACKET_MAX);
-               }
-
-               if (((pfd[0].revents | pfd[1].revents) & POLLHUP) == 0)
+                       process_input(pfd[1].fd, 2);
+               /* Always finish to read data when available */
+               if ((pfd[0].revents | pfd[1].revents) & POLLIN)
                        continue;
-               /* did it die? */
-               pid = waitpid(writer, &status, WNOHANG);
-               if (!pid) {
-                       fprintf(stderr, "Hmph, HUP?\n");
-                       continue;
-               }
-               if (!WIFEXITED(status) || WEXITSTATUS(status) > 0)
-                       send_sideband(1, 3, deadchild, strlen(deadchild),
-                                     LARGE_PACKET_MAX);
+
+               if (waitpid(writer, &status, 0) < 0)
+                       error_clnt("%s", lostchild);
+               else if (!WIFEXITED(status) || WEXITSTATUS(status) > 0)
+                       error_clnt("%s", deadchild);
                packet_flush(1);
                break;
        }