1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
10 #include "zypp/base/Logger.h"
11 #include "zypp/Fetcher.h"
12 #include "zypp/MediaSetAccess.h"
13 #include "zypp/PathInfo.h"
16 using namespace zypp::filesystem;
21 Fetcher::Fetcher( const Url &url, const Pathname &path )
22 : _url(url), _path(path)
27 void Fetcher::enqueue( const OnMediaLocation &resource )
29 _resources.push_back(resource);
38 void Fetcher::insertCache( const Pathname &cache_dir )
40 _caches.push_back(cache_dir);
43 void Fetcher::start( const Pathname &dest_dir )
45 MediaSetAccess media(_url, _path);
47 for ( list<OnMediaLocation>::const_iterator it_res = _resources.begin(); it_res != _resources.end(); ++it_res )
49 bool got_from_cache = false;
50 for ( list<Pathname>::const_iterator it_cache = _caches.begin(); it_cache != _caches.end(); ++it_cache )
52 // Pathinfos could be cached to avoid too many stats?
53 PathInfo info(*it_cache);
56 // does the current file exists in the current cache?
57 Pathname cached_file = *it_cache + (*it_res).filename();
58 if ( PathInfo( cached_file ).isExist() )
61 if ( is_checksum( cached_file, (*it_res).checksum() ) )
64 MIL << "file " << (*it_res).filename() << " found in previous cache. Using cached copy." << endl;
65 // checksum is already checked.
66 // we could later implement double failover and try to download if file copy fails.
68 // replicate the complete path in the target directory
69 Pathname dest_full_path = dest_dir + (*it_res).filename();
70 if ( assert_dir( dest_full_path.dirname() ) != 0 )
71 ZYPP_THROW( Exception("Can't create " + dest_full_path.dirname().asString()));
73 if ( filesystem::copy(cached_file, dest_full_path ) != 0 )
75 //ZYPP_THROW(SourceIOException("Can't copy " + cached_file.asString() + " to " + destination.asString()));
76 ERR << "Can't copy " << cached_file + " to " + dest_dir << endl;
81 got_from_cache = true;
87 // File exists in cache but with a different checksum
88 // so just try next cache
94 // skip bad cache directory and try with next one
95 ERR << "Skipping cache : " << *it_cache << endl;
100 if ( ! got_from_cache )
102 // try to get the file from the net
105 Pathname tmp_file = media.provideFile(*it_res);
106 Pathname dest_full_path = dest_dir + (*it_res).filename();
107 if ( assert_dir( dest_full_path.dirname() ) != 0 )
108 ZYPP_THROW( Exception("Can't create " + dest_full_path.dirname().asString()));
109 if ( filesystem::copy(tmp_file, dest_full_path ) != 0 )
111 ZYPP_THROW( Exception("Can't copy " + tmp_file.asString() + " to " + dest_dir.asString()));
114 catch (const Exception & excpt_r)
116 ZYPP_CAUGHT(excpt_r);
117 ZYPP_THROW(Exception("Can't provide " + (*it_res).filename().asString() + " : " + excpt_r.msg() ));
122 // We got the file from cache
123 // continue with next file
128 // callback::SendReport<DigestReport> report;
129 // if ( checksum.empty() )
131 // MIL << "File " << file_url << " has no checksum available." << std::endl;
132 // if ( report->askUserToAcceptNoDigest(file_to_download) )
134 // MIL << "User accepted " << file_url << " with no checksum." << std::endl;
139 // ZYPP_THROW(SourceMetadataException( file_url.asString() + " " + N_(" miss checksum.") ));
144 // if (! is_checksum( destination, checksum))
145 // ZYPP_THROW(SourceMetadataException( file_url.asString() + " " + N_(" fails checksum verification.") ));