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;
26 void Fetcher::enqueue( const OnMediaLocation &resource )
28 _resources.push_back(resource);
37 void Fetcher::addCachePath( const Pathname &cache_dir )
39 _caches.push_back(cache_dir);
42 void Fetcher::start( const Pathname &dest_dir, MediaSetAccess &media )
44 for ( list<OnMediaLocation>::const_iterator it_res = _resources.begin(); it_res != _resources.end(); ++it_res )
46 bool got_from_cache = false;
47 for ( list<Pathname>::const_iterator it_cache = _caches.begin(); it_cache != _caches.end(); ++it_cache )
49 // Pathinfos could be cached to avoid too many stats?
50 PathInfo info(*it_cache);
53 // does the current file exists in the current cache?
54 Pathname cached_file = *it_cache + (*it_res).filename();
55 if ( PathInfo( cached_file ).isExist() )
58 if ( is_checksum( cached_file, (*it_res).checksum() ) )
61 MIL << "file " << (*it_res).filename() << " found in previous cache. Using cached copy." << endl;
62 // checksum is already checked.
63 // we could later implement double failover and try to download if file copy fails.
65 // replicate the complete path in the target directory
66 Pathname dest_full_path = dest_dir + (*it_res).filename();
67 if ( assert_dir( dest_full_path.dirname() ) != 0 )
68 ZYPP_THROW( Exception("Can't create " + dest_full_path.dirname().asString()));
70 if ( filesystem::copy(cached_file, dest_full_path ) != 0 )
72 //ZYPP_THROW(SourceIOException("Can't copy " + cached_file.asString() + " to " + destination.asString()));
73 ERR << "Can't copy " << cached_file + " to " + dest_dir << endl;
78 got_from_cache = true;
84 // File exists in cache but with a different checksum
85 // so just try next cache
91 // skip bad cache directory and try with next one
92 ERR << "Skipping cache : " << *it_cache << endl;
97 if ( ! got_from_cache )
99 // try to get the file from the net
102 Pathname tmp_file = media.provideFile(*it_res);
103 Pathname dest_full_path = dest_dir + (*it_res).filename();
104 if ( assert_dir( dest_full_path.dirname() ) != 0 )
105 ZYPP_THROW( Exception("Can't create " + dest_full_path.dirname().asString()));
106 if ( filesystem::copy(tmp_file, dest_full_path ) != 0 )
108 ZYPP_THROW( Exception("Can't copy " + tmp_file.asString() + " to " + dest_dir.asString()));
111 catch (const Exception & excpt_r)
113 ZYPP_CAUGHT(excpt_r);
114 ZYPP_THROW(Exception("Can't provide " + (*it_res).filename().asString() + " : " + excpt_r.msg() ));
119 // We got the file from cache
120 // continue with next file
125 // callback::SendReport<DigestReport> report;
126 // if ( checksum.empty() )
128 // MIL << "File " << file_url << " has no checksum available." << std::endl;
129 // if ( report->askUserToAcceptNoDigest(file_to_download) )
131 // MIL << "User accepted " << file_url << " with no checksum." << std::endl;
136 // ZYPP_THROW(SourceMetadataException( file_url.asString() + " " + N_(" miss checksum.") ));
141 // if (! is_checksum( destination, checksum))
142 // ZYPP_THROW(SourceMetadataException( file_url.asString() + " " + N_(" fails checksum verification.") ));