file.c 15 KB

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