]> git.saurik.com Git - apt.git/blob - apt-pkg/cachefile.cc
support optional PulseInterval in ListUpdate
[apt.git] / apt-pkg / cachefile.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: cachefile.cc,v 1.8 2002/04/27 04:28:04 jgg Exp $
4 /* ######################################################################
5
6 CacheFile - Simple wrapper class for opening, generating and whatnot
7
8 This class implements a simple 2 line mechanism to open various sorts
9 of caches. It can operate as root, as not root, show progress and so on,
10 it transparently handles everything necessary.
11
12 ##################################################################### */
13 /*}}}*/
14 // Include Files /*{{{*/
15 #include <apt-pkg/cachefile.h>
16 #include <apt-pkg/error.h>
17 #include <apt-pkg/sourcelist.h>
18 #include <apt-pkg/pkgcachegen.h>
19 #include <apt-pkg/configuration.h>
20 #include <apt-pkg/policy.h>
21 #include <apt-pkg/pkgsystem.h>
22 #include <apt-pkg/acquire-item.h>
23 #include <apt-pkg/fileutl.h>
24
25 #include <apti18n.h>
26 /*}}}*/
27
28 // CacheFile::CacheFile - Constructor /*{{{*/
29 // ---------------------------------------------------------------------
30 /* */
31 pkgCacheFile::pkgCacheFile() : Map(0), Cache(0), DCache(0), Policy(0)
32 {
33 }
34 /*}}}*/
35 // CacheFile::~CacheFile - Destructor /*{{{*/
36 // ---------------------------------------------------------------------
37 /* */
38 pkgCacheFile::~pkgCacheFile()
39 {
40 delete DCache;
41 delete Policy;
42 delete Cache;
43 delete Map;
44 _system->UnLock(true);
45 }
46 /*}}}*/
47 // CacheFile::BuildCaches - Open and build the cache files /*{{{*/
48 // ---------------------------------------------------------------------
49 /* */
50 bool pkgCacheFile::BuildCaches(OpProgress &Progress,bool WithLock)
51 {
52 if (WithLock == true)
53 if (_system->Lock() == false)
54 return false;
55
56 if (_config->FindB("Debug::NoLocking",false) == true)
57 WithLock = false;
58
59 if (_error->PendingError() == true)
60 return false;
61
62 // Read the source list
63 pkgSourceList List;
64 if (List.ReadMainList() == false)
65 return _error->Error(_("The list of sources could not be read."));
66
67 // Read the caches
68 bool Res = pkgMakeStatusCache(List,Progress,&Map,!WithLock);
69 Progress.Done();
70 if (Res == false)
71 return _error->Error(_("The package lists or status file could not be parsed or opened."));
72
73 /* This sux, remove it someday */
74 if (_error->empty() == false)
75 _error->Warning(_("You may want to run apt-get update to correct these problems"));
76
77 Cache = new pkgCache(Map);
78 if (_error->PendingError() == true)
79 return false;
80 return true;
81 }
82 /*}}}*/
83 // CacheFile::Open - Open the cache files, creating if necessary /*{{{*/
84 // ---------------------------------------------------------------------
85 /* */
86 bool pkgCacheFile::Open(OpProgress &Progress,bool WithLock)
87 {
88 if (BuildCaches(Progress,WithLock) == false)
89 return false;
90
91 // The policy engine
92 Policy = new pkgPolicy(Cache);
93 if (_error->PendingError() == true)
94 return false;
95 if (ReadPinFile(*Policy) == false)
96 return false;
97
98 // Create the dependency cache
99 DCache = new pkgDepCache(Cache,Policy);
100 if (_error->PendingError() == true)
101 return false;
102
103 DCache->Init(&Progress);
104 Progress.Done();
105 if (_error->PendingError() == true)
106 return false;
107
108 return true;
109 }
110 /*}}}*/
111
112 // CacheFile::ListUpdate - update the cache files /*{{{*/
113 // ---------------------------------------------------------------------
114 /* This is a simple wrapper to update the cache. it will fetch stuff
115 * from the network (or any other sources defined in sources.list)
116 */
117 bool pkgCacheFile::ListUpdate(pkgAcquireStatus &Stat,
118 pkgSourceList &List,
119 int PulseInterval)
120 {
121 pkgAcquire::RunResult res;
122 pkgAcquire Fetcher(&Stat);
123
124 // Populate it with the source selection
125 if (List.GetIndexes(&Fetcher) == false)
126 return false;
127
128 // Run scripts
129 RunScripts("APT::Update::Pre-Invoke");
130
131 // check arguments
132 if(PulseInterval>0)
133 res = Fetcher.Run(PulseInterval);
134 else
135 res = Fetcher.Run();
136
137 if (res == pkgAcquire::Failed)
138 return false;
139
140 bool Failed = false;
141 bool TransientNetworkFailure = false;
142 for (pkgAcquire::ItemIterator I = Fetcher.ItemsBegin();
143 I != Fetcher.ItemsEnd(); I++)
144 {
145 if ((*I)->Status == pkgAcquire::Item::StatDone)
146 continue;
147
148 (*I)->Finished();
149
150 fprintf(stderr,_("Failed to fetch %s %s\n"),(*I)->DescURI().c_str(),
151 (*I)->ErrorText.c_str());
152
153 if ((*I)->Status == pkgAcquire::Item::StatTransientNetworkError)
154 {
155 TransientNetworkFailure = true;
156 continue;
157 }
158
159 Failed = true;
160 }
161
162 // Clean out any old list files
163 // Keep "APT::Get::List-Cleanup" name for compatibility, but
164 // this is really a global option for the APT library now
165 if (!TransientNetworkFailure && !Failed &&
166 (_config->FindB("APT::Get::List-Cleanup",true) == true ||
167 _config->FindB("APT::List-Cleanup",true) == true))
168 {
169 if (Fetcher.Clean(_config->FindDir("Dir::State::lists")) == false ||
170 Fetcher.Clean(_config->FindDir("Dir::State::lists") + "partial/") == false)
171 // something went wrong with the clean
172 return false;
173 }
174
175 if (TransientNetworkFailure == true)
176 _error->Warning(_("Some index files failed to download, they have been ignored, or old ones used instead."));
177 else if (Failed == true)
178 return _error->Error(_("Some index files failed to download, they have been ignored, or old ones used instead."));
179
180
181 // Run the scripts if all was fine
182 RunScripts("APT::Update::Post-Invoke");
183 return true;
184 }
185 /*}}}*/
186
187 // CacheFile::Close - close the cache files /*{{{*/
188 // ---------------------------------------------------------------------
189 /* */
190 void pkgCacheFile::Close()
191 {
192 delete DCache;
193 delete Policy;
194 delete Cache;
195 delete Map;
196 _system->UnLock(true);
197
198 Map = 0;
199 DCache = 0;
200 Policy = 0;
201 Cache = 0;
202 }
203 /*}}}*/