]>
Commit | Line | Data |
---|---|---|
b24c88b3 RC |
1 | /* |
2 | * Copyright (c) 2001, Robert Collins. | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License as published by | |
6 | * the Free Software Foundation; either version 2 of the License, or | |
7 | * (at your option) any later version. | |
8 | * | |
9 | * A copy of the GNU General Public License can be found at | |
10 | * http://www.gnu.org/ | |
11 | * | |
12 | * Written by Robert Collins <rbtcollins@hotmail.com> | |
13 | * | |
14 | */ | |
15 | ||
16 | /* this is the parent class for all IO operations. It's flexable enough to be cover for | |
17 | * HTTP access, local file access, and files being extracted from archives. | |
18 | * It also encapsulates the idea of an archive, and all non-archives become the special | |
19 | * case. | |
20 | */ | |
21 | ||
22 | #if 0 | |
23 | static const char *cvsid = | |
24 | "\n%%% $Id$\n"; | |
25 | #endif | |
26 | ||
27 | #include "win32.h" | |
28 | #include <stdio.h> | |
29 | #include "log.h" | |
30 | #include "port.h" | |
31 | ||
32 | #include "io_stream.h" | |
33 | #include "io_stream_file.h" | |
34 | #include "io_stream_cygfile.h" | |
35 | #include "mkdir.h" | |
36 | ||
37 | /* Static members */ | |
38 | io_stream * | |
39 | io_stream::factory (io_stream * parent) | |
40 | { | |
41 | /* something like, | |
42 | * if !next_file_name | |
43 | * return NULL | |
44 | * switch (magic_id(peek (parent), max_magic_length)) | |
45 | * case io_stream * foo = new tar | |
46 | * case io_stream * foo = new bz2 | |
47 | * return foo | |
48 | */ | |
49 | log (LOG_TIMESTAMP, "io_stream::factory has been called"); | |
50 | return NULL; | |
51 | } | |
52 | ||
53 | io_stream * | |
54 | io_stream::open (const char *name, const char *mode) | |
55 | { | |
56 | if (!name || IsBadStringPtr (name, MAX_PATH) || !name[0] || | |
57 | !mode || IsBadStringPtr (mode, 5) || !mode[0]) | |
58 | return NULL; | |
59 | /* iterate through the known url prefix's */ | |
60 | if (!strncasecmp ("file://", name, 7)) | |
61 | { | |
62 | io_stream_file *rv = new io_stream_file (&name[7], mode); | |
63 | if (!rv->error ()) | |
64 | return rv; | |
65 | delete rv; | |
66 | return NULL; | |
67 | } | |
68 | if (!strncasecmp ("cygfile://", name, 10)) | |
69 | { | |
70 | io_stream_cygfile *rv = new io_stream_cygfile (&name[10], mode); | |
71 | if (!rv->error ()) | |
72 | return rv; | |
73 | delete rv; | |
74 | return NULL; | |
75 | } | |
76 | return NULL; | |
77 | } | |
78 | ||
79 | int | |
80 | io_stream::mkpath_p (path_type_t isadir, const char *name) | |
81 | { | |
82 | if (!name || IsBadStringPtr (name, MAX_PATH) || !name[0]) | |
83 | return 1; | |
84 | /* iterate through the known url prefix's */ | |
85 | if (!strncasecmp ("file://", name, 7)) | |
86 | { | |
87 | return mkdir_p (isadir == PATH_TO_DIR ? 1 : 0, &name[7]); | |
88 | } | |
89 | if (!strncasecmp ("cygfile://", name, 10)) | |
90 | { | |
91 | return cygmkdir_p (isadir == PATH_TO_DIR ? 1 : 0, &name[10]); | |
92 | } | |
93 | return 1; | |
94 | } | |
95 | ||
96 | /* remove a file or directory. */ | |
97 | int | |
98 | io_stream::remove (const char *name) | |
99 | { | |
100 | if (!name || IsBadStringPtr (name, MAX_PATH) || !name[0]) | |
101 | return 1; | |
102 | /* iterate through the known url prefix's */ | |
103 | if (!strncasecmp ("file://", name, 7)) | |
104 | return io_stream_file::remove (&name[7]); | |
105 | if (!strncasecmp ("cygfile://", name, 10)) | |
106 | return io_stream_cygfile::remove (&name[10]); | |
107 | return 1; | |
108 | } | |
109 | ||
110 | int | |
111 | io_stream::mklink (const char *from, const char *to, | |
112 | io_stream_link_t linktype) | |
113 | { | |
6dc75764 | 114 | log (LOG_BABBLE, "io_stream::mklink (%s->%s)", from, to); |
b24c88b3 RC |
115 | if (!from || IsBadStringPtr (from, MAX_PATH) || |
116 | !to || IsBadStringPtr (to, MAX_PATH)) | |
7c7034e8 RC |
117 | { |
118 | log (LOG_TIMESTAMP, | |
119 | "invalid string in from or to parameters to mklink"); | |
120 | return 1; | |
121 | } | |
b24c88b3 RC |
122 | /* iterate through the known url prefixes */ |
123 | if (!strncasecmp ("file://", from, 7)) | |
124 | { | |
125 | /* file urls can symlink or hardlink to file url's. */ | |
126 | /* TODO: allow linking to cygfile url's */ | |
127 | if (!strncasecmp ("file://", to, 7)) | |
128 | return io_stream_file::mklink (&from[7], &to[7], linktype); | |
129 | log (LOG_TIMESTAMP, "Attempt to link across url providers"); | |
130 | return 1; | |
131 | } | |
132 | if (!strncasecmp ("cygfile://", from, 10)) | |
133 | { | |
134 | /* cygfile urls can symlink or hardlink to cygfile urls's. */ | |
135 | /* TODO: allow -> file urls */ | |
136 | if (!strncasecmp ("cygfile://", to, 10)) | |
137 | return io_stream_cygfile::mklink (&from[10], &to[10], linktype); | |
138 | log (LOG_TIMESTAMP, "Attempt to link across url providers"); | |
139 | return 1; | |
140 | } | |
141 | #if 0 | |
142 | if (!strmcasecmp ("http://", from, 7)) | |
143 | { | |
144 | /* http urls can symlink to http or ftp url's */ | |
145 | } | |
7c7034e8 RC |
146 | #endif |
147 | log (LOG_TIMESTAMP, "Unsupported url providers for %s", from); | |
148 | return 1; | |
149 | } | |
150 | ||
151 | int | |
152 | io_stream::move_copy (const char *from, const char *to) | |
153 | { | |
154 | /* parameters are ok - checked before calling us, and we are private */ | |
155 | io_stream *in = io_stream::open (to, "wb"); | |
156 | io_stream *out = io_stream::open (from, "rb"); | |
341988b9 RC |
157 | if (io_stream::copy (in, out)) |
158 | { | |
159 | log (LOG_TIMESTAMP, "Failed copy of %s to %s", from, to); | |
160 | delete out; | |
161 | io_stream::remove (to); | |
162 | delete in; | |
163 | return 1; | |
164 | } | |
165 | /* TODO: | |
166 | out->set_mtime (in->get_mtime ()); | |
167 | */ | |
168 | delete in; | |
169 | delete out; | |
170 | io_stream::remove (from); | |
171 | return 0; | |
172 | } | |
173 | ||
df62e023 | 174 | ssize_t io_stream::copy (io_stream * in, io_stream * out) |
341988b9 RC |
175 | { |
176 | if (!in || !out) | |
177 | return -1; | |
df62e023 RC |
178 | char |
179 | buffer[16384]; | |
180 | ssize_t | |
181 | countin, | |
182 | countout; | |
7c7034e8 RC |
183 | while ((countin = in->read (buffer, 16384)) > 0) |
184 | { | |
185 | countout = out->write (buffer, countin); | |
186 | if (countout != countin) | |
df62e023 RC |
187 | { |
188 | log (LOG_TIMESTAMP, "io_stream::copy failed to write %ld bytes", | |
189 | countin); | |
190 | return countout ? countout : -1; | |
191 | } | |
7c7034e8 | 192 | } |
341988b9 | 193 | |
7c7034e8 RC |
194 | /* TODO: |
195 | out->set_mtime (in->get_mtime ()); | |
196 | */ | |
7c7034e8 RC |
197 | return 0; |
198 | } | |
199 | ||
200 | int | |
201 | io_stream::move (const char *from, const char *to) | |
202 | { | |
203 | if (!from || IsBadStringPtr (from, MAX_PATH) || | |
204 | !to || IsBadStringPtr (to, MAX_PATH)) | |
205 | { | |
206 | log (LOG_TIMESTAMP, "invalid string in from or to parameters to move"); | |
207 | return 1; | |
208 | } | |
209 | /* iterate through the known url prefixes */ | |
210 | if (!strncasecmp ("file://", from, 7)) | |
211 | { | |
212 | /* TODO: allow 'move' to cygfile url's */ | |
bb849dbd | 213 | if (strncasecmp ("file://", to, 7)) |
7c7034e8 RC |
214 | return io_stream::move_copy (from, to); |
215 | return io_stream_file::move (&from[7], &to[7]); | |
216 | } | |
217 | if (!strncasecmp ("cygfile://", from, 10)) | |
218 | { | |
219 | /* TODO: allow -> file urls */ | |
bb849dbd | 220 | if (strncasecmp ("cygfile://", to, 10)) |
7c7034e8 RC |
221 | return io_stream::move_copy (from, to); |
222 | return io_stream_cygfile::move (&from[10], &to[10]); | |
223 | } | |
224 | #if 0 | |
225 | if (!strmcasecmp ("http://", from, 7)) | |
226 | { | |
227 | /* http urls can symlink to http or ftp url's */ | |
228 | } | |
b24c88b3 RC |
229 | #endif |
230 | log (LOG_TIMESTAMP, "Unsupported url providers for %s", from); | |
231 | return 1; | |
232 | } | |
233 | ||
234 | char * | |
235 | io_stream::gets (char *buffer, size_t length) | |
236 | { | |
237 | char *pos = buffer; | |
238 | size_t count = 0; | |
239 | while (count + 1 < length && read (pos, 1) == 1) | |
240 | { | |
241 | count++; | |
242 | pos++; | |
243 | if (*(pos - 1) == '\n') | |
244 | { | |
edef4f57 CV |
245 | --pos; /* end of line, remove from buffer */ |
246 | if (pos > buffer && *(pos - 1) == '\r') | |
247 | --pos; | |
b24c88b3 RC |
248 | break; |
249 | } | |
250 | } | |
251 | if (count == 0 || error ()) | |
252 | /* EOF when no chars found, or an error */ | |
253 | return NULL; | |
254 | *pos = '\0'; | |
255 | return buffer; | |
256 | } | |
257 | ||
258 | int | |
259 | io_stream::exists (const char *name) | |
260 | { | |
261 | if (!name || IsBadStringPtr (name, MAX_PATH) || !name[0]) | |
262 | return 1; | |
263 | /* iterate through the known url prefix's */ | |
264 | if (!strncasecmp ("file://", name, 7)) | |
265 | return io_stream_file::exists (&name[7]); | |
266 | if (!strncasecmp ("cygfile://", name, 10)) | |
267 | return io_stream_cygfile::exists (&name[10]); | |
268 | return 1; | |
269 | } | |
270 | ||
271 | /* virtual members */ | |
272 | ||
273 | io_stream::~io_stream () | |
274 | { | |
e9440f0f RC |
275 | if (!destroyed) |
276 | log (LOG_TIMESTAMP, "io_stream::~io_stream: It looks like a class hasn't overriden the destructor!"); | |
b24c88b3 RC |
277 | return; |
278 | } |