]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Copyright (c) 2001, 2002, 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 "LogSingleton.h" | |
28 | ||
29 | #include "io_stream.h" | |
30 | ||
31 | #include <stdexcept> | |
32 | #include "IOStreamProvider.h" | |
33 | #include <map> | |
34 | #include "String++.h" | |
35 | ||
36 | using namespace std; | |
37 | ||
38 | typedef map <std::string, IOStreamProvider *, casecompare_lt_op> providersType; | |
39 | static providersType *providers; | |
40 | static size_t longestPrefix = 0; | |
41 | static int inited = 0; | |
42 | ||
43 | void | |
44 | io_stream::registerProvider (IOStreamProvider &theProvider, | |
45 | const std::string& urlPrefix) | |
46 | { | |
47 | if (!inited) | |
48 | { | |
49 | providers = new providersType; | |
50 | inited = true; | |
51 | } | |
52 | theProvider.key = urlPrefix; | |
53 | if (providers->find (urlPrefix) != providers->end()) | |
54 | throw new invalid_argument ("urlPrefix already registered!"); | |
55 | (*providers)[urlPrefix] = &theProvider; | |
56 | if (urlPrefix.size() > longestPrefix) | |
57 | longestPrefix = urlPrefix.size(); | |
58 | } | |
59 | ||
60 | static IOStreamProvider const * | |
61 | findProvider (const std::string& path) | |
62 | { | |
63 | if (path.size() < longestPrefix) | |
64 | return NULL; | |
65 | for (providersType::const_iterator i = providers->begin(); | |
66 | i != providers->end(); ++i) | |
67 | { | |
68 | if (!casecompare(path, i->first, i->first.size())) | |
69 | return i->second; | |
70 | } | |
71 | return NULL; | |
72 | } | |
73 | ||
74 | /* Static members */ | |
75 | io_stream * | |
76 | io_stream::factory (io_stream * parent) | |
77 | { | |
78 | /* something like, | |
79 | * if !next_file_name | |
80 | * return NULL | |
81 | * switch (magic_id(peek (parent), max_magic_length)) | |
82 | * case io_stream * foo = new tar | |
83 | * case io_stream * foo = new bz2 | |
84 | * return foo | |
85 | */ | |
86 | log (LOG_TIMESTAMP) << "io_stream::factory has been called" << endLog; | |
87 | return NULL; | |
88 | } | |
89 | ||
90 | #define url_scheme_not_registered(name) \ | |
91 | throw new invalid_argument ((std::string("URL Scheme for '")+ \ | |
92 | name+"' not registered!").c_str()) | |
93 | ||
94 | io_stream * | |
95 | io_stream::open (const std::string& name, const std::string& mode) | |
96 | { | |
97 | IOStreamProvider const *p = findProvider (name); | |
98 | if (!p) | |
99 | url_scheme_not_registered (name); | |
100 | io_stream *rv = p->open (&name.c_str()[p->key.size()], mode); | |
101 | if (!rv->error ()) | |
102 | return rv; | |
103 | delete rv; | |
104 | return NULL; | |
105 | } | |
106 | ||
107 | int | |
108 | io_stream::mkpath_p (path_type_t isadir, const std::string& name) | |
109 | { | |
110 | IOStreamProvider const *p = findProvider (name); | |
111 | if (!p) | |
112 | url_scheme_not_registered (name); | |
113 | return p->mkdir_p (isadir, &name.c_str()[p->key.size()]); | |
114 | } | |
115 | ||
116 | /* remove a file or directory. */ | |
117 | int | |
118 | io_stream::remove (const std::string& name) | |
119 | { | |
120 | IOStreamProvider const *p = findProvider (name); | |
121 | if (!p) | |
122 | url_scheme_not_registered (name); | |
123 | return p->remove (&name.c_str()[p->key.size()]); | |
124 | } | |
125 | ||
126 | int | |
127 | io_stream::mklink (const std::string& from, const std::string& to, | |
128 | io_stream_link_t linktype) | |
129 | { | |
130 | log (LOG_BABBLE) << "io_stream::mklink (" << from << "->" << to << ")" | |
131 | << endLog; | |
132 | IOStreamProvider const *fromp = findProvider (from); | |
133 | IOStreamProvider const *top = findProvider (to); | |
134 | if (!fromp) | |
135 | url_scheme_not_registered (from); | |
136 | if (!top) | |
137 | url_scheme_not_registered (to); | |
138 | if (fromp != top) | |
139 | throw new invalid_argument ("Attempt to link across url providers."); | |
140 | return fromp->mklink (&from.c_str()[fromp->key.size()], | |
141 | &to.c_str()[top->key.size()], linktype); | |
142 | } | |
143 | ||
144 | int | |
145 | io_stream::move_copy (const std::string& from, const std::string& to) | |
146 | { | |
147 | /* parameters are ok - checked before calling us, and we are private */ | |
148 | io_stream *in = io_stream::open (to, "wb"); | |
149 | io_stream *out = io_stream::open (from, "rb"); | |
150 | if (io_stream::copy (in, out)) | |
151 | { | |
152 | log (LOG_TIMESTAMP) << "Failed copy of " << from << " to " << to | |
153 | << endLog; | |
154 | delete out; | |
155 | io_stream::remove (to); | |
156 | delete in; | |
157 | return 1; | |
158 | } | |
159 | /* TODO: | |
160 | out->set_mtime (in->get_mtime ()); | |
161 | */ | |
162 | delete in; | |
163 | delete out; | |
164 | io_stream::remove (from); | |
165 | return 0; | |
166 | } | |
167 | ||
168 | ssize_t io_stream::copy (io_stream * in, io_stream * out) | |
169 | { | |
170 | if (!in || !out) | |
171 | return -1; | |
172 | char | |
173 | buffer[16384]; | |
174 | ssize_t | |
175 | countin, | |
176 | countout; | |
177 | while ((countin = in->read (buffer, 16384)) > 0) | |
178 | { | |
179 | countout = out->write (buffer, countin); | |
180 | if (countout != countin) | |
181 | { | |
182 | log (LOG_TIMESTAMP) << "io_stream::copy failed to write " | |
183 | << countin << " bytes" << endLog; | |
184 | return countout ? countout : -1; | |
185 | } | |
186 | } | |
187 | // XXX FIXME: What if countin < 0? return an error | |
188 | ||
189 | /* TODO: | |
190 | out->set_mtime (in->get_mtime ()); | |
191 | */ | |
192 | return 0; | |
193 | } | |
194 | ||
195 | int | |
196 | io_stream::move (const std::string& from, const std::string& to) | |
197 | { | |
198 | IOStreamProvider const *fromp = findProvider (from); | |
199 | IOStreamProvider const *top = findProvider (to); | |
200 | if (!fromp) | |
201 | url_scheme_not_registered (from); | |
202 | if (!top) | |
203 | url_scheme_not_registered (to); | |
204 | if (fromp != top) | |
205 | return io_stream::move_copy (from, to); | |
206 | return fromp->move (&from.c_str()[fromp->key.size()], | |
207 | &to.c_str()[top->key.size()]); | |
208 | } | |
209 | ||
210 | char * | |
211 | io_stream::gets (char *buffer, size_t length) | |
212 | { | |
213 | char *pos = buffer; | |
214 | size_t count = 0; | |
215 | while (count + 1 < length && read (pos, 1) == 1) | |
216 | { | |
217 | count++; | |
218 | pos++; | |
219 | if (*(pos - 1) == '\n') | |
220 | { | |
221 | --pos; /* end of line, remove from buffer */ | |
222 | if (pos > buffer && *(pos - 1) == '\r') | |
223 | --pos; | |
224 | break; | |
225 | } | |
226 | } | |
227 | if (count == 0 || error ()) | |
228 | /* EOF when no chars found, or an error */ | |
229 | return NULL; | |
230 | *pos = '\0'; | |
231 | return buffer; | |
232 | } | |
233 | ||
234 | int | |
235 | io_stream::exists (const std::string& name) | |
236 | { | |
237 | IOStreamProvider const *p = findProvider (name); | |
238 | if (!p) | |
239 | url_scheme_not_registered (name); | |
240 | return p->exists (&name.c_str()[p->key.size()]); | |
241 | } | |
242 | ||
243 | /* virtual members */ | |
244 | ||
245 | io_stream::~io_stream () {} |