file.c 17 KB

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