file.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  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_ulbuf;
  262. size_t xfer_ulblen;
  263. curl_off_t bytecount = 0;
  264. struct_stat file_stat;
  265. const char *sendbuf;
  266. bool eos = FALSE;
  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. result = Curl_multi_xfer_ulbuf_borrow(data, &xfer_ulbuf, &xfer_ulblen);
  302. if(result)
  303. goto out;
  304. while(!result && !eos) {
  305. size_t nread;
  306. ssize_t nwrite;
  307. size_t readcount;
  308. result = Curl_client_read(data, xfer_ulbuf, xfer_ulblen, &readcount, &eos);
  309. if(result)
  310. break;
  311. if(!readcount)
  312. break;
  313. nread = readcount;
  314. /* skip bytes before resume point */
  315. if(data->state.resume_from) {
  316. if((curl_off_t)nread <= data->state.resume_from) {
  317. data->state.resume_from -= nread;
  318. nread = 0;
  319. sendbuf = xfer_ulbuf;
  320. }
  321. else {
  322. sendbuf = xfer_ulbuf + data->state.resume_from;
  323. nread -= (size_t)data->state.resume_from;
  324. data->state.resume_from = 0;
  325. }
  326. }
  327. else
  328. sendbuf = xfer_ulbuf;
  329. /* write the data to the target */
  330. nwrite = write(fd, sendbuf, nread);
  331. if((size_t)nwrite != nread) {
  332. result = CURLE_SEND_ERROR;
  333. break;
  334. }
  335. bytecount += nread;
  336. Curl_pgrsSetUploadCounter(data, bytecount);
  337. if(Curl_pgrsUpdate(data))
  338. result = CURLE_ABORTED_BY_CALLBACK;
  339. else
  340. result = Curl_speedcheck(data, Curl_now());
  341. }
  342. if(!result && Curl_pgrsUpdate(data))
  343. result = CURLE_ABORTED_BY_CALLBACK;
  344. out:
  345. close(fd);
  346. Curl_multi_xfer_ulbuf_release(data, xfer_ulbuf);
  347. return result;
  348. }
  349. /*
  350. * file_do() is the protocol-specific function for the do-phase, separated
  351. * from the connect-phase above. Other protocols merely setup the transfer in
  352. * the do-phase, to have it done in the main transfer loop but since some
  353. * platforms we support don't allow select()ing etc on file handles (as
  354. * opposed to sockets) we instead perform the whole do-operation in this
  355. * function.
  356. */
  357. static CURLcode file_do(struct Curl_easy *data, bool *done)
  358. {
  359. /* This implementation ignores the host name in conformance with
  360. RFC 1738. Only local files (reachable via the standard file system)
  361. are supported. This means that files on remotely mounted directories
  362. (via NFS, Samba, NT sharing) can be accessed through a file:// URL
  363. */
  364. CURLcode result = CURLE_OK;
  365. struct_stat statbuf; /* struct_stat instead of struct stat just to allow the
  366. Windows version to have a different struct without
  367. having to redefine the simple word 'stat' */
  368. curl_off_t expected_size = -1;
  369. bool size_known;
  370. bool fstated = FALSE;
  371. int fd;
  372. struct FILEPROTO *file;
  373. char *xfer_buf;
  374. size_t xfer_blen;
  375. *done = TRUE; /* unconditionally */
  376. if(data->state.upload)
  377. return file_upload(data);
  378. file = data->req.p.file;
  379. /* get the fd from the connection phase */
  380. fd = file->fd;
  381. /* VMS: This only works reliable for STREAMLF files */
  382. if(-1 != fstat(fd, &statbuf)) {
  383. if(!S_ISDIR(statbuf.st_mode))
  384. expected_size = statbuf.st_size;
  385. /* and store the modification time */
  386. data->info.filetime = statbuf.st_mtime;
  387. fstated = TRUE;
  388. }
  389. if(fstated && !data->state.range && data->set.timecondition) {
  390. if(!Curl_meets_timecondition(data, data->info.filetime)) {
  391. *done = TRUE;
  392. return CURLE_OK;
  393. }
  394. }
  395. if(fstated) {
  396. time_t filetime;
  397. struct tm buffer;
  398. const struct tm *tm = &buffer;
  399. char header[80];
  400. int headerlen;
  401. char accept_ranges[24]= { "Accept-ranges: bytes\r\n" };
  402. if(expected_size >= 0) {
  403. headerlen = msnprintf(header, sizeof(header),
  404. "Content-Length: %" CURL_FORMAT_CURL_OFF_T "\r\n",
  405. expected_size);
  406. result = Curl_client_write(data, CLIENTWRITE_HEADER, header, headerlen);
  407. if(result)
  408. return result;
  409. result = Curl_client_write(data, CLIENTWRITE_HEADER,
  410. accept_ranges, strlen(accept_ranges));
  411. if(result != CURLE_OK)
  412. return result;
  413. }
  414. filetime = (time_t)statbuf.st_mtime;
  415. result = Curl_gmtime(filetime, &buffer);
  416. if(result)
  417. return result;
  418. /* format: "Tue, 15 Nov 1994 12:45:26 GMT" */
  419. headerlen = msnprintf(header, sizeof(header),
  420. "Last-Modified: %s, %02d %s %4d %02d:%02d:%02d GMT\r\n%s",
  421. Curl_wkday[tm->tm_wday?tm->tm_wday-1:6],
  422. tm->tm_mday,
  423. Curl_month[tm->tm_mon],
  424. tm->tm_year + 1900,
  425. tm->tm_hour,
  426. tm->tm_min,
  427. tm->tm_sec,
  428. data->req.no_body ? "": "\r\n");
  429. result = Curl_client_write(data, CLIENTWRITE_HEADER, header, headerlen);
  430. if(result)
  431. return result;
  432. /* set the file size to make it available post transfer */
  433. Curl_pgrsSetDownloadSize(data, expected_size);
  434. if(data->req.no_body)
  435. return result;
  436. }
  437. /* Check whether file range has been specified */
  438. result = Curl_range(data);
  439. if(result)
  440. return result;
  441. /* Adjust the start offset in case we want to get the N last bytes
  442. * of the stream if the filesize could be determined */
  443. if(data->state.resume_from < 0) {
  444. if(!fstated) {
  445. failf(data, "Can't get the size of file.");
  446. return CURLE_READ_ERROR;
  447. }
  448. data->state.resume_from += (curl_off_t)statbuf.st_size;
  449. }
  450. if(data->state.resume_from > 0) {
  451. /* We check explicitly if we have a start offset, because
  452. * expected_size may be -1 if we don't know how large the file is,
  453. * in which case we should not adjust it. */
  454. if(data->state.resume_from <= expected_size)
  455. expected_size -= data->state.resume_from;
  456. else {
  457. failf(data, "failed to resume file:// transfer");
  458. return CURLE_BAD_DOWNLOAD_RESUME;
  459. }
  460. }
  461. /* A high water mark has been specified so we obey... */
  462. if(data->req.maxdownload > 0)
  463. expected_size = data->req.maxdownload;
  464. if(!fstated || (expected_size <= 0))
  465. size_known = FALSE;
  466. else
  467. size_known = TRUE;
  468. /* The following is a shortcut implementation of file reading
  469. this is both more efficient than the former call to download() and
  470. it avoids problems with select() and recv() on file descriptors
  471. in Winsock */
  472. if(size_known)
  473. Curl_pgrsSetDownloadSize(data, expected_size);
  474. if(data->state.resume_from) {
  475. if(data->state.resume_from !=
  476. lseek(fd, data->state.resume_from, SEEK_SET))
  477. return CURLE_BAD_DOWNLOAD_RESUME;
  478. }
  479. result = Curl_multi_xfer_buf_borrow(data, &xfer_buf, &xfer_blen);
  480. if(result)
  481. goto out;
  482. while(!result) {
  483. ssize_t nread;
  484. /* Don't fill a whole buffer if we want less than all data */
  485. size_t bytestoread;
  486. if(size_known) {
  487. bytestoread = (expected_size < (curl_off_t)(xfer_blen-1)) ?
  488. curlx_sotouz(expected_size) : (xfer_blen-1);
  489. }
  490. else
  491. bytestoread = xfer_blen-1;
  492. nread = read(fd, xfer_buf, bytestoread);
  493. if(nread > 0)
  494. xfer_buf[nread] = 0;
  495. if(nread <= 0 || (size_known && (expected_size == 0)))
  496. break;
  497. if(size_known)
  498. expected_size -= nread;
  499. result = Curl_client_write(data, CLIENTWRITE_BODY, xfer_buf, nread);
  500. if(result)
  501. goto out;
  502. if(Curl_pgrsUpdate(data))
  503. result = CURLE_ABORTED_BY_CALLBACK;
  504. else
  505. result = Curl_speedcheck(data, Curl_now());
  506. if(result)
  507. goto out;
  508. }
  509. if(Curl_pgrsUpdate(data))
  510. result = CURLE_ABORTED_BY_CALLBACK;
  511. out:
  512. Curl_multi_xfer_buf_release(data, xfer_buf);
  513. return result;
  514. }
  515. #endif