Use xstrdup instead of strdup in builtin-{tar,zip}-tree.c
[git.git] / builtin-upload-archive.c
blob3bdb607e375e5c1556281ab32aaf683d07836f81
1 /*
2 * Copyright (c) 2006 Franck Bui-Huu
3 */
4 #include <time.h>
5 #include "cache.h"
6 #include "builtin.h"
7 #include "archive.h"
8 #include "pkt-line.h"
10 static const char upload_archive_usage[] =
11 "git-upload-archive <repo>";
14 int cmd_upload_archive(int argc, const char **argv, const char *prefix)
16 struct archiver ar;
17 const char *sent_argv[MAX_ARGS];
18 const char *arg_cmd = "argument ";
19 char *p, buf[4096];
20 int treeish_idx;
21 int sent_argc;
22 int len;
24 if (argc != 2)
25 usage(upload_archive_usage);
27 if (strlen(argv[1]) > sizeof(buf))
28 die("insanely long repository name");
30 strcpy(buf, argv[1]); /* enter-repo smudges its argument */
32 if (!enter_repo(buf, 0))
33 die("not a git archive");
35 /* put received options in sent_argv[] */
36 sent_argc = 1;
37 sent_argv[0] = "git-upload-archive";
38 for (p = buf;;) {
39 /* This will die if not enough free space in buf */
40 len = packet_read_line(0, p, (buf + sizeof buf) - p);
41 if (len == 0)
42 break; /* got a flush */
43 if (sent_argc > MAX_ARGS - 2)
44 die("Too many options (>29)");
46 if (p[len-1] == '\n') {
47 p[--len] = 0;
49 if (len < strlen(arg_cmd) ||
50 strncmp(arg_cmd, p, strlen(arg_cmd)))
51 die("'argument' token or flush expected");
53 len -= strlen(arg_cmd);
54 memmove(p, p + strlen(arg_cmd), len);
55 sent_argv[sent_argc++] = p;
56 p += len;
57 *p++ = 0;
59 sent_argv[sent_argc] = NULL;
61 /* parse all options sent by the client */
62 treeish_idx = parse_archive_args(sent_argc, sent_argv, &ar);
64 parse_treeish_arg(sent_argv + treeish_idx, &ar.args, prefix);
65 parse_pathspec_arg(sent_argv + treeish_idx + 1, &ar.args);
67 packet_write(1, "ACK\n");
68 packet_flush(1);
70 return ar.write_archive(&ar.args);