2
0

file.c 15 KB

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