file.c 18 KB

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