fixes to compile with latest gcc
[platform/upstream/libzypp.git] / zypp / Fetcher.cc
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9
10 #include "zypp/base/Logger.h"
11 #include "zypp/Fetcher.h"
12 #include "zypp/MediaSetAccess.h"
13 #include "zypp/PathInfo.h"
14
15 using namespace std;
16 using namespace zypp::filesystem;
17
18 namespace zypp
19 {
20
21 Fetcher::Fetcher( const Url &url, const Pathname &path )
22   : _url(url), _path(path)
23 {
24
25 }
26
27 void Fetcher::enqueue( const OnMediaLocation &resource )
28 {
29   _resources.push_back(resource);
30 }
31
32 void Fetcher::reset()
33 {
34   _resources.clear();
35   _caches.clear();
36 }
37
38 void Fetcher::insertCache( const Pathname &cache_dir )
39 {
40   _caches.push_back(cache_dir);
41 }
42
43 void Fetcher::start( const Pathname &dest_dir )
44 {
45   MediaSetAccess media(_url, _path);
46
47   for ( list<OnMediaLocation>::const_iterator it_res = _resources.begin(); it_res != _resources.end(); ++it_res )
48   {
49     bool got_from_cache = false;
50     for ( list<Pathname>::const_iterator it_cache = _caches.begin(); it_cache != _caches.end(); ++it_cache )
51     {
52       // Pathinfos could be cached to avoid too many stats?
53       PathInfo info(*it_cache);
54       if ( info.isDir() )
55       {
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() )
59         {
60           // check the checksum
61           if ( is_checksum( cached_file, (*it_res).checksum() ) )
62           {
63             // cached
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.
67
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()));
72
73             if ( filesystem::copy(cached_file, dest_full_path ) != 0 )
74             { //copy_file2dir
75               //ZYPP_THROW(SourceIOException("Can't copy " + cached_file.asString() + " to " + destination.asString()));
76               ERR << "Can't copy " << cached_file + " to " + dest_dir << endl;
77               // try next cache
78               continue;
79             }
80
81             got_from_cache = true;
82             break;
83           }
84         }
85         else
86         {
87           // File exists in cache but with a different checksum
88           // so just try next cache
89           continue;
90         }
91       }
92       else
93       {
94         // skip bad cache directory and try with next one
95         ERR << "Skipping cache : " << *it_cache << endl;
96         continue;
97       }
98     }
99
100     if ( ! got_from_cache )
101     {
102       // try to get the file from the net
103       try
104       {
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 )
110         {
111           ZYPP_THROW( Exception("Can't copy " + tmp_file.asString() + " to " + dest_dir.asString()));
112         }
113       }
114       catch (const Exception & excpt_r)
115       {
116         ZYPP_CAUGHT(excpt_r);
117         ZYPP_THROW(Exception("Can't provide " + (*it_res).filename().asString() + " : " + excpt_r.msg() ));
118       }
119     }
120     else
121     {
122       // We got the file from cache
123       // continue with next file
124       continue;
125     }
126   }
127 }
128 //       callback::SendReport<DigestReport> report;
129 //       if ( checksum.empty() )
130 //       {
131 //         MIL << "File " <<  file_url << " has no checksum available." << std::endl;
132 //         if ( report->askUserToAcceptNoDigest(file_to_download) )
133 //         {
134 //           MIL << "User accepted " <<  file_url << " with no checksum." << std::endl;
135 //           return;
136 //         }
137 //         else
138 //         {
139 //           ZYPP_THROW(SourceMetadataException( file_url.asString() + " " + N_(" miss checksum.") ));
140 //         }
141 //       }
142 //       else
143 //       {
144 //         if (! is_checksum( destination, checksum))
145 //           ZYPP_THROW(SourceMetadataException( file_url.asString() + " " + N_(" fails checksum verification.") ));
146 //       }
147
148 } // namespace zypp