file.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2002, 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 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$
  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. #include <sys/types.h>
  32. #include <sys/stat.h>
  33. #include <errno.h>
  34. #if defined(WIN32) && !defined(__GNUC__) || defined(__MINGW32__)
  35. #include <winsock.h>
  36. #include <time.h>
  37. #include <io.h>
  38. #include <fcntl.h>
  39. #else
  40. #ifdef HAVE_SYS_SOCKET_H
  41. #include <sys/socket.h>
  42. #endif
  43. #ifdef HAVE_NETINET_IN_H
  44. #include <netinet/in.h>
  45. #endif
  46. #include <sys/time.h>
  47. #include <sys/resource.h>
  48. #ifdef HAVE_UNISTD_H
  49. #include <unistd.h>
  50. #endif
  51. #ifdef HAVE_NETDB_H
  52. #include <netdb.h>
  53. #endif
  54. #ifdef HAVE_ARPA_INET_H
  55. #include <arpa/inet.h>
  56. #endif
  57. #ifdef HAVE_NET_IF_H
  58. #include <net/if.h>
  59. #endif
  60. #include <sys/ioctl.h>
  61. #include <signal.h>
  62. #ifdef HAVE_SYS_PARAM_H
  63. #include <sys/param.h>
  64. #endif
  65. #ifdef HAVE_SYS_STAT_H
  66. #include <sys/stat.h>
  67. #endif
  68. #ifdef HAVE_FCNTL_H
  69. #include <fcntl.h>
  70. #endif
  71. #endif
  72. #include "urldata.h"
  73. #include <curl/curl.h>
  74. #include "progress.h"
  75. #include "sendf.h"
  76. #include "escape.h"
  77. #define _MPRINTF_REPLACE /* use our functions only */
  78. #include <curl/mprintf.h>
  79. /* The last #include file should be: */
  80. #ifdef MALLOCDEBUG
  81. #include "memdebug.h"
  82. #endif
  83. /* Emulate a connect-then-transfer protocol. We connect to the file here */
  84. CURLcode Curl_file_connect(struct connectdata *conn)
  85. {
  86. char *actual_path = curl_unescape(conn->path, 0);
  87. struct FILE *file;
  88. int fd;
  89. #if defined(WIN32) || defined(__EMX__)
  90. int i;
  91. #endif
  92. file = (struct FILE *)malloc(sizeof(struct FILE));
  93. if(!file)
  94. return CURLE_OUT_OF_MEMORY;
  95. memset(file, 0, sizeof(struct FILE));
  96. conn->proto.file = file;
  97. #if defined(WIN32) || defined(__EMX__)
  98. /* change path separators from '/' to '\\' for Windows and OS/2 */
  99. for (i=0; actual_path[i] != '\0'; ++i)
  100. if (actual_path[i] == '/')
  101. actual_path[i] = '\\';
  102. fd = open(actual_path, O_RDONLY | O_BINARY); /* no CR/LF translation! */
  103. #else
  104. fd = open(actual_path, O_RDONLY);
  105. #endif
  106. free(actual_path);
  107. if(fd == -1) {
  108. failf(conn->data, "Couldn't open file %s", conn->path);
  109. return CURLE_FILE_COULDNT_READ_FILE;
  110. }
  111. file->fd = fd;
  112. return CURLE_OK;
  113. }
  114. /* This is the do-phase, separated from the connect-phase above */
  115. CURLcode Curl_file(struct connectdata *conn)
  116. {
  117. /* This implementation ignores the host name in conformance with
  118. RFC 1738. Only local files (reachable via the standard file system)
  119. are supported. This means that files on remotely mounted directories
  120. (via NFS, Samba, NT sharing) can be accessed through a file:// URL
  121. */
  122. CURLcode res = CURLE_OK;
  123. struct stat statbuf;
  124. double expected_size=-1;
  125. ssize_t nread;
  126. struct SessionHandle *data = conn->data;
  127. char *buf = data->state.buffer;
  128. int bytecount = 0;
  129. struct timeval start = Curl_tvnow();
  130. struct timeval now = start;
  131. int fd;
  132. /* get the fd from the connection phase */
  133. fd = conn->proto.file->fd;
  134. /*VMS?? -- This only works reliable for STREAMLF files */
  135. if( -1 != fstat(fd, &statbuf)) {
  136. /* we could stat it, then read out the size */
  137. expected_size = (double)statbuf.st_size;
  138. }
  139. /* Added by Dolbneff A.V & Spiridonoff A.V */
  140. if (conn->resume_from <= expected_size)
  141. expected_size -= conn->resume_from;
  142. else
  143. /* Is this error code suitable in such situation? */
  144. return CURLE_FTP_BAD_DOWNLOAD_RESUME;
  145. if (expected_size == 0)
  146. return CURLE_OK;
  147. /* The following is a shortcut implementation of file reading
  148. this is both more efficient than the former call to download() and
  149. it avoids problems with select() and recv() on file descriptors
  150. in Winsock */
  151. if(expected_size != -1)
  152. Curl_pgrsSetDownloadSize(data, expected_size);
  153. if(conn->resume_from)
  154. /* Added by Dolbneff A.V & Spiridonoff A.V */
  155. lseek(fd, conn->resume_from, SEEK_SET);
  156. while (res == CURLE_OK) {
  157. nread = read(fd, buf, BUFSIZE-1);
  158. if ( nread > 0)
  159. buf[nread] = 0;
  160. if (nread <= 0)
  161. break;
  162. bytecount += nread;
  163. /* NOTE: The following call to fwrite does CR/LF translation on
  164. Windows systems if the target is stdout. Use -O or -o parameters
  165. to prevent CR/LF translation (this then goes to a binary mode
  166. file descriptor). */
  167. res = Curl_client_write(data, CLIENTWRITE_BODY, buf, nread);
  168. if(res)
  169. return res;
  170. now = Curl_tvnow();
  171. if(Curl_pgrsUpdate(conn))
  172. res = CURLE_ABORTED_BY_CALLBACK;
  173. }
  174. now = Curl_tvnow();
  175. if(Curl_pgrsUpdate(conn))
  176. res = CURLE_ABORTED_BY_CALLBACK;
  177. close(fd);
  178. return res;
  179. }
  180. /*
  181. * local variables:
  182. * eval: (load-file "../curl-mode.el")
  183. * end:
  184. * vim600: fdm=marker
  185. * vim: et sw=2 ts=2 sts=2 tw=78
  186. */
  187. #endif