file.c 15 KB

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