]> cygwin.com Git - cygwin-apps/setup.git/blame - geturl.cc
2001-01-04 Gary R. Van Sickle <g.r.vansickle@worldnet.att.net>
[cygwin-apps/setup.git] / geturl.cc
CommitLineData
23c9e63c 1/*
ca9506cc
RC
2
3
85553593 4 * Copyright (c) 2000, 2001, Red Hat, Inc.
23c9e63c
DD
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * A copy of the GNU General Public License can be found at
12 * http://www.gnu.org/
13 *
14 * Written by DJ Delorie <dj@cygnus.com>
15 *
16 */
17
18/* The purpose of this file is to act as a pretty interface to
19 netio.cc. We add a progress dialog and some convenience functions
20 (like collect to string or file */
21
b24c88b3
RC
22#if 0
23static const char *cvsid =
24 "\n%%% $Id$\n";
25#endif
8507f105 26
23c9e63c
DD
27#include "win32.h"
28#include "commctrl.h"
29
30#include <stdio.h>
31#include <stdlib.h>
32#include <errno.h>
33
34#include "dialog.h"
35#include "geturl.h"
36#include "resource.h"
37#include "netio.h"
38#include "msg.h"
89b1a15b 39#include "log.h"
ca9506cc
RC
40#include "io_stream.h"
41#include "io_stream_memory.h"
2f9645a1
CV
42#include "state.h"
43#include "diskfull.h"
85b43844 44#include "mount.h"
23c9e63c 45
ab57ceaa
RC
46#include "threebar.h"
47extern ThreeBarProgressPage Progress;
48
23c9e63c 49static int max_bytes = 0;
7cefe128 50static int is_local_install = 0;
23c9e63c 51
2f9645a1
CV
52int total_download_bytes = 0;
53int total_download_bytes_sofar = 0;
54
23c9e63c
DD
55static DWORD start_tics;
56
57static void
ab57ceaa 58init_dialog (char const *url, int length, HWND owner)
23c9e63c 59{
7cefe128
RC
60 if (is_local_install)
61 return;
ab57ceaa 62
341988b9
RC
63 char const *sl = url;
64 char const *cp;
b24c88b3 65 for (cp = url; *cp; cp++)
23c9e63c 66 if (*cp == '/' || *cp == '\\' || *cp == ':')
b24c88b3 67 sl = cp + 1;
23c9e63c 68 max_bytes = length;
ab57ceaa
RC
69 Progress.SetText1("Downloading...");
70 Progress.SetText2(sl);
71 Progress.SetText3("Connecting...");
72 Progress.SetBar1(0);
1fd6d0a2 73 start_tics = GetTickCount ();
23c9e63c
DD
74}
75
76
77static void
78progress (int bytes)
79{
7cefe128
RC
80 if (is_local_install)
81 return;
23c9e63c 82 static char buf[100];
ab57ceaa 83 double kbps;
c168185f 84 static unsigned int last_tics = 0;
1fd6d0a2 85 DWORD tics = GetTickCount ();
b24c88b3 86 if (tics == start_tics) // to prevent division by zero
23c9e63c 87 return;
b24c88b3 88 if (tics < last_tics + 200) // to prevent flickering updates
23c9e63c
DD
89 return;
90 last_tics = tics;
91
ab57ceaa
RC
92 kbps = ((double)bytes) / (double)(tics - start_tics);
93 if (max_bytes > 0)
23c9e63c 94 {
ab57ceaa
RC
95 int perc = (int)(100.0 * ((double)bytes) / (double)max_bytes);
96 Progress.SetBar1(bytes, max_bytes);
b7301c43 97 sprintf (buf, "%d %% (%dk/%dk) %03.1f kb/s\n",
b24c88b3 98 perc, bytes / 1000, max_bytes / 1000, kbps);
2f9645a1 99 if (total_download_bytes > 0)
b24c88b3 100 {
ab57ceaa 101 Progress.SetBar2(total_download_bytes_sofar + bytes, total_download_bytes);
b24c88b3 102 }
23c9e63c
DD
103 }
104 else
ab57ceaa 105 sprintf (buf, "%d %2.1f kb/s\n", bytes, kbps);
23c9e63c 106
ab57ceaa 107 Progress.SetText3(buf);
23c9e63c
DD
108}
109
341988b9 110io_stream *
ab57ceaa 111get_url_to_membuf (char const *_url, HWND owner)
23c9e63c 112{
ab57ceaa 113 log (LOG_BABBLE, "get_url_to_membuf %s", _url);
7cefe128 114 is_local_install = (source == IDC_SOURCE_CWD);
ab57ceaa 115 init_dialog (_url, 0, owner);
23c9e63c
DD
116 NetIO *n = NetIO::open (_url);
117 if (!n || !n->ok ())
118 {
119 delete n;
341988b9 120 log (LOG_BABBLE, "get_url_to_membuf failed!");
23c9e63c
DD
121 return 0;
122 }
123
124 if (n->file_size)
125 max_bytes = n->file_size;
126
4fe323f9
RC
127 io_stream_memory *membuf = new io_stream_memory ();
128
341988b9 129 int total_bytes = 0;
23c9e63c
DD
130 progress (0);
131 while (1)
132 {
ca9506cc 133 char buf[2048];
4fe323f9 134 ssize_t rlen, wlen;
ca9506cc
RC
135 rlen = n->read (buf, 2048);
136 if (rlen > 0)
4fe323f9
RC
137 {
138 wlen = membuf->write (buf, rlen);
139 if (wlen != rlen)
140 /* FIXME: Show an error message */
141 break;
142 total_bytes += rlen;
143 progress (total_bytes);
144 }
ca9506cc
RC
145 else
146 break;
23c9e63c
DD
147 }
148
341988b9 149 if (membuf->seek (0, IO_SEEK_SET))
c168185f 150 {
8e58f8fd
RC
151 if (n)
152 delete n;
ca9506cc
RC
153 if (membuf)
154 delete membuf;
341988b9 155 log (LOG_BABBLE, "get_url_to_membuf(): seek (0) failed for membuf!");
c168185f
RC
156 return 0;
157 }
4fe323f9 158
8e58f8fd
RC
159 if (n)
160 delete n;
341988b9
RC
161 return membuf;
162}
163
164char *
ab57ceaa 165get_url_to_string (char const *_url, HWND owner)
341988b9 166{
ab57ceaa 167 io_stream *stream = get_url_to_membuf (_url, owner);
341988b9
RC
168 if (!stream)
169 return 0;
170 size_t bytes = stream->get_size ();
171 if (!bytes)
172 {
173 /* zero length, or error retrieving length */
174 delete stream;
175 log (LOG_BABBLE, "get_url_to_string(): couldn't retrieve buffer size, or zero length buffer");
176 return 0;
177 }
178 char *rv = new char [bytes + 1];
179 if (!rv)
180 {
181 delete stream;
182 log (LOG_BABBLE, "get_url_to_string(): new failed for rv!");
183 return 0;
184 }
185 /* membufs are quite safe */
186 stream->read (rv, bytes);
187 rv [bytes] = '\0';
188 delete stream;
23c9e63c
DD
189 return rv;
190}
191
192int
85553593 193get_url_to_file (char *_url, char *_filename, int expected_length,
ab57ceaa 194 HWND owner, BOOL allow_ftp_auth)
23c9e63c 195{
89b1a15b 196 log (LOG_BABBLE, "get_url_to_file %s %s", _url, _filename);
2f9645a1
CV
197 if (total_download_bytes > 0)
198 {
85b43844 199 int df = diskfull (get_root_dir ());
ab57ceaa 200 Progress.SetBar3(df);
2f9645a1 201 }
ab57ceaa 202 init_dialog (_url, expected_length, owner);
23c9e63c 203
b24c88b3 204 remove (_filename); /* but ignore errors */
23c9e63c 205
85553593 206 NetIO *n = NetIO::open (_url, allow_ftp_auth);
23c9e63c
DD
207 if (!n || !n->ok ())
208 {
209 delete n;
89b1a15b 210 log (LOG_BABBLE, "get_url_to_file failed!");
23c9e63c
DD
211 return 1;
212 }
213
1fd6d0a2 214 FILE *f = fopen (_filename, "wb");
23c9e63c
DD
215 if (!f)
216 {
b24c88b3 217 const char *err = strerror (errno);
23c9e63c
DD
218 if (!err)
219 err = "(unknown error)";
ab57ceaa 220 fatal (owner, IDS_ERR_OPEN_WRITE, _filename, err);
23c9e63c
DD
221 }
222
223 if (n->file_size)
224 max_bytes = n->file_size;
225
226 int total_bytes = 0;
227 progress (0);
228 while (1)
229 {
4a83b7b0 230 char buf[8192];
23c9e63c
DD
231 int count;
232 count = n->read (buf, sizeof (buf));
233 if (count <= 0)
234 break;
235 fwrite (buf, 1, count, f);
236 total_bytes += count;
237 progress (total_bytes);
238 }
239
2f9645a1
CV
240 total_download_bytes_sofar += total_bytes;
241
23c9e63c 242 fclose (f);
8e58f8fd
RC
243 if (n)
244 delete n;
23c9e63c 245
2f9645a1
CV
246 if (total_download_bytes > 0)
247 {
85b43844 248 int df = diskfull (get_root_dir ());
ab57ceaa 249 Progress.SetBar3(df);
2f9645a1
CV
250 }
251
23c9e63c
DD
252 return 0;
253}
254
This page took 0.058152 seconds and 5 git commands to generate.