master
1/***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9 *
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at https://curl.se/docs/copyright.html.
13 *
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
17 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 * SPDX-License-Identifier: curl
22 *
23 ***************************************************************************/
24#include "tool_setup.h"
26#include "tool_cfgable.h"
27#include "tool_msgs.h"
28#include "tool_cb_wrt.h"
29#include "tool_operate.h"
31#ifdef _WIN32
32#define OPENMODE (_S_IREAD | _S_IWRITE)
33#else
34#define OPENMODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)
35#endif
37#define MAX_CLOBBER_ATTEMPTS 9999
39/* 'fname' contains [path + ] file name */
40static bool clobber_name(struct dynbuf *fbuf, const char *fname,
41 int next_num)
42{
43 /* skip the directory, if present */
44 const char *slash = strrchr(fname, '/');
45 const char *base = fname;
46 const char *dot;
47#ifdef _WIN32
48 const char *bslash = strrchr(fname, '\\');
49 /* use the one furthest away */
50 if(bslash && (!slash || (bslash > slash)))
51 slash = bslash;
52#endif
53 if(slash)
54 base = slash + 1;
56 dot = strrchr(base, '.');
57 curlx_dyn_reset(fbuf);
58 if(dot && (dot != base) && dot[1]) {
59 /* there is an extension after the dot and the dot is not the first file
60 name character */
61 int prefix = (int)(dot - fname);
62 if(curlx_dyn_addf(fbuf, "%.*s-%d.%s", prefix, fname, next_num, dot + 1))
63 return FALSE;
64 }
65 else {
66 /* without any extension */
67 if(curlx_dyn_addf(fbuf, "%s.%d", fname, next_num))
68 return FALSE;
69 }
70 return TRUE;
71}
73/* create/open a local file for writing, return TRUE on success */
74bool tool_create_output_file(struct OutStruct *outs,
75 struct OperationConfig *config)
76{
77 FILE *file = NULL;
78 const char *fname = outs->filename;
79 DEBUGASSERT(outs);
80 DEBUGASSERT(config);
81 DEBUGASSERT(fname && *fname);
83 if(config->file_clobber_mode == CLOBBER_ALWAYS ||
84 (config->file_clobber_mode == CLOBBER_DEFAULT &&
85 !outs->is_cd_filename)) {
86 /* open file for writing */
87 file = curlx_fopen(fname, "wb");
88 }
89 else {
90 int fd;
91 do {
92 fd = curlx_open(fname, O_CREAT | O_WRONLY | O_EXCL | CURL_O_BINARY,
93 OPENMODE);
94 /* Keep retrying in the hope that it is not interrupted sometime */
95 /* !checksrc! disable ERRNOVAR 1 */
96 } while(fd == -1 && errno == EINTR);
97 if(config->file_clobber_mode == CLOBBER_NEVER && fd == -1) {
98 int next_num = 1;
99 struct dynbuf fbuffer;
100 char *newfile;
101 int max_num = MAX_CLOBBER_ATTEMPTS;
102#ifdef DEBUGBUILD
103 char *p = curl_getenv("CURL_MAX_CLOBBER");
104 if(p) {
105 const char *pp = p;
106 curl_off_t val;
107 if(!curlx_str_number(&pp, &val, 100000))
108 max_num = (int)val;
109 curl_free(p);
110 }
111#endif
112 curlx_dyn_init(&fbuffer, 1025);
113 /* !checksrc! disable ERRNOVAR 1 */
114 while(fd == -1 && /* have not successfully opened a file */
115 /* because we keep having files that already exist */
116 (errno == EEXIST || errno == EISDIR) &&
117 /* and we have not reached the retry limit */
118 (next_num < max_num)) {
119 if(!clobber_name(&fbuffer, fname, next_num))
120 return FALSE;
121 next_num++;
122 do {
123 fd = curlx_open(curlx_dyn_ptr(&fbuffer),
124 O_CREAT | O_WRONLY | O_EXCL | CURL_O_BINARY,
125 OPENMODE);
126 /* Keep retrying in the hope that it is not interrupted sometime */
127 } while(fd == -1 && errno == EINTR);
128 }
129 newfile = curlx_dyn_ptr(&fbuffer); /* remember the new one */
130 if(newfile) {
131 outs->filename = newfile;
132 outs->alloc_filename = TRUE;
133 }
134 }
135 /* An else statement to not overwrite existing files and not retry with
136 new numbered names (which would cover
137 config->file_clobber_mode == CLOBBER_DEFAULT && outs->is_cd_filename)
138 is not needed because we would have failed earlier, in the while loop
139 and `fd` would now be -1 */
140 if(fd != -1) {
141 file = curlx_fdopen(fd, "wb");
142 if(!file)
143 curlx_close(fd);
144 }
145 }
147 if(!file) {
148 char errbuf[STRERROR_LEN];
149 warnf("Failed to open the file %s: %s", fname,
150 curlx_strerror(errno, errbuf, sizeof(errbuf)));
151 return FALSE;
152 }
153 outs->regular_file = TRUE;
154 outs->fopened = TRUE;
155 outs->stream = file;
156 outs->bytes = 0;
157 outs->init = 0;
158 return TRUE;
159}
161#ifdef _WIN32
162static size_t win_console(intptr_t fhnd, struct OutStruct *outs,
163 char *buffer, size_t bytes,
164 size_t *retp)
165{
166 DWORD chars_written;
167 unsigned char *rbuf = (unsigned char *)buffer;
168 DWORD rlen = (DWORD)bytes;
170#define IS_TRAILING_BYTE(x) (0x80 <= (x) && (x) < 0xC0)
172 /* attempt to complete an incomplete UTF-8 sequence from previous call. the
173 sequence does not have to be well-formed. */
174 if(outs->utf8seq[0] && rlen) {
175 bool complete = FALSE;
176 /* two byte sequence (lead byte 110yyyyy) */
177 if(0xC0 <= outs->utf8seq[0] && outs->utf8seq[0] < 0xE0) {
178 outs->utf8seq[1] = *rbuf++;
179 --rlen;
180 complete = TRUE;
181 }
182 /* three byte sequence (lead byte 1110zzzz) */
183 else if(0xE0 <= outs->utf8seq[0] && outs->utf8seq[0] < 0xF0) {
184 if(!outs->utf8seq[1]) {
185 outs->utf8seq[1] = *rbuf++;
186 --rlen;
187 }
188 if(rlen && !outs->utf8seq[2]) {
189 outs->utf8seq[2] = *rbuf++;
190 --rlen;
191 complete = TRUE;
192 }
193 }
194 /* four byte sequence (lead byte 11110uuu) */
195 else if(0xF0 <= outs->utf8seq[0] && outs->utf8seq[0] < 0xF8) {
196 if(!outs->utf8seq[1]) {
197 outs->utf8seq[1] = *rbuf++;
198 --rlen;
199 }
200 if(rlen && !outs->utf8seq[2]) {
201 outs->utf8seq[2] = *rbuf++;
202 --rlen;
203 }
204 if(rlen && !outs->utf8seq[3]) {
205 outs->utf8seq[3] = *rbuf++;
206 --rlen;
207 complete = TRUE;
208 }
209 }
211 if(complete) {
212 WCHAR prefix[3] = { 0 }; /* UTF-16 (1-2 WCHARs) + NUL */
214 if(MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)outs->utf8seq, -1,
215 prefix, CURL_ARRAYSIZE(prefix))) {
216 DEBUGASSERT(prefix[2] == L'\0');
217 if(!WriteConsoleW((HANDLE)fhnd, prefix, prefix[1] ? 2 : 1,
218 &chars_written, NULL)) {
219 return CURL_WRITEFUNC_ERROR;
220 }
221 }
222 /* else: UTF-8 input was not well formed and OS is pre-Vista which drops
223 invalid characters instead of writing U+FFFD to output. */
224 memset(outs->utf8seq, 0, sizeof(outs->utf8seq));
225 }
226 }
228 /* suppress an incomplete utf-8 sequence at end of rbuf */
229 if(!outs->utf8seq[0] && rlen && (rbuf[rlen - 1] & 0x80)) {
230 /* check for lead byte from a two, three or four byte sequence */
231 if(0xC0 <= rbuf[rlen - 1] && rbuf[rlen - 1] < 0xF8) {
232 outs->utf8seq[0] = rbuf[rlen - 1];
233 rlen -= 1;
234 }
235 else if(rlen >= 2 && IS_TRAILING_BYTE(rbuf[rlen - 1])) {
236 /* check for lead byte from a three or four byte sequence */
237 if(0xE0 <= rbuf[rlen - 2] && rbuf[rlen - 2] < 0xF8) {
238 outs->utf8seq[0] = rbuf[rlen - 2];
239 outs->utf8seq[1] = rbuf[rlen - 1];
240 rlen -= 2;
241 }
242 else if(rlen >= 3 && IS_TRAILING_BYTE(rbuf[rlen - 2])) {
243 /* check for lead byte from a four byte sequence */
244 if(0xF0 <= rbuf[rlen - 3] && rbuf[rlen - 3] < 0xF8) {
245 outs->utf8seq[0] = rbuf[rlen - 3];
246 outs->utf8seq[1] = rbuf[rlen - 2];
247 outs->utf8seq[2] = rbuf[rlen - 1];
248 rlen -= 3;
249 }
250 }
251 }
252 }
254 if(rlen) {
255 /* calculate buffer size for wide characters */
256 DWORD len = (DWORD)MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)rbuf,
257 (int)rlen, NULL, 0);
258 if(!len)
259 return CURL_WRITEFUNC_ERROR;
261 /* grow the buffer if needed */
262 if(len > global->term.len) {
263 wchar_t *buf = curlx_realloc(global->term.buf, len * sizeof(wchar_t));
264 if(!buf)
265 return CURL_WRITEFUNC_ERROR;
266 global->term.len = len;
267 global->term.buf = buf;
268 }
270 len = (DWORD)MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)rbuf, (int)rlen,
271 global->term.buf,
272 (int)len);
273 if(!len)
274 return CURL_WRITEFUNC_ERROR;
276 if(!WriteConsoleW((HANDLE) fhnd, global->term.buf,
277 len, &chars_written, NULL))
278 return CURL_WRITEFUNC_ERROR;
279 }
281 *retp = bytes;
282 return 0;
283}
284#endif /* _WIN32 */
286/*
287 * callback for CURLOPT_WRITEFUNCTION
288 */
289size_t tool_write_cb(char *buffer, size_t sz, size_t nmemb, void *userdata)
290{
291 size_t rc;
292 struct per_transfer *per = userdata;
293 struct OutStruct *outs = &per->outs;
294 struct OperationConfig *config = per->config;
295 size_t bytes = sz * nmemb;
296 bool is_tty = (bool)global->isatty;
297#ifdef _WIN32
298 CONSOLE_SCREEN_BUFFER_INFO console_info;
299 intptr_t fhnd;
300#endif
302 if(outs->out_null)
303 return bytes;
305#ifdef DEBUGBUILD
306 if(config->show_headers) {
307 if(bytes > (size_t)CURL_MAX_HTTP_HEADER) {
308 warnf("Header data size exceeds write limit");
309 return CURL_WRITEFUNC_ERROR;
310 }
311 }
312 else {
313 if(bytes > (size_t)CURL_MAX_WRITE_SIZE) {
314 warnf("Data size exceeds write limit");
315 return CURL_WRITEFUNC_ERROR;
316 }
317 }
319 {
320 /* Some internal congruency checks on received OutStruct */
321 bool check_fails = FALSE;
322 if(outs->filename) {
323 /* regular file */
324 if(!*outs->filename)
325 check_fails = TRUE;
326 if(!outs->regular_file)
327 check_fails = TRUE;
328 if(outs->fopened && !outs->stream)
329 check_fails = TRUE;
330 if(!outs->fopened && outs->stream)
331 check_fails = TRUE;
332 if(!outs->fopened && outs->bytes)
333 check_fails = TRUE;
334 }
335 else {
336 /* standard stream */
337 if(!outs->stream || outs->regular_file || outs->fopened)
338 check_fails = TRUE;
339 if(outs->alloc_filename || outs->is_cd_filename || outs->init)
340 check_fails = TRUE;
341 }
342 if(check_fails) {
343 warnf("Invalid output struct data for write callback");
344 return CURL_WRITEFUNC_ERROR;
345 }
346 }
347#endif /* DEBUGBUILD */
349 if(!outs->stream && !tool_create_output_file(outs, per->config))
350 return CURL_WRITEFUNC_ERROR;
352 if(is_tty && (outs->bytes < 2000) && !config->terminal_binary_ok) {
353 /* binary output to terminal? */
354 if(memchr(buffer, 0, bytes)) {
355 warnf("Binary output can mess up your terminal. "
356 "Use \"--output -\" to tell curl to output it to your terminal "
357 "anyway, or consider \"--output <FILE>\" to save to a file.");
358 return CURL_WRITEFUNC_ERROR;
359 }
360 }
362#ifdef _WIN32
363 fhnd = _get_osfhandle(fileno(outs->stream));
364 /* if Windows console then UTF-8 must be converted to UTF-16 */
365 if(isatty(fileno(outs->stream)) &&
366 GetConsoleScreenBufferInfo((HANDLE)fhnd, &console_info)) {
367 size_t retval = win_console(fhnd, outs, buffer, bytes, &rc);
368 if(retval)
369 return retval;
370 }
371 else
372#endif
373 {
374 if(per->hdrcbdata.headlist &&
375 tool_write_headers(&per->hdrcbdata, outs->stream))
376 return CURL_WRITEFUNC_ERROR;
377 rc = fwrite(buffer, sz, nmemb, outs->stream);
378 }
380 if(bytes == rc)
381 /* we added this amount of data to the output */
382 outs->bytes += bytes;
384 if(config->readbusy) {
385 config->readbusy = FALSE;
386 curl_easy_pause(per->curl, CURLPAUSE_CONT);
387 }
389 if(config->nobuffer) {
390 /* output buffering disabled */
391 int res;
392 do {
393 res = fflush(outs->stream);
394 /* Keep retrying in the hope that it is not interrupted sometime */
395 /* !checksrc! disable ERRNOVAR 1 */
396 } while(res && errno == EINTR);
397 if(res)
398 return CURL_WRITEFUNC_ERROR;
399 }
401 return rc;
402}
Diagnostics4 git calls · 33 ms
CallStatusSizeTime033 ms
discover200152 B3 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=2 git.response.bytes=152 git.server.agent=gitty/1.0 git.server.object_format=sha1 git.server.caps=agent ls-refs fetch object-format git.server.fetch=shallow filter sideband-all git.protocol.version=2 git.pkt.frames=8 git.pkt.data=6 git.pkt.flush=2 git.pkt.delim=0 git.pkt.bytes=152 git.pkt.payload.max=34
ls-refs200149 B · hit3 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=3 git.response.bytes=149 git.response.cache=HIT git.response.duration_ms=3 git.protocol.version=2 git.pkt.frames=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=149 git.pkt.payload.max=78 git.ls_refs.count=2 git.ls_refs.head=refs/heads/master
fetchblob:none · deepen 10200287 KiB · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=28 git.response.bytes=293470 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=blob:none git.fetch.filter.requested=tree:0 git.fetch.filter.fallback=blob:none git.fetch.filter.learned=false git.fetch.wants=31c911efe5652725e930f4d2e60c9060e5f6646d git.fetch.haves.count=0 git.fetch.deepen=10 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=10 git.pkt.data=8 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=293470 git.pkt.payload.max=65516 git.sideband.band1.bytes=293354 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=87 git.pack.bytes=293354 git.pack.ofs_delta=0 git.pack.ref_delta=1 git.pack.chain.max=1 git.pack.trailer=6a646bcda6740bab42e321b51998b52ea15146c0 git.pack.full=86Open pack map
fetch2004.3 KiB · hit28 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=4389 git.response.cache=HIT git.response.duration_ms=28 git.fetch.wants=2a17df0745d30d290db1f15af7de6e9cde48a8f5 git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=4389 git.pkt.payload.max=4368 git.sideband.band1.bytes=4367 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0
Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.
- Remote
- https://5ad2236b39de4c8bd8f6270ea63f3ea9.artifacts.cloudflare.net/git/public-v0/curl.git
- Ref
- refs/heads/master
- Commit
- 31c911efe5652725e930f4d2e60c9060e5f6646d
- cf-ray
- a5e148dec9bfa140
- Request
- b60bd957…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format