file.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2004, 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 <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. #ifdef HAVE_UNISTD_H
  47. #include <unistd.h>
  48. #endif
  49. #ifdef HAVE_NETDB_H
  50. #include <netdb.h>
  51. #endif
  52. #ifdef HAVE_ARPA_INET_H
  53. #include <arpa/inet.h>
  54. #endif
  55. #ifdef HAVE_NET_IF_H
  56. #include <net/if.h>
  57. #endif
  58. #include <sys/ioctl.h>
  59. #include <signal.h>
  60. #ifdef HAVE_SYS_PARAM_H
  61. #include <sys/param.h>
  62. #endif
  63. #ifdef HAVE_SYS_STAT_H
  64. #include <sys/stat.h>
  65. #endif
  66. #ifdef HAVE_FCNTL_H
  67. #include <fcntl.h>
  68. #endif
  69. #endif
  70. #include "urldata.h"
  71. #include <curl/curl.h>
  72. #include "progress.h"
  73. #include "sendf.h"
  74. #include "escape.h"
  75. #include "file.h"
  76. #include "speedcheck.h"
  77. #include "getinfo.h"
  78. #include "transfer.h"
  79. #include "url.h"
  80. #include "curl_memory.h"
  81. #define _MPRINTF_REPLACE /* use our functions only */
  82. #include <curl/mprintf.h>
  83. /* The last #include file should be: */
  84. #include "memdebug.h"
  85. /*
  86. * Curl_file_connect() gets called from Curl_protocol_connect() to allow us to
  87. * do protocol-specific actions at connect-time. We emulate a
  88. * connect-then-transfer protocol and "connect" to the file here
  89. */
  90. CURLcode Curl_file_connect(struct connectdata *conn)
  91. {
  92. char *real_path = curl_unescape(conn->path, 0);
  93. struct FILEPROTO *file;
  94. int fd;
  95. #if defined(WIN32) || defined(__EMX__)
  96. int i;
  97. char *actual_path;
  98. #endif
  99. if(!real_path)
  100. return CURLE_OUT_OF_MEMORY;
  101. file = (struct FILEPROTO *)calloc(sizeof(struct FILEPROTO), 1);
  102. if(!file) {
  103. free(real_path);
  104. return CURLE_OUT_OF_MEMORY;
  105. }
  106. conn->proto.file = file;
  107. #if defined(WIN32) || defined(__EMX__)
  108. /* If the first character is a slash, and there's
  109. something that looks like a drive at the beginning of
  110. the path, skip the slash. If we remove the initial
  111. slash in all cases, paths without drive letters end up
  112. relative to the current directory which isn't how
  113. browsers work.
  114. Some browsers accept | instead of : as the drive letter
  115. separator, so we do too.
  116. On other platforms, we need the slash to indicate an
  117. absolute pathname. On Windows, absolute paths start
  118. with a drive letter.
  119. */
  120. actual_path = real_path;
  121. if ((actual_path[0] == '/') &&
  122. actual_path[1] &&
  123. (actual_path[2] == ':' || actual_path[2] == '|'))
  124. {
  125. actual_path[2] = ':';
  126. actual_path++;
  127. }
  128. /* change path separators from '/' to '\\' for Windows and OS/2 */
  129. for (i=0; actual_path[i] != '\0'; ++i)
  130. if (actual_path[i] == '/')
  131. actual_path[i] = '\\';
  132. fd = open(actual_path, O_RDONLY | O_BINARY); /* no CR/LF translation! */
  133. file->path = actual_path;
  134. #else
  135. fd = open(real_path, O_RDONLY);
  136. file->path = real_path;
  137. #endif
  138. file->freepath = real_path; /* free this when done */
  139. if(!conn->data->set.upload && (fd == -1)) {
  140. failf(conn->data, "Couldn't open file %s", conn->path);
  141. Curl_file_done(conn, CURLE_FILE_COULDNT_READ_FILE);
  142. return CURLE_FILE_COULDNT_READ_FILE;
  143. }
  144. file->fd = fd;
  145. return CURLE_OK;
  146. }
  147. #if defined(WIN32) && (SIZEOF_CURL_OFF_T > 4)
  148. #define lseek(x,y,z) _lseeki64(x, y, z)
  149. #endif
  150. CURLcode Curl_file_done(struct connectdata *conn,
  151. CURLcode status)
  152. {
  153. struct FILEPROTO *file = conn->proto.file;
  154. (void)status; /* not used */
  155. Curl_safefree(file->freepath);
  156. return CURLE_OK;
  157. }
  158. #if defined(WIN32) || defined(__EMX__)
  159. #define DIRSEP '\\'
  160. #else
  161. #define DIRSEP '/'
  162. #endif
  163. static CURLcode file_upload(struct connectdata *conn)
  164. {
  165. struct FILEPROTO *file = conn->proto.file;
  166. char *dir = strchr(file->path, DIRSEP);
  167. FILE *fp;
  168. CURLcode res=CURLE_OK;
  169. struct SessionHandle *data = conn->data;
  170. char *buf = data->state.buffer;
  171. size_t nread;
  172. size_t nwrite;
  173. curl_off_t bytecount = 0;
  174. struct timeval now = Curl_tvnow();
  175. /*
  176. * Since FILE: doesn't do the full init, we need to provide some extra
  177. * assignments here.
  178. */
  179. conn->fread = data->set.fread;
  180. conn->fread_in = data->set.in;
  181. conn->upload_fromhere = buf;
  182. if(!dir)
  183. return CURLE_FILE_COULDNT_READ_FILE; /* fix: better error code */
  184. if(!dir[1])
  185. return CURLE_FILE_COULDNT_READ_FILE; /* fix: better error code */
  186. fp = fopen(file->path, "wb");
  187. if(!fp) {
  188. failf(data, "Can't open %s for writing", file->path);
  189. return CURLE_WRITE_ERROR;
  190. }
  191. if(-1 != data->set.infilesize)
  192. /* known size of data to "upload" */
  193. Curl_pgrsSetUploadSize(data, data->set.infilesize);
  194. while (res == CURLE_OK) {
  195. int readcount;
  196. res = Curl_fillreadbuffer(conn, BUFSIZE, &readcount);
  197. if(res)
  198. return res;
  199. nread = (size_t)readcount;
  200. if (nread <= 0)
  201. break;
  202. /* write the data to the target */
  203. nwrite = fwrite(buf, 1, nread, fp);
  204. if(nwrite != nread) {
  205. res = CURLE_SEND_ERROR;
  206. break;
  207. }
  208. bytecount += nread;
  209. Curl_pgrsSetUploadCounter(data, bytecount);
  210. if(Curl_pgrsUpdate(conn))
  211. res = CURLE_ABORTED_BY_CALLBACK;
  212. else
  213. res = Curl_speedcheck(data, now);
  214. }
  215. if(!res && Curl_pgrsUpdate(conn))
  216. res = CURLE_ABORTED_BY_CALLBACK;
  217. fclose(fp);
  218. return res;
  219. }
  220. /*
  221. * Curl_file() is the protocol-specific function for the do-phase, separated
  222. * from the connect-phase above. Other protocols merely setup the transfer in
  223. * the do-phase, to have it done in the main transfer loop but since some
  224. * platforms we support don't allow select()ing etc on file handles (as
  225. * opposed to sockets) we instead perform the whole do-operation in this
  226. * function.
  227. */
  228. CURLcode Curl_file(struct connectdata *conn)
  229. {
  230. /* This implementation ignores the host name in conformance with
  231. RFC 1738. Only local files (reachable via the standard file system)
  232. are supported. This means that files on remotely mounted directories
  233. (via NFS, Samba, NT sharing) can be accessed through a file:// URL
  234. */
  235. CURLcode res = CURLE_OK;
  236. struct stat statbuf;
  237. curl_off_t expected_size=0;
  238. bool fstated=FALSE;
  239. ssize_t nread;
  240. struct SessionHandle *data = conn->data;
  241. char *buf = data->state.buffer;
  242. curl_off_t bytecount = 0;
  243. int fd;
  244. struct timeval now = Curl_tvnow();
  245. Curl_readwrite_init(conn);
  246. Curl_initinfo(data);
  247. Curl_pgrsStartNow(data);
  248. if(data->set.upload)
  249. return file_upload(conn);
  250. /* get the fd from the connection phase */
  251. fd = conn->proto.file->fd;
  252. /* VMS: This only works reliable for STREAMLF files */
  253. if( -1 != fstat(fd, &statbuf)) {
  254. /* we could stat it, then read out the size */
  255. expected_size = statbuf.st_size;
  256. fstated = TRUE;
  257. }
  258. /* If we have selected NOBODY and HEADER, it means that we only want file
  259. information. Which for FILE can't be much more than the file size and
  260. date. */
  261. if(conn->bits.no_body && data->set.include_header && fstated) {
  262. CURLcode result;
  263. snprintf(buf, sizeof(data->state.buffer),
  264. "Content-Length: %" FORMAT_OFF_T "\r\n", expected_size);
  265. result = Curl_client_write(data, CLIENTWRITE_BOTH, buf, 0);
  266. if(result)
  267. return result;
  268. result = Curl_client_write(data, CLIENTWRITE_BOTH,
  269. (char *)"Accept-ranges: bytes\r\n", 0);
  270. if(result)
  271. return result;
  272. #ifdef HAVE_STRFTIME
  273. if(fstated) {
  274. struct tm *tm;
  275. time_t cuClock = (time_t)statbuf.st_mtime;
  276. #ifdef HAVE_GMTIME_R
  277. struct tm buffer;
  278. tm = (struct tm *)gmtime_r(&cuClock, &buffer);
  279. #else
  280. tm = gmtime(&cuClock);
  281. #endif
  282. /* format: "Tue, 15 Nov 1994 12:45:26 GMT" */
  283. strftime(buf, BUFSIZE-1, "Last-Modified: %a, %d %b %Y %H:%M:%S GMT\r\n",
  284. tm);
  285. result = Curl_client_write(data, CLIENTWRITE_BOTH, buf, 0);
  286. }
  287. #endif
  288. return result;
  289. }
  290. /* Added by Dolbneff A.V & Spiridonoff A.V */
  291. if (conn->resume_from <= expected_size)
  292. expected_size -= conn->resume_from;
  293. else
  294. /* Is this error code suitable in such situation? */
  295. return CURLE_FTP_BAD_DOWNLOAD_RESUME;
  296. if (fstated && (expected_size == 0))
  297. return CURLE_OK;
  298. /* The following is a shortcut implementation of file reading
  299. this is both more efficient than the former call to download() and
  300. it avoids problems with select() and recv() on file descriptors
  301. in Winsock */
  302. if(fstated)
  303. Curl_pgrsSetDownloadSize(data, expected_size);
  304. if(conn->resume_from)
  305. lseek(fd, conn->resume_from, SEEK_SET);
  306. Curl_pgrsTime(data, TIMER_STARTTRANSFER);
  307. while (res == CURLE_OK) {
  308. nread = read(fd, buf, BUFSIZE-1);
  309. if ( nread > 0)
  310. buf[nread] = 0;
  311. if (nread <= 0)
  312. break;
  313. bytecount += nread;
  314. res = Curl_client_write(data, CLIENTWRITE_BODY, buf, nread);
  315. if(res)
  316. return res;
  317. Curl_pgrsSetDownloadCounter(data, bytecount);
  318. if(Curl_pgrsUpdate(conn))
  319. res = CURLE_ABORTED_BY_CALLBACK;
  320. else
  321. res = Curl_speedcheck(data, now);
  322. }
  323. if(Curl_pgrsUpdate(conn))
  324. res = CURLE_ABORTED_BY_CALLBACK;
  325. close(fd);
  326. return res;
  327. }
  328. #endif