file.c 11 KB

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