Debian Codegraph @

deps/curl/lib/file.c

raw
1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2020, Daniel Stenberg, <[email protected]>, 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.haxx.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 ***************************************************************************/
22
23 #include "curl_setup.h"
24
25 #ifndef CURL_DISABLE_FILE
26
27 #ifdef HAVE_NETINET_IN_H
28 #include <netinet/in.h>
29 #endif
30 #ifdef HAVE_NETDB_H
31 #include <netdb.h>
32 #endif
33 #ifdef HAVE_ARPA_INET_H
34 #include <arpa/inet.h>
35 #endif
36 #ifdef HAVE_NET_IF_H
37 #include <net/if.h>
38 #endif
39 #ifdef HAVE_SYS_IOCTL_H
40 #include <sys/ioctl.h>
41 #endif
42
43 #ifdef HAVE_SYS_PARAM_H
44 #include <sys/param.h>
45 #endif
46
47 #ifdef HAVE_FCNTL_H
48 #include <fcntl.h>
49 #endif
50
51 #include "strtoofft.h"
52 #include "urldata.h"
53 #include <curl/curl.h>
54 #include "progress.h"
55 #include "sendf.h"
56 #include "escape.h"
57 #include "file.h"
58 #include "speedcheck.h"
59 #include "getinfo.h"
60 #include "transfer.h"
61 #include "url.h"
62 #include "parsedate.h" /* for the week day and month names */
63 #include "warnless.h"
64 #include "curl_range.h"
65 /* The last 3 #include files should be in this order */
66 #include "curl_printf.h"
67 #include "curl_memory.h"
68 #include "memdebug.h"
69
70 #if defined(WIN32) || defined(MSDOS) || defined(__EMX__) || \
71 defined(__SYMBIAN32__)
72 #define DOS_FILESYSTEM 1
73 #endif
74
75 #ifdef OPEN_NEEDS_ARG3
76 # define open_readonly(p,f) open((p),(f),(0))
77 #else
78 # define open_readonly(p,f) open((p),(f))
79 #endif
80
81 /*
82 * Forward declarations.
83 */
84
85 static CURLcode file_do(struct connectdata *, bool *done);
86 static CURLcode file_done(struct connectdata *conn,
87 CURLcode status, bool premature);
88 static CURLcode file_connect(struct connectdata *conn, bool *done);
89 static CURLcode file_disconnect(struct connectdata *conn,
90 bool dead_connection);
91 static CURLcode file_setup_connection(struct connectdata *conn);
92
93 /*
94 * FILE scheme handler.
95 */
96
97 const struct Curl_handler Curl_handler_file = {
98 "FILE", /* scheme */
99 file_setup_connection, /* setup_connection */
100 file_do, /* do_it */
101 file_done, /* done */
102 ZERO_NULL, /* do_more */
103 file_connect, /* connect_it */
104 ZERO_NULL, /* connecting */
105 ZERO_NULL, /* doing */
106 ZERO_NULL, /* proto_getsock */
107 ZERO_NULL, /* doing_getsock */
108 ZERO_NULL, /* domore_getsock */
109 ZERO_NULL, /* perform_getsock */
110 file_disconnect, /* disconnect */
111 ZERO_NULL, /* readwrite */
112 ZERO_NULL, /* connection_check */
113 0, /* defport */
114 CURLPROTO_FILE, /* protocol */
115 PROTOPT_NONETWORK | PROTOPT_NOURLQUERY /* flags */
116 };
117
118
119 static CURLcode file_setup_connection(struct connectdata *conn)
120 {
121 /* allocate the FILE specific struct */
122 conn->data->req.protop = calloc(1, sizeof(struct FILEPROTO));
123 if(!conn->data->req.protop)
124 return CURLE_OUT_OF_MEMORY;
125
126 return CURLE_OK;
127 }
128
129 /*
130 * file_connect() gets called from Curl_protocol_connect() to allow us to
131 * do protocol-specific actions at connect-time. We emulate a
132 * connect-then-transfer protocol and "connect" to the file here
133 */
134 static CURLcode file_connect(struct connectdata *conn, bool *done)
135 {
136 struct Curl_easy *data = conn->data;
137 char *real_path;
138 struct FILEPROTO *file = data->req.protop;
139 int fd = -1;
140 #ifdef DOS_FILESYSTEM
141 size_t i;
142 char *actual_path;
143 #endif
144 size_t real_path_len;
145
146 CURLcode result = Curl_urldecode(data, data->state.up.path, 0, &real_path,
147 &real_path_len, FALSE);
148 if(result)
149 return result;
150
151 #ifdef DOS_FILESYSTEM
152 /* If the first character is a slash, and there's
153 something that looks like a drive at the beginning of
154 the path, skip the slash. If we remove the initial
155 slash in all cases, paths without drive letters end up
156 relative to the current directory which isn't how
157 browsers work.
158
159 Some browsers accept | instead of : as the drive letter
160 separator, so we do too.
161
162 On other platforms, we need the slash to indicate an
163 absolute pathname. On Windows, absolute paths start
164 with a drive letter.
165 */
166 actual_path = real_path;
167 if((actual_path[0] == '/') &&
168 actual_path[1] &&
169 (actual_path[2] == ':' || actual_path[2] == '|')) {
170 actual_path[2] = ':';
171 actual_path++;
172 real_path_len--;
173 }
174
175 /* change path separators from '/' to '\\' for DOS, Windows and OS/2 */
176 for(i = 0; i < real_path_len; ++i)
177 if(actual_path[i] == '/')
178 actual_path[i] = '\\';
179 else if(!actual_path[i]) { /* binary zero */
180 Curl_safefree(real_path);
181 return CURLE_URL_MALFORMAT;
182 }
183
184 if(strncmp("\\\\", actual_path, 2))
185 /* refuse to open path that starts with two backslashes */
186 fd = open_readonly(actual_path, O_RDONLY|O_BINARY);
187 file->path = actual_path;
188 #else
189 if(memchr(real_path, 0, real_path_len)) {
190 /* binary zeroes indicate foul play */
191 Curl_safefree(real_path);
192 return CURLE_URL_MALFORMAT;
193 }
194
195 fd = open_readonly(real_path, O_RDONLY);
196 file->path = real_path;
197 #endif
198 file->freepath = real_path; /* free this when done */
199
200 file->fd = fd;
201 if(!data->set.upload && (fd == -1)) {
202 failf(data, "Couldn't open file %s", data->state.up.path);
203 file_done(conn, CURLE_FILE_COULDNT_READ_FILE, FALSE);
204 return CURLE_FILE_COULDNT_READ_FILE;
205 }
206 *done = TRUE;
207
208 return CURLE_OK;
209 }
210
211 static CURLcode file_done(struct connectdata *conn,
212 CURLcode status, bool premature)
213 {
214 struct FILEPROTO *file = conn->data->req.protop;
215 (void)status; /* not used */
216 (void)premature; /* not used */
217
218 if(file) {
219 Curl_safefree(file->freepath);
220 file->path = NULL;
221 if(file->fd != -1)
222 close(file->fd);
223 file->fd = -1;
224 }
225
226 return CURLE_OK;
227 }
228
229 static CURLcode file_disconnect(struct connectdata *conn,
230 bool dead_connection)
231 {
232 struct FILEPROTO *file = conn->data->req.protop;
233 (void)dead_connection; /* not used */
234
235 if(file) {
236 Curl_safefree(file->freepath);
237 file->path = NULL;
238 if(file->fd != -1)
239 close(file->fd);
240 file->fd = -1;
241 }
242
243 return CURLE_OK;
244 }
245
246 #ifdef DOS_FILESYSTEM
247 #define DIRSEP '\\'
248 #else
249 #define DIRSEP '/'
250 #endif
251
252 static CURLcode file_upload(struct connectdata *conn)
253 {
254 struct FILEPROTO *file = conn->data->req.protop;
255 const char *dir = strchr(file->path, DIRSEP);
256 int fd;
257 int mode;
258 CURLcode result = CURLE_OK;
259 struct Curl_easy *data = conn->data;
260 char *buf = data->state.buffer;
261 curl_off_t bytecount = 0;
262 struct_stat file_stat;
263 const char *buf2;
264
265 /*
266 * Since FILE: doesn't do the full init, we need to provide some extra
267 * assignments here.
268 */
269 conn->data->req.upload_fromhere = buf;
270
271 if(!dir)
272 return CURLE_FILE_COULDNT_READ_FILE; /* fix: better error code */
273
274 if(!dir[1])
275 return CURLE_FILE_COULDNT_READ_FILE; /* fix: better error code */
276
277 #ifdef O_BINARY
278 #define MODE_DEFAULT O_WRONLY|O_CREAT|O_BINARY
279 #else
280 #define MODE_DEFAULT O_WRONLY|O_CREAT
281 #endif
282
283 if(data->state.resume_from)
284 mode = MODE_DEFAULT|O_APPEND;
285 else
286 mode = MODE_DEFAULT|O_TRUNC;
287
288 fd = open(file->path, mode, conn->data->set.new_file_perms);
289 if(fd < 0) {
290 failf(data, "Can't open %s for writing", file->path);
291 return CURLE_WRITE_ERROR;
292 }
293
294 if(-1 != data->state.infilesize)
295 /* known size of data to "upload" */
296 Curl_pgrsSetUploadSize(data, data->state.infilesize);
297
298 /* treat the negative resume offset value as the case of "-" */
299 if(data->state.resume_from < 0) {
300 if(fstat(fd, &file_stat)) {
301 close(fd);
302 failf(data, "Can't get the size of %s", file->path);
303 return CURLE_WRITE_ERROR;
304 }
305 data->state.resume_from = (curl_off_t)file_stat.st_size;
306 }
307
308 while(!result) {
309 size_t nread;
310 size_t nwrite;
311 size_t readcount;
312 result = Curl_fillreadbuffer(conn, data->set.buffer_size, &readcount);
313 if(result)
314 break;
315
316 if(!readcount)
317 break;
318
319 nread = readcount;
320
321 /*skip bytes before resume point*/
322 if(data->state.resume_from) {
323 if((curl_off_t)nread <= data->state.resume_from) {
324 data->state.resume_from -= nread;
325 nread = 0;
326 buf2 = buf;
327 }
328 else {
329 buf2 = buf + data->state.resume_from;
330 nread -= (size_t)data->state.resume_from;
331 data->state.resume_from = 0;
332 }
333 }
334 else
335 buf2 = buf;
336
337 /* write the data to the target */
338 nwrite = write(fd, buf2, nread);
339 if(nwrite != nread) {
340 result = CURLE_SEND_ERROR;
341 break;
342 }
343
344 bytecount += nread;
345
346 Curl_pgrsSetUploadCounter(data, bytecount);
347
348 if(Curl_pgrsUpdate(conn))
349 result = CURLE_ABORTED_BY_CALLBACK;
350 else
351 result = Curl_speedcheck(data, Curl_now());
352 }
353 if(!result && Curl_pgrsUpdate(conn))
354 result = CURLE_ABORTED_BY_CALLBACK;
355
356 close(fd);
357
358 return result;
359 }
360
361 /*
362 * file_do() is the protocol-specific function for the do-phase, separated
363 * from the connect-phase above. Other protocols merely setup the transfer in
364 * the do-phase, to have it done in the main transfer loop but since some
365 * platforms we support don't allow select()ing etc on file handles (as
366 * opposed to sockets) we instead perform the whole do-operation in this
367 * function.
368 */
369 static CURLcode file_do(struct connectdata *conn, bool *done)
370 {
371 /* This implementation ignores the host name in conformance with
372 RFC 1738. Only local files (reachable via the standard file system)
373 are supported. This means that files on remotely mounted directories
374 (via NFS, Samba, NT sharing) can be accessed through a file:// URL
375 */
376 CURLcode result = CURLE_OK;
377 struct_stat statbuf; /* struct_stat instead of struct stat just to allow the
378 Windows version to have a different struct without
379 having to redefine the simple word 'stat' */
380 curl_off_t expected_size = 0;
381 bool size_known;
382 bool fstated = FALSE;
383 struct Curl_easy *data = conn->data;
384 char *buf = data->state.buffer;
385 curl_off_t bytecount = 0;
386 int fd;
387 struct FILEPROTO *file;
388
389 *done = TRUE; /* unconditionally */
390
391 Curl_pgrsStartNow(data);
392
393 if(data->set.upload)
394 return file_upload(conn);
395
396 file = conn->data->req.protop;
397
398 /* get the fd from the connection phase */
399 fd = file->fd;
400
401 /* VMS: This only works reliable for STREAMLF files */
402 if(-1 != fstat(fd, &statbuf)) {
403 /* we could stat it, then read out the size */
404 expected_size = statbuf.st_size;
405 /* and store the modification time */
406 data->info.filetime = statbuf.st_mtime;
407 fstated = TRUE;
408 }
409
410 if(fstated && !data->state.range && data->set.timecondition) {
411 if(!Curl_meets_timecondition(data, data->info.filetime)) {
412 *done = TRUE;
413 return CURLE_OK;
414 }
415 }
416
417 if(fstated) {
418 time_t filetime;
419 struct tm buffer;
420 const struct tm *tm = &buffer;
421 char header[80];
422 msnprintf(header, sizeof(header),
423 "Content-Length: %" CURL_FORMAT_CURL_OFF_T "\r\n",
424 expected_size);
425 result = Curl_client_write(conn, CLIENTWRITE_HEADER, header, 0);
426 if(result)
427 return result;
428
429 result = Curl_client_write(conn, CLIENTWRITE_HEADER,
430 (char *)"Accept-ranges: bytes\r\n", 0);
431 if(result)
432 return result;
433
434 filetime = (time_t)statbuf.st_mtime;
435 result = Curl_gmtime(filetime, &buffer);
436 if(result)
437 return result;
438
439 /* format: "Tue, 15 Nov 1994 12:45:26 GMT" */
440 msnprintf(header, sizeof(header),
441 "Last-Modified: %s, %02d %s %4d %02d:%02d:%02d GMT\r\n%s",
442 Curl_wkday[tm->tm_wday?tm->tm_wday-1:6],
443 tm->tm_mday,
444 Curl_month[tm->tm_mon],
445 tm->tm_year + 1900,
446 tm->tm_hour,
447 tm->tm_min,
448 tm->tm_sec,
449 data->set.opt_no_body ? "": "\r\n");
450 result = Curl_client_write(conn, CLIENTWRITE_HEADER, header, 0);
451 if(result)
452 return result;
453 /* set the file size to make it available post transfer */
454 Curl_pgrsSetDownloadSize(data, expected_size);
455 if(data->set.opt_no_body)
456 return result;
457 }
458
459 /* Check whether file range has been specified */
460 result = Curl_range(conn);
461 if(result)
462 return result;
463
464 /* Adjust the start offset in case we want to get the N last bytes
465 * of the stream if the filesize could be determined */
466 if(data->state.resume_from < 0) {
467 if(!fstated) {
468 failf(data, "Can't get the size of file.");
469 return CURLE_READ_ERROR;
470 }
471 data->state.resume_from += (curl_off_t)statbuf.st_size;
472 }
473
474 if(data->state.resume_from <= expected_size)
475 expected_size -= data->state.resume_from;
476 else {
477 failf(data, "failed to resume file:// transfer");
478 return CURLE_BAD_DOWNLOAD_RESUME;
479 }
480
481 /* A high water mark has been specified so we obey... */
482 if(data->req.maxdownload > 0)
483 expected_size = data->req.maxdownload;
484
485 if(!fstated || (expected_size == 0))
486 size_known = FALSE;
487 else
488 size_known = TRUE;
489
490 /* The following is a shortcut implementation of file reading
491 this is both more efficient than the former call to download() and
492 it avoids problems with select() and recv() on file descriptors
493 in Winsock */
494 if(fstated)
495 Curl_pgrsSetDownloadSize(data, expected_size);
496
497 if(data->state.resume_from) {
498 if(data->state.resume_from !=
499 lseek(fd, data->state.resume_from, SEEK_SET))
500 return CURLE_BAD_DOWNLOAD_RESUME;
501 }
502
503 Curl_pgrsTime(data, TIMER_STARTTRANSFER);
504
505 while(!result) {
506 ssize_t nread;
507 /* Don't fill a whole buffer if we want less than all data */
508 size_t bytestoread;
509
510 if(size_known) {
511 bytestoread = (expected_size < data->set.buffer_size) ?
512 curlx_sotouz(expected_size) : (size_t)data->set.buffer_size;
513 }
514 else
515 bytestoread = data->set.buffer_size-1;
516
517 nread = read(fd, buf, bytestoread);
518
519 if(nread > 0)
520 buf[nread] = 0;
521
522 if(nread <= 0 || (size_known && (expected_size == 0)))
523 break;
524
525 bytecount += nread;
526 if(size_known)
527 expected_size -= nread;
528
529 result = Curl_client_write(conn, CLIENTWRITE_BODY, buf, nread);
530 if(result)
531 return result;
532
533 Curl_pgrsSetDownloadCounter(data, bytecount);
534
535 if(Curl_pgrsUpdate(conn))
536 result = CURLE_ABORTED_BY_CALLBACK;
537 else
538 result = Curl_speedcheck(data, Curl_now());
539 }
540 if(Curl_pgrsUpdate(conn))
541 result = CURLE_ABORTED_BY_CALLBACK;
542
543 return result;
544 }
545
546 #endif