file.c 5.2 KB

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