]>
git.saurik.com Git - apt-legacy.git/blob - apt-inst/deb/dpkgdb.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: dpkgdb.cc,v 1.7.2.1 2004/01/16 18:58:50 mdz Exp $
4 /* ######################################################################
6 DPKGv1 Database Implemenation
8 This class provides parsers and other implementations for the DPKGv1
9 database. It reads the diversion file, the list files and the status
10 file to build both the list of currently installed files and the
11 currently installed package list.
13 ##################################################################### */
15 // Include Files /*{{{*/
16 #include <apt-pkg/dpkgdb.h>
17 #include <apt-pkg/configuration.h>
18 #include <apt-pkg/error.h>
19 #include <apt-pkg/progress.h>
20 #include <apt-pkg/tagfile.h>
21 #include <apt-pkg/strutl.h>
25 #include <sys/types.h>
36 // EraseDir - Erase A Directory /*{{{*/
37 // ---------------------------------------------------------------------
38 /* This is necessary to create a new empty sub directory. The caller should
39 invoke mkdir after this with the proper permissions and check for
40 error. Maybe stick this in fileutils */
41 static bool EraseDir(const char *Dir
)
43 // First we try a simple RM
44 if (rmdir(Dir
) == 0 ||
48 // A file? Easy enough..
52 return _error
->Errno("unlink",_("Failed to remove %s"),Dir
);
57 if (errno
!= ENOTEMPTY
)
58 return _error
->Errno("rmdir",_("Failed to remove %s"),Dir
);
61 pid_t Pid
= ExecFork();
63 // Spawn the subprocess
66 execlp(_config
->Find("Dir::Bin::rm","/bin/rm").c_str(),
67 "rm","-rf","--",Dir
,(char *)NULL
);
70 return ExecWait(Pid
,_config
->Find("dir::bin::rm","/bin/rm").c_str());
73 // DpkgDB::debDpkgDB - Constructor /*{{{*/
74 // ---------------------------------------------------------------------
76 debDpkgDB::debDpkgDB() : CacheMap(0), FileMap(0)
78 AdminDir
= flNotFile(_config
->Find("Dir::State::status"));
83 // DpkgDB::~debDpkgDB - Destructor /*{{{*/
84 // ---------------------------------------------------------------------
86 debDpkgDB::~debDpkgDB()
99 // DpkgDB::InitMetaTmp - Get the temp dir for meta information /*{{{*/
100 // ---------------------------------------------------------------------
101 /* This creats+empties the meta temporary directory /var/lib/dpkg/tmp.ci
102 Only one package at a time can be using the returned meta directory. */
103 bool debDpkgDB::InitMetaTmp(string
&Dir
)
105 string Tmp
= AdminDir
+ "tmp.ci/";
106 if (EraseDir(Tmp
.c_str()) == false)
107 return _error
->Error(_("Unable to create %s"),Tmp
.c_str());
108 if (mkdir(Tmp
.c_str(),0755) != 0)
109 return _error
->Errno("mkdir",_("Unable to create %s"),Tmp
.c_str());
111 // Verify it is on the same filesystem as the main info directory
114 if (stat((AdminDir
+ "info").c_str(),&St
) != 0)
115 return _error
->Errno("stat",_("Failed to stat %sinfo"),AdminDir
.c_str());
117 if (stat(Tmp
.c_str(),&St
) != 0)
118 return _error
->Errno("stat",_("Failed to stat %s"),Tmp
.c_str());
119 if (Dev
!= St
.st_dev
)
120 return _error
->Error(_("The info and temp directories need to be on the same filesystem"));
127 // DpkgDB::ReadyPkgCache - Prepare the cache with the current status /*{{{*/
128 // ---------------------------------------------------------------------
129 /* This reads in the status file into an empty cache. This really needs
130 to be somehow unified with the high level APT notion of the Database
131 directory, but there is no clear way on how to do that yet. */
132 bool debDpkgDB::ReadyPkgCache(OpProgress
&Progress
)
136 Progress
.OverallProgress(1,1,1,_("Reading package lists"));
146 if (pkgMakeOnlyStatusCache(Progress
,&CacheMap
) == false)
148 Cache
->DropProgress();
153 // DpkgDB::ReadFList - Read the File Listings in /*{{{*/
154 // ---------------------------------------------------------------------
155 /* This reads the file listing in from the state directory. This is a
156 performance critical routine, as it needs to parse about 50k lines of
157 text spread over a hundred or more files. For an initial cold start
158 most of the time is spent in reading file inodes and so on, not
160 bool debDpkgDB::ReadFList(OpProgress
&Progress
)
162 // Count the number of packages we need to read information for
163 unsigned long Total
= 0;
164 pkgCache
&Cache
= this->Cache
->GetCache();
165 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
167 // Only not installed packages have no files.
168 if (I
->CurrentState
== pkgCache::State::NotInstalled
)
173 /* Switch into the admin dir, this prevents useless lookups for the
175 string Cwd
= SafeGetCWD();
176 if (chdir((AdminDir
+ "info/").c_str()) != 0)
177 return _error
->Errno("chdir",_("Failed to change to the admin dir %sinfo"),AdminDir
.c_str());
179 // Allocate a buffer. Anything larger than this buffer will be mmaped
180 unsigned long BufSize
= 32*1024;
181 char *Buffer
= new char[BufSize
];
183 // Begin Loading them
184 unsigned long Count
= 0;
186 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
188 /* Only not installed packages have no files. ConfFile packages have
189 file lists but we don't want to read them in */
190 if (I
->CurrentState
== pkgCache::State::NotInstalled
||
191 I
->CurrentState
== pkgCache::State::ConfigFiles
)
194 // Fetch a package handle to associate with the file
195 pkgFLCache::PkgIterator FlPkg
= FList
->GetPkg(I
.Name(),0,true);
196 if (FlPkg
.end() == true)
198 _error
->Error(_("Internal error getting a package name"));
202 Progress
.OverallProgress(Count
,Total
,1,_("Reading file listing"));
204 // Open the list file
205 snprintf(Name
,sizeof(Name
),"%s.list",I
.Name());
206 int Fd
= open(Name
,O_RDONLY
);
208 /* Okay this is very strange and bad.. Best thing is to bail and
209 instruct the user to look into it. */
211 if (Fd
== -1 || fstat(Fd
,&Stat
) != 0)
213 _error
->Errno("open",_("Failed to open the list file '%sinfo/%s'. If you "
214 "cannot restore this file then make it empty "
215 "and immediately re-install the same version of the package!"),
216 AdminDir
.c_str(),Name
);
220 // Set File to be a memory buffer containing the whole file
222 if ((unsigned)Stat
.st_size
< BufSize
)
224 if (read(Fd
,Buffer
,Stat
.st_size
) != Stat
.st_size
)
226 _error
->Errno("read",_("Failed reading the list file %sinfo/%s"),
227 AdminDir
.c_str(),Name
);
236 File
= (char *)mmap(0,Stat
.st_size
,PROT_READ
,MAP_PRIVATE
,Fd
,0);
237 if (File
== (char *)(-1))
239 _error
->Errno("mmap",_("Failed reading the list file %sinfo/%s"),
240 AdminDir
.c_str(),Name
);
247 const char *Start
= File
;
248 const char *End
= File
;
249 const char *Finish
= File
+ Stat
.st_size
;
250 for (; End
< Finish
; End
++)
252 // Not an end of line
253 if (*End
!= '\n' && End
+ 1 < Finish
)
259 pkgFLCache::NodeIterator Node
= FList
->GetNode(Start
,End
,
260 FlPkg
.Offset(),true,false);
261 if (Node
.end() == true)
263 _error
->Error(_("Internal error getting a node"));
268 // Skip past the end of line
269 for (; *End
== '\n' && End
< Finish
; End
++);
274 if ((unsigned)Stat
.st_size
>= BufSize
)
275 munmap((caddr_t
)File
,Stat
.st_size
);
285 if (chdir(Cwd
.c_str()) != 0)
288 return !_error
->PendingError();
291 // DpkgDB::ReadDiversions - Load the diversions file /*{{{*/
292 // ---------------------------------------------------------------------
293 /* Read the diversion file in from disk. This is usually invoked by
294 LoadChanges before performing an operation that uses the FLCache. */
295 bool debDpkgDB::ReadDiversions()
298 if (stat((AdminDir
+ "diversions").c_str(),&Stat
) != 0)
301 if (_error
->PendingError() == true)
304 FILE *Fd
= fopen((AdminDir
+ "diversions").c_str(),"r");
306 return _error
->Errno("fopen",_("Failed to open the diversions file %sdiversions"),AdminDir
.c_str());
308 FList
->BeginDiverLoad();
315 // Read the three lines in
316 if (fgets(From
,sizeof(From
),Fd
) == 0)
318 if (fgets(To
,sizeof(To
),Fd
) == 0 ||
319 fgets(Package
,sizeof(Package
),Fd
) == 0)
321 _error
->Error(_("The diversion file is corrupted"));
326 unsigned long Len
= strlen(From
);
327 if (Len
< 2 || From
[Len
-1] != '\n')
328 _error
->Error(_("Invalid line in the diversion file: %s"),From
);
332 if (Len
< 2 || To
[Len
-1] != '\n')
333 _error
->Error(_("Invalid line in the diversion file: %s"),To
);
336 Len
= strlen(Package
);
337 if (Len
< 2 || Package
[Len
-1] != '\n')
338 _error
->Error(_("Invalid line in the diversion file: %s"),Package
);
342 // Make sure the lines were parsed OK
343 if (_error
->PendingError() == true)
347 if (strcmp(Package
,":") == 0)
349 pkgFLCache::PkgIterator FlPkg
= FList
->GetPkg(Package
,0,true);
350 if (FlPkg
.end() == true)
352 _error
->Error(_("Internal error getting a package name"));
356 // Install the diversion
357 if (FList
->AddDiversion(FlPkg
,From
,To
) == false)
359 _error
->Error(_("Internal error adding a diversion"));
363 if (_error
->PendingError() == false)
364 FList
->FinishDiverLoad();
366 DiverInode
= Stat
.st_ino
;
367 DiverTime
= Stat
.st_mtime
;
370 return !_error
->PendingError();
373 // DpkgDB::ReadFileList - Read the file listing /*{{{*/
374 // ---------------------------------------------------------------------
375 /* Read in the file listing. The file listing is created from three
376 sources, *.list, Conffile sections and the Diversion table. */
377 bool debDpkgDB::ReadyFileList(OpProgress
&Progress
)
380 return _error
->Error(_("The pkg cache must be initialized first"));
383 Progress
.OverallProgress(1,1,1,_("Reading file listing"));
387 // Create the cache and read in the file listing
388 FileMap
= new DynamicMMap(MMap::Public
);
389 FList
= new pkgFLCache(*FileMap
);
390 if (_error
->PendingError() == true ||
391 ReadFList(Progress
) == false ||
392 ReadConfFiles() == false ||
393 ReadDiversions() == false)
402 cout
<< "Node: " << FList
->HeaderP
->NodeCount
<< ',' << FList
->HeaderP
->UniqNodes
<< endl
;
403 cout
<< "Dir: " << FList
->HeaderP
->DirCount
<< endl
;
404 cout
<< "Package: " << FList
->HeaderP
->PackageCount
<< endl
;
405 cout
<< "HashSize: " << FList
->HeaderP
->HashSize
<< endl
;
406 cout
<< "Size: " << FileMap
->Size() << endl
;
412 // DpkgDB::ReadConfFiles - Read the conf file sections from the s-file /*{{{*/
413 // ---------------------------------------------------------------------
414 /* Reading the conf files is done by reparsing the status file. This is
415 actually rather fast so it is no big deal. */
416 bool debDpkgDB::ReadConfFiles()
418 FileFd
File(_config
->FindFile("Dir::State::status"),FileFd::ReadOnly
);
419 pkgTagFile
Tags(&File
);
420 if (_error
->PendingError() == true)
423 pkgTagSection Section
;
426 // Skip to the next section
427 unsigned long Offset
= Tags
.Offset();
428 if (Tags
.Step(Section
) == false)
434 if (Section
.Find("Conffiles",Start
,Stop
) == false)
437 const char *PkgStart
;
439 if (Section
.Find("Package",PkgStart
,PkgEnd
) == false)
440 return _error
->Error(_("Failed to find a Package: header, offset %lu"),Offset
);
442 // Snag a package record for it
443 pkgFLCache::PkgIterator FlPkg
= FList
->GetPkg(PkgStart
,PkgEnd
,true);
444 if (FlPkg
.end() == true)
445 return _error
->Error(_("Internal error getting a package name"));
447 // Parse the conf file lines
450 for (; isspace(*Start
) != 0 && Start
< Stop
; Start
++);
454 // Split it into words
455 const char *End
= Start
;
456 for (; isspace(*End
) == 0 && End
< Stop
; End
++);
457 const char *StartMd5
= End
;
458 for (; isspace(*StartMd5
) != 0 && StartMd5
< Stop
; StartMd5
++);
459 const char *EndMd5
= StartMd5
;
460 for (; isspace(*EndMd5
) == 0 && EndMd5
< Stop
; EndMd5
++);
461 if (StartMd5
== EndMd5
|| Start
== End
)
462 return _error
->Error(_("Bad ConfFile section in the status file. Offset %lu"),Offset
);
464 // Insert a new entry
465 unsigned char MD5
[16];
466 if (Hex2Num(string(StartMd5
,EndMd5
-StartMd5
),MD5
,16) == false)
467 return _error
->Error(_("Error parsing MD5. Offset %lu"),Offset
);
469 if (FList
->AddConfFile(Start
,End
,FlPkg
,MD5
) == false)
478 // DpkgDB::LoadChanges - Read in any changed state files /*{{{*/
479 // ---------------------------------------------------------------------
480 /* The only file in the dpkg system that can change while packages are
481 unpacking is the diversions file. */
482 bool debDpkgDB::LoadChanges()
485 if (stat((AdminDir
+ "diversions").c_str(),&Stat
) != 0)
487 if (DiverInode
== Stat
.st_ino
&& DiverTime
== Stat
.st_mtime
)
489 return ReadDiversions();