|
1
|
1 #include "foobar2000-sdk-pch.h"
|
|
|
2 #include "filesystem.h"
|
|
|
3 #include "unpack.h"
|
|
|
4 #include "archive.h"
|
|
|
5 #include "hasher_md5.h"
|
|
|
6 #include "mem_block_container.h"
|
|
|
7 #include "filesystem_transacted.h"
|
|
|
8
|
|
|
9 static constexpr char unpack_prefix[] = "unpack://";
|
|
|
10 static constexpr unsigned unpack_prefix_len = 9;
|
|
|
11
|
|
|
12
|
|
|
13 #ifndef _WIN32
|
|
|
14 #include <unistd.h>
|
|
|
15 #include <sys/stat.h>
|
|
|
16 #include <errno.h>
|
|
|
17 #endif
|
|
|
18
|
|
|
19 void unpacker::g_open(service_ptr_t<file> & p_out,const service_ptr_t<file> & p,abort_callback & p_abort)
|
|
|
20 {
|
|
|
21 for (auto ptr : enumerate()) {
|
|
|
22 p->reopen(p_abort);
|
|
|
23 try {
|
|
|
24 ptr->open(p_out, p, p_abort);
|
|
|
25 return;
|
|
|
26 } catch (exception_io_data const&) {}
|
|
|
27 }
|
|
|
28 throw exception_io_data();
|
|
|
29 }
|
|
|
30
|
|
|
31 void file::seek_probe(t_filesize p_position, abort_callback & p_abort) {
|
|
|
32 try { seek(p_position, p_abort); } catch(exception_io_seek_out_of_range const &) {throw exception_io_data();}
|
|
|
33 }
|
|
|
34
|
|
|
35 void file::seek_ex(t_sfilesize p_position, file::t_seek_mode p_mode, abort_callback &p_abort) {
|
|
|
36 switch(p_mode) {
|
|
|
37 case seek_from_beginning:
|
|
|
38 seek(p_position,p_abort);
|
|
|
39 break;
|
|
|
40 case seek_from_current:
|
|
|
41 seek(p_position + get_position(p_abort),p_abort);
|
|
|
42 break;
|
|
|
43 case seek_from_eof:
|
|
|
44 seek(p_position + get_size_ex(p_abort),p_abort);
|
|
|
45 break;
|
|
|
46 default:
|
|
|
47 throw exception_io_data();
|
|
|
48 }
|
|
|
49 }
|
|
|
50
|
|
|
51 static void makeBuffer(pfc::array_t<uint8_t> & buffer, size_t size) {
|
|
|
52 for(;;) {// Tolerant malloc - allocate a smaller buffer if we're unable to acquire the requested size.
|
|
|
53 try {
|
|
|
54 buffer.set_size_discard( size );
|
|
|
55 return;
|
|
|
56 } catch(std::bad_alloc const &) {
|
|
|
57 if (size < 256) throw;
|
|
|
58 size >>= 1;
|
|
|
59 }
|
|
|
60 }
|
|
|
61 }
|
|
|
62
|
|
|
63 t_filesize file::g_transfer(stream_reader * p_src,stream_writer * p_dst,t_filesize p_bytes,abort_callback & p_abort) {
|
|
|
64 pfc::array_t<t_uint8> temp;
|
|
|
65 makeBuffer(temp, (t_size)pfc::min_t<t_filesize>(1024*1024*8,p_bytes));
|
|
|
66 void* ptr = temp.get_ptr();
|
|
|
67 t_filesize done = 0;
|
|
|
68 while(done<p_bytes) {
|
|
|
69 p_abort.check_e();
|
|
|
70 t_size delta = (t_size)pfc::min_t<t_filesize>(temp.get_size(),p_bytes-done);
|
|
|
71 delta = p_src->read(ptr,delta,p_abort);
|
|
|
72 if (delta<=0) break;
|
|
|
73 p_dst->write(ptr,delta,p_abort);
|
|
|
74 done += delta;
|
|
|
75 }
|
|
|
76 return done;
|
|
|
77 }
|
|
|
78
|
|
|
79 void file::g_transfer_object(stream_reader * p_src,stream_writer * p_dst,t_filesize p_bytes,abort_callback & p_abort) {
|
|
|
80 if (g_transfer(p_src,p_dst,p_bytes,p_abort) != p_bytes)
|
|
|
81 throw exception_io_data_truncation();
|
|
|
82 }
|
|
|
83
|
|
|
84
|
|
|
85 void filesystem::g_get_canonical_path(const char * path,pfc::string_base & out)
|
|
|
86 {
|
|
|
87 // TRACK_CALL_TEXT("filesystem::g_get_canonical_path");
|
|
|
88 for (auto ptr : enumerate()) {
|
|
|
89 if (ptr->get_canonical_path(path, out)) return;
|
|
|
90 }
|
|
|
91 //no one wants to process this, let's copy over
|
|
|
92 out = path;
|
|
|
93 }
|
|
|
94
|
|
|
95 void filesystem::g_get_display_path(const char* path, pfc::string_base& out, filesystem::ptr& reuseMe) {
|
|
|
96
|
|
|
97 if (reuseMe.is_valid() && reuseMe->is_our_path(path)) {
|
|
|
98 if (!reuseMe->get_display_path(path, out)) {
|
|
|
99 // should not get here
|
|
|
100 out = path;
|
|
|
101 }
|
|
|
102 } else {
|
|
|
103 if (!g_get_interface(reuseMe, path)) {
|
|
|
104 out = path;
|
|
|
105 return;
|
|
|
106 }
|
|
|
107 if (!reuseMe->get_display_path(path, out)) {
|
|
|
108 // should not get here
|
|
|
109 out = path;
|
|
|
110 }
|
|
|
111 }
|
|
|
112 }
|
|
|
113
|
|
|
114 void filesystem::g_get_display_path(const char * path,pfc::string_base & out)
|
|
|
115 {
|
|
|
116 // TRACK_CALL_TEXT("filesystem::g_get_display_path");
|
|
|
117 service_ptr_t<filesystem> ptr;
|
|
|
118 if (!g_get_interface(ptr,path))
|
|
|
119 {
|
|
|
120 //no one wants to process this, let's copy over
|
|
|
121 out = path;
|
|
|
122 }
|
|
|
123 else
|
|
|
124 {
|
|
|
125 if (!ptr->get_display_path(path,out))
|
|
|
126 out = path;
|
|
|
127 }
|
|
|
128 }
|
|
|
129
|
|
|
130 pfc::string8 filesystem::g_get_native_path( const char * path, abort_callback & a ) {
|
|
|
131 pfc::string8 ret;
|
|
|
132 g_get_native_path( path, ret, a);
|
|
|
133 return ret;
|
|
|
134 }
|
|
|
135
|
|
|
136 bool filesystem::g_get_native_path( const char * path, pfc::string_base & out, abort_callback & a) {
|
|
|
137 // Is proper file:// path?
|
|
|
138 if (foobar2000_io::extract_native_path( path, out ) ) return true;
|
|
|
139
|
|
|
140 {
|
|
|
141 filesystem_v3::ptr fs;
|
|
|
142 if (fs &= tryGet(path)) {
|
|
|
143 auto n = fs->getNativePath(path, a);
|
|
|
144 if (n.is_valid()) {
|
|
|
145 out = n->c_str(); return true;
|
|
|
146 }
|
|
|
147 }
|
|
|
148 }
|
|
|
149
|
|
|
150 // Set anyway
|
|
|
151 out = path;
|
|
|
152
|
|
|
153 // Maybe just a file:// less local path? Check for other protocol markers
|
|
|
154 // If no :// present, return true anyway
|
|
|
155 return strstr( path, "://" ) == NULL;
|
|
|
156 }
|
|
|
157
|
|
|
158 filesystem::ptr filesystem::getLocalFS() {
|
|
|
159 return get("file://dummy");
|
|
|
160 }
|
|
|
161
|
|
|
162 filesystem::ptr filesystem::tryGet(const char* path) {
|
|
|
163 filesystem::ptr rv;
|
|
|
164 g_get_interface(rv, path);
|
|
|
165 return rv;
|
|
|
166 }
|
|
|
167
|
|
|
168 filesystem::ptr filesystem::g_get_interface(const char * path) {
|
|
|
169 filesystem::ptr rv;
|
|
|
170 if (!g_get_interface(rv, path)) throw exception_io_no_handler_for_path();
|
|
|
171 return rv;
|
|
|
172
|
|
|
173 }
|
|
|
174
|
|
|
175 #define USE_FSCACHE 1
|
|
|
176 #if USE_FSCACHE
|
|
|
177 #include <unordered_map>
|
|
|
178
|
|
|
179 static pfc::readWriteLock fsCacheGuard;
|
|
|
180
|
|
|
181 typedef size_t protoHash_t;
|
|
|
182 static protoHash_t protoHash(const char * URL) {
|
|
|
183 const char* delim = strstr(URL, "://");
|
|
|
184 if (delim == nullptr) return 0;
|
|
|
185
|
|
|
186 union {
|
|
|
187 protoHash_t hash;
|
|
|
188 char chars[sizeof(protoHash_t)];
|
|
|
189 } u;
|
|
|
190 u.hash = 0;
|
|
|
191 unsigned i = 0;
|
|
|
192 for (const char* walk = URL; walk != delim; ++walk) {
|
|
|
193 u.chars[i] ^= pfc::ascii_tolower_lookup(*walk);
|
|
|
194 i = (i + 1) % std::size(u.chars);
|
|
|
195 }
|
|
|
196 return u.hash;
|
|
|
197 }
|
|
|
198
|
|
|
199 // Do not use service_ptr in static objects, do not try to release them in static object destructor
|
|
|
200 static std::unordered_multimap< protoHash_t, filesystem* > fsCache;
|
|
|
201
|
|
|
202 static bool read_fs_cache(protoHash_t key, const char * path, filesystem::ptr& ret) {
|
|
|
203 auto range = fsCache.equal_range(key);
|
|
|
204 for (auto walk = range.first; walk != range.second; ++walk) {
|
|
|
205 if (walk->second->is_our_path(path)) {
|
|
|
206 ret = walk->second;
|
|
|
207 return true;
|
|
|
208 }
|
|
|
209 }
|
|
|
210 return false;
|
|
|
211 }
|
|
|
212
|
|
|
213 bool filesystem::g_get_interface(service_ptr_t<filesystem> & p_out,const char * path)
|
|
|
214 {
|
|
|
215 PFC_ASSERT( path != nullptr );
|
|
|
216 PFC_ASSERT( path[0] != 0 );
|
|
|
217
|
|
|
218 const auto key = protoHash(path);
|
|
|
219
|
|
|
220 {
|
|
|
221 PFC_INSYNC_READ(fsCacheGuard);
|
|
|
222 if (read_fs_cache(key, path, p_out)) return true;
|
|
|
223 }
|
|
|
224
|
|
|
225 for (auto ptr : enumerate()) {
|
|
|
226 if (ptr->is_our_path(path)) {
|
|
|
227 {
|
|
|
228 PFC_INSYNC_WRITE(fsCacheGuard);
|
|
|
229 filesystem::ptr dummy; // make sure it didn't just get added
|
|
|
230 if (!read_fs_cache(key, path, dummy)) {
|
|
|
231 auto addref = ptr;
|
|
|
232 fsCache.insert({ key, addref.detach()});
|
|
|
233 }
|
|
|
234 }
|
|
|
235 p_out = std::move(ptr);
|
|
|
236 return true;
|
|
|
237 }
|
|
|
238 }
|
|
|
239 return false;
|
|
|
240 }
|
|
|
241
|
|
|
242
|
|
|
243 #else
|
|
|
244 bool filesystem::g_get_interface(service_ptr_t<filesystem>& p_out, const char* path)
|
|
|
245 {
|
|
|
246 PFC_ASSERT(path != nullptr);
|
|
|
247 PFC_ASSERT(path[0] != 0);
|
|
|
248
|
|
|
249 for (auto ptr : enumerate()) {
|
|
|
250 if (ptr->is_our_path(path)) {
|
|
|
251 p_out = std::move(ptr);
|
|
|
252 return true;
|
|
|
253 }
|
|
|
254 }
|
|
|
255 return false;
|
|
|
256 }
|
|
|
257
|
|
|
258 #endif
|
|
|
259
|
|
|
260 void filesystem::g_open(service_ptr_t<file> & p_out,const char * path,t_open_mode mode,abort_callback & p_abort)
|
|
|
261 {
|
|
|
262 TRACK_CALL_TEXT("filesystem::g_open");
|
|
|
263 g_get_interface(path)->open(p_out,path,mode,p_abort);
|
|
|
264 }
|
|
|
265
|
|
|
266
|
|
|
267 void filesystem::g_open_timeout(service_ptr_t<file> & p_out,const char * p_path,t_open_mode p_mode,double p_timeout,abort_callback & p_abort) {
|
|
|
268 FB2K_RETRY_ON_SHARING_VIOLATION( g_open(p_out, p_path, p_mode, p_abort), p_abort, p_timeout);
|
|
|
269 }
|
|
|
270
|
|
|
271 bool filesystem::g_exists(const char * p_path,abort_callback & p_abort)
|
|
|
272 {
|
|
|
273 t_filestats stats;
|
|
|
274 bool dummy;
|
|
|
275 try {
|
|
|
276 g_get_stats(p_path,stats,dummy,p_abort);
|
|
|
277 } catch(exception_io_not_found const &) {return false;}
|
|
|
278 return true;
|
|
|
279 }
|
|
|
280
|
|
|
281 bool filesystem::g_exists_writeable(const char * p_path,abort_callback & p_abort)
|
|
|
282 {
|
|
|
283 t_filestats stats;
|
|
|
284 bool writeable;
|
|
|
285 try {
|
|
|
286 g_get_stats(p_path,stats,writeable,p_abort);
|
|
|
287 } catch(exception_io_not_found const &) {return false;}
|
|
|
288 return writeable;
|
|
|
289 }
|
|
|
290
|
|
|
291 void filesystem::g_remove(const char * p_path,abort_callback & p_abort) {
|
|
|
292 g_get_interface(p_path)->remove(p_path,p_abort);
|
|
|
293 }
|
|
|
294
|
|
|
295 void filesystem::g_remove_timeout(const char * p_path,double p_timeout,abort_callback & p_abort) {
|
|
|
296 FB2K_RETRY_FILE_MOVE( g_remove(p_path, p_abort), p_abort, p_timeout );
|
|
|
297 }
|
|
|
298
|
|
|
299 void filesystem::g_move_timeout(const char * p_src,const char * p_dst,double p_timeout,abort_callback & p_abort) {
|
|
|
300 FB2K_RETRY_FILE_MOVE( g_move(p_src, p_dst, p_abort), p_abort, p_timeout );
|
|
|
301 }
|
|
|
302
|
|
|
303 void filesystem::g_copy_timeout(const char * p_src,const char * p_dst,double p_timeout,abort_callback & p_abort) {
|
|
|
304 FB2K_RETRY_FILE_MOVE( g_copy(p_src, p_dst, p_abort), p_abort, p_timeout );
|
|
|
305 }
|
|
|
306
|
|
|
307 void filesystem::g_create_directory(const char * p_path,abort_callback & p_abort)
|
|
|
308 {
|
|
|
309 g_get_interface(p_path)->create_directory(p_path,p_abort);
|
|
|
310 }
|
|
|
311
|
|
|
312 void filesystem::g_move(const char * src,const char * dst,abort_callback & p_abort) {
|
|
|
313 for (auto ptr : enumerate()) {
|
|
|
314 if (ptr->is_our_path(src) && ptr->is_our_path(dst)) {
|
|
|
315 ptr->move(src, dst, p_abort);
|
|
|
316 return;
|
|
|
317 }
|
|
|
318 }
|
|
|
319 throw exception_io_no_handler_for_path();
|
|
|
320 }
|
|
|
321
|
|
|
322 void filesystem::g_link(const char * p_src,const char * p_dst,abort_callback & p_abort) {
|
|
|
323 p_abort.check();
|
|
|
324 pfc::string8 srcN, dstN;
|
|
|
325 if (!extract_native_path(p_src, srcN) || !extract_native_path(p_dst, dstN)) throw exception_io_no_handler_for_path();
|
|
|
326 #ifdef _WIN32
|
|
|
327 WIN32_IO_OP( CreateHardLink( pfc::stringcvt::string_os_from_utf8( dstN ), pfc::stringcvt::string_os_from_utf8( srcN ), NULL) );
|
|
|
328 #else
|
|
|
329 NIX_IO_OP( symlink( srcN, dstN ) == 0 );
|
|
|
330 #endif
|
|
|
331 }
|
|
|
332
|
|
|
333 void filesystem::g_link_timeout(const char * p_src,const char * p_dst,double p_timeout,abort_callback & p_abort) {
|
|
|
334 FB2K_RETRY_FILE_MOVE( g_link(p_src, p_dst, p_abort), p_abort, p_timeout );
|
|
|
335 }
|
|
|
336
|
|
|
337
|
|
|
338 void filesystem::g_list_directory(const char * p_path,directory_callback & p_out,abort_callback & p_abort)
|
|
|
339 {
|
|
|
340 TRACK_CALL_TEXT("filesystem::g_list_directory");
|
|
|
341 g_get_interface(p_path)->list_directory(p_path,p_out,p_abort);
|
|
|
342 }
|
|
|
343
|
|
|
344
|
|
|
345 static void path_pack_string(pfc::string_base & out,const char * src)
|
|
|
346 {
|
|
|
347 out.add_char('|');
|
|
|
348 out << (unsigned) strlen(src);
|
|
|
349 out.add_char('|');
|
|
|
350 out << src;
|
|
|
351 out.add_char('|');
|
|
|
352 }
|
|
|
353
|
|
|
354 static int path_unpack_string(pfc::string_base & out,const char * src)
|
|
|
355 {
|
|
|
356 int ptr=0;
|
|
|
357 if (src[ptr++]!='|') return -1;
|
|
|
358 int len = atoi(src+ptr);
|
|
|
359 if (len<=0) return -1;
|
|
|
360 while(src[ptr]!=0 && src[ptr]!='|') ptr++;
|
|
|
361 if (src[ptr]!='|') return -1;
|
|
|
362 ptr++;
|
|
|
363 int start = ptr;
|
|
|
364 while(ptr-start<len)
|
|
|
365 {
|
|
|
366 if (src[ptr]==0) return -1;
|
|
|
367 ptr++;
|
|
|
368 }
|
|
|
369 if (src[ptr]!='|') return -1;
|
|
|
370 out.set_string(&src[start],len);
|
|
|
371 ptr++;
|
|
|
372 return ptr;
|
|
|
373 }
|
|
|
374
|
|
|
375
|
|
|
376 void filesystem::g_open_precache(service_ptr_t<file> & p_out,const char * p_path,abort_callback & p_abort) {
|
|
|
377 service_ptr_t<filesystem> fs = g_get_interface(p_path);
|
|
|
378 if (fs->is_remote(p_path)) throw exception_io_object_is_remote();
|
|
|
379 fs->open(p_out,p_path,open_mode_read,p_abort);
|
|
|
380 }
|
|
|
381
|
|
|
382 bool filesystem::g_is_remote(const char * p_path) {
|
|
|
383 return g_get_interface(p_path)->is_remote(p_path);
|
|
|
384 }
|
|
|
385
|
|
|
386 bool filesystem::g_is_recognized_and_remote(const char * p_path) {
|
|
|
387 service_ptr_t<filesystem> fs;
|
|
|
388 if (g_get_interface(fs,p_path)) return fs->is_remote(p_path);
|
|
|
389 else return false;
|
|
|
390 }
|
|
|
391
|
|
|
392 bool filesystem::g_is_remote_or_unrecognized(const char * p_path) {
|
|
|
393 service_ptr_t<filesystem> fs;
|
|
|
394 if (g_get_interface(fs,p_path)) return fs->is_remote(p_path);
|
|
|
395 else return true;
|
|
|
396 }
|
|
|
397
|
|
|
398 bool filesystem::g_relative_path_create(const char * file_path,const char * playlist_path,pfc::string_base & out)
|
|
|
399 {
|
|
|
400
|
|
|
401 bool rv = false;
|
|
|
402 service_ptr_t<filesystem> fs;
|
|
|
403
|
|
|
404 if (g_get_interface(fs,file_path))
|
|
|
405 rv = fs->relative_path_create(file_path,playlist_path,out);
|
|
|
406
|
|
|
407 return rv;
|
|
|
408 }
|
|
|
409
|
|
|
410 bool filesystem::g_relative_path_parse(const char * relative_path,const char * playlist_path,pfc::string_base & out)
|
|
|
411 {
|
|
|
412 for (auto ptr : enumerate()) {
|
|
|
413 if (ptr->relative_path_parse(relative_path, playlist_path, out)) return true;
|
|
|
414 }
|
|
|
415 return false;
|
|
|
416 }
|
|
|
417
|
|
|
418 namespace {
|
|
|
419 class archive_callback_lambda : public archive_callback {
|
|
|
420 private:
|
|
|
421 abort_callback& m_abort;
|
|
|
422 public:
|
|
|
423 bool is_aborting() const override { return m_abort.is_aborting(); }
|
|
|
424 abort_callback_event get_abort_event() const override { return m_abort.get_abort_event(); }
|
|
|
425
|
|
|
426 archive_callback_lambda(abort_callback& a) : m_abort(a) {}
|
|
|
427 bool on_entry(archive*, const char* url, const t_filestats& p_stats, const service_ptr_t<file>& p_reader) override {
|
|
|
428 f(url, p_stats, p_reader);
|
|
|
429 return true;
|
|
|
430 }
|
|
|
431
|
|
|
432 archive::list_func_t f;
|
|
|
433 };
|
|
|
434 }
|
|
|
435
|
|
|
436 void archive::archive_list(const char * path, file::ptr reader, list_func_t f, bool wantReaders, abort_callback& a ) {
|
|
|
437 archive_callback_lambda cb(a);
|
|
|
438 cb.f = f;
|
|
|
439 this->archive_list(path, reader, cb, wantReaders);
|
|
|
440 }
|
|
|
441
|
|
|
442 bool archive::is_our_archive( const char * path ) {
|
|
|
443 archive_v2::ptr v2;
|
|
|
444 if ( v2 &= this ) return v2->is_our_archive( path );
|
|
|
445 return true; // accept all files
|
|
|
446 }
|
|
|
447
|
|
|
448 void archive_impl::extract_filename_ext(const char * path, pfc::string_base & outFN) {
|
|
|
449 pfc::string8 dummy, subpath;
|
|
|
450 if (archive_impl::g_parse_unpack_path(path, dummy, subpath)) {
|
|
|
451 outFN = pfc::filename_ext_v2( subpath );
|
|
|
452 } else {
|
|
|
453 PFC_ASSERT(!"???");
|
|
|
454 filesystem_v3::extract_filename_ext(path, outFN);
|
|
|
455 }
|
|
|
456 }
|
|
|
457
|
|
|
458 bool archive_impl::get_display_name_short(const char* in, pfc::string_base& out) {
|
|
|
459 extract_filename_ext(in ,out);
|
|
|
460 return true;
|
|
|
461 }
|
|
|
462
|
|
|
463 bool archive_impl::get_canonical_path(const char * path,pfc::string_base & out)
|
|
|
464 {
|
|
|
465 if (is_our_path(path))
|
|
|
466 {
|
|
|
467 pfc::string8 archive,file,archive_canonical;
|
|
|
468 if (g_parse_unpack_path(path,archive,file))
|
|
|
469 {
|
|
|
470 g_get_canonical_path(archive,archive_canonical);
|
|
|
471 make_unpack_path(out,archive_canonical,file);
|
|
|
472
|
|
|
473 return true;
|
|
|
474 }
|
|
|
475 else return false;
|
|
|
476 }
|
|
|
477 else return false;
|
|
|
478 }
|
|
|
479
|
|
|
480 bool archive_impl::is_our_path(const char * path)
|
|
|
481 {
|
|
|
482 if (!g_is_unpack_path(path)) return false;
|
|
|
483 const char * type = get_archive_type();
|
|
|
484 path += 9;
|
|
|
485 while(*type)
|
|
|
486 {
|
|
|
487 if (*type!=*path) return false;
|
|
|
488 type++;
|
|
|
489 path++;
|
|
|
490 }
|
|
|
491 if (*path!='|') return false;
|
|
|
492 return true;
|
|
|
493 }
|
|
|
494
|
|
|
495 bool archive_impl::get_display_path(const char * path,pfc::string_base & out)
|
|
|
496 {
|
|
|
497 pfc::string8 archive,file;
|
|
|
498 if (g_parse_unpack_path(path,archive,file))
|
|
|
499 {
|
|
|
500 g_get_display_path(archive,out);
|
|
|
501 out.add_string("|");
|
|
|
502 out.add_string(file);
|
|
|
503 return true;
|
|
|
504 }
|
|
|
505 else return false;
|
|
|
506 }
|
|
|
507
|
|
|
508 void archive_impl::open(service_ptr_t<file> & p_out,const char * path,t_open_mode mode, abort_callback & p_abort)
|
|
|
509 {
|
|
|
510 if (mode != open_mode_read) throw exception_io_denied();
|
|
|
511 pfc::string8 archive,file;
|
|
|
512 if (!g_parse_unpack_path(path,archive,file)) throw exception_io_not_found();
|
|
|
513 open_archive(p_out,archive,file,p_abort);
|
|
|
514 }
|
|
|
515
|
|
|
516
|
|
|
517 void archive_impl::remove(const char * path,abort_callback & p_abort) {
|
|
|
518 (void)p_abort; (void)path;
|
|
|
519 pfc::throw_exception_with_message< exception_io_denied> ("Cannot delete files within archives");
|
|
|
520 }
|
|
|
521
|
|
|
522 void archive_impl::move(const char * src,const char * dst,abort_callback & p_abort) {
|
|
|
523 (void)p_abort; (void)src; (void)dst;
|
|
|
524 pfc::throw_exception_with_message< exception_io_denied> ("Cannot move files within archives");
|
|
|
525 }
|
|
|
526
|
|
|
527 void archive_impl::move_overwrite(const char* src, const char* dst, abort_callback& abort) {
|
|
|
528 (void)abort; (void)src; (void)dst;
|
|
|
529 pfc::throw_exception_with_message< exception_io_denied> ("Cannot move files within archives");
|
|
|
530 }
|
|
|
531
|
|
|
532 bool archive_impl::is_remote(const char * src) {
|
|
|
533 pfc::string8 archive,file;
|
|
|
534 if (g_parse_unpack_path(src,archive,file)) return g_is_remote(archive);
|
|
|
535 else throw exception_io_not_found();
|
|
|
536 }
|
|
|
537
|
|
|
538 bool archive_impl::relative_path_create(const char * file_path,const char * playlist_path,pfc::string_base & out) {
|
|
|
539 pfc::string8 archive,file;
|
|
|
540 if (g_parse_unpack_path(file_path,archive,file))
|
|
|
541 {
|
|
|
542 pfc::string8 archive_rel;
|
|
|
543 if (g_relative_path_create(archive,playlist_path,archive_rel))
|
|
|
544 {
|
|
|
545 pfc::string8 out_path;
|
|
|
546 make_unpack_path(out_path,archive_rel,file);
|
|
|
547 out.set_string(out_path);
|
|
|
548 return true;
|
|
|
549 }
|
|
|
550 }
|
|
|
551 return false;
|
|
|
552 }
|
|
|
553
|
|
|
554 bool archive_impl::relative_path_parse(const char * relative_path,const char * playlist_path,pfc::string_base & out)
|
|
|
555 {
|
|
|
556 if (!is_our_path(relative_path)) return false;
|
|
|
557 pfc::string8 archive_rel,file;
|
|
|
558 if (g_parse_unpack_path(relative_path,archive_rel,file))
|
|
|
559 {
|
|
|
560 pfc::string8 archive;
|
|
|
561 if (g_relative_path_parse(archive_rel,playlist_path,archive))
|
|
|
562 {
|
|
|
563 pfc::string8 out_path;
|
|
|
564 make_unpack_path(out_path,archive,file);
|
|
|
565 out.set_string(out_path);
|
|
|
566 return true;
|
|
|
567 }
|
|
|
568 }
|
|
|
569 return false;
|
|
|
570 }
|
|
|
571
|
|
|
572 bool archive_impl::g_parse_unpack_path_ex(const char * path,pfc::string_base & archive,pfc::string_base & file, pfc::string_base & type) {
|
|
|
573 PFC_ASSERT( g_is_unpack_path(path) );
|
|
|
574 const char * base = path + unpack_prefix_len; // strstr(path, "//");
|
|
|
575 const char * split = strchr(path,'|');
|
|
|
576 if (base == NULL || split == NULL || base > split) return false;
|
|
|
577 // base += 2;
|
|
|
578 type.set_string( base, split - base );
|
|
|
579 int delta = path_unpack_string(archive,split);
|
|
|
580 if (delta<0) return false;
|
|
|
581 split += delta;
|
|
|
582 file = split;
|
|
|
583 return true;
|
|
|
584 }
|
|
|
585 bool archive_impl::g_parse_unpack_path(const char * path,pfc::string_base & archive,pfc::string_base & file) {
|
|
|
586 PFC_ASSERT( g_is_unpack_path(path) );
|
|
|
587 path = strchr(path,'|');
|
|
|
588 if (!path) return false;
|
|
|
589 int delta = path_unpack_string(archive,path);
|
|
|
590 if (delta<0) return false;
|
|
|
591 path += delta;
|
|
|
592 file = path;
|
|
|
593 return true;
|
|
|
594 }
|
|
|
595
|
|
|
596 bool archive_impl::g_is_unpack_path(const char * path) {
|
|
|
597 return strncmp(path,unpack_prefix,unpack_prefix_len) == 0;
|
|
|
598 }
|
|
|
599
|
|
|
600 void archive_impl::g_make_unpack_path(pfc::string_base & path,const char * archive,const char * file,const char * name)
|
|
|
601 {
|
|
|
602 path = unpack_prefix;
|
|
|
603 path += name;
|
|
|
604 path_pack_string(path,archive);
|
|
|
605 path += file;
|
|
|
606 }
|
|
|
607
|
|
|
608 void archive_impl::make_unpack_path(pfc::string_base & path,const char * archive,const char * file) {g_make_unpack_path(path,archive,file,get_archive_type());}
|
|
|
609
|
|
|
610 fb2k::arrayRef archive_impl::archive_list_v4( fsItemFilePtr item, file::ptr readerOptional, abort_callback & a ) {
|
|
|
611
|
|
|
612 const auto baseStats = item->getStatsOpportunist();
|
|
|
613 PFC_ASSERT( ! baseStats.is_folder() );
|
|
|
614 auto ret = fb2k::arrayMutable::arrayWithCapacity(256);
|
|
|
615
|
|
|
616 auto reader = readerOptional;
|
|
|
617 if ( reader.is_empty() ) reader = item->openRead(a);
|
|
|
618 try {
|
|
|
619 this->archive_list( item->canonicalPath()->c_str(), reader, [&] ( const char * URL, t_filestats const & stats, file::ptr ) {
|
|
|
620 t_filestats2 stats2 = t_filestats2::from_legacy( stats );
|
|
|
621 stats2.set_file(); stats2.set_remote( baseStats.is_remote() ); stats2.set_readonly(true);
|
|
|
622 archive * blah = this; // multi inheritance fix, more than one path to filesystem which has makeItemFileStd()
|
|
|
623 ret->add(blah->makeItemFileStd(URL, stats2));
|
|
|
624 }, false, a);
|
|
|
625 } catch( exception_io_data const & ) {
|
|
|
626 if ( ret->count() == 0 ) throw;
|
|
|
627 }
|
|
|
628 return ret->makeConst();
|
|
|
629
|
|
|
630 }
|
|
|
631
|
|
|
632 namespace {
|
|
|
633
|
|
|
634 class directory_callback_isempty : public directory_callback
|
|
|
635 {
|
|
|
636 bool m_isempty;
|
|
|
637 public:
|
|
|
638 directory_callback_isempty() : m_isempty(true) {}
|
|
|
639 bool on_entry(filesystem *,abort_callback &,const char *,bool,const t_filestats &) override
|
|
|
640 {
|
|
|
641 m_isempty = false;
|
|
|
642 return false;
|
|
|
643 }
|
|
|
644 bool isempty() {return m_isempty;}
|
|
|
645 };
|
|
|
646
|
|
|
647 class directory_callback_dummy : public directory_callback
|
|
|
648 {
|
|
|
649 public:
|
|
|
650 bool on_entry(filesystem *,abort_callback &,const char *,bool,const t_filestats &) override {return false;}
|
|
|
651 };
|
|
|
652
|
|
|
653 }
|
|
|
654
|
|
|
655 bool filesystem::g_is_empty_directory(const char * path,abort_callback & p_abort)
|
|
|
656 {
|
|
|
657 directory_callback_isempty callback;
|
|
|
658 try {
|
|
|
659 g_list_directory(path,callback,p_abort);
|
|
|
660 } catch(exception_io const &) {return false;}
|
|
|
661 return callback.isempty();
|
|
|
662 }
|
|
|
663
|
|
|
664 bool filesystem::g_is_valid_directory(const char * path,abort_callback & p_abort) {
|
|
|
665 if ( path == NULL || path[0] == 0 ) return false;
|
|
|
666
|
|
|
667 return get(path)->directory_exists( path, p_abort );
|
|
|
668 }
|
|
|
669
|
|
|
670 bool directory_callback_impl::on_entry(filesystem * owner,abort_callback & p_abort,const char * url,bool is_subdirectory,const t_filestats & p_stats) {
|
|
|
671 p_abort.check_e();
|
|
|
672 if (is_subdirectory) {
|
|
|
673 if (m_recur) {
|
|
|
674 try {
|
|
|
675 owner->list_directory(url,*this,p_abort);
|
|
|
676 } catch(exception_io const &) {}
|
|
|
677 }
|
|
|
678 } else {
|
|
|
679 m_data.add_item(pfc::rcnew_t<t_entry>(url,p_stats));
|
|
|
680 }
|
|
|
681 return true;
|
|
|
682 }
|
|
|
683
|
|
|
684 namespace {
|
|
|
685 class directory_callback_impl_copy : public directory_callback
|
|
|
686 {
|
|
|
687 public:
|
|
|
688 directory_callback_impl_copy(const char * p_target, filesystem::ptr fs) : m_fs(fs)
|
|
|
689 {
|
|
|
690 m_target = p_target;
|
|
|
691 m_target.fix_dir_separator();
|
|
|
692 }
|
|
|
693
|
|
|
694 bool on_entry(filesystem * owner,abort_callback & p_abort,const char * url,bool is_subdirectory,const t_filestats & p_stats) override {
|
|
|
695 (void)p_stats;
|
|
|
696 const char * fn = url + pfc::scan_filename(url);
|
|
|
697 t_size truncat = m_target.length();
|
|
|
698 m_target += fn;
|
|
|
699 if (is_subdirectory) {
|
|
|
700 try {
|
|
|
701 m_fs->create_directory(m_target,p_abort);
|
|
|
702 } catch(exception_io_already_exists const &) {}
|
|
|
703 m_target.end_with_slash();
|
|
|
704 owner->list_directory(url,*this,p_abort);
|
|
|
705 } else {
|
|
|
706 _copy(url, m_target, owner, p_abort);
|
|
|
707 }
|
|
|
708 m_target.truncate(truncat);
|
|
|
709 return true;
|
|
|
710 }
|
|
|
711 void _copy(const char * src, const char * dst, filesystem * srcFS, abort_callback & p_abort) {
|
|
|
712 service_ptr_t<file> r_src,r_dst;
|
|
|
713 t_filesize size;
|
|
|
714
|
|
|
715 srcFS->open(r_src,src,filesystem::open_mode_read,p_abort);
|
|
|
716 size = r_src->get_size_ex(p_abort);
|
|
|
717 m_fs->open(r_dst,dst,filesystem::open_mode_write_new,p_abort);
|
|
|
718
|
|
|
719 if (size > 0) {
|
|
|
720 try {
|
|
|
721 file::g_transfer_object(r_src,r_dst,size,p_abort);
|
|
|
722 file::g_copy_timestamps(r_src, r_dst, p_abort);
|
|
|
723 } catch(...) {
|
|
|
724 r_dst.release();
|
|
|
725 try {m_fs->remove(dst,fb2k::noAbort);} catch(...) {}
|
|
|
726 throw;
|
|
|
727 }
|
|
|
728 }
|
|
|
729 }
|
|
|
730 private:
|
|
|
731 pfc::string8_fastalloc m_target;
|
|
|
732 filesystem::ptr m_fs;
|
|
|
733 };
|
|
|
734 }
|
|
|
735
|
|
|
736 file::ptr filesystem::openEx(const char * path, filesystem::t_open_mode mode, abort_callback & abort, double timeout) {
|
|
|
737 file::ptr f;
|
|
|
738 retryOnSharingViolation([&] {
|
|
|
739 this->open(f, path, mode, abort);
|
|
|
740 }, timeout, abort);
|
|
|
741 return f;
|
|
|
742 }
|
|
|
743
|
|
|
744 file::ptr filesystem::openRead(const char * path, abort_callback & abort, double timeout) {
|
|
|
745 return this->openEx(path, open_mode_read, abort, timeout);
|
|
|
746 }
|
|
|
747
|
|
|
748 file::ptr filesystem::openWriteExisting(const char * path, abort_callback & abort, double timeout) {
|
|
|
749 return this->openEx(path, open_mode_write_existing, abort, timeout);
|
|
|
750 }
|
|
|
751
|
|
|
752 file::ptr filesystem::openWriteNew(const char * path, abort_callback & abort, double timeout) {
|
|
|
753 return this->openEx( path, open_mode_write_new, abort, timeout );
|
|
|
754 }
|
|
|
755
|
|
|
756 void filesystem::remove_(const char* path, abort_callback& a, double timeout) {
|
|
|
757 retryOnSharingViolation(timeout, a, [&] {
|
|
|
758 this->remove(path, a);
|
|
|
759 });
|
|
|
760 }
|
|
|
761
|
|
|
762 void filesystem::copy_directory_contents(const char* p_src, const char* p_dst, abort_callback& p_abort) {
|
|
|
763 directory_callback_impl_copy cb(p_dst, this);
|
|
|
764 list_directory(p_src, cb, p_abort);
|
|
|
765 }
|
|
|
766
|
|
|
767 void filesystem::copy_directory(const char * src, const char * dst, abort_callback & p_abort) {
|
|
|
768 try {
|
|
|
769 this->create_directory( dst, p_abort );
|
|
|
770 } catch(exception_io_already_exists const &) {}
|
|
|
771 this->copy_directory_contents(src, dst, p_abort);
|
|
|
772 }
|
|
|
773
|
|
|
774 void filesystem::g_copy_directory(const char * src,const char * dst,abort_callback & p_abort) {
|
|
|
775 filesystem::ptr dstFS = filesystem::g_get_interface(dst);
|
|
|
776 try {
|
|
|
777 dstFS->create_directory( dst, p_abort );
|
|
|
778 } catch(exception_io_already_exists const &) {}
|
|
|
779 directory_callback_impl_copy cb(dst, dstFS);
|
|
|
780 g_list_directory(src,cb,p_abort);
|
|
|
781 }
|
|
|
782
|
|
|
783 void filesystem::g_copy(const char * src,const char * dst,abort_callback & p_abort) {
|
|
|
784 service_ptr_t<file> r_src,r_dst;
|
|
|
785 t_filesize size;
|
|
|
786
|
|
|
787 g_open(r_src,src,open_mode_read,p_abort);
|
|
|
788 size = r_src->get_size_ex(p_abort);
|
|
|
789 g_open(r_dst,dst,open_mode_write_new,p_abort);
|
|
|
790
|
|
|
791 if (size > 0) {
|
|
|
792 try {
|
|
|
793 file::g_transfer_object(r_src,r_dst,size,p_abort);
|
|
|
794 } catch(...) {
|
|
|
795 r_dst.release();
|
|
|
796 try {g_remove(dst,fb2k::noAbort);} catch(...) {}
|
|
|
797 throw;
|
|
|
798 }
|
|
|
799 }
|
|
|
800
|
|
|
801 try {
|
|
|
802 file::g_copy_timestamps(r_src, r_dst, p_abort);
|
|
|
803 } catch (exception_io const &) {}
|
|
|
804 }
|
|
|
805
|
|
|
806 void stream_reader::read_object(void * p_buffer,t_size p_bytes,abort_callback & p_abort) {
|
|
|
807 if (read(p_buffer,p_bytes,p_abort) != p_bytes) throw exception_io_data_truncation();
|
|
|
808 }
|
|
|
809
|
|
|
810 t_filestats file::get_stats(abort_callback & p_abort)
|
|
|
811 {
|
|
|
812 t_filestats temp;
|
|
|
813 temp.m_size = get_size(p_abort);
|
|
|
814 temp.m_timestamp = get_timestamp(p_abort);
|
|
|
815 return temp;
|
|
|
816 }
|
|
|
817
|
|
|
818 t_filesize stream_reader::skip(t_filesize p_bytes,abort_callback & p_abort)
|
|
|
819 {
|
|
|
820 t_uint8 temp[256];
|
|
|
821 t_filesize todo = p_bytes, done = 0;
|
|
|
822 while(todo > 0) {
|
|
|
823 t_size delta,deltadone;
|
|
|
824 delta = sizeof(temp);
|
|
|
825 if (delta > todo) delta = (t_size) todo;
|
|
|
826 deltadone = read(temp,delta,p_abort);
|
|
|
827 done += deltadone;
|
|
|
828 todo -= deltadone;
|
|
|
829 if (deltadone < delta) break;
|
|
|
830 }
|
|
|
831 return done;
|
|
|
832 }
|
|
|
833
|
|
|
834 void stream_reader::skip_object(t_filesize p_bytes,abort_callback & p_abort) {
|
|
|
835 if (skip(p_bytes,p_abort) != p_bytes) throw exception_io_data_truncation();
|
|
|
836 }
|
|
|
837
|
|
|
838 void filesystem::g_open_write_new(service_ptr_t<file> & p_out,const char * p_path,abort_callback & p_abort) {
|
|
|
839 g_open(p_out,p_path,open_mode_write_new,p_abort);
|
|
|
840 }
|
|
|
841 void file::g_transfer_file(const service_ptr_t<file> & p_from,const service_ptr_t<file> & p_to,abort_callback & p_abort) {
|
|
|
842 t_filesize length = p_from->get_size(p_abort);
|
|
|
843 p_from->reopen( p_abort );
|
|
|
844 // p_from->seek(0,p_abort);
|
|
|
845 p_to->seek(0,p_abort);
|
|
|
846 p_to->set_eof(p_abort);
|
|
|
847 if (length == filesize_invalid) {
|
|
|
848 g_transfer(p_from, p_to, filesize_invalid, p_abort);
|
|
|
849 } else if (length > 0) {
|
|
|
850 g_transfer_object(p_from,p_to,length,p_abort);
|
|
|
851 }
|
|
|
852 }
|
|
|
853
|
|
|
854 void filesystem::g_open_temp(service_ptr_t<file> & p_out,abort_callback & p_abort) {
|
|
|
855 g_open(p_out,"tempfile://",open_mode_write_new,p_abort);
|
|
|
856 }
|
|
|
857
|
|
|
858 void filesystem::g_open_tempmem(service_ptr_t<file> & p_out,abort_callback & p_abort) {
|
|
|
859 g_open(p_out,"tempmem://",open_mode_write_new,p_abort);
|
|
|
860 }
|
|
|
861
|
|
|
862 file::ptr filesystem::g_open_tempmem() {
|
|
|
863 file::ptr f; g_open_tempmem(f, fb2k::noAbort); return f;
|
|
|
864 }
|
|
|
865
|
|
|
866 void archive_impl::list_directory(const char * p_path,directory_callback & p_out,abort_callback & p_abort) {
|
|
|
867 (void)p_path; (void)p_out; (void)p_abort;
|
|
|
868 throw exception_io_not_found();
|
|
|
869 }
|
|
|
870
|
|
|
871 void archive_impl::list_directory_ex(const char* p_path, directory_callback& p_out, unsigned listMode, abort_callback& p_abort) {
|
|
|
872 (void)p_path; (void)p_out; (void)listMode; (void)p_abort;
|
|
|
873 throw exception_io_not_found();
|
|
|
874 }
|
|
|
875
|
|
|
876 void archive_impl::list_directory_v3(const char* path, directory_callback_v3& callback, unsigned listMode, abort_callback& p_abort) {
|
|
|
877 (void)path; (void)callback; (void)listMode; (void)p_abort;
|
|
|
878 throw exception_io_not_found();
|
|
|
879 }
|
|
|
880
|
|
|
881 void archive_impl::create_directory(const char *,abort_callback &) {
|
|
|
882 throw exception_io_denied();
|
|
|
883 }
|
|
|
884
|
|
|
885 void archive_impl::make_directory(const char*, abort_callback&, bool*) {
|
|
|
886 throw exception_io_denied();
|
|
|
887 }
|
|
|
888
|
|
|
889 void filesystem::g_get_stats(const char * p_path,t_filestats & p_stats,bool & p_is_writeable,abort_callback & p_abort) {
|
|
|
890 TRACK_CALL_TEXT("filesystem::g_get_stats");
|
|
|
891 return g_get_interface(p_path)->get_stats(p_path,p_stats,p_is_writeable,p_abort);
|
|
|
892 }
|
|
|
893
|
|
|
894 // Due to multi inheritance from archive and filesystem_v3, filesystem_v3's get_stats() isn't overriding archive's method. Fix that here.
|
|
|
895 void archive_impl::get_stats(const char* p_path, t_filestats& p_stats, bool& p_is_writeable, abort_callback& p_abort) {
|
|
|
896 filesystem_v3::get_stats(p_path, p_stats, p_is_writeable, p_abort);
|
|
|
897 }
|
|
|
898
|
|
|
899 t_filestats2 archive_impl::get_stats2(const char * p_path,unsigned s2flags,abort_callback & p_abort) {
|
|
|
900 pfc::string8 archive,file;
|
|
|
901 if (g_parse_unpack_path(p_path,archive,file)) {
|
|
|
902 if (g_is_remote(archive)) throw exception_io_object_is_remote();
|
|
|
903 t_filestats2 ret = get_stats2_in_archive(archive,file,s2flags,p_abort);
|
|
|
904 ret.set_readonly(true);
|
|
|
905 ret.set_folder(false);
|
|
|
906 ret.set_remote(false);
|
|
|
907 return ret;
|
|
|
908 }
|
|
|
909 else throw exception_io_not_found();
|
|
|
910 }
|
|
|
911
|
|
|
912
|
|
|
913 bool file::is_eof(abort_callback & p_abort) {
|
|
|
914 t_filesize position,size;
|
|
|
915 position = get_position(p_abort);
|
|
|
916 size = get_size(p_abort);
|
|
|
917 if (size == filesize_invalid) return false;
|
|
|
918 return position >= size;
|
|
|
919 }
|
|
|
920
|
|
|
921 t_filetimestamp foobar2000_io::filetimestamp_from_system_timer()
|
|
|
922 {
|
|
|
923 return pfc::fileTimeNow();
|
|
|
924 }
|
|
|
925
|
|
|
926 void stream_reader::read_string_ex(pfc::string_base & p_out,t_size p_bytes,abort_callback & p_abort) {
|
|
|
927 const t_size expBase = 64*1024;
|
|
|
928 if (p_bytes > expBase) {
|
|
|
929 pfc::array_t<char> temp;
|
|
|
930 t_size allocWalk = expBase;
|
|
|
931 t_size done = 0;
|
|
|
932 for(;;) {
|
|
|
933 const t_size target = pfc::min_t(allocWalk, p_bytes);
|
|
|
934 temp.set_size(target);
|
|
|
935 read_object(temp.get_ptr() + done, target - done, p_abort);
|
|
|
936 if (target == p_bytes) break;
|
|
|
937 done = target;
|
|
|
938 allocWalk <<= 1;
|
|
|
939 }
|
|
|
940 p_out.set_string(temp.get_ptr(), p_bytes);
|
|
|
941 } else {
|
|
|
942 pfc::string_buffer buf(p_out, p_bytes);
|
|
|
943 read_object(buf.get_ptr(),p_bytes,p_abort);
|
|
|
944 }
|
|
|
945 }
|
|
|
946 void stream_reader::read_string(pfc::string_base & p_out,abort_callback & p_abort)
|
|
|
947 {
|
|
|
948 t_uint32 length;
|
|
|
949 read_lendian_t(length,p_abort);
|
|
|
950 read_string_ex(p_out,length,p_abort);
|
|
|
951 }
|
|
|
952
|
|
|
953 void stream_reader::read_string_raw(pfc::string_base & p_out,abort_callback & p_abort, size_t sanity) {
|
|
|
954 enum {delta = 1024};
|
|
|
955 char buffer[delta];
|
|
|
956 p_out.reset();
|
|
|
957 size_t didRead = 0;
|
|
|
958 for(;;) {
|
|
|
959 auto delta_done = read(buffer,delta,p_abort);
|
|
|
960 p_out.add_string(buffer,delta_done);
|
|
|
961 if (delta_done < delta) break;
|
|
|
962 didRead += delta;
|
|
|
963 if (didRead > sanity) throw exception_io_data();
|
|
|
964 }
|
|
|
965 }
|
|
|
966
|
|
|
967 void stream_writer::write_string(const char * p_string,t_size p_len,abort_callback & p_abort) {
|
|
|
968 t_uint32 len = pfc::downcast_guarded<t_uint32>(pfc::strlen_max(p_string,p_len));
|
|
|
969 write_lendian_t(len,p_abort);
|
|
|
970 write_object(p_string,len,p_abort);
|
|
|
971 }
|
|
|
972
|
|
|
973 void stream_writer::write_string(const char * p_string,abort_callback & p_abort) {
|
|
|
974 write_string(p_string,SIZE_MAX,p_abort);
|
|
|
975 }
|
|
|
976
|
|
|
977 void stream_writer::write_string_raw(const char * p_string,abort_callback & p_abort) {
|
|
|
978 write_object(p_string,strlen(p_string),p_abort);
|
|
|
979 }
|
|
|
980
|
|
|
981 void file::truncate(t_uint64 p_position,abort_callback & p_abort) {
|
|
|
982 if (p_position < get_size(p_abort)) resize(p_position,p_abort);
|
|
|
983 }
|
|
|
984
|
|
|
985
|
|
|
986 #ifdef _WIN32
|
|
|
987 namespace {
|
|
|
988 //rare/weird win32 errors that didn't make it to the main API
|
|
|
989 PFC_DECLARE_EXCEPTION(exception_io_device_not_ready, exception_io,"Device not ready");
|
|
|
990 PFC_DECLARE_EXCEPTION(exception_io_invalid_drive, exception_io_not_found,"Drive not found");
|
|
|
991 PFC_DECLARE_EXCEPTION(exception_io_win32, exception_io,"Generic win32 I/O error");
|
|
|
992 PFC_DECLARE_EXCEPTION(exception_io_buffer_overflow, exception_io,"The file name is too long");
|
|
|
993 PFC_DECLARE_EXCEPTION(exception_io_invalid_path_syntax, exception_io,"Invalid path syntax");
|
|
|
994
|
|
|
995 class exception_io_win32_ex : public exception_io_win32 {
|
|
|
996 public:
|
|
|
997 static pfc::string8 format(DWORD code) {
|
|
|
998 pfc::string8 ret;
|
|
|
999 ret << "I/O error (win32 ";
|
|
|
1000 if (code & 0x80000000) {
|
|
|
1001 ret << "0x" << pfc::format_hex(code, 8);
|
|
|
1002 } else {
|
|
|
1003 ret << "#" << (uint32_t)code;
|
|
|
1004 }
|
|
|
1005 ret << ")";
|
|
|
1006 return ret;
|
|
|
1007 }
|
|
|
1008 exception_io_win32_ex(DWORD p_code) : m_msg(format(p_code)) {}
|
|
|
1009 exception_io_win32_ex(const exception_io_win32_ex & p_other) {*this = p_other;}
|
|
|
1010 const char * what() const throw() {return m_msg;}
|
|
|
1011 private:
|
|
|
1012 pfc::string8 m_msg;
|
|
|
1013 };
|
|
|
1014 }
|
|
|
1015
|
|
|
1016 PFC_NORETURN void foobar2000_io::win32_file_write_failure(DWORD p_code, const char * path) {
|
|
|
1017 if (p_code == ERROR_ACCESS_DENIED) {
|
|
|
1018 const DWORD attr = uGetFileAttributes(path);
|
|
|
1019 if (attr != ~0 && (attr & FILE_ATTRIBUTE_READONLY) != 0) throw exception_io_denied_readonly();
|
|
|
1020 }
|
|
|
1021 exception_io_from_win32(p_code);
|
|
|
1022 }
|
|
|
1023
|
|
|
1024 PFC_NORETURN void foobar2000_io::exception_io_from_win32(DWORD p_code) {
|
|
|
1025 #if PFC_DEBUG
|
|
|
1026 PFC_DEBUGLOG << "exception_io_from_win32: " << p_code;
|
|
|
1027 #endif
|
|
|
1028 //pfc::string_fixed_t<32> debugMsg; debugMsg << "Win32 I/O error #" << (t_uint32)p_code;
|
|
|
1029 //TRACK_CALL_TEXT(debugMsg);
|
|
|
1030 switch(p_code) {
|
|
|
1031 case ERROR_ALREADY_EXISTS:
|
|
|
1032 case ERROR_FILE_EXISTS:
|
|
|
1033 throw exception_io_already_exists();
|
|
|
1034 case ERROR_NETWORK_ACCESS_DENIED:
|
|
|
1035 case ERROR_ACCESS_DENIED:
|
|
|
1036 throw exception_io_denied();
|
|
|
1037 case ERROR_WRITE_PROTECT:
|
|
|
1038 throw exception_io_write_protected();
|
|
|
1039 case ERROR_BUSY:
|
|
|
1040 case ERROR_PATH_BUSY:
|
|
|
1041 case ERROR_SHARING_VIOLATION:
|
|
|
1042 case ERROR_LOCK_VIOLATION:
|
|
|
1043 throw exception_io_sharing_violation();
|
|
|
1044 case ERROR_HANDLE_DISK_FULL:
|
|
|
1045 case ERROR_DISK_FULL:
|
|
|
1046 throw exception_io_device_full();
|
|
|
1047 case ERROR_FILE_NOT_FOUND:
|
|
|
1048 case ERROR_PATH_NOT_FOUND:
|
|
|
1049 throw exception_io_not_found();
|
|
|
1050 case ERROR_BROKEN_PIPE:
|
|
|
1051 case ERROR_NO_DATA:
|
|
|
1052 throw exception_io_no_data();
|
|
|
1053 case ERROR_NETWORK_UNREACHABLE:
|
|
|
1054 case ERROR_NETNAME_DELETED:
|
|
|
1055 throw exception_io_network_not_reachable();
|
|
|
1056 case ERROR_NOT_READY:
|
|
|
1057 throw exception_io_device_not_ready();
|
|
|
1058 case ERROR_NO_SUCH_DEVICE:
|
|
|
1059 case ERROR_INVALID_DRIVE:
|
|
|
1060 throw exception_io_invalid_drive();
|
|
|
1061 case ERROR_CRC:
|
|
|
1062 case ERROR_FILE_CORRUPT:
|
|
|
1063 case ERROR_DISK_CORRUPT:
|
|
|
1064 throw exception_io_file_corrupted();
|
|
|
1065 case ERROR_BUFFER_OVERFLOW:
|
|
|
1066 throw exception_io_buffer_overflow();
|
|
|
1067 case ERROR_DISK_CHANGE:
|
|
|
1068 throw exception_io_disk_change();
|
|
|
1069 case ERROR_DIR_NOT_EMPTY:
|
|
|
1070 throw exception_io_directory_not_empty();
|
|
|
1071 case ERROR_INVALID_NAME:
|
|
|
1072 throw exception_io_invalid_path_syntax();
|
|
|
1073 case ERROR_NO_SYSTEM_RESOURCES:
|
|
|
1074 case ERROR_NONPAGED_SYSTEM_RESOURCES:
|
|
|
1075 case ERROR_PAGED_SYSTEM_RESOURCES:
|
|
|
1076 case ERROR_WORKING_SET_QUOTA:
|
|
|
1077 case ERROR_PAGEFILE_QUOTA:
|
|
|
1078 case ERROR_COMMITMENT_LIMIT:
|
|
|
1079 throw exception_io("Insufficient system resources");
|
|
|
1080 case ERROR_IO_DEVICE:
|
|
|
1081 throw exception_io("Device error");
|
|
|
1082 case ERROR_BAD_NETPATH:
|
|
|
1083 // known to be inflicted by momentary net connectivity issues - NOT the same as exception_io_not_found
|
|
|
1084 throw exception_io_network_not_reachable("Network path not found");
|
|
|
1085 #if FB2K_SUPPORT_TRANSACTED_FILESYSTEM
|
|
|
1086 case ERROR_TRANSACTIONAL_OPEN_NOT_ALLOWED:
|
|
|
1087 case ERROR_TRANSACTIONS_UNSUPPORTED_REMOTE:
|
|
|
1088 case ERROR_RM_NOT_ACTIVE:
|
|
|
1089 case ERROR_RM_METADATA_CORRUPT:
|
|
|
1090 case ERROR_DIRECTORY_NOT_RM:
|
|
|
1091 throw exception_io_transactions_unsupported();
|
|
|
1092 case ERROR_TRANSACTIONAL_CONFLICT:
|
|
|
1093 throw exception_io_transactional_conflict();
|
|
|
1094 case ERROR_TRANSACTION_ALREADY_ABORTED:
|
|
|
1095 throw exception_io_transaction_aborted();
|
|
|
1096 case ERROR_EFS_NOT_ALLOWED_IN_TRANSACTION:
|
|
|
1097 throw exception_io("Transacted updates of encrypted content are not supported");
|
|
|
1098 #endif // FB2K_SUPPORT_TRANSACTED_FILESYSTEM
|
|
|
1099 case ERROR_UNEXP_NET_ERR:
|
|
|
1100 // QNAP threw this when messing with very long file paths and concurrent conversion, probably SMB daemon crashed
|
|
|
1101 throw exception_io_network_not_reachable("Unexpected network error");
|
|
|
1102 case ERROR_NOT_SAME_DEVICE:
|
|
|
1103 throw exception_io("Source and destination must be on the same device");
|
|
|
1104 case 0x80310000:
|
|
|
1105 throw exception_io("Drive locked by BitLocker");
|
|
|
1106 case ERROR_INVALID_FUNCTION:
|
|
|
1107 // Happens when trying to link files on FAT32 etc
|
|
|
1108 throw exception_io_unsupported_feature();
|
|
|
1109 #if 0
|
|
|
1110 case ERROR_BAD_LENGTH:
|
|
|
1111 FB2K_BugCheckEx("ERROR_BAD_LENGTH");
|
|
|
1112 #endif
|
|
|
1113 default:
|
|
|
1114 throw exception_io_win32_ex(p_code);
|
|
|
1115 }
|
|
|
1116 }
|
|
|
1117 #else
|
|
|
1118 PFC_NORETURN void foobar2000_io::exception_io_from_nix(int code) {
|
|
|
1119 switch(code) {
|
|
|
1120 case EPERM:
|
|
|
1121 case EACCES:
|
|
|
1122 throw exception_io_denied();
|
|
|
1123 case ENOENT:
|
|
|
1124 case ENODEV:
|
|
|
1125 throw exception_io_not_found();
|
|
|
1126 case EIO:
|
|
|
1127 pfc::throw_exception_with_message<exception_io>("Generic I/O error (EIO)");
|
|
|
1128 case EBUSY:
|
|
|
1129 throw exception_io_sharing_violation();
|
|
|
1130 case EEXIST:
|
|
|
1131 throw exception_io_already_exists();
|
|
|
1132 case ENOSPC:
|
|
|
1133 throw exception_io_device_full();
|
|
|
1134 case EROFS:
|
|
|
1135 throw exception_io_denied_readonly();
|
|
|
1136 case ENOTEMPTY:
|
|
|
1137 throw exception_io_directory_not_empty();
|
|
|
1138 case ESPIPE:
|
|
|
1139 // Should not actually get here
|
|
|
1140 PFC_ASSERT(!"Trying to seek a nonseekable stream");
|
|
|
1141 throw exception_io_object_not_seekable();
|
|
|
1142 case ENOTDIR:
|
|
|
1143 throw exception_io_not_directory();
|
|
|
1144 case ENAMETOOLONG:
|
|
|
1145 pfc::throw_exception_with_message<exception_io>("Name too long");
|
|
|
1146 default:
|
|
|
1147 pfc::throw_exception_with_message< exception_io>( PFC_string_formatter() << "Unknown I/O error (#" << code << ")");
|
|
|
1148 }
|
|
|
1149 }
|
|
|
1150 void nix_pre_io_op() {
|
|
|
1151 errno = 0;
|
|
|
1152 }
|
|
|
1153 PFC_NORETURN void foobar2000_io::nix_io_op_fail() {
|
|
|
1154 exception_io_from_nix( errno );
|
|
|
1155 }
|
|
|
1156 #endif
|
|
|
1157
|
|
|
1158 t_filesize file::get_size_ex(abort_callback & p_abort) {
|
|
|
1159 t_filesize temp = get_size(p_abort);
|
|
|
1160 if (temp == filesize_invalid) throw exception_io_no_length();
|
|
|
1161 return temp;
|
|
|
1162 }
|
|
|
1163
|
|
|
1164 void file::ensure_local() {
|
|
|
1165 if (is_remote()) throw exception_io_object_is_remote();
|
|
|
1166 }
|
|
|
1167
|
|
|
1168 void file::ensure_seekable() {
|
|
|
1169 if (!can_seek()) throw exception_io_object_not_seekable();
|
|
|
1170 }
|
|
|
1171
|
|
|
1172 bool filesystem::g_is_recognized_path(const char * p_path) {
|
|
|
1173 filesystem::ptr obj;
|
|
|
1174 return g_get_interface(obj,p_path);
|
|
|
1175 }
|
|
|
1176
|
|
|
1177 t_filesize file::get_remaining(abort_callback & p_abort) {
|
|
|
1178 t_filesize length = get_size_ex(p_abort);
|
|
|
1179 t_filesize position = get_position(p_abort);
|
|
|
1180 pfc::dynamic_assert(position <= length);
|
|
|
1181 return length - position;
|
|
|
1182 }
|
|
|
1183
|
|
|
1184 bool file::probe_remaining_ex(t_filesize bytes, abort_callback& p_abort) {
|
|
|
1185 t_filesize length = get_size(p_abort);
|
|
|
1186 if (length != filesize_invalid) {
|
|
|
1187 t_filesize remaining = length - get_position(p_abort);
|
|
|
1188 if (remaining < bytes) return false;
|
|
|
1189 }
|
|
|
1190 return true;
|
|
|
1191 }
|
|
|
1192
|
|
|
1193 void file::probe_remaining(t_filesize bytes, abort_callback & p_abort) {
|
|
|
1194 if (!probe_remaining_ex(bytes, p_abort)) throw exception_io_data_truncation();
|
|
|
1195 }
|
|
|
1196
|
|
|
1197 t_filesize file::g_transfer(service_ptr_t<file> p_src,service_ptr_t<file> p_dst,t_filesize p_bytes,abort_callback & p_abort) {
|
|
|
1198 return g_transfer(pfc::implicit_cast<stream_reader*>(p_src.get_ptr()),pfc::implicit_cast<stream_writer*>(p_dst.get_ptr()),p_bytes,p_abort);
|
|
|
1199 }
|
|
|
1200
|
|
|
1201 void file::g_transfer_object(service_ptr_t<file> p_src,service_ptr_t<file> p_dst,t_filesize p_bytes,abort_callback & p_abort) {
|
|
|
1202 if (p_bytes > 1024) /* don't bother on small objects */
|
|
|
1203 {
|
|
|
1204 t_filesize srcFileSize = p_src->get_size(p_abort); // detect truncation
|
|
|
1205 if (srcFileSize != ~0) {
|
|
|
1206 t_filesize remaining = srcFileSize - p_src->get_position(p_abort);
|
|
|
1207 if (p_bytes > remaining) throw exception_io_data_truncation();
|
|
|
1208 }
|
|
|
1209
|
|
|
1210 t_filesize oldsize = p_dst->get_size(p_abort); // pre-resize the target file
|
|
|
1211 if (oldsize != filesize_invalid) {
|
|
|
1212 t_filesize newpos = p_dst->get_position(p_abort) + p_bytes;
|
|
|
1213 if (newpos > oldsize) p_dst->resize(newpos ,p_abort);
|
|
|
1214 }
|
|
|
1215
|
|
|
1216 }
|
|
|
1217 g_transfer_object(pfc::implicit_cast<stream_reader*>(p_src.get_ptr()),pfc::implicit_cast<stream_writer*>(p_dst.get_ptr()),p_bytes,p_abort);
|
|
|
1218 }
|
|
|
1219
|
|
|
1220
|
|
|
1221 void foobar2000_io::generate_temp_location_for_file(pfc::string_base & p_out, const char * p_origpath,const char * p_extension,const char * p_magic) {
|
|
|
1222 hasher_md5_result hash;
|
|
|
1223 {
|
|
|
1224 auto hasher = hasher_md5::get();
|
|
|
1225 hasher_md5_state state;
|
|
|
1226 hasher->initialize(state);
|
|
|
1227 hasher->process(state,p_origpath,strlen(p_origpath));
|
|
|
1228 hasher->process(state,p_extension,strlen(p_extension));
|
|
|
1229 hasher->process(state,p_magic,strlen(p_magic));
|
|
|
1230 hash = hasher->get_result(state);
|
|
|
1231 }
|
|
|
1232
|
|
|
1233 p_out = p_origpath;
|
|
|
1234 p_out.truncate(p_out.scan_filename());
|
|
|
1235 p_out += "temp-";
|
|
|
1236 p_out += pfc::format_hexdump(hash.m_data,sizeof(hash.m_data),"");
|
|
|
1237 p_out += ".";
|
|
|
1238 p_out += p_extension;
|
|
|
1239 }
|
|
|
1240
|
|
|
1241 t_filesize file::skip_seek(t_filesize p_bytes,abort_callback & p_abort) {
|
|
|
1242 const t_filesize size = get_size(p_abort);
|
|
|
1243 if (size != filesize_invalid) {
|
|
|
1244 const t_filesize position = get_position(p_abort);
|
|
|
1245 const t_filesize toskip = pfc::min_t( p_bytes, size - position );
|
|
|
1246 seek(position + toskip,p_abort);
|
|
|
1247 return toskip;
|
|
|
1248 } else {
|
|
|
1249 this->seek_ex( p_bytes, seek_from_current, p_abort );
|
|
|
1250 return p_bytes;
|
|
|
1251 }
|
|
|
1252 }
|
|
|
1253
|
|
|
1254 t_filesize file::skip(t_filesize p_bytes,abort_callback & p_abort) {
|
|
|
1255 if (p_bytes > 1024 && can_seek()) {
|
|
|
1256 const t_filesize size = get_size(p_abort);
|
|
|
1257 if (size != filesize_invalid) {
|
|
|
1258 const t_filesize position = get_position(p_abort);
|
|
|
1259 const t_filesize toskip = pfc::min_t( p_bytes, size - position );
|
|
|
1260 seek(position + toskip,p_abort);
|
|
|
1261 return toskip;
|
|
|
1262 }
|
|
|
1263 }
|
|
|
1264 return stream_reader::skip(p_bytes,p_abort);
|
|
|
1265 }
|
|
|
1266
|
|
|
1267 bool foobar2000_io::is_native_filesystem( const char * p_fspath ) {
|
|
|
1268 return _extract_native_path_ptr( p_fspath );
|
|
|
1269 }
|
|
|
1270
|
|
|
1271 bool foobar2000_io::_extract_native_path_ptr(const char * & p_fspath) {
|
|
|
1272 static const char header[] = "file://"; static const t_size headerLen = 7;
|
|
|
1273 if (strncmp(p_fspath,header,headerLen) != 0) return false;
|
|
|
1274 p_fspath += headerLen;
|
|
|
1275 return true;
|
|
|
1276 }
|
|
|
1277 bool foobar2000_io::extract_native_path(const char * p_fspath,pfc::string_base & p_native) {
|
|
|
1278 if (strstr(p_fspath, "://") != nullptr) {
|
|
|
1279 if (!_extract_native_path_ptr(p_fspath)) return false;
|
|
|
1280 }
|
|
|
1281 p_native = p_fspath;
|
|
|
1282 #ifndef _WIN32
|
|
|
1283 expandHomeDir( p_native );
|
|
|
1284 #endif
|
|
|
1285 return true;
|
|
|
1286 }
|
|
|
1287
|
|
|
1288 bool foobar2000_io::extract_native_path_ex(const char * p_fspath, pfc::string_base & p_native) {
|
|
|
1289 if (!_extract_native_path_ptr(p_fspath)) return false;
|
|
|
1290 if (p_fspath[0] != '\\' || p_fspath[1] != '\\') {
|
|
|
1291 p_native = "\\\\?\\";
|
|
|
1292 p_native += p_fspath;
|
|
|
1293 } else {
|
|
|
1294 p_native = p_fspath;
|
|
|
1295 }
|
|
|
1296 return true;
|
|
|
1297 }
|
|
|
1298
|
|
|
1299 static bool extract_native_path_fsv3(const char* in, pfc::string_base& out, abort_callback& a) {
|
|
|
1300 if (foobar2000_io::extract_native_path(in, out)) return true;
|
|
|
1301 filesystem_v3::ptr v3;
|
|
|
1302 if (v3 &= filesystem::tryGet(in)) {
|
|
|
1303 auto n = v3->getNativePath(in, a);
|
|
|
1304 if ( n.is_valid() ) {
|
|
|
1305 out = n->c_str(); return true;
|
|
|
1306 }
|
|
|
1307 }
|
|
|
1308 return false;
|
|
|
1309 }
|
|
|
1310
|
|
|
1311 bool foobar2000_io::extract_native_path_archive_aware_ex(const char* in, pfc::string_base& out, abort_callback& a) {
|
|
|
1312 if (extract_native_path_fsv3(in, out, a)) return true;
|
|
|
1313
|
|
|
1314 if (archive_impl::g_is_unpack_path(in)) {
|
|
|
1315 pfc::string8 arc, dummy;
|
|
|
1316 if (archive_impl::g_parse_unpack_path(in, arc, dummy)) {
|
|
|
1317 return extract_native_path_fsv3(arc, out, a);
|
|
|
1318 }
|
|
|
1319 }
|
|
|
1320 return false;
|
|
|
1321 }
|
|
|
1322
|
|
|
1323 bool foobar2000_io::extract_native_path_archive_aware(const char * in, pfc::string_base & out) {
|
|
|
1324 if (foobar2000_io::extract_native_path(in, out)) return true;
|
|
|
1325 if (archive_impl::g_is_unpack_path(in)) {
|
|
|
1326 pfc::string8 arc, dummy;
|
|
|
1327 if (archive_impl::g_parse_unpack_path(in, arc, dummy)) {
|
|
|
1328 return foobar2000_io::extract_native_path(arc, out);
|
|
|
1329 }
|
|
|
1330 }
|
|
|
1331 return false;
|
|
|
1332 }
|
|
|
1333
|
|
|
1334 pfc::string stream_reader::read_string(abort_callback & p_abort) {
|
|
|
1335 t_uint32 len;
|
|
|
1336 read_lendian_t(len,p_abort);
|
|
|
1337 return read_string_ex(len,p_abort);
|
|
|
1338 }
|
|
|
1339 pfc::string stream_reader::read_string_ex(t_size p_len,abort_callback & p_abort) {
|
|
|
1340 pfc::string temp;
|
|
|
1341 this->read_string_ex(temp, p_len, p_abort);
|
|
|
1342 return temp;
|
|
|
1343 }
|
|
|
1344
|
|
|
1345
|
|
|
1346 void filesystem::remove_directory_content(const char * path, abort_callback & abort) {
|
|
|
1347 class myCallback : public directory_callback {
|
|
|
1348 public:
|
|
|
1349 bool on_entry(filesystem * p_owner,abort_callback & p_abort,const char * p_url,bool p_is_subdirectory,const t_filestats &) {
|
|
|
1350 if (p_is_subdirectory) p_owner->list_directory(p_url, *this, p_abort);
|
|
|
1351 try {
|
|
|
1352 p_owner->remove(p_url, p_abort);
|
|
|
1353 } catch(exception_io_not_found const &) {}
|
|
|
1354 return true;
|
|
|
1355 }
|
|
|
1356 };
|
|
|
1357 myCallback cb;
|
|
|
1358 list_directory(path, cb, abort);
|
|
|
1359 }
|
|
|
1360 void filesystem::remove_object_recur(const char * path, abort_callback & abort) {
|
|
|
1361 filesystem_v3::ptr v3;
|
|
|
1362 if (v3 &= this) {
|
|
|
1363 // the new way
|
|
|
1364 // fsItemFolder may implement removeRecur() more efficiently
|
|
|
1365 auto item = v3->findItem(path, abort);
|
|
|
1366 fsItemFolderPtr folder;
|
|
|
1367 if (folder &= item) folder->removeRecur(abort);
|
|
|
1368 else item->remove(abort);
|
|
|
1369 return;
|
|
|
1370 }
|
|
|
1371
|
|
|
1372 // the classic way
|
|
|
1373 try {
|
|
|
1374 remove_directory_content(path, abort);
|
|
|
1375 } catch(exception_io_not_found const &) {}
|
|
|
1376 remove(path, abort);
|
|
|
1377
|
|
|
1378 }
|
|
|
1379
|
|
|
1380 void filesystem::g_remove_object_recur_timeout(const char * path, double timeout, abort_callback & abort) {
|
|
|
1381 FB2K_RETRY_FILE_MOVE( g_remove_object_recur(path, abort), abort, timeout );
|
|
|
1382 }
|
|
|
1383
|
|
|
1384 void filesystem::g_remove_object_recur(const char * path, abort_callback & abort) {
|
|
|
1385 g_get_interface(path)->remove_object_recur(path, abort);
|
|
|
1386 }
|
|
|
1387
|
|
|
1388 void foobar2000_io::purgeOldFiles(const char * directory, t_filetimestamp period, abort_callback & abort) {
|
|
|
1389
|
|
|
1390 class myCallback : public directory_callback {
|
|
|
1391 public:
|
|
|
1392 myCallback(t_filetimestamp period) : m_base(filetimestamp_from_system_timer() - period) {}
|
|
|
1393 bool on_entry(filesystem *,abort_callback & p_abort,const char * p_url,bool p_is_subdirectory,const t_filestats & p_stats) {
|
|
|
1394 if (!p_is_subdirectory && p_stats.m_timestamp < m_base) {
|
|
|
1395 try {
|
|
|
1396 filesystem::g_remove_timeout(p_url, 1, p_abort);
|
|
|
1397 } catch(exception_io_not_found const &) {}
|
|
|
1398 }
|
|
|
1399 return true;
|
|
|
1400 }
|
|
|
1401 private:
|
|
|
1402 const t_filetimestamp m_base;
|
|
|
1403 };
|
|
|
1404
|
|
|
1405 myCallback cb(period);
|
|
|
1406 filesystem::g_list_directory(directory, cb, abort);
|
|
|
1407 }
|
|
|
1408
|
|
|
1409 void stream_reader::read_string_nullterm( pfc::string_base & out, abort_callback & abort ) {
|
|
|
1410 enum { bufCount = 256 };
|
|
|
1411 char buffer[bufCount];
|
|
|
1412 out.reset();
|
|
|
1413 size_t w = 0;
|
|
|
1414 for(;;) {
|
|
|
1415 char & c = buffer[w];
|
|
|
1416 this->read_object( &c, 1, abort );
|
|
|
1417 if (c == 0) {
|
|
|
1418 out.add_string( buffer, w ); break;
|
|
|
1419 }
|
|
|
1420 if (++w == bufCount ) {
|
|
|
1421 out.add_string( buffer, bufCount ); w = 0;
|
|
|
1422 }
|
|
|
1423 }
|
|
|
1424 }
|
|
|
1425
|
|
|
1426 t_filesize stream_reader::skip_till_eof(abort_callback & abort) {
|
|
|
1427 t_filesize atOnce = 1024 * 1024;
|
|
|
1428 t_filesize done = 0;
|
|
|
1429 for (;; ) {
|
|
|
1430 abort.check();
|
|
|
1431 t_filesize did = this->skip(atOnce, abort);
|
|
|
1432 done += did;
|
|
|
1433 if (did != atOnce) break;
|
|
|
1434 }
|
|
|
1435 return done;
|
|
|
1436 }
|
|
|
1437
|
|
|
1438 uint8_t stream_reader::read_byte( abort_callback & abort ) {
|
|
|
1439 uint8_t b;
|
|
|
1440 read_object(&b, 1, abort );
|
|
|
1441 return b;
|
|
|
1442 }
|
|
|
1443
|
|
|
1444 bool foobar2000_io::matchContentType(const char * fullString, const char * ourType) {
|
|
|
1445 t_size lim = pfc::string_find_first(fullString, ';');
|
|
|
1446 if (lim != ~0) {
|
|
|
1447 while(lim > 0 && fullString[lim-1] == ' ') --lim;
|
|
|
1448 }
|
|
|
1449 return pfc::stricmp_ascii_ex(fullString,lim, ourType, SIZE_MAX) == 0;
|
|
|
1450 }
|
|
|
1451
|
|
|
1452 const char * foobar2000_io::contentTypeFromExtension( const char * ext ) {
|
|
|
1453 if ( pfc::stringEqualsI_ascii( ext, "mp3" ) ) return "audio/mpeg";
|
|
|
1454 if ( pfc::stringEqualsI_ascii( ext, "flac" ) ) return "audio/flac";
|
|
|
1455 if ( pfc::stringEqualsI_ascii( ext, "mp4" ) ) return "application/mp4"; // We don't know if it's audio-only or other.
|
|
|
1456 if ( pfc::stringEqualsI_ascii( ext, "m4a" ) ) return "audio/mp4";
|
|
|
1457 if ( pfc::stringEqualsI_ascii( ext, "mpc" ) ) return "audio/musepack";
|
|
|
1458 if ( pfc::stringEqualsI_ascii( ext, "ogg" ) ) return "audio/ogg";
|
|
|
1459 if ( pfc::stringEqualsI_ascii( ext, "opus" ) ) return "audio/opus";
|
|
|
1460 if ( pfc::stringEqualsI_ascii( ext, "wav" ) ) return "audio/vnd.wave";
|
|
|
1461 if ( pfc::stringEqualsI_ascii( ext, "wv" ) ) return "audio/wavpack";
|
|
|
1462 if ( pfc::stringEqualsI_ascii( ext, "txt" ) || pfc::stringEqualsI_ascii( ext, "cue" ) || pfc::stringEqualsI_ascii( ext, "log" ) ) return "text/plain";
|
|
|
1463 return "application/binary";
|
|
|
1464 }
|
|
|
1465
|
|
|
1466 const char * foobar2000_io::extensionFromContentType( const char * contentType ) {
|
|
|
1467 if (matchContentType_MP3( contentType )) return "mp3";
|
|
|
1468 if (matchContentType_FLAC( contentType )) return "flac";
|
|
|
1469 if (matchContentType_MP4audio( contentType)) return "m4a";
|
|
|
1470 if (matchContentType_MP4( contentType)) return "mp4";
|
|
|
1471 if (matchContentType_Musepack( contentType )) return "mpc";
|
|
|
1472 if (matchContentType_Ogg( contentType )) return "ogg";
|
|
|
1473 if (matchContentType_Opus( contentType )) return "opus";
|
|
|
1474 if (matchContentType_WAV( contentType )) return "wav";
|
|
|
1475 if (matchContentType_WavPack( contentType )) return "wv";
|
|
|
1476 if (matchContentType(contentType, "image/jpeg")) return "jpg";
|
|
|
1477 if (matchContentType(contentType, "image/png")) return "png";
|
|
|
1478 return "";
|
|
|
1479 }
|
|
|
1480
|
|
|
1481 bool foobar2000_io::matchContentType_MP3( const char * type) {
|
|
|
1482 return matchContentType(type,"audio/mp3") || matchContentType(type,"audio/mpeg") || matchContentType(type,"audio/mpg") || matchContentType(type,"audio/x-mp3") || matchContentType(type,"audio/x-mpeg") || matchContentType(type,"audio/x-mpg");
|
|
|
1483 }
|
|
|
1484 bool foobar2000_io::matchContentType_MP4( const char * type ) {
|
|
|
1485 return matchContentType_MP4audio(type)
|
|
|
1486 || matchContentType(type, "video/mp4") || matchContentType(type, "video/x-mp4")
|
|
|
1487 || matchContentType(type, "application/mp4") || matchContentType(type, "application/x-mp4");
|
|
|
1488
|
|
|
1489 }
|
|
|
1490 bool foobar2000_io::matchContentType_MP4audio( const char * type ) {
|
|
|
1491 // Gerbera uses audio/x-m4a instead of audio/mp4 ....
|
|
|
1492 return matchContentType(type, "audio/mp4") || matchContentType(type, "audio/x-mp4") ||
|
|
|
1493 matchContentType(type, "audio/m4a") || matchContentType(type, "audio/x-m4a");
|
|
|
1494 }
|
|
|
1495 bool foobar2000_io::matchContentType_Ogg( const char * type) {
|
|
|
1496 return matchContentType(type, "application/ogg") || matchContentType(type, "application/x-ogg") || matchContentType(type, "audio/ogg") || matchContentType(type, "audio/x-ogg");
|
|
|
1497 }
|
|
|
1498 bool foobar2000_io::matchContentType_Opus( const char * type) {
|
|
|
1499 return matchContentType(type, "audio/opus") || matchContentType(type, "audio/x-opus");
|
|
|
1500 }
|
|
|
1501 bool foobar2000_io::matchContentType_WAV( const char * type ) {
|
|
|
1502 return matchContentType(type, "audio/vnd.wave" ) || matchContentType(type, "audio/wav") || matchContentType(type, "audio/wave") || matchContentType(type, "audio/x-wav") || matchContentType(type, "audio/x-wave");
|
|
|
1503 }
|
|
|
1504 bool foobar2000_io::matchContentType_FLAC( const char * type) {
|
|
|
1505 return matchContentType(type, "audio/flac") || matchContentType(type, "audio/x-flac") || matchContentType(type, "application/flac") || matchContentType(type, "application/x-flac");
|
|
|
1506 }
|
|
|
1507 bool foobar2000_io::matchContentType_WavPack( const char * type) {
|
|
|
1508 return matchContentType( type, "audio/wavpack" ) || matchContentType( type, "audio/x-wavpack");
|
|
|
1509 }
|
|
|
1510 bool foobar2000_io::matchContentType_Musepack( const char * type) {
|
|
|
1511 return matchContentType(type,"audio/musepack") || matchContentType(type,"audio/x-musepack");
|
|
|
1512 }
|
|
|
1513
|
|
|
1514 pfc::string8 foobar2000_io::getProtocol(const char* fullString) {
|
|
|
1515 const char* s = strstr(fullString, "://");
|
|
|
1516 if (s == nullptr) throw exception_io_no_handler_for_path();
|
|
|
1517 pfc::string8 ret;
|
|
|
1518 ret.set_string_nc(fullString, s - fullString);
|
|
|
1519 return ret;
|
|
|
1520 }
|
|
|
1521 const char * foobar2000_io::afterProtocol( const char * fullString ) {
|
|
|
1522 const char * s = strstr( fullString, "://" );
|
|
|
1523 if ( s != nullptr ) return s + 3;
|
|
|
1524 s = strchr(fullString, ':' );
|
|
|
1525 if ( s != nullptr && s[1] != '\\' && s[1] != 0 ) return s + 1;
|
|
|
1526 PFC_ASSERT(!"Should not get here");
|
|
|
1527 return fullString;
|
|
|
1528 }
|
|
|
1529
|
|
|
1530 bool foobar2000_io::testIfHasProtocol( const char * input ) {
|
|
|
1531 // Take arbitrary untrusted string, return whether looks like foo://
|
|
|
1532 if ( pfc::char_is_ascii_alpha(input[0])) {
|
|
|
1533 const char * walk = input+1;
|
|
|
1534 while(pfc::char_is_ascii_alpha(*walk)) ++ walk;
|
|
|
1535 if ( walk[0] == ':' && walk[1] == '/' && walk[2] == '/') return true;
|
|
|
1536 }
|
|
|
1537 return false;
|
|
|
1538 }
|
|
|
1539
|
|
|
1540 bool foobar2000_io::matchProtocol(const char * fullString, const char * protocolName) {
|
|
|
1541 const t_size len = strlen(protocolName);
|
|
|
1542 if (!pfc::stringEqualsI_ascii_ex(fullString, len, protocolName, len)) return false;
|
|
|
1543 return fullString[len] == ':' && fullString[len+1] == '/' && fullString[len+2] == '/';
|
|
|
1544 }
|
|
|
1545 void foobar2000_io::substituteProtocol(pfc::string_base & out, const char * fullString, const char * protocolName) {
|
|
|
1546 const char * base = strstr(fullString, "://");
|
|
|
1547 if (base) {
|
|
|
1548 out = protocolName; out << base;
|
|
|
1549 } else {
|
|
|
1550 PFC_ASSERT(!"Should not get here");
|
|
|
1551 out = fullString;
|
|
|
1552 }
|
|
|
1553 }
|
|
|
1554
|
|
|
1555 void filesystem::move_overwrite(const char * src, const char * dst, abort_callback & abort) {
|
|
|
1556 {
|
|
|
1557 filesystem_v2::ptr v2;
|
|
|
1558 if (v2 &= this) {
|
|
|
1559 v2->move_overwrite(src, dst, abort); return;
|
|
|
1560 }
|
|
|
1561 }
|
|
|
1562 try {
|
|
|
1563 this->remove(dst, abort);
|
|
|
1564 } catch (exception_io_not_found const &) {}
|
|
|
1565 this->move(src, dst, abort);
|
|
|
1566 }
|
|
|
1567
|
|
|
1568 void filesystem::replace_file(const char * src, const char * dst, abort_callback & abort) {
|
|
|
1569 filesystem_v2::ptr v2;
|
|
|
1570 if ( v2 &= this ) {
|
|
|
1571 v2->replace_file( src, dst, abort ); return;
|
|
|
1572 }
|
|
|
1573 move_overwrite( src, dst, abort );
|
|
|
1574 }
|
|
|
1575
|
|
|
1576 void filesystem::make_directory(const char * path, abort_callback & abort, bool * didCreate) {
|
|
|
1577 filesystem_v2::ptr v2;
|
|
|
1578 if ( v2 &= this ) {
|
|
|
1579 v2->make_directory( path, abort, didCreate );
|
|
|
1580 return;
|
|
|
1581 }
|
|
|
1582 bool rv = false;
|
|
|
1583 try {
|
|
|
1584 create_directory( path, abort );
|
|
|
1585 rv = true;
|
|
|
1586 } catch(exception_io_already_exists const &) {
|
|
|
1587 }
|
|
|
1588 if (didCreate != nullptr) * didCreate = rv;
|
|
|
1589 }
|
|
|
1590
|
|
|
1591 bool filesystem::make_directory_check(const char * path, abort_callback & abort) {
|
|
|
1592 bool rv = false;
|
|
|
1593 make_directory(path, abort, &rv);
|
|
|
1594 return rv;
|
|
|
1595 }
|
|
|
1596
|
|
|
1597 bool filesystem::directory_exists(const char * path, abort_callback & abort) {
|
|
|
1598 filesystem_v2::ptr v2;
|
|
|
1599 if ( v2 &= this ) {
|
|
|
1600 return v2->directory_exists( path, abort );
|
|
|
1601 }
|
|
|
1602 try {
|
|
|
1603 directory_callback_dummy cb;
|
|
|
1604 list_directory(path, cb, abort);
|
|
|
1605 return true;
|
|
|
1606 } catch (exception_io const &) { return false; }
|
|
|
1607 }
|
|
|
1608
|
|
|
1609 bool filesystem::exists(const char* path, abort_callback& a) {
|
|
|
1610 // for rare cases of code test if EITHER FILE OR FOLDER exists at path
|
|
|
1611 filesystem_v3::ptr v3;
|
|
|
1612 if (v3 &= this) {
|
|
|
1613 try {
|
|
|
1614 v3->get_stats2(path, stats2_fileOrFolder, a);
|
|
|
1615 return true;
|
|
|
1616 } catch (exception_io_not_found const &) { return false; }
|
|
|
1617 }
|
|
|
1618 filesystem_v2::ptr v2;
|
|
|
1619 if (v2 &= this) {
|
|
|
1620 return v2->file_exists(path, a) || v2->directory_exists(path, a);
|
|
|
1621 }
|
|
|
1622
|
|
|
1623 try {
|
|
|
1624 t_filestats stats; bool writable;
|
|
|
1625 get_stats(path, stats, writable, a);
|
|
|
1626 return true;
|
|
|
1627 } catch (exception_io const &) { }
|
|
|
1628 try {
|
|
|
1629 directory_callback_dummy cb;
|
|
|
1630 list_directory(path, cb, a);
|
|
|
1631 return true;
|
|
|
1632 } catch (exception_io const &) { }
|
|
|
1633 return false;
|
|
|
1634 }
|
|
|
1635
|
|
|
1636 bool filesystem::file_exists(const char * path, abort_callback & abort) {
|
|
|
1637 filesystem_v2::ptr v2;
|
|
|
1638 if ( v2 &= this ) {
|
|
|
1639 return v2->file_exists( path, abort );
|
|
|
1640 }
|
|
|
1641 try {
|
|
|
1642 t_filestats stats; bool writable;
|
|
|
1643 get_stats(path, stats, writable, abort );
|
|
|
1644 return true;
|
|
|
1645 } catch(exception_io const &) { return false; }
|
|
|
1646 }
|
|
|
1647
|
|
|
1648 char filesystem::pathSeparator() {
|
|
|
1649 filesystem_v2::ptr v2;
|
|
|
1650 if ( v2 &= this ) return v2->pathSeparator();
|
|
|
1651 return '/';
|
|
|
1652 }
|
|
|
1653
|
|
|
1654 pfc::string8 filesystem::extract_filename_ext(const char* path) {
|
|
|
1655 pfc::string8 ret; this->extract_filename_ext(path, ret); return ret;
|
|
|
1656 }
|
|
|
1657 pfc::string8 filesystem::get_extension(const char* path) {
|
|
|
1658 return pfc::string_extension(this->extract_filename_ext(path));
|
|
|
1659 }
|
|
|
1660 pfc::string8 filesystem::g_get_extension(const char* path) {
|
|
|
1661 auto fs = tryGet(path);
|
|
|
1662 if (fs.is_valid()) return fs->get_extension(path);
|
|
|
1663 return pfc::string_extension(path);
|
|
|
1664 }
|
|
|
1665 void filesystem::extract_filename_ext(const char * path, pfc::string_base & outFN) {
|
|
|
1666 filesystem_v2::ptr v2;
|
|
|
1667 if ( v2 &= this ) {
|
|
|
1668 v2->extract_filename_ext( path, outFN );
|
|
|
1669 return;
|
|
|
1670 }
|
|
|
1671 outFN = pfc::filename_ext_v2( path );
|
|
|
1672 }
|
|
|
1673
|
|
|
1674 bool filesystem::get_parent_helper( const char * path, char separator, pfc::string_base & out ) {
|
|
|
1675 auto proto = path;
|
|
|
1676 path = afterProtocol(path);
|
|
|
1677
|
|
|
1678 auto sep_ptr = strrchr( path, separator );
|
|
|
1679 if ( sep_ptr == path ) return false;
|
|
|
1680 if ( sep_ptr >= path + 1 && sep_ptr[-1] == separator ) return false;
|
|
|
1681
|
|
|
1682 out.set_string(proto, path - proto);
|
|
|
1683 out.add_string(path, sep_ptr - path);
|
|
|
1684 return true;
|
|
|
1685 }
|
|
|
1686
|
|
|
1687 fb2k::stringRef filesystem::parentPath(const char* path) {
|
|
|
1688 pfc::string8 temp;
|
|
|
1689 if (!get_parent_path(path, temp)) return nullptr;
|
|
|
1690 return fb2k::makeString(temp);
|
|
|
1691 }
|
|
|
1692
|
|
|
1693 bool filesystem::get_parent_path(const char * path, pfc::string_base & out) {
|
|
|
1694 filesystem_v2::ptr v2;
|
|
|
1695 if ( v2 &= this ) {
|
|
|
1696 return v2->get_parent_path(path, out);
|
|
|
1697 }
|
|
|
1698 return get_parent_helper( path, '/', out );
|
|
|
1699 }
|
|
|
1700
|
|
|
1701 void filesystem::read_whole_file(const char * path, mem_block_container & out, pfc::string_base & outContentType, size_t maxBytes, abort_callback & abort) {
|
|
|
1702 filesystem_v2::ptr v2;
|
|
|
1703 if ( v2 &= this ) {
|
|
|
1704 v2->read_whole_file( path, out, outContentType, maxBytes, abort );
|
|
|
1705 return;
|
|
|
1706 }
|
|
|
1707 read_whole_file_fallback(path, out, outContentType, maxBytes, abort);
|
|
|
1708 }
|
|
|
1709
|
|
|
1710 void filesystem::read_whole_file_fallback(const char * path, mem_block_container & out, pfc::string_base & outContentType, size_t maxBytes, abort_callback & abort) {
|
|
|
1711 auto f = this->openRead( path, abort, 0 );
|
|
|
1712 if (!f->get_content_type(outContentType)) outContentType = "";
|
|
|
1713 auto s64 = f->get_size( abort );
|
|
|
1714 if ( s64 == filesize_invalid ) {
|
|
|
1715 // unknown length, perform streamed read
|
|
|
1716 size_t done = 0, alloc = 0;
|
|
|
1717
|
|
|
1718 while(alloc < maxBytes ) {
|
|
|
1719 if ( alloc == 0 ) alloc = 4096;
|
|
|
1720 else {
|
|
|
1721 size_t next = alloc * 2;
|
|
|
1722 if ( next <= alloc ) throw exception_io_data();
|
|
|
1723 alloc = next;
|
|
|
1724 }
|
|
|
1725 if ( alloc > maxBytes ) alloc = maxBytes;
|
|
|
1726
|
|
|
1727 out.set_size( alloc );
|
|
|
1728 size_t delta = alloc - done;
|
|
|
1729 size_t deltaGot = f->read( (uint8_t*) out.get_ptr() + done, delta, abort );
|
|
|
1730 PFC_ASSERT( deltaGot <= delta );
|
|
|
1731 done += deltaGot;
|
|
|
1732 if ( deltaGot != delta ) {
|
|
|
1733 out.set_size( done ); return;
|
|
|
1734 }
|
|
|
1735 }
|
|
|
1736 // maxbytes reached
|
|
|
1737 PFC_ASSERT( done == maxBytes );
|
|
|
1738 // corner case check
|
|
|
1739 if ( f->skip(1, abort) != 0 ) throw exception_io_data();
|
|
|
1740 } else if ( s64 > maxBytes ) {
|
|
|
1741 throw exception_io_data();
|
|
|
1742 } else {
|
|
|
1743 size_t s = (size_t) s64;
|
|
|
1744 out.set_size( s );
|
|
|
1745 if (s > 0) f->read_object( out.get_ptr(), s, abort);
|
|
|
1746 }
|
|
|
1747 }
|
|
|
1748
|
|
|
1749 bool filesystem::is_transacted() {
|
|
|
1750 #if FB2K_SUPPORT_TRANSACTED_FILESYSTEM
|
|
|
1751 filesystem_transacted::ptr p;
|
|
|
1752 return ( p &= this );
|
|
|
1753 #else
|
|
|
1754 return false;
|
|
|
1755 #endif
|
|
|
1756 }
|
|
|
1757
|
|
|
1758 void filesystem::rewrite_file(const char* path, abort_callback& abort, double opTimeout, const void* payload, size_t bytes) {
|
|
|
1759 this->rewrite_file(path, abort, opTimeout, [&](file::ptr f) {
|
|
|
1760 f->write(payload, bytes, abort);
|
|
|
1761 });
|
|
|
1762 }
|
|
|
1763
|
|
|
1764 void filesystem::rewrite_file(const char * path, abort_callback & abort, double opTimeout, std::function<void(file::ptr) > worker) {
|
|
|
1765 if ( this->is_transacted() ) {
|
|
|
1766 auto f = this->openWriteNew( path, abort, opTimeout );
|
|
|
1767 worker(f);
|
|
|
1768 } else {
|
|
|
1769 pfc::string_formatter temp(path); temp << ".new.tmp";
|
|
|
1770 try {
|
|
|
1771 {
|
|
|
1772 auto f = this->openWriteNew( temp, abort, opTimeout );
|
|
|
1773 worker(f);
|
|
|
1774 f->flushFileBuffers( abort );
|
|
|
1775 }
|
|
|
1776
|
|
|
1777 retryOnSharingViolation(opTimeout, abort, [&] {
|
|
|
1778 this->replace_file(temp, path, abort);
|
|
|
1779 });
|
|
|
1780
|
|
|
1781 } catch(...) {
|
|
|
1782 try {
|
|
|
1783 retryOnSharingViolation(opTimeout, abort, [&] { this->remove(temp, fb2k::noAbort); } );
|
|
|
1784 } catch(...) {}
|
|
|
1785 throw;
|
|
|
1786 }
|
|
|
1787 }
|
|
|
1788 }
|
|
|
1789
|
|
|
1790 void filesystem::rewrite_directory(const char * path, abort_callback & abort, double opTimeout, std::function<void(const char *) > worker) {
|
|
|
1791 if ( this->is_transacted() ) {
|
|
|
1792 // so simple
|
|
|
1793 if ( ! this->make_directory_check( path, abort) ) {
|
|
|
1794 retryFileDelete(opTimeout, abort, [&] { this->remove_directory_content(path, abort); });
|
|
|
1795 }
|
|
|
1796 worker( path );
|
|
|
1797 } else {
|
|
|
1798 // so complex
|
|
|
1799 pfc::string8 fnNew( path ); fnNew += ".new.tmp";
|
|
|
1800 pfc::string8 fnOld( path ); fnOld += ".old.tmp";
|
|
|
1801
|
|
|
1802 if ( !this->make_directory_check( fnNew, abort ) ) {
|
|
|
1803 // folder.new folder already existed? clear contents
|
|
|
1804 try {
|
|
|
1805 retryFileDelete(opTimeout, abort, [&] { this->remove_directory_content(fnNew, abort); });
|
|
|
1806 } catch(exception_io_not_found const &) {}
|
|
|
1807 }
|
|
|
1808
|
|
|
1809 // write to folder.new
|
|
|
1810 worker( fnNew );
|
|
|
1811
|
|
|
1812 bool haveOld = false;
|
|
|
1813 if ( directory_exists( path, abort ) ) {
|
|
|
1814 // move folder to folder.old
|
|
|
1815 if (this->directory_exists(fnOld, abort)) {
|
|
|
1816 try {
|
|
|
1817 retryFileDelete(opTimeout, abort, [&] { this->remove_object_recur(fnOld, abort); });
|
|
|
1818 } catch(exception_io_not_found const &) {}
|
|
|
1819 }
|
|
|
1820 try {
|
|
|
1821 retryFileMove(opTimeout, abort, [&] { this->move( path, fnOld, abort ); } ) ;
|
|
|
1822 haveOld = true;
|
|
|
1823 } catch(exception_io_not_found const &) {}
|
|
|
1824 }
|
|
|
1825
|
|
|
1826 // move folder.new to folder
|
|
|
1827 retryFileMove( opTimeout, abort, [&] {
|
|
|
1828 this->move( fnNew, path, abort );
|
|
|
1829 } );
|
|
|
1830
|
|
|
1831 if ( haveOld ) {
|
|
|
1832 // delete folder.old if we made one
|
|
|
1833 try {
|
|
|
1834 retryFileDelete( opTimeout, abort, [&] { this->remove_object_recur( fnOld, abort); } );
|
|
|
1835 } catch (exception_io_not_found const &) {}
|
|
|
1836 }
|
|
|
1837 }
|
|
|
1838 }
|
|
|
1839
|
|
|
1840 void filesystem_v2::list_directory(const char * p_path, directory_callback & p_out, abort_callback & p_abort) {
|
|
|
1841 list_directory_ex(p_path, p_out, listMode::filesAndFolders | listMode::hidden, p_abort);
|
|
|
1842 }
|
|
|
1843
|
|
|
1844 void filesystem_v2::extract_filename_ext(const char * path, pfc::string_base & outFN) {
|
|
|
1845 outFN = pfc::filename_ext_v2(path, this->pathSeparator() );
|
|
|
1846 }
|
|
|
1847
|
|
|
1848 bool filesystem_v2::get_parent_path(const char * path, pfc::string_base & out) {
|
|
|
1849 return get_parent_helper(path, pathSeparator(), out);
|
|
|
1850 }
|
|
|
1851
|
|
|
1852 void filesystem_v2::replace_file(const char * src, const char * dst, abort_callback & abort) {
|
|
|
1853 this->move_overwrite( src, dst, abort );
|
|
|
1854 }
|
|
|
1855
|
|
|
1856 void filesystem_v2::read_whole_file(const char * path, mem_block_container & out, pfc::string_base & outContentType, size_t maxBytes, abort_callback & abort) {
|
|
|
1857 read_whole_file_fallback( path, out, outContentType, maxBytes, abort );
|
|
|
1858 }
|
|
|
1859
|
|
|
1860 bool filesystem_v2::make_directory_check(const char * path, abort_callback & abort) {
|
|
|
1861 bool rv = false;
|
|
|
1862 make_directory(path, abort, &rv);
|
|
|
1863 return rv;
|
|
|
1864 }
|
|
|
1865
|
|
|
1866 #if FB2K_SUPPORT_TRANSACTED_FILESYSTEM
|
|
|
1867 filesystem_transacted::ptr filesystem_transacted::create( const char * pathFor ) {
|
|
|
1868 service_enum_t<filesystem_transacted_entry> e;
|
|
|
1869 filesystem_transacted_entry::ptr p;
|
|
|
1870 while(e.next(p)) {
|
|
|
1871 if ( p->is_our_path( pathFor ) ) {
|
|
|
1872 auto ret = p->create(pathFor);
|
|
|
1873 if (ret.is_valid()) return ret;
|
|
|
1874 }
|
|
|
1875 }
|
|
|
1876 return nullptr;
|
|
|
1877 }
|
|
|
1878 #endif
|
|
|
1879
|
|
|
1880 bool filesystem::commit_if_transacted(abort_callback &abort) {
|
|
|
1881 (void)abort;
|
|
|
1882 bool rv = false;
|
|
|
1883 #if FB2K_SUPPORT_TRANSACTED_FILESYSTEM
|
|
|
1884 filesystem_transacted::ptr t;
|
|
|
1885 if ( t &= this ) {
|
|
|
1886 t->commit( abort ); rv = true;
|
|
|
1887 }
|
|
|
1888 #endif
|
|
|
1889 return rv;
|
|
|
1890 }
|
|
|
1891
|
|
|
1892 t_filestats filesystem::get_stats(const char * path, abort_callback & abort) {
|
|
|
1893 t_filestats s; bool dummy;
|
|
|
1894 this->get_stats(path, s, dummy, abort);
|
|
|
1895 return s;
|
|
|
1896 }
|
|
|
1897
|
|
|
1898 bool file_dynamicinfo_v2::get_dynamic_info(class file_info & p_out) {
|
|
|
1899 t_filesize dummy = 0;
|
|
|
1900 return this->get_dynamic_info_v2(p_out, dummy);
|
|
|
1901 }
|
|
|
1902
|
|
|
1903 size_t file::lowLevelIO_(const GUID & guid, size_t arg1, void * arg2, size_t arg2size, abort_callback & abort) {
|
|
|
1904 {
|
|
|
1905 file_v2::ptr f;
|
|
|
1906 if (f &= this) return f->lowLevelIO(guid, arg1, arg2, arg2size, abort);
|
|
|
1907 }
|
|
|
1908 {
|
|
|
1909 file_lowLevelIO::ptr f;
|
|
|
1910 if (f &= this) return f->lowLevelIO(guid, arg1, arg2, arg2size, abort);
|
|
|
1911 }
|
|
|
1912 return 0;
|
|
|
1913 }
|
|
|
1914
|
|
|
1915 bool file::flushFileBuffers(abort_callback & abort) {
|
|
|
1916 return this->lowLevelIO_(file_lowLevelIO::guid_flushFileBuffers, 0, nullptr, 0, abort) != 0;
|
|
|
1917 }
|
|
|
1918
|
|
|
1919 bool file::getFileTimes(filetimes_t & out, abort_callback & a) {
|
|
|
1920 return this->lowLevelIO_(file_lowLevelIO::guid_getFileTimes, 0, &out, sizeof(out), a) != 0;
|
|
|
1921 }
|
|
|
1922
|
|
|
1923 bool file::setFileTimes(filetimes_t const & in, abort_callback & a) {
|
|
|
1924 return this->lowLevelIO_(file_lowLevelIO::guid_setFileTimes, 0, (void*)&in, sizeof(in), a) != 0;
|
|
|
1925 }
|
|
|
1926
|
|
|
1927 bool file::g_copy_creation_time(service_ptr_t<file> from, service_ptr_t<file> to, abort_callback& a) {
|
|
|
1928 bool rv = false;
|
|
|
1929 auto ft = from->get_time_created(a);
|
|
|
1930 if (ft != filetimestamp_invalid) {
|
|
|
1931 filetimes_t ft2;
|
|
|
1932 ft2.creation = ft;
|
|
|
1933 rv = to->setFileTimes(ft2, a);
|
|
|
1934 }
|
|
|
1935 return rv;
|
|
|
1936 }
|
|
|
1937 bool file::g_copy_timestamps(file::ptr from, file::ptr to, abort_callback& a) {
|
|
|
1938 {
|
|
|
1939 filetimes_t filetimes = {};
|
|
|
1940 if (from->getFileTimes(filetimes, a)) {
|
|
|
1941 return to->setFileTimes(filetimes, a);
|
|
|
1942 }
|
|
|
1943 }
|
|
|
1944 filetimes_t filetimes = {};
|
|
|
1945 auto stats = from->get_stats2_(stats2_timestamp | stats2_timestampCreate, a);
|
|
|
1946 if (stats.m_timestamp != filetimestamp_invalid || stats.m_timestampCreate != filetimestamp_invalid) {
|
|
|
1947 filetimes.lastWrite = stats.m_timestamp; filetimes.creation = stats.m_timestampCreate;
|
|
|
1948 return to->setFileTimes(filetimes, a);
|
|
|
1949 }
|
|
|
1950 return false;
|
|
|
1951 }
|
|
|
1952
|
|
|
1953 service_ptr file::get_metadata_(abort_callback& a) {
|
|
|
1954 service_ptr ret;
|
|
|
1955 file_v2::ptr getter;
|
|
|
1956 if (getter &= this) ret = getter->get_metadata(a);
|
|
|
1957 return ret;
|
|
|
1958 }
|
|
|
1959
|
|
|
1960 drivespace_t filesystem_v3::getDriveSpace(const char*, abort_callback&) {
|
|
|
1961 throw pfc::exception_not_implemented();
|
|
|
1962 }
|
|
|
1963
|
|
|
1964 t_filestats2 filesystem::get_stats2_(const char* p_path, uint32_t s2flags, abort_callback& p_abort) {
|
|
|
1965 filesystem_v3::ptr api3;
|
|
|
1966 t_filestats2 ret;
|
|
|
1967 if (api3 &= this) {
|
|
|
1968 ret = api3->get_stats2(p_path, s2flags, p_abort);
|
|
|
1969 } else {
|
|
|
1970 if (this->directory_exists(p_path, p_abort)) {
|
|
|
1971 ret.set_folder(true);
|
|
|
1972 } else if (s2flags & (stats2_size | stats2_timestamp | stats2_canWrite)) {
|
|
|
1973 t_filestats temp;
|
|
|
1974 bool canWrite = false;
|
|
|
1975 this->get_stats(p_path, temp, canWrite, p_abort);
|
|
|
1976 ret.m_size = temp.m_size;
|
|
|
1977 ret.m_timestamp = temp.m_timestamp;
|
|
|
1978 ret.set_file();
|
|
|
1979 ret.set_readonly(!canWrite);
|
|
|
1980 }
|
|
|
1981 if ( s2flags & stats2_remote ) ret.set_remote(this->is_remote(p_path));
|
|
|
1982 }
|
|
|
1983 return ret;
|
|
|
1984
|
|
|
1985 }
|
|
|
1986
|
|
|
1987 t_filestats2 filesystem::g_get_stats2(const char* p_path, uint32_t s2flags, abort_callback& p_abort) {
|
|
|
1988 return get(p_path)->get_stats2_(p_path, s2flags, p_abort);
|
|
|
1989 }
|
|
|
1990
|
|
|
1991 void filesystem_v3::get_stats(const char* p_path, t_filestats& p_stats, bool& p_is_writeable, abort_callback& p_abort) {
|
|
|
1992 t_filestats2 s2 = this->get_stats2(p_path, stats2_canWrite | stats2_size | stats2_timestamp, p_abort);
|
|
|
1993 p_stats = s2.to_legacy();
|
|
|
1994 p_is_writeable = s2.can_write();
|
|
|
1995 }
|
|
|
1996
|
|
|
1997 t_filetimestamp file_v2::get_timestamp(abort_callback& p_abort) {
|
|
|
1998 return this->get_stats2(stats2_timestamp, p_abort).m_timestamp;
|
|
|
1999 }
|
|
|
2000 bool file_v2::is_remote() {
|
|
|
2001 return this->get_stats2(stats2_remote, fb2k::noAbort).is_remote();
|
|
|
2002 }
|
|
|
2003
|
|
|
2004 t_filesize file_v2::get_size(abort_callback& p_abort) {
|
|
|
2005 return this->get_stats2(stats2_size, p_abort).m_size;
|
|
|
2006 }
|
|
|
2007
|
|
|
2008 pfc::string8 file::get_content_type() {
|
|
|
2009 pfc::string8 ret;
|
|
|
2010 if (!this->get_content_type(ret)) ret.clear();
|
|
|
2011 return ret;
|
|
|
2012
|
|
|
2013 }
|
|
|
2014 t_filestats2 file::get_stats2_(uint32_t f, abort_callback& a) {
|
|
|
2015 t_filestats2 ret;
|
|
|
2016
|
|
|
2017 file_v2::ptr v2;
|
|
|
2018 if (v2 &= this) {
|
|
|
2019 ret = v2->get_stats2(f, a);
|
|
|
2020 PFC_ASSERT(ret.is_file());
|
|
|
2021 } else {
|
|
|
2022 if (f & stats2_size) ret.m_size = this->get_size(a);
|
|
|
2023 if (f & stats2_timestamp) ret.m_timestamp = this->get_timestamp(a);
|
|
|
2024 ret.set_file();
|
|
|
2025 ret.set_remote(this->is_remote());
|
|
|
2026 // we do not know if it's readonly or not, can_write() tells us if the file was open for writing, not if it can possibly be opened for writing
|
|
|
2027 }
|
|
|
2028 return ret;
|
|
|
2029 }
|
|
|
2030
|
|
|
2031 pfc::string8 t_filestats2::format_attribs(uint32_t attr, const char * delim) {
|
|
|
2032 pfc::string8 ret;
|
|
|
2033 if (attr != 0) {
|
|
|
2034 const char* arr[5] = {};
|
|
|
2035 size_t w = 0;
|
|
|
2036 ret.prealloc(64);
|
|
|
2037 if (attr & attr_readonly) {
|
|
|
2038 arr[w++] = "read-only";
|
|
|
2039 }
|
|
|
2040 if (attr & attr_folder) {
|
|
|
2041 arr[w++] = "folder";
|
|
|
2042 }
|
|
|
2043 if (attr & attr_hidden) {
|
|
|
2044 arr[w++] = "hidden";
|
|
|
2045 }
|
|
|
2046 if (attr & attr_system) {
|
|
|
2047 arr[w++] = "system";
|
|
|
2048 }
|
|
|
2049 if (attr & attr_remote) {
|
|
|
2050 arr[w++] = "remote";
|
|
|
2051 }
|
|
|
2052 PFC_ASSERT(w <= PFC_TABSIZE(arr));
|
|
|
2053 for (size_t f = 0; f < w; ++f) {
|
|
|
2054 if (f > 0) ret += delim;
|
|
|
2055 ret += arr[f];
|
|
|
2056 }
|
|
|
2057 }
|
|
|
2058 return ret;
|
|
|
2059 }
|
|
|
2060
|
|
|
2061 t_filetimestamp file::get_time_created(abort_callback& a) {
|
|
|
2062 t_filetimestamp ret;
|
|
|
2063 ret = get_stats2_(stats2_timestampCreate, a).m_timestampCreate;
|
|
|
2064 if (ret != filetimestamp_invalid) return ret;
|
|
|
2065
|
|
|
2066 filetimes_t ft;
|
|
|
2067 if (this->getFileTimes(ft, a)) return ft.creation;
|
|
|
2068 return filetimestamp_invalid;
|
|
|
2069 }
|
|
|
2070
|
|
|
2071 bool filesystem::get_display_name_short_(const char* path, pfc::string_base& out) {
|
|
|
2072
|
|
|
2073 try {
|
|
|
2074 filesystem_v3::ptr v3;
|
|
|
2075 if (v3 &= this) return v3->get_display_name_short(path, out);
|
|
|
2076 } catch(...) {return false;} // handle nonsense path etc
|
|
|
2077
|
|
|
2078 pfc::string8 temp;
|
|
|
2079 extract_filename_ext(path, temp);
|
|
|
2080 if (temp.length() == 0) return false;
|
|
|
2081 out = temp;
|
|
|
2082 return true;
|
|
|
2083 }
|
|
|
2084
|
|
|
2085 bool filesystem_v3::get_display_name_short(const char* path, pfc::string_base& out) {
|
|
|
2086 pfc::string8 temp;
|
|
|
2087 extract_filename_ext(path, temp);
|
|
|
2088 if (temp.length() == 0) return false;
|
|
|
2089 out = temp;
|
|
|
2090 return true;
|
|
|
2091 }
|
|
|
2092
|
|
|
2093 fb2k::memBlockRef filesystem::readWholeFile(const char* path, size_t maxBytes, abort_callback& abort) {
|
|
|
2094 mem_block_container_impl temp;
|
|
|
2095 pfc::string8 contentType;
|
|
|
2096 this->read_whole_file(path, temp, contentType, maxBytes, abort);
|
|
|
2097 return fb2k::memBlock::blockWithData(std::move(temp.m_data));
|
|
|
2098 }
|
|
|
2099
|
|
|
2100 fb2k::memBlockRef filesystem::g_readWholeFile(const char * path, size_t sizeSanity, abort_callback & aborter) {
|
|
|
2101 return get(path)->readWholeFile(path, sizeSanity, aborter);
|
|
|
2102 }
|
|
|
2103
|
|
|
2104 namespace {
|
|
|
2105 class directory_callback_v3_to_legacy : public directory_callback_v3 {
|
|
|
2106 public:
|
|
|
2107 directory_callback* m_chain = nullptr;
|
|
|
2108
|
|
|
2109 bool on_entry(filesystem* owner, const char* URL, t_filestats2 const& stats, abort_callback& abort) override {
|
|
|
2110 return m_chain->on_entry(owner, abort, URL, stats.is_folder(), stats.as_legacy());
|
|
|
2111 }
|
|
|
2112 };
|
|
|
2113 }
|
|
|
2114
|
|
|
2115 void filesystem_v3::list_directory_ex(const char* p_path, directory_callback& p_out, unsigned listMode, abort_callback& p_abort) {
|
|
|
2116 directory_callback_v3_to_legacy wrap;
|
|
|
2117 wrap.m_chain = &p_out;
|
|
|
2118 this->list_directory_v3(p_path, wrap, listMode, p_abort);
|
|
|
2119 }
|
|
|
2120
|
|
|
2121 bool filesystem_v3::directory_exists(const char* path, abort_callback& abort) {
|
|
|
2122 try {
|
|
|
2123 return get_stats2(path, stats2_fileOrFolder, abort).is_folder();
|
|
|
2124 } catch (exception_io_not_found const &) { return false; }
|
|
|
2125 }
|
|
|
2126
|
|
|
2127 bool filesystem_v3::file_exists(const char* path, abort_callback& abort) {
|
|
|
2128 try {
|
|
|
2129 return get_stats2(path, stats2_fileOrFolder, abort).is_file();
|
|
|
2130 } catch (exception_io_not_found const &) { return false; }
|
|
|
2131 }
|
|
|
2132
|
|
|
2133
|
|
|
2134 namespace {
|
|
|
2135 class directory_callback_v3_to_lambda : public directory_callback_v3 {
|
|
|
2136 public:
|
|
|
2137 filesystem::list_callback_t f;
|
|
|
2138 bool on_entry(filesystem*, const char* URL, t_filestats2 const& stats, abort_callback&) override {
|
|
|
2139 f(URL, stats);
|
|
|
2140 return true;
|
|
|
2141 }
|
|
|
2142 };
|
|
|
2143 class directory_callback_to_lambda : public directory_callback {
|
|
|
2144 public:
|
|
|
2145 filesystem::list_callback_t f;
|
|
|
2146
|
|
|
2147 bool m_enforceListMode = false;
|
|
|
2148 unsigned m_listMode = 0;
|
|
|
2149
|
|
|
2150 bool on_entry(filesystem* p_owner, abort_callback& p_abort, const char* p_url, bool p_is_subdirectory, const t_filestats& p_stats) override {
|
|
|
2151 (void)p_owner; p_abort.check();
|
|
|
2152 if (m_enforceListMode) {
|
|
|
2153 if (p_is_subdirectory) {
|
|
|
2154 if ( (m_listMode & listMode::folders) == 0 ) return true;
|
|
|
2155 } else {
|
|
|
2156 if ((m_listMode & listMode::files) == 0) return true;
|
|
|
2157 }
|
|
|
2158 }
|
|
|
2159
|
|
|
2160 t_filestats2 stats;
|
|
|
2161 stats.set_folder(p_is_subdirectory);
|
|
|
2162 stats.m_size = p_stats.m_size;
|
|
|
2163 stats.m_timestamp = p_stats.m_timestamp;
|
|
|
2164 f(p_url, stats);
|
|
|
2165 return true;
|
|
|
2166 }
|
|
|
2167 };
|
|
|
2168 }
|
|
|
2169
|
|
|
2170 void filesystem::list_directory_(const char* path, list_callback_t f, unsigned listMode, abort_callback& a) {
|
|
|
2171
|
|
|
2172 {
|
|
|
2173 filesystem_v3::ptr v3;
|
|
|
2174 if (v3 &= this) {
|
|
|
2175 directory_callback_v3_to_lambda cb;
|
|
|
2176 cb.f = f;
|
|
|
2177 v3->list_directory_v3(path, cb, listMode, a);
|
|
|
2178 return;
|
|
|
2179 }
|
|
|
2180 }
|
|
|
2181
|
|
|
2182 {
|
|
|
2183 filesystem_v2::ptr v2;
|
|
|
2184 if (v2 &= this) {
|
|
|
2185 directory_callback_to_lambda cb;
|
|
|
2186 cb.f = f;
|
|
|
2187 v2->list_directory_ex(path, cb, listMode, a);
|
|
|
2188 return;
|
|
|
2189 }
|
|
|
2190 }
|
|
|
2191
|
|
|
2192 directory_callback_to_lambda cb;
|
|
|
2193 cb.m_listMode = listMode;
|
|
|
2194 cb.m_enforceListMode = true;
|
|
|
2195 cb.f = f;
|
|
|
2196 this->list_directory(path, cb, a);
|
|
|
2197 }
|
|
|
2198
|
|
|
2199
|
|
|
2200 fsItemBase::ptr filesystem_v3::findItem(const char* path, abort_callback& p_abort) {
|
|
|
2201 #if PFC_DEBUG
|
|
|
2202 try {
|
|
|
2203 #endif
|
|
|
2204 pfc::string8 canonical;
|
|
|
2205 if (get_canonical_path(path, canonical)) {
|
|
|
2206 auto stats = this->get_stats2(path, stats2_all, p_abort);
|
|
|
2207 if ( stats.is_folder() ) {
|
|
|
2208 return makeItemFolderStd(canonical, stats);
|
|
|
2209 } else {
|
|
|
2210 return makeItemFileStd(canonical, stats );
|
|
|
2211 }
|
|
|
2212 }
|
|
|
2213 throw exception_io_not_found();
|
|
|
2214 #if PFC_DEBUG
|
|
|
2215 } catch (std::exception const& e) {
|
|
|
2216 try {
|
|
|
2217 FB2K_console_formatter() << "filesystem::findItem error: " << e;
|
|
|
2218 FB2K_console_formatter() << "problem path: " << path;
|
|
|
2219 } catch (...) {}
|
|
|
2220 throw;
|
|
|
2221 }
|
|
|
2222 #endif
|
|
|
2223 }
|
|
|
2224 fsItemFile::ptr filesystem_v3::findItemFile(const char* path, abort_callback& p_abort) {
|
|
|
2225 #if PFC_DEBUG
|
|
|
2226 try {
|
|
|
2227 #endif
|
|
|
2228 pfc::string8 canonical;
|
|
|
2229 if (get_canonical_path(path, canonical)) {
|
|
|
2230 auto stats = this->get_stats2( canonical, stats2_all, p_abort);
|
|
|
2231 if ( stats.is_file() ) {
|
|
|
2232 return makeItemFileStd(canonical, stats );
|
|
|
2233 }
|
|
|
2234 }
|
|
|
2235 throw exception_io_not_found();
|
|
|
2236 #if PFC_DEBUG
|
|
|
2237 } catch (std::exception const& e) {
|
|
|
2238 try {
|
|
|
2239 FB2K_console_formatter() << "filesystem::findItemFile error: " << e;
|
|
|
2240 FB2K_console_formatter() << "problem path: " << path;
|
|
|
2241 } catch (...) {}
|
|
|
2242 throw;
|
|
|
2243 }
|
|
|
2244 #endif
|
|
|
2245 }
|
|
|
2246 fsItemFolder::ptr filesystem_v3::findItemFolder(const char* path, abort_callback& p_abort) {
|
|
|
2247 #if PFC_DEBUG
|
|
|
2248 try {
|
|
|
2249 #endif
|
|
|
2250 pfc::string8 canonical;
|
|
|
2251 if (get_canonical_path(path, canonical)) {
|
|
|
2252 auto stats = this->get_stats2( canonical, stats2_all, p_abort);
|
|
|
2253 if ( stats.is_folder() ) {
|
|
|
2254 return makeItemFolderStd(canonical, stats );
|
|
|
2255 }
|
|
|
2256 }
|
|
|
2257 throw exception_io_not_found();
|
|
|
2258 #if PFC_DEBUG
|
|
|
2259 } catch (std::exception const& e) {
|
|
|
2260 try {
|
|
|
2261 FB2K_console_formatter() << "filesystem::findItemFolder error: " << e;
|
|
|
2262 FB2K_console_formatter() << "problem path: " << path;
|
|
|
2263 } catch (...) {}
|
|
|
2264 throw;
|
|
|
2265 }
|
|
|
2266 #endif
|
|
|
2267 }
|
|
|
2268
|
|
|
2269 fsItemFolder::ptr filesystem_v3::findParentFolder(const char* path, abort_callback& p_abort) {
|
|
|
2270 auto parent = parentPath(path);
|
|
|
2271 if (parent == nullptr) {
|
|
|
2272 #if PFC_DEBUG
|
|
|
2273 FB2K_console_formatter() << "filesystem::findParentFolder error: cannot walk up from root path";
|
|
|
2274 FB2K_console_formatter() << "problem path: " << path;
|
|
|
2275 #endif
|
|
|
2276 throw exception_io_not_found();
|
|
|
2277 }
|
|
|
2278 return findItemFolder(parent->c_str(), p_abort);
|
|
|
2279 }
|
|
|
2280
|
|
|
2281 fb2k::stringRef filesystem_v3::fileNameSanity(const char* fn) {
|
|
|
2282 auto ret = fb2k::makeString(pfc::io::path::validateFileName(fn).c_str());
|
|
|
2283 if (ret->length() == 0) ret = fb2k::makeString("_");
|
|
|
2284 return ret;
|
|
|
2285 }
|
|
|
2286
|
|
|
2287 static void readStatsMultiStd(fb2k::arrayRef items, uint32_t s2flags, t_filestats2* outStats, abort_callback& abort) {
|
|
|
2288 const size_t count = items->size();
|
|
|
2289 for (size_t w = 0; w < count; ++w) {
|
|
|
2290 abort.check();
|
|
|
2291 auto& out = outStats[w];
|
|
|
2292 try {
|
|
|
2293 fsItemPtr f; f ^= items->itemAt(w);
|
|
|
2294 out = f->getStats2(s2flags, abort);
|
|
|
2295 } catch (exception_aborted const &) {
|
|
|
2296 throw;
|
|
|
2297 } catch (exception_io const &) {
|
|
|
2298 out = filestats2_invalid;
|
|
|
2299 }
|
|
|
2300 }
|
|
|
2301 }
|
|
|
2302
|
|
|
2303 void filesystem_v3::readStatsMulti(fb2k::arrayRef items, uint32_t s2flags, t_filestats2* outStats, abort_callback& abort) {
|
|
|
2304 readStatsMultiStd(items, s2flags, outStats, abort);
|
|
|
2305 }
|
|
|
2306
|
|
|
2307 pfc::string8 t_filestats::describe() const {
|
|
|
2308 pfc::string8 ret;
|
|
|
2309 ret << "size: ";
|
|
|
2310 if (m_size != filesize_invalid) ret << m_size;
|
|
|
2311 else ret << "N/A";
|
|
|
2312 ret << "\n";
|
|
|
2313 ret << "last-modified: ";
|
|
|
2314 if (m_timestamp != filetimestamp_invalid) ret << m_timestamp;
|
|
|
2315 else ret << "N/A";
|
|
|
2316 ret << "\n";
|
|
|
2317 return ret;
|
|
|
2318 }
|
|
|
2319
|
|
|
2320 pfc::string8 t_filestats2::describe() const {
|
|
|
2321 pfc::string8 ret;
|
|
|
2322 ret << "size: ";
|
|
|
2323 if (m_size != filesize_invalid) ret << m_size;
|
|
|
2324 else ret << "N/A";
|
|
|
2325 ret << "\n";
|
|
|
2326 ret << "last-modified: ";
|
|
|
2327 if (m_timestamp != filetimestamp_invalid) ret << m_timestamp;
|
|
|
2328 else ret << "N/A";
|
|
|
2329 ret << "\n";
|
|
|
2330 ret << "created: ";
|
|
|
2331 if (m_timestampCreate != filetimestamp_invalid) ret << m_timestampCreate;
|
|
|
2332 else ret << "N/A";
|
|
|
2333 ret << "\n";
|
|
|
2334 ret << "attribs: " << format_attribs() << "\n";
|
|
|
2335 ret << "attribs valid: " << format_attribs(m_attribsValid) << "\n";
|
|
|
2336 return ret;
|
|
|
2337 }
|
|
|
2338
|
|
|
2339
|
|
|
2340 #ifndef _WIN32
|
|
|
2341 t_filestats foobar2000_io::nixMakeFileStats(const struct stat & st) {
|
|
|
2342 t_filestats out = filestats_invalid;
|
|
|
2343 out.m_size = st.st_size;
|
|
|
2344 #ifdef __APPLE__
|
|
|
2345 out.m_timestamp = pfc::fileTimeUtoW(st.st_mtimespec);
|
|
|
2346 #else // Linux
|
|
|
2347 out.m_timestamp = pfc::fileTimeUtoW(st.st_mtim);
|
|
|
2348 #endif
|
|
|
2349 return out;
|
|
|
2350 }
|
|
|
2351
|
|
|
2352 bool foobar2000_io::nixQueryReadonly( const struct stat & st ) {
|
|
|
2353 return (st.st_mode & 0222) == 0;
|
|
|
2354 }
|
|
|
2355
|
|
|
2356 bool foobar2000_io::nixQueryDirectory( const struct stat & st ) {
|
|
|
2357 return (st.st_mode & S_IFDIR) != 0;
|
|
|
2358 }
|
|
|
2359
|
|
|
2360 t_filestats2 foobar2000_io::nixMakeFileStats2(const struct stat &st) {
|
|
|
2361 t_filestats2 ret = t_filestats2::from_legacy( nixMakeFileStats( st ) );
|
|
|
2362 #ifdef __APPLE__
|
|
|
2363 ret.m_timestampCreate = pfc::fileTimeUtoW(st.st_birthtimespec);
|
|
|
2364 #else // Linux
|
|
|
2365 ret.m_timestampCreate = pfc::fileTimeUtoW(st.st_ctim);
|
|
|
2366 #endif
|
|
|
2367 ret.set_readonly(nixQueryReadonly(st));
|
|
|
2368 if ( st.st_mode & S_IFDIR ) ret.set_folder();
|
|
|
2369 else if (st.st_mode & S_IFREG ) ret.set_file();
|
|
|
2370 ret.set_remote(false);
|
|
|
2371 return ret;
|
|
|
2372 }
|
|
|
2373
|
|
|
2374 bool foobar2000_io::compactHomeDir(pfc::string_base & str) {
|
|
|
2375 const char * prefix = "file://";
|
|
|
2376 size_t base = 0;
|
|
|
2377 if (pfc::strcmp_partial( str, prefix ) == 0) base = strlen(prefix);
|
|
|
2378 const char * homedir = getenv("HOME");
|
|
|
2379 if (homedir == NULL) return false;
|
|
|
2380 const char * strBase = str.get_ptr() + base;
|
|
|
2381 if (pfc::strcmp_partial(strBase, homedir) == 0) {
|
|
|
2382 const char * strPast = strBase + strlen(homedir);
|
|
|
2383 if (*strPast == 0 || *strPast == '/') {
|
|
|
2384 pfc::string8 temp ( strPast );
|
|
|
2385 str.truncate( base );
|
|
|
2386 str += "~";
|
|
|
2387 str += temp;
|
|
|
2388 return true;
|
|
|
2389 }
|
|
|
2390 }
|
|
|
2391
|
|
|
2392 return false;
|
|
|
2393 }
|
|
|
2394
|
|
|
2395 bool foobar2000_io::expandHomeDir(pfc::string_base & str) {
|
|
|
2396 const char * prefix = "file://";
|
|
|
2397 size_t base = 0;
|
|
|
2398 if (pfc::strcmp_partial( str, prefix ) == 0) base = strlen(prefix);
|
|
|
2399 const char * strBase = str.get_ptr() + base;
|
|
|
2400 if (strBase[0] == '~') {
|
|
|
2401 if (strBase[1] == 0 || strBase[1] == '/') {
|
|
|
2402 const char * homedir = getenv("HOME");
|
|
|
2403 if (homedir == NULL) return false;
|
|
|
2404 pfc::string8 temp( strBase + 1 );
|
|
|
2405 str.truncate( base );
|
|
|
2406 str += homedir;
|
|
|
2407 str += temp;
|
|
|
2408 return true;
|
|
|
2409 }
|
|
|
2410 }
|
|
|
2411
|
|
|
2412 return false;
|
|
|
2413 }
|
|
|
2414
|
|
|
2415 #endif // _WIN32
|
|
|
2416
|
|
|
2417 bool filesystem::g_get_display_name_short( const char * path, pfc::string_base & out ) {
|
|
|
2418 auto i = tryGet( path );
|
|
|
2419 if ( i.is_valid() ) {
|
|
|
2420 if (i->get_display_name_short_(path, out)) return true;
|
|
|
2421 }
|
|
|
2422 out = path;
|
|
|
2423 return false;
|
|
|
2424 }
|
|
|
2425
|
|
|
2426
|
|
|
2427 bool filesystem::g_compare_paths(const char* p1, const char* p2, int& result) {
|
|
|
2428 if (strcmp(p1, p2) == 0) {
|
|
|
2429 result = 0; return true;
|
|
|
2430 }
|
|
|
2431
|
|
|
2432 {
|
|
|
2433 auto s1 = strstr(p1, "://");
|
|
|
2434 auto s2 = strstr(p2, "://");
|
|
|
2435 if (s1 == nullptr || s2 == nullptr) {
|
|
|
2436 PFC_ASSERT(!"Invalid arguments");
|
|
|
2437 return false;
|
|
|
2438 }
|
|
|
2439 size_t prefix = s1 - p1;
|
|
|
2440 if (prefix != (size_t)(s2 - p2)) return false; // protocol mismatch
|
|
|
2441 if (memcmp(p1, p2, prefix) != 0) return false; // protocol mismatch
|
|
|
2442 }
|
|
|
2443
|
|
|
2444 filesystem::ptr fs;
|
|
|
2445 if (!g_get_interface(fs, p1)) {
|
|
|
2446 PFC_ASSERT(!"Invalid arguments");
|
|
|
2447 return false;
|
|
|
2448 }
|
|
|
2449 pfc::string8 temp1(p1), temp2(p2);
|
|
|
2450 auto delim = fs->pathSeparator();
|
|
|
2451 temp1.end_with(delim); temp2.end_with(delim);
|
|
|
2452 if (strcmp(temp1, temp2) == 0) { result = 0; return true; }
|
|
|
2453
|
|
|
2454 //result 1 if p2 is a subpath of p1, -1 if p1 is a subpath of p2
|
|
|
2455 if (pfc::string_has_prefix(temp1, temp2)) {
|
|
|
2456 // temp1 starts with temp2
|
|
|
2457 // p1 a subfolder of p2
|
|
|
2458 result = -1;
|
|
|
2459 return true;
|
|
|
2460 } else if (pfc::string_has_prefix(temp2, temp1)) {
|
|
|
2461 // temp2 starts with temp1
|
|
|
2462 // p2 a subfolder of p1
|
|
|
2463 result = 1;
|
|
|
2464 return true;
|
|
|
2465 } else {
|
|
|
2466 return false;
|
|
|
2467 }
|
|
|
2468 }
|
|
|
2469
|
|
|
2470 size_t file::receive(void* ptr, size_t bytes, abort_callback& a) {
|
|
|
2471 stream_receive::ptr obj;
|
|
|
2472 if (obj &= this) return obj->receive(ptr, bytes, a);
|
|
|
2473 else return this->read(ptr, bytes, a);
|
|
|
2474 }
|
|
|
2475
|
|
|
2476 void filesystem::g_readStatsMulti(fb2k::arrayRef items, uint32_t s2flags, t_filestats2* outStats, abort_callback& abort) {
|
|
|
2477 if (items->size() == 0) return;
|
|
|
2478 fsItemPtr aFile; aFile ^= items->itemAt(0);
|
|
|
2479 filesystem_v3::ptr fs;
|
|
|
2480 if (fs &= aFile->getFS()) {
|
|
|
2481 fs->readStatsMulti(items, s2flags, outStats, abort);
|
|
|
2482 } else {
|
|
|
2483 readStatsMultiStd(items, s2flags, outStats, abort);
|
|
|
2484 }
|
|
|
2485 }
|
|
|
2486
|
|
|
2487 void file::set_stats(t_filestats2 const& stats, abort_callback& a) {
|
|
|
2488 if (stats.haveTimestamp() || stats.haveTimestampCreate()) {
|
|
|
2489 filetimes_t ft;
|
|
|
2490 ft.creation = stats.m_timestampCreate;
|
|
|
2491 ft.lastWrite = stats.m_timestamp;
|
|
|
2492 this->setFileTimes(ft, a);
|
|
|
2493 }
|
|
|
2494 }
|
|
|
2495
|
|
|
2496 fb2k::stringRef filesystem::fileNameSanity_(const char* fn) {
|
|
|
2497 filesystem_v3::ptr v3;
|
|
|
2498 if (v3 &= this) return v3->fileNameSanity(fn);
|
|
|
2499 throw pfc::exception_not_implemented();
|
|
|
2500 }
|
|
|
2501
|
|
|
2502 drivespace_t filesystem::getDriveSpace_(const char* pathAt, abort_callback& abort) {
|
|
|
2503 filesystem_v3::ptr v3;
|
|
|
2504 if (v3 &= this) return v3->getDriveSpace(pathAt, abort);
|
|
|
2505 throw pfc::exception_not_implemented();
|
|
|
2506 }
|
|
|
2507
|
|
|
2508 size_t stream_receive::read_using_receive(void* ptr_, size_t bytes, abort_callback& a) {
|
|
|
2509 size_t walk = 0;
|
|
|
2510 auto ptr = reinterpret_cast<uint8_t*>(ptr_);
|
|
|
2511 while(walk < bytes) {
|
|
|
2512 size_t want = bytes-walk;
|
|
|
2513 size_t delta = this->receive(ptr+walk, want, a);
|
|
|
2514 PFC_ASSERT( delta <= want );
|
|
|
2515 if ( delta == 0 ) break;
|
|
|
2516 walk += delta;
|
|
|
2517 }
|
|
|
2518 return walk;
|
|
|
2519 } |