1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2014, Daniel Stenberg, <daniel@haxx.se>, et al.
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 http://curl.haxx.se/docs/copyright.html.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
23 #include "curl_setup.h"
25 #ifndef CURL_DISABLE_FILE
27 #ifdef HAVE_NETINET_IN_H
28 #include <netinet/in.h>
33 #ifdef HAVE_ARPA_INET_H
34 #include <arpa/inet.h>
39 #ifdef HAVE_SYS_IOCTL_H
40 #include <sys/ioctl.h>
43 #ifdef HAVE_SYS_PARAM_H
44 #include <sys/param.h>
51 #include "strtoofft.h"
53 #include <curl/curl.h>
58 #include "speedcheck.h"
62 #include "curl_memory.h"
63 #include "parsedate.h" /* for the week day and month names */
66 #define _MPRINTF_REPLACE /* use our functions only */
67 #include <curl/mprintf.h>
69 /* The last #include file should be: */
72 #if defined(WIN32) || defined(MSDOS) || defined(__EMX__) || \
73 defined(__SYMBIAN32__)
74 #define DOS_FILESYSTEM 1
77 #ifdef OPEN_NEEDS_ARG3
78 # define open_readonly(p,f) open((p),(f),(0))
80 # define open_readonly(p,f) open((p),(f))
84 * Forward declarations.
87 static CURLcode file_do(struct connectdata *, bool *done);
88 static CURLcode file_done(struct connectdata *conn,
89 CURLcode status, bool premature);
90 static CURLcode file_connect(struct connectdata *conn, bool *done);
91 static CURLcode file_disconnect(struct connectdata *conn,
92 bool dead_connection);
93 static CURLcode file_setup_connection(struct connectdata *conn);
96 * FILE scheme handler.
99 const struct Curl_handler Curl_handler_file = {
101 file_setup_connection, /* setup_connection */
103 file_done, /* done */
104 ZERO_NULL, /* do_more */
105 file_connect, /* connect_it */
106 ZERO_NULL, /* connecting */
107 ZERO_NULL, /* doing */
108 ZERO_NULL, /* proto_getsock */
109 ZERO_NULL, /* doing_getsock */
110 ZERO_NULL, /* domore_getsock */
111 ZERO_NULL, /* perform_getsock */
112 file_disconnect, /* disconnect */
113 ZERO_NULL, /* readwrite */
115 CURLPROTO_FILE, /* protocol */
116 PROTOPT_NONETWORK | PROTOPT_NOURLQUERY /* flags */
120 static CURLcode file_setup_connection(struct connectdata *conn)
122 /* allocate the FILE specific struct */
123 conn->data->req.protop = calloc(1, sizeof(struct FILEPROTO));
124 if(!conn->data->req.protop)
125 return CURLE_OUT_OF_MEMORY;
131 Check if this is a range download, and if so, set the internal variables
132 properly. This code is copied from the FTP implementation and might as
133 well be factored out.
135 static CURLcode file_range(struct connectdata *conn)
138 curl_off_t totalsize=-1;
141 struct SessionHandle *data = conn->data;
143 if(data->state.use_range && data->state.range) {
144 from=curlx_strtoofft(data->state.range, &ptr, 0);
145 while(*ptr && (ISSPACE(*ptr) || (*ptr=='-')))
147 to=curlx_strtoofft(ptr, &ptr2, 0);
149 /* we didn't get any digit */
152 if((-1 == to) && (from>=0)) {
154 data->state.resume_from = from;
155 DEBUGF(infof(data, "RANGE %" CURL_FORMAT_CURL_OFF_T " to end of file\n",
160 data->req.maxdownload = -from;
161 data->state.resume_from = from;
162 DEBUGF(infof(data, "RANGE the last %" CURL_FORMAT_CURL_OFF_T " bytes\n",
168 data->req.maxdownload = totalsize+1; /* include last byte */
169 data->state.resume_from = from;
170 DEBUGF(infof(data, "RANGE from %" CURL_FORMAT_CURL_OFF_T
171 " getting %" CURL_FORMAT_CURL_OFF_T " bytes\n",
172 from, data->req.maxdownload));
174 DEBUGF(infof(data, "range-download from %" CURL_FORMAT_CURL_OFF_T
175 " to %" CURL_FORMAT_CURL_OFF_T ", totally %"
176 CURL_FORMAT_CURL_OFF_T " bytes\n",
177 from, to, data->req.maxdownload));
180 data->req.maxdownload = -1;
185 * file_connect() gets called from Curl_protocol_connect() to allow us to
186 * do protocol-specific actions at connect-time. We emulate a
187 * connect-then-transfer protocol and "connect" to the file here
189 static CURLcode file_connect(struct connectdata *conn, bool *done)
191 struct SessionHandle *data = conn->data;
193 struct FILEPROTO *file = data->req.protop;
195 #ifdef DOS_FILESYSTEM
201 real_path = curl_easy_unescape(data, data->state.path, 0, &real_path_len);
203 return CURLE_OUT_OF_MEMORY;
205 #ifdef DOS_FILESYSTEM
206 /* If the first character is a slash, and there's
207 something that looks like a drive at the beginning of
208 the path, skip the slash. If we remove the initial
209 slash in all cases, paths without drive letters end up
210 relative to the current directory which isn't how
213 Some browsers accept | instead of : as the drive letter
214 separator, so we do too.
216 On other platforms, we need the slash to indicate an
217 absolute pathname. On Windows, absolute paths start
220 actual_path = real_path;
221 if((actual_path[0] == '/') &&
223 (actual_path[2] == ':' || actual_path[2] == '|')) {
224 actual_path[2] = ':';
229 /* change path separators from '/' to '\\' for DOS, Windows and OS/2 */
230 for(i=0; i < real_path_len; ++i)
231 if(actual_path[i] == '/')
232 actual_path[i] = '\\';
233 else if(!actual_path[i]) /* binary zero */
234 return CURLE_URL_MALFORMAT;
236 fd = open_readonly(actual_path, O_RDONLY|O_BINARY);
237 file->path = actual_path;
239 if(memchr(real_path, 0, real_path_len))
240 /* binary zeroes indicate foul play */
241 return CURLE_URL_MALFORMAT;
243 fd = open_readonly(real_path, O_RDONLY);
244 file->path = real_path;
246 file->freepath = real_path; /* free this when done */
249 if(!data->set.upload && (fd == -1)) {
250 failf(data, "Couldn't open file %s", data->state.path);
251 file_done(conn, CURLE_FILE_COULDNT_READ_FILE, FALSE);
252 return CURLE_FILE_COULDNT_READ_FILE;
259 static CURLcode file_done(struct connectdata *conn,
260 CURLcode status, bool premature)
262 struct FILEPROTO *file = conn->data->req.protop;
263 (void)status; /* not used */
264 (void)premature; /* not used */
267 Curl_safefree(file->freepath);
277 static CURLcode file_disconnect(struct connectdata *conn,
278 bool dead_connection)
280 struct FILEPROTO *file = conn->data->req.protop;
281 (void)dead_connection; /* not used */
284 Curl_safefree(file->freepath);
294 #ifdef DOS_FILESYSTEM
300 static CURLcode file_upload(struct connectdata *conn)
302 struct FILEPROTO *file = conn->data->req.protop;
303 const char *dir = strchr(file->path, DIRSEP);
306 CURLcode result = CURLE_OK;
307 struct SessionHandle *data = conn->data;
308 char *buf = data->state.buffer;
311 curl_off_t bytecount = 0;
312 struct timeval now = Curl_tvnow();
313 struct_stat file_stat;
317 * Since FILE: doesn't do the full init, we need to provide some extra
320 conn->fread_func = data->set.fread_func;
321 conn->fread_in = data->set.in;
322 conn->data->req.upload_fromhere = buf;
325 return CURLE_FILE_COULDNT_READ_FILE; /* fix: better error code */
328 return CURLE_FILE_COULDNT_READ_FILE; /* fix: better error code */
331 #define MODE_DEFAULT O_WRONLY|O_CREAT|O_BINARY
333 #define MODE_DEFAULT O_WRONLY|O_CREAT
336 if(data->state.resume_from)
337 mode = MODE_DEFAULT|O_APPEND;
339 mode = MODE_DEFAULT|O_TRUNC;
341 fd = open(file->path, mode, conn->data->set.new_file_perms);
343 failf(data, "Can't open %s for writing", file->path);
344 return CURLE_WRITE_ERROR;
347 if(-1 != data->state.infilesize)
348 /* known size of data to "upload" */
349 Curl_pgrsSetUploadSize(data, data->state.infilesize);
351 /* treat the negative resume offset value as the case of "-" */
352 if(data->state.resume_from < 0) {
353 if(fstat(fd, &file_stat)) {
355 failf(data, "Can't get the size of %s", file->path);
356 return CURLE_WRITE_ERROR;
359 data->state.resume_from = (curl_off_t)file_stat.st_size;
364 result = Curl_fillreadbuffer(conn, BUFSIZE, &readcount);
368 if(readcount <= 0) /* fix questionable compare error. curlvms */
371 nread = (size_t)readcount;
373 /*skip bytes before resume point*/
374 if(data->state.resume_from) {
375 if((curl_off_t)nread <= data->state.resume_from ) {
376 data->state.resume_from -= nread;
381 buf2 = buf + data->state.resume_from;
382 nread -= (size_t)data->state.resume_from;
383 data->state.resume_from = 0;
389 /* write the data to the target */
390 nwrite = write(fd, buf2, nread);
391 if(nwrite != nread) {
392 result = CURLE_SEND_ERROR;
398 Curl_pgrsSetUploadCounter(data, bytecount);
400 if(Curl_pgrsUpdate(conn))
401 result = CURLE_ABORTED_BY_CALLBACK;
403 result = Curl_speedcheck(data, now);
405 if(!result && Curl_pgrsUpdate(conn))
406 result = CURLE_ABORTED_BY_CALLBACK;
414 * file_do() is the protocol-specific function for the do-phase, separated
415 * from the connect-phase above. Other protocols merely setup the transfer in
416 * the do-phase, to have it done in the main transfer loop but since some
417 * platforms we support don't allow select()ing etc on file handles (as
418 * opposed to sockets) we instead perform the whole do-operation in this
421 static CURLcode file_do(struct connectdata *conn, bool *done)
423 /* This implementation ignores the host name in conformance with
424 RFC 1738. Only local files (reachable via the standard file system)
425 are supported. This means that files on remotely mounted directories
426 (via NFS, Samba, NT sharing) can be accessed through a file:// URL
428 CURLcode result = CURLE_OK;
429 struct_stat statbuf; /* struct_stat instead of struct stat just to allow the
430 Windows version to have a different struct without
431 having to redefine the simple word 'stat' */
432 curl_off_t expected_size=0;
435 struct SessionHandle *data = conn->data;
436 char *buf = data->state.buffer;
437 curl_off_t bytecount = 0;
439 struct timeval now = Curl_tvnow();
440 struct FILEPROTO *file;
442 *done = TRUE; /* unconditionally */
445 Curl_pgrsStartNow(data);
448 return file_upload(conn);
450 file = conn->data->req.protop;
452 /* get the fd from the connection phase */
455 /* VMS: This only works reliable for STREAMLF files */
456 if(-1 != fstat(fd, &statbuf)) {
457 /* we could stat it, then read out the size */
458 expected_size = statbuf.st_size;
459 /* and store the modification time */
460 data->info.filetime = (long)statbuf.st_mtime;
464 if(fstated && !data->state.range && data->set.timecondition) {
465 if(!Curl_meets_timecondition(data, (time_t)data->info.filetime)) {
471 /* If we have selected NOBODY and HEADER, it means that we only want file
472 information. Which for FILE can't be much more than the file size and
474 if(data->set.opt_no_body && data->set.include_header && fstated) {
475 snprintf(buf, sizeof(data->state.buffer),
476 "Content-Length: %" CURL_FORMAT_CURL_OFF_T "\r\n", expected_size);
477 result = Curl_client_write(conn, CLIENTWRITE_BOTH, buf, 0);
481 result = Curl_client_write(conn, CLIENTWRITE_BOTH,
482 (char *)"Accept-ranges: bytes\r\n", 0);
487 time_t filetime = (time_t)statbuf.st_mtime;
489 const struct tm *tm = &buffer;
490 result = Curl_gmtime(filetime, &buffer);
494 /* format: "Tue, 15 Nov 1994 12:45:26 GMT" */
495 snprintf(buf, BUFSIZE-1,
496 "Last-Modified: %s, %02d %s %4d %02d:%02d:%02d GMT\r\n",
497 Curl_wkday[tm->tm_wday?tm->tm_wday-1:6],
499 Curl_month[tm->tm_mon],
504 result = Curl_client_write(conn, CLIENTWRITE_BOTH, buf, 0);
506 /* if we fstat()ed the file, set the file size to make it available post-
509 Curl_pgrsSetDownloadSize(data, expected_size);
513 /* Check whether file range has been specified */
516 /* Adjust the start offset in case we want to get the N last bytes
517 * of the stream iff the filesize could be determined */
518 if(data->state.resume_from < 0) {
520 failf(data, "Can't get the size of file.");
521 return CURLE_READ_ERROR;
524 data->state.resume_from += (curl_off_t)statbuf.st_size;
527 if(data->state.resume_from <= expected_size)
528 expected_size -= data->state.resume_from;
530 failf(data, "failed to resume file:// transfer");
531 return CURLE_BAD_DOWNLOAD_RESUME;
534 /* A high water mark has been specified so we obey... */
535 if(data->req.maxdownload > 0)
536 expected_size = data->req.maxdownload;
538 if(fstated && (expected_size == 0))
541 /* The following is a shortcut implementation of file reading
542 this is both more efficient than the former call to download() and
543 it avoids problems with select() and recv() on file descriptors
546 Curl_pgrsSetDownloadSize(data, expected_size);
548 if(data->state.resume_from) {
549 if(data->state.resume_from !=
550 lseek(fd, data->state.resume_from, SEEK_SET))
551 return CURLE_BAD_DOWNLOAD_RESUME;
554 Curl_pgrsTime(data, TIMER_STARTTRANSFER);
557 /* Don't fill a whole buffer if we want less than all data */
559 (expected_size < CURL_OFF_T_C(BUFSIZE) - CURL_OFF_T_C(1)) ?
560 curlx_sotouz(expected_size) : BUFSIZE - 1;
562 nread = read(fd, buf, bytestoread);
567 if(nread <= 0 || expected_size == 0)
571 expected_size -= nread;
573 result = Curl_client_write(conn, CLIENTWRITE_BODY, buf, nread);
577 Curl_pgrsSetDownloadCounter(data, bytecount);
579 if(Curl_pgrsUpdate(conn))
580 result = CURLE_ABORTED_BY_CALLBACK;
582 result = Curl_speedcheck(data, now);
584 if(Curl_pgrsUpdate(conn))
585 result = CURLE_ABORTED_BY_CALLBACK;