file.c 16 KB

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