interplane.Get Omega
Read in your language

git

[Ported from github.com/git/git] Sync with Git 2.56.1

Brought by drakestapleton

branch master Branches Tags History

git / fetch-object-info.c

199 lines · 5717 bytes Raw Blame History
1#include "git-compat-util.h"
2#include "gettext.h"
3#include "hex.h"
4#include "object.h"
5#include "pkt-line.h"
6#include "connect.h"
7#include "oid-array.h"
8#include "odb.h"
9#include "fetch-object-info.h"
10#include "string-list.h"
11
12/* Sends object-info command and its arguments into the request buffer. */
13static void send_object_info_request(const int fd_out,
14 const struct string_list *server_options,
15 const struct oid_array *oids,
16 unsigned ask_size,
17 unsigned ask_type)
18{
19 struct strbuf req_buf = STRBUF_INIT;
20
21 write_command_and_capabilities(&req_buf, "object-info", server_options);
22
23 if (ask_size)
24 packet_buf_write(&req_buf, "size");
25
26 if (ask_type)
27 packet_buf_write(&req_buf, "type");
28
29 if (oids)
30 for (size_t i = 0; i < oids->nr; i++)
31 packet_buf_write(&req_buf, "oid %s",
32 oid_to_hex(&oids->oid[i]));
33
34 packet_buf_flush(&req_buf);
35 if (write_in_full(fd_out, req_buf.buf, req_buf.len) < 0)
36 die_errno(_("unable to write request to remote"));
37
38 strbuf_release(&req_buf);
39}
40
41static int parse_object_size(const char *s, size_t *res)
42{
43 uintmax_t uim;
44
45 if (!s[0] || s[strspn(s, "0123456789")])
46 return -1;
47 errno = 0;
48 uim = strtoumax(s, NULL, 10);
49 if (errno || uim > SIZE_MAX)
50 return -1;
51 *res = uim;
52 return 0;
53}
54
55void fetch_object_info(const enum protocol_version version,
56 const struct string_list *server_options,
57 const struct oid_array *oids,
58 struct packet_reader *reader,
59 struct fetch_object_info_results *results,
60 const int stateless_rpc,
61 const int fd_out)
62{
63 unsigned ask_size = 0;
64 unsigned ask_type = 0;
65 int size_index = -1;
66 int type_index = -1;
67 size_t wanted;
68
69 results->nr = oids->nr;
70 CALLOC_ARRAY(results->unrecognized, results->nr);
71
72 switch (version) {
73 case protocol_v2:
74 if (!server_supports_v2("object-info"))
75 die(_("object-info capability is not enabled on the server"));
76
77 if (results->wants_size &&
78 server_supports_feature("object-info", "size", 0))
79 ask_size = 1;
80
81 if (results->wants_type &&
82 server_supports_feature("object-info", "type", 0))
83 ask_type = 1;
84
85 /*
86 * Even if no options are left, we still send the oid so we get
87 * at least an existence check.
88 */
89 send_object_info_request(fd_out, server_options, oids, ask_size,
90 ask_type);
91 break;
92 case protocol_v1:
93 case protocol_v0:
94 die(_("object-info requires protocol v2"));
95 case protocol_unknown_version:
96 BUG("unknown protocol version");
97 }
98 wanted = ask_size + ask_type;
99
100 for (size_t i = 0; i < wanted; i++) {
101 if (packet_reader_read(reader) != PACKET_READ_NORMAL) {
102 check_stateless_delimiter(stateless_rpc, reader,
103 "stateless delimiter expected");
104 die(_("object-info: expected %" PRIuMAX " attributes, got %" PRIuMAX),
105 (uintmax_t)wanted, (uintmax_t)i);
106 }
107
108 if (!strcmp(reader->line, "size")) {
109 if (!ask_size)
110 die(_("object-info: unrequested 'size' attribute"));
111 if (results->sizes)
112 die(_("object-info: duplicate 'size' attribute"));
113 size_index = (int)i;
114 CALLOC_ARRAY(results->sizes, results->nr);
115 } else if (!strcmp(reader->line, "type")) {
116 if (!ask_type)
117 die(_("object-info: unrequested 'type' attribute"));
118 if (results->types)
119 die(_("object-info: duplicate 'type' attribute"));
120 type_index = (int)i;
121 CALLOC_ARRAY(results->types, results->nr);
122 } else {
123 die(_("object-info: unknown attribute '%s'"),
124 reader->line);
125 }
126 }
127
128 for (size_t i = 0; i < oids->nr; i++) {
129 struct string_list object_info_values = STRING_LIST_INIT_DUP;
130
131 if (packet_reader_read(reader) != PACKET_READ_NORMAL)
132 die(_("object-info: expected %" PRIuMAX " objects, got %" PRIuMAX),
133 (uintmax_t)oids->nr, (uintmax_t)i);
134
135 string_list_split(&object_info_values, reader->line, " ", -1);
136
137 if (strcmp(object_info_values.items[0].string,
138 oid_to_hex(&oids->oid[i])))
139 die(_("object-info: expected OID: %s, got %s"),
140 oid_to_hex(&oids->oid[i]),
141 object_info_values.items[0].string);
142
143 /*
144 * If the response is two elements but the second one is an
145 * empty string, that means that the OID is unrecognized by the
146 * server.
147 */
148 if (object_info_values.nr >= 2 &&
149 !strcmp(object_info_values.items[1].string, "")) {
150 results->unrecognized[i] = 1;
151 string_list_clear(&object_info_values, 0);
152 continue;
153 }
154
155 /*
156 * Because we only ask for attributes the server said it
157 * supports, we expect the answer to have one value per
158 * requested attribute, plus the OID.
159 */
160 if (wanted + 1 != object_info_values.nr)
161 die("object-info: unexpected number of attributes: %s",
162 reader->line);
163
164 if (results->sizes &&
165 parse_object_size(object_info_values.items[size_index + 1].string,
166 &results->sizes[i]))
167 die("object-info: object %s has invalid size %s",
168 object_info_values.items[0].string,
169 object_info_values.items[size_index + 1].string);
170
171 if (results->types) {
172 const char *type_str =
173 object_info_values.items[type_index + 1].string;
174 int type = type_from_string_gently(type_str, -1, 1);
175
176 if (type < 0)
177 die(_("object-info: object %s has invalid type '%s'"),
178 object_info_values.items[0].string, type_str);
179
180 results->types[i] = type;
181 }
182
183 string_list_clear(&object_info_values, 0);
184 }
185
186 if (packet_reader_read(reader) != PACKET_READ_FLUSH)
187 die(_("object-info: expected flush after %" PRIuMAX " objects"),
188 (uintmax_t)oids->nr);
189
190 check_stateless_delimiter(stateless_rpc, reader, "stateless delimiter expected");
191}
192
193void free_fetch_object_info_results(struct fetch_object_info_results *results)
194{
195 free(results->sizes);
196 free(results->types);
197 free(results->unrecognized);
198 memset(results, 0, sizeof(*results));
199}