]> git.saurik.com Git - apt.git/blob - apt-pkg/cachefile.cc
96d9672c2e9527686cb0547374a6317ac279dab5
[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 #ifdef __GNUG__
16 #pragma implementation "apt-pkg/cachefile.h"
17 #endif
18
19 #include <apt-pkg/cachefile.h>
20 #include <apt-pkg/error.h>
21 #include <apt-pkg/sourcelist.h>
22 #include <apt-pkg/pkgcachegen.h>
23 #include <apt-pkg/configuration.h>
24 #include <apt-pkg/policy.h>
25 #include <apt-pkg/pkgsystem.h>
26 #include <apt-pkg/acquire-item.h>
27
28 #include <apti18n.h>
29 /*}}}*/
30
31 // CacheFile::CacheFile - Constructor /*{{{*/
32 // ---------------------------------------------------------------------
33 /* */
34 pkgCacheFile::pkgCacheFile() : Map(0), Cache(0), DCache(0), Policy(0)
35 {
36 }
37 /*}}}*/
38 // CacheFile::~CacheFile - Destructor /*{{{*/
39 // ---------------------------------------------------------------------
40 /* */
41 pkgCacheFile::~pkgCacheFile()
42 {
43 delete DCache;
44 delete Policy;
45 delete Cache;
46 delete Map;
47 _system->UnLock(true);
48 }
49 /*}}}*/
50 // CacheFile::BuildCaches - Open and build the cache files /*{{{*/
51 // ---------------------------------------------------------------------
52 /* */
53 bool pkgCacheFile::BuildCaches(OpProgress &Progress,bool WithLock)
54 {
55 if (WithLock == true)
56 if (_system->Lock() == false)
57 return false;
58
59 if (_config->FindB("Debug::NoLocking",false) == true)
60 WithLock = false;
61
62 if (_error->PendingError() == true)
63 return false;
64
65 // Read the source list
66 pkgSourceList List;
67 if (List.ReadMainList() == false)
68 return _error->Error(_("The list of sources could not be read."));
69
70 // Read the caches
71 bool Res = pkgMakeStatusCache(List,Progress,&Map,!WithLock);
72 Progress.Done();
73 if (Res == false)
74 return _error->Error(_("The package lists or status file could not be parsed or opened."));
75
76 /* This sux, remove it someday */
77 if (_error->empty() == false)
78 _error->Warning(_("You may want to run apt-get update to correct these problems"));
79
80 Cache = new pkgCache(Map);
81 if (_error->PendingError() == true)
82 return false;
83 return true;
84 }
85 /*}}}*/
86 // CacheFile::Open - Open the cache files, creating if necessary /*{{{*/
87 // ---------------------------------------------------------------------
88 /* */
89 bool pkgCacheFile::Open(OpProgress &Progress,bool WithLock)
90 {
91 if (BuildCaches(Progress,WithLock) == false)
92 return false;
93
94 // The policy engine
95 Policy = new pkgPolicy(Cache);
96 if (_error->PendingError() == true)
97 return false;
98 if (ReadPinFile(*Policy) == false)
99 return false;
100
101 // Create the dependency cache
102 DCache = new pkgDepCache(Cache,Policy);
103 if (_error->PendingError() == true)
104 return false;
105
106 DCache->Init(&Progress);
107 Progress.Done();
108 if (_error->PendingError() == true)
109 return false;
110
111 return true;
112 }
113 /*}}}*/
114
115 // CacheFile::ListUpdate - update the cache files /*{{{*/
116 // ---------------------------------------------------------------------
117 /* */
118 bool pkgCacheFile::ListUpdate(pkgAcquireStatus &Stat, pkgSourceList &List)
119 {
120 pkgAcquire Fetcher(&Stat);
121
122 // Populate it with the source selection
123 if (List.GetIndexes(&Fetcher) == false)
124 return false;
125
126 // Run it
127 if (Fetcher.Run() == pkgAcquire::Failed)
128 return false;
129
130 bool Failed = false;
131 for (pkgAcquire::ItemIterator I = Fetcher.ItemsBegin(); I != Fetcher.ItemsEnd(); I++)
132 {
133 if ((*I)->Status == pkgAcquire::Item::StatDone)
134 continue;
135
136 (*I)->Finished();
137
138 _error->Warning(_("Failed to fetch %s %s\n"),
139 (*I)->DescURI().c_str(),
140 (*I)->ErrorText.c_str());
141 Failed = true;
142 }
143
144 // Clean out any old list files (if it was not a failure)
145 // Keep "APT::Get::List-Cleanup" name for compatibility, but
146 // this is really a global option for the APT library now
147 if (!Failed && (_config->FindB("APT::Get::List-Cleanup",true) == true ||
148 _config->FindB("APT::List-Cleanup",true) == true))
149 {
150 if (Fetcher.Clean(_config->FindDir("Dir::State::lists")) == false ||
151 Fetcher.Clean(_config->FindDir("Dir::State::lists") + "partial/") == false)
152 return false;
153 }
154
155
156 return (Failed == false);
157 }
158 /*}}}*/
159
160 // CacheFile::Close - close the cache files /*{{{*/
161 // ---------------------------------------------------------------------
162 /* */
163 void pkgCacheFile::Close()
164 {
165 delete DCache;
166 delete Policy;
167 delete Cache;
168 delete Map;
169 _system->UnLock(true);
170
171 Map = 0;
172 DCache = 0;
173 Policy = 0;
174 Cache = 0;
175 }
176 /*}}}*/