PageRenderTime 27ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/curl/lib/file.c

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