file.c 17 KB

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