rename the insert cache method
[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()
22 {
23
24 }
25
26 void Fetcher::enqueue( const OnMediaLocation &resource )
27 {
28   _resources.push_back(resource);
29 }
30
31 void Fetcher::reset()
32 {
33   _resources.clear();
34   _caches.clear();
35 }
36
37 void Fetcher::addCachePath( const Pathname &cache_dir )
38 {
39   _caches.push_back(cache_dir);
40 }
41
42 void Fetcher::start( const Pathname &dest_dir, MediaSetAccess &media )
43 {
44   for ( list<OnMediaLocation>::const_iterator it_res = _resources.begin(); it_res != _resources.end(); ++it_res )
45   {
46     bool got_from_cache = false;
47     for ( list<Pathname>::const_iterator it_cache = _caches.begin(); it_cache != _caches.end(); ++it_cache )
48     {
49       // Pathinfos could be cached to avoid too many stats?
50       PathInfo info(*it_cache);
51       if ( info.isDir() )
52       {
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() )
56         {
57           // check the checksum
58           if ( is_checksum( cached_file, (*it_res).checksum() ) )
59           {
60             // cached
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.
64
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()));
69
70             if ( filesystem::copy(cached_file, dest_full_path ) != 0 )
71             { //copy_file2dir
72               //ZYPP_THROW(SourceIOException("Can't copy " + cached_file.asString() + " to " + destination.asString()));
73               ERR << "Can't copy " << cached_file + " to " + dest_dir << endl;
74               // try next cache
75               continue;
76             }
77
78             got_from_cache = true;
79             break;
80           }
81         }
82         else
83         {
84           // File exists in cache but with a different checksum
85           // so just try next cache
86           continue;
87         }
88       }
89       else
90       {
91         // skip bad cache directory and try with next one
92         ERR << "Skipping cache : " << *it_cache << endl;
93         continue;
94       }
95     }
96
97     if ( ! got_from_cache )
98     {
99       // try to get the file from the net
100       try
101       {
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 )
107         {
108           ZYPP_THROW( Exception("Can't copy " + tmp_file.asString() + " to " + dest_dir.asString()));
109         }
110       }
111       catch (const Exception & excpt_r)
112       {
113         ZYPP_CAUGHT(excpt_r);
114         ZYPP_THROW(Exception("Can't provide " + (*it_res).filename().asString() + " : " + excpt_r.msg() ));
115       }
116     }
117     else
118     {
119       // We got the file from cache
120       // continue with next file
121       continue;
122     }
123   }
124 }
125 //       callback::SendReport<DigestReport> report;
126 //       if ( checksum.empty() )
127 //       {
128 //         MIL << "File " <<  file_url << " has no checksum available." << std::endl;
129 //         if ( report->askUserToAcceptNoDigest(file_to_download) )
130 //         {
131 //           MIL << "User accepted " <<  file_url << " with no checksum." << std::endl;
132 //           return;
133 //         }
134 //         else
135 //         {
136 //           ZYPP_THROW(SourceMetadataException( file_url.asString() + " " + N_(" miss checksum.") ));
137 //         }
138 //       }
139 //       else
140 //       {
141 //         if (! is_checksum( destination, checksum))
142 //           ZYPP_THROW(SourceMetadataException( file_url.asString() + " " + N_(" fails checksum verification.") ));
143 //       }
144
145 } // namespace zypp