file.c 17 KB

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