file.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2016, 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. #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. 0, /* defport */
  100. CURLPROTO_FILE, /* protocol */
  101. PROTOPT_NONETWORK | PROTOPT_NOURLQUERY /* flags */
  102. };
  103. static CURLcode file_setup_connection(struct connectdata *conn)
  104. {
  105. /* allocate the FILE specific struct */
  106. conn->data->req.protop = calloc(1, sizeof(struct FILEPROTO));
  107. if(!conn->data->req.protop)
  108. return CURLE_OUT_OF_MEMORY;
  109. return CURLE_OK;
  110. }
  111. /*
  112. Check if this is a range download, and if so, set the internal variables
  113. properly. This code is copied from the FTP implementation and might as
  114. well be factored out.
  115. */
  116. static CURLcode file_range(struct connectdata *conn)
  117. {
  118. curl_off_t from, to;
  119. curl_off_t totalsize=-1;
  120. char *ptr;
  121. char *ptr2;
  122. struct Curl_easy *data = conn->data;
  123. if(data->state.use_range && data->state.range) {
  124. from=curlx_strtoofft(data->state.range, &ptr, 0);
  125. while(*ptr && (ISSPACE(*ptr) || (*ptr=='-')))
  126. ptr++;
  127. to=curlx_strtoofft(ptr, &ptr2, 0);
  128. if(ptr == ptr2) {
  129. /* we didn't get any digit */
  130. to=-1;
  131. }
  132. if((-1 == to) && (from>=0)) {
  133. /* X - */
  134. data->state.resume_from = from;
  135. DEBUGF(infof(data, "RANGE %" CURL_FORMAT_CURL_OFF_T " to end of file\n",
  136. from));
  137. }
  138. else if(from < 0) {
  139. /* -Y */
  140. data->req.maxdownload = -from;
  141. data->state.resume_from = from;
  142. DEBUGF(infof(data, "RANGE the last %" CURL_FORMAT_CURL_OFF_T " bytes\n",
  143. -from));
  144. }
  145. else {
  146. /* X-Y */
  147. totalsize = to-from;
  148. data->req.maxdownload = totalsize+1; /* include last byte */
  149. data->state.resume_from = from;
  150. DEBUGF(infof(data, "RANGE from %" CURL_FORMAT_CURL_OFF_T
  151. " getting %" CURL_FORMAT_CURL_OFF_T " bytes\n",
  152. from, data->req.maxdownload));
  153. }
  154. DEBUGF(infof(data, "range-download from %" CURL_FORMAT_CURL_OFF_T
  155. " to %" CURL_FORMAT_CURL_OFF_T ", totally %"
  156. CURL_FORMAT_CURL_OFF_T " bytes\n",
  157. from, to, data->req.maxdownload));
  158. }
  159. else
  160. data->req.maxdownload = -1;
  161. return CURLE_OK;
  162. }
  163. /*
  164. * file_connect() gets called from Curl_protocol_connect() to allow us to
  165. * do protocol-specific actions at connect-time. We emulate a
  166. * connect-then-transfer protocol and "connect" to the file here
  167. */
  168. static CURLcode file_connect(struct connectdata *conn, bool *done)
  169. {
  170. struct Curl_easy *data = conn->data;
  171. char *real_path;
  172. struct FILEPROTO *file = data->req.protop;
  173. int fd;
  174. #ifdef DOS_FILESYSTEM
  175. size_t i;
  176. char *actual_path;
  177. #endif
  178. size_t real_path_len;
  179. CURLcode result = Curl_urldecode(data, data->state.path, 0, &real_path,
  180. &real_path_len, FALSE);
  181. if(result)
  182. return result;
  183. #ifdef DOS_FILESYSTEM
  184. /* If the first character is a slash, and there's
  185. something that looks like a drive at the beginning of
  186. the path, skip the slash. If we remove the initial
  187. slash in all cases, paths without drive letters end up
  188. relative to the current directory which isn't how
  189. browsers work.
  190. Some browsers accept | instead of : as the drive letter
  191. separator, so we do too.
  192. On other platforms, we need the slash to indicate an
  193. absolute pathname. On Windows, absolute paths start
  194. with a drive letter.
  195. */
  196. actual_path = real_path;
  197. if((actual_path[0] == '/') &&
  198. actual_path[1] &&
  199. (actual_path[2] == ':' || actual_path[2] == '|')) {
  200. actual_path[2] = ':';
  201. actual_path++;
  202. real_path_len--;
  203. }
  204. /* change path separators from '/' to '\\' for DOS, Windows and OS/2 */
  205. for(i=0; i < real_path_len; ++i)
  206. if(actual_path[i] == '/')
  207. actual_path[i] = '\\';
  208. else if(!actual_path[i]) { /* binary zero */
  209. Curl_safefree(real_path);
  210. return CURLE_URL_MALFORMAT;
  211. }
  212. fd = open_readonly(actual_path, O_RDONLY|O_BINARY);
  213. file->path = actual_path;
  214. #else
  215. if(memchr(real_path, 0, real_path_len)) {
  216. /* binary zeroes indicate foul play */
  217. Curl_safefree(real_path);
  218. return CURLE_URL_MALFORMAT;
  219. }
  220. fd = open_readonly(real_path, O_RDONLY);
  221. file->path = real_path;
  222. #endif
  223. file->freepath = real_path; /* free this when done */
  224. file->fd = fd;
  225. if(!data->set.upload && (fd == -1)) {
  226. failf(data, "Couldn't open file %s", data->state.path);
  227. file_done(conn, CURLE_FILE_COULDNT_READ_FILE, FALSE);
  228. return CURLE_FILE_COULDNT_READ_FILE;
  229. }
  230. *done = TRUE;
  231. return CURLE_OK;
  232. }
  233. static CURLcode file_done(struct connectdata *conn,
  234. CURLcode status, bool premature)
  235. {
  236. struct FILEPROTO *file = conn->data->req.protop;
  237. (void)status; /* not used */
  238. (void)premature; /* not used */
  239. if(file) {
  240. Curl_safefree(file->freepath);
  241. file->path = NULL;
  242. if(file->fd != -1)
  243. close(file->fd);
  244. file->fd = -1;
  245. }
  246. return CURLE_OK;
  247. }
  248. static CURLcode file_disconnect(struct connectdata *conn,
  249. bool dead_connection)
  250. {
  251. struct FILEPROTO *file = conn->data->req.protop;
  252. (void)dead_connection; /* not used */
  253. if(file) {
  254. Curl_safefree(file->freepath);
  255. file->path = NULL;
  256. if(file->fd != -1)
  257. close(file->fd);
  258. file->fd = -1;
  259. }
  260. return CURLE_OK;
  261. }
  262. #ifdef DOS_FILESYSTEM
  263. #define DIRSEP '\\'
  264. #else
  265. #define DIRSEP '/'
  266. #endif
  267. static CURLcode file_upload(struct connectdata *conn)
  268. {
  269. struct FILEPROTO *file = conn->data->req.protop;
  270. const char *dir = strchr(file->path, DIRSEP);
  271. int fd;
  272. int mode;
  273. CURLcode result = CURLE_OK;
  274. struct Curl_easy *data = conn->data;
  275. char *buf = data->state.buffer;
  276. size_t nread;
  277. size_t nwrite;
  278. curl_off_t bytecount = 0;
  279. struct timeval now = Curl_tvnow();
  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. else
  316. data->state.resume_from = (curl_off_t)file_stat.st_size;
  317. }
  318. while(!result) {
  319. int readcount;
  320. result = Curl_fillreadbuffer(conn, BUFSIZE, &readcount);
  321. if(result)
  322. break;
  323. if(readcount <= 0) /* fix questionable compare error. curlvms */
  324. break;
  325. nread = (size_t)readcount;
  326. /*skip bytes before resume point*/
  327. if(data->state.resume_from) {
  328. if((curl_off_t)nread <= data->state.resume_from) {
  329. data->state.resume_from -= nread;
  330. nread = 0;
  331. buf2 = buf;
  332. }
  333. else {
  334. buf2 = buf + data->state.resume_from;
  335. nread -= (size_t)data->state.resume_from;
  336. data->state.resume_from = 0;
  337. }
  338. }
  339. else
  340. buf2 = buf;
  341. /* write the data to the target */
  342. nwrite = write(fd, buf2, nread);
  343. if(nwrite != nread) {
  344. result = CURLE_SEND_ERROR;
  345. break;
  346. }
  347. bytecount += nread;
  348. Curl_pgrsSetUploadCounter(data, bytecount);
  349. if(Curl_pgrsUpdate(conn))
  350. result = CURLE_ABORTED_BY_CALLBACK;
  351. else
  352. result = Curl_speedcheck(data, now);
  353. }
  354. if(!result && Curl_pgrsUpdate(conn))
  355. result = CURLE_ABORTED_BY_CALLBACK;
  356. close(fd);
  357. return result;
  358. }
  359. /*
  360. * file_do() is the protocol-specific function for the do-phase, separated
  361. * from the connect-phase above. Other protocols merely setup the transfer in
  362. * the do-phase, to have it done in the main transfer loop but since some
  363. * platforms we support don't allow select()ing etc on file handles (as
  364. * opposed to sockets) we instead perform the whole do-operation in this
  365. * function.
  366. */
  367. static CURLcode file_do(struct connectdata *conn, bool *done)
  368. {
  369. /* This implementation ignores the host name in conformance with
  370. RFC 1738. Only local files (reachable via the standard file system)
  371. are supported. This means that files on remotely mounted directories
  372. (via NFS, Samba, NT sharing) can be accessed through a file:// URL
  373. */
  374. CURLcode result = CURLE_OK;
  375. struct_stat statbuf; /* struct_stat instead of struct stat just to allow the
  376. Windows version to have a different struct without
  377. having to redefine the simple word 'stat' */
  378. curl_off_t expected_size=0;
  379. bool size_known;
  380. bool fstated=FALSE;
  381. ssize_t nread;
  382. struct Curl_easy *data = conn->data;
  383. char *buf = data->state.buffer;
  384. curl_off_t bytecount = 0;
  385. int fd;
  386. struct timeval now = Curl_tvnow();
  387. struct FILEPROTO *file;
  388. *done = TRUE; /* unconditionally */
  389. Curl_initinfo(data);
  390. Curl_pgrsStartNow(data);
  391. if(data->set.upload)
  392. return file_upload(conn);
  393. file = conn->data->req.protop;
  394. /* get the fd from the connection phase */
  395. fd = file->fd;
  396. /* VMS: This only works reliable for STREAMLF files */
  397. if(-1 != fstat(fd, &statbuf)) {
  398. /* we could stat it, then read out the size */
  399. expected_size = statbuf.st_size;
  400. /* and store the modification time */
  401. data->info.filetime = (long)statbuf.st_mtime;
  402. fstated = TRUE;
  403. }
  404. if(fstated && !data->state.range && data->set.timecondition) {
  405. if(!Curl_meets_timecondition(data, (time_t)data->info.filetime)) {
  406. *done = TRUE;
  407. return CURLE_OK;
  408. }
  409. }
  410. /* If we have selected NOBODY and HEADER, it means that we only want file
  411. information. Which for FILE can't be much more than the file size and
  412. date. */
  413. if(data->set.opt_no_body && data->set.include_header && fstated) {
  414. time_t filetime;
  415. struct tm buffer;
  416. const struct tm *tm = &buffer;
  417. snprintf(buf, CURL_BUFSIZE(data->set.buffer_size),
  418. "Content-Length: %" CURL_FORMAT_CURL_OFF_T "\r\n", expected_size);
  419. result = Curl_client_write(conn, CLIENTWRITE_BOTH, buf, 0);
  420. if(result)
  421. return result;
  422. result = Curl_client_write(conn, CLIENTWRITE_BOTH,
  423. (char *)"Accept-ranges: bytes\r\n", 0);
  424. if(result)
  425. return result;
  426. filetime = (time_t)statbuf.st_mtime;
  427. result = Curl_gmtime(filetime, &buffer);
  428. if(result)
  429. return result;
  430. /* format: "Tue, 15 Nov 1994 12:45:26 GMT" */
  431. snprintf(buf, BUFSIZE-1,
  432. "Last-Modified: %s, %02d %s %4d %02d:%02d:%02d GMT\r\n",
  433. Curl_wkday[tm->tm_wday?tm->tm_wday-1:6],
  434. tm->tm_mday,
  435. Curl_month[tm->tm_mon],
  436. tm->tm_year + 1900,
  437. tm->tm_hour,
  438. tm->tm_min,
  439. tm->tm_sec);
  440. result = Curl_client_write(conn, CLIENTWRITE_BOTH, buf, 0);
  441. if(!result)
  442. /* set the file size to make it available post transfer */
  443. Curl_pgrsSetDownloadSize(data, expected_size);
  444. return result;
  445. }
  446. /* Check whether file range has been specified */
  447. file_range(conn);
  448. /* Adjust the start offset in case we want to get the N last bytes
  449. * of the stream iff the filesize could be determined */
  450. if(data->state.resume_from < 0) {
  451. if(!fstated) {
  452. failf(data, "Can't get the size of file.");
  453. return CURLE_READ_ERROR;
  454. }
  455. else
  456. data->state.resume_from += (curl_off_t)statbuf.st_size;
  457. }
  458. if(data->state.resume_from <= expected_size)
  459. expected_size -= data->state.resume_from;
  460. else {
  461. failf(data, "failed to resume file:// transfer");
  462. return CURLE_BAD_DOWNLOAD_RESUME;
  463. }
  464. /* A high water mark has been specified so we obey... */
  465. if(data->req.maxdownload > 0)
  466. expected_size = data->req.maxdownload;
  467. if(!fstated || (expected_size == 0))
  468. size_known = FALSE;
  469. else
  470. size_known = TRUE;
  471. /* The following is a shortcut implementation of file reading
  472. this is both more efficient than the former call to download() and
  473. it avoids problems with select() and recv() on file descriptors
  474. in Winsock */
  475. if(fstated)
  476. Curl_pgrsSetDownloadSize(data, expected_size);
  477. if(data->state.resume_from) {
  478. if(data->state.resume_from !=
  479. lseek(fd, data->state.resume_from, SEEK_SET))
  480. return CURLE_BAD_DOWNLOAD_RESUME;
  481. }
  482. Curl_pgrsTime(data, TIMER_STARTTRANSFER);
  483. while(!result) {
  484. /* Don't fill a whole buffer if we want less than all data */
  485. size_t bytestoread;
  486. if(size_known) {
  487. bytestoread =
  488. (expected_size < CURL_OFF_T_C(BUFSIZE) - CURL_OFF_T_C(1)) ?
  489. curlx_sotouz(expected_size) : BUFSIZE - 1;
  490. }
  491. else
  492. bytestoread = BUFSIZE-1;
  493. nread = read(fd, buf, bytestoread);
  494. if(nread > 0)
  495. buf[nread] = 0;
  496. if(nread <= 0 || (size_known && (expected_size == 0)))
  497. break;
  498. bytecount += nread;
  499. if(size_known)
  500. expected_size -= nread;
  501. result = Curl_client_write(conn, CLIENTWRITE_BODY, buf, nread);
  502. if(result)
  503. return result;
  504. Curl_pgrsSetDownloadCounter(data, bytecount);
  505. if(Curl_pgrsUpdate(conn))
  506. result = CURLE_ABORTED_BY_CALLBACK;
  507. else
  508. result = Curl_speedcheck(data, now);
  509. }
  510. if(Curl_pgrsUpdate(conn))
  511. result = CURLE_ABORTED_BY_CALLBACK;
  512. return result;
  513. }
  514. #endif