]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/credential-cache.c
Merge branch 'ea/blame-use-oideq'
[thirdparty/git.git] / builtin / credential-cache.c
CommitLineData
b5dd96b7
JK
1#include "builtin.h"
2#include "parse-options.h"
3
4#ifndef NO_UNIX_SOCKETS
5
e2770979
JK
6#include "credential.h"
7#include "string-list.h"
e2770979
JK
8#include "unix-socket.h"
9#include "run-command.h"
10
11#define FLAG_SPAWN 0x1
12#define FLAG_RELAY 0x2
13
14static int send_request(const char *socket, const struct strbuf *out)
15{
16 int got_data = 0;
17 int fd = unix_stream_connect(socket);
18
19 if (fd < 0)
20 return -1;
21
22 if (write_in_full(fd, out->buf, out->len) < 0)
23 die_errno("unable to write to cache daemon");
24 shutdown(fd, SHUT_WR);
25
26 while (1) {
27 char in[1024];
28 int r;
29
30 r = read_in_full(fd, in, sizeof(in));
1f180e5e 31 if (r == 0 || (r < 0 && errno == ECONNRESET))
e2770979
JK
32 break;
33 if (r < 0)
34 die_errno("read error from cache daemon");
35 write_or_die(1, in, r);
36 got_data = 1;
37 }
9c60d9fa 38 close(fd);
e2770979
JK
39 return got_data;
40}
41
42static void spawn_daemon(const char *socket)
43{
d3180279 44 struct child_process daemon = CHILD_PROCESS_INIT;
e2770979
JK
45 char buf[128];
46 int r;
47
c0e190c1
JH
48 strvec_pushl(&daemon.args,
49 "credential-cache--daemon", socket,
50 NULL);
51 daemon.git_cmd = 1;
e2770979
JK
52 daemon.no_stdin = 1;
53 daemon.out = -1;
54
55 if (start_command(&daemon))
56 die_errno("unable to start cache daemon");
57 r = read_in_full(daemon.out, buf, sizeof(buf));
58 if (r < 0)
59 die_errno("unable to read result code from cache daemon");
60 if (r != 3 || memcmp(buf, "ok\n", 3))
61 die("cache daemon did not start: %.*s", r, buf);
62 close(daemon.out);
63}
64
65static void do_cache(const char *socket, const char *action, int timeout,
66 int flags)
67{
68 struct strbuf buf = STRBUF_INIT;
69
70 strbuf_addf(&buf, "action=%s\n", action);
71 strbuf_addf(&buf, "timeout=%d\n", timeout);
72 if (flags & FLAG_RELAY) {
73 if (strbuf_read(&buf, 0, 0) < 0)
74 die_errno("unable to relay credential");
75 }
76
8ec6c8d7 77 if (send_request(socket, &buf) < 0) {
35a71f14 78 if (errno != ENOENT && errno != ECONNREFUSED)
98c2924c 79 die_errno("unable to connect to cache daemon");
8ec6c8d7
JK
80 if (flags & FLAG_SPAWN) {
81 spawn_daemon(socket);
82 if (send_request(socket, &buf) < 0)
83 die_errno("unable to connect to cache daemon");
84 }
e2770979
JK
85 }
86 strbuf_release(&buf);
87}
88
60759baa
DL
89static char *get_socket_path(void)
90{
91 struct stat sb;
92 char *old_dir, *socket;
a2e2c046 93 old_dir = expand_user_path("~/.git-credential-cache", 0);
60759baa
DL
94 if (old_dir && !stat(old_dir, &sb) && S_ISDIR(sb.st_mode))
95 socket = xstrfmt("%s/socket", old_dir);
96 else
97 socket = xdg_cache_home("credential/socket");
98 free(old_dir);
99 return socket;
100}
101
b5dd96b7 102int cmd_credential_cache(int argc, const char **argv, const char *prefix)
e2770979
JK
103{
104 char *socket_path = NULL;
105 int timeout = 900;
106 const char *op;
107 const char * const usage[] = {
d96a0313 108 "git credential-cache [<options>] <action>",
e2770979
JK
109 NULL
110 };
111 struct option options[] = {
112 OPT_INTEGER(0, "timeout", &timeout,
113 "number of seconds to cache credentials"),
114 OPT_STRING(0, "socket", &socket_path, "path",
115 "path of cache-daemon socket"),
116 OPT_END()
117 };
118
b5dd96b7 119 argc = parse_options(argc, argv, prefix, options, usage, 0);
e2770979
JK
120 if (!argc)
121 usage_with_options(usage, options);
122 op = argv[0];
123
124 if (!socket_path)
60759baa 125 socket_path = get_socket_path();
e2770979
JK
126 if (!socket_path)
127 die("unable to find a suitable socket path; use --socket");
128
129 if (!strcmp(op, "exit"))
130 do_cache(socket_path, op, timeout, 0);
131 else if (!strcmp(op, "get") || !strcmp(op, "erase"))
132 do_cache(socket_path, op, timeout, FLAG_RELAY);
133 else if (!strcmp(op, "store"))
134 do_cache(socket_path, op, timeout, FLAG_RELAY|FLAG_SPAWN);
135 else
136 ; /* ignore unknown operation */
137
138 return 0;
139}
b5dd96b7
JK
140
141#else
142
143int cmd_credential_cache(int argc, const char **argv, const char *prefix)
144{
145 const char * const usage[] = {
146 "git credential-cache [options] <action>",
147 "",
148 "credential-cache is disabled in this build of Git",
149 NULL
150 };
151 struct option options[] = { OPT_END() };
152
153 argc = parse_options(argc, argv, prefix, options, usage, 0);
154 die(_("credential-cache unavailable; no unix socket support"));
155}
156
157#endif /* NO_UNIX_SOCKETS */