gitweb (SyntaxHighlighter): interpret #l<line-number>
[git/dscho.git] / credential-cache.c
blobdc98372e59d1baf2a9714ecde9ffc96589b9cbac
1 #include "cache.h"
2 #include "credential.h"
3 #include "string-list.h"
4 #include "parse-options.h"
5 #include "unix-socket.h"
6 #include "run-command.h"
8 #define FLAG_SPAWN 0x1
9 #define FLAG_RELAY 0x2
11 static int send_request(const char *socket, const struct strbuf *out)
13 int got_data = 0;
14 int fd = unix_stream_connect(socket);
16 if (fd < 0)
17 return -1;
19 if (write_in_full(fd, out->buf, out->len) < 0)
20 die_errno("unable to write to cache daemon");
21 shutdown(fd, SHUT_WR);
23 while (1) {
24 char in[1024];
25 int r;
27 r = read_in_full(fd, in, sizeof(in));
28 if (r == 0)
29 break;
30 if (r < 0)
31 die_errno("read error from cache daemon");
32 write_or_die(1, in, r);
33 got_data = 1;
35 return got_data;
38 static void spawn_daemon(const char *socket)
40 struct child_process daemon;
41 const char *argv[] = { NULL, NULL, NULL };
42 char buf[128];
43 int r;
45 memset(&daemon, 0, sizeof(daemon));
46 argv[0] = "git-credential-cache--daemon";
47 argv[1] = socket;
48 daemon.argv = argv;
49 daemon.no_stdin = 1;
50 daemon.out = -1;
52 if (start_command(&daemon))
53 die_errno("unable to start cache daemon");
54 r = read_in_full(daemon.out, buf, sizeof(buf));
55 if (r < 0)
56 die_errno("unable to read result code from cache daemon");
57 if (r != 3 || memcmp(buf, "ok\n", 3))
58 die("cache daemon did not start: %.*s", r, buf);
59 close(daemon.out);
62 static void do_cache(const char *socket, const char *action, int timeout,
63 int flags)
65 struct strbuf buf = STRBUF_INIT;
67 strbuf_addf(&buf, "action=%s\n", action);
68 strbuf_addf(&buf, "timeout=%d\n", timeout);
69 if (flags & FLAG_RELAY) {
70 if (strbuf_read(&buf, 0, 0) < 0)
71 die_errno("unable to relay credential");
74 if (!send_request(socket, &buf))
75 return;
76 if (flags & FLAG_SPAWN) {
77 spawn_daemon(socket);
78 send_request(socket, &buf);
80 strbuf_release(&buf);
83 int main(int argc, const char **argv)
85 char *socket_path = NULL;
86 int timeout = 900;
87 const char *op;
88 const char * const usage[] = {
89 "git credential-cache [options] <action>",
90 NULL
92 struct option options[] = {
93 OPT_INTEGER(0, "timeout", &timeout,
94 "number of seconds to cache credentials"),
95 OPT_STRING(0, "socket", &socket_path, "path",
96 "path of cache-daemon socket"),
97 OPT_END()
100 argc = parse_options(argc, argv, NULL, options, usage, 0);
101 if (!argc)
102 usage_with_options(usage, options);
103 op = argv[0];
105 if (!socket_path)
106 socket_path = expand_user_path("~/.git-credential-cache/socket");
107 if (!socket_path)
108 die("unable to find a suitable socket path; use --socket");
110 if (!strcmp(op, "exit"))
111 do_cache(socket_path, op, timeout, 0);
112 else if (!strcmp(op, "get") || !strcmp(op, "erase"))
113 do_cache(socket_path, op, timeout, FLAG_RELAY);
114 else if (!strcmp(op, "store"))
115 do_cache(socket_path, op, timeout, FLAG_RELAY|FLAG_SPAWN);
116 else
117 ; /* ignore unknown operation */
119 return 0;