]>
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 /*{{{*/
17 #pragma implementation "apt-pkg/dpkgdb.h"
20 #include <apt-pkg/dpkgdb.h>
21 #include <apt-pkg/configuration.h>
22 #include <apt-pkg/error.h>
23 #include <apt-pkg/progress.h>
24 #include <apt-pkg/tagfile.h>
25 #include <apt-pkg/strutl.h>
29 #include <sys/types.h>
40 // EraseDir - Erase A Directory /*{{{*/
41 // ---------------------------------------------------------------------
42 /* This is necessary to create a new empty sub directory. The caller should
43 invoke mkdir after this with the proper permissions and check for
44 error. Maybe stick this in fileutils */
45 static bool EraseDir(const char *Dir
)
47 // First we try a simple RM
48 if (rmdir(Dir
) == 0 ||
52 // A file? Easy enough..
56 return _error
->Errno("unlink",_("Failed to remove %s"),Dir
);
61 if (errno
!= ENOTEMPTY
)
62 return _error
->Errno("rmdir",_("Failed to remove %s"),Dir
);
65 pid_t Pid
= ExecFork();
67 // Spawn the subprocess
70 execlp(_config
->Find("Dir::Bin::rm","/bin/rm").c_str(),
71 "rm","-rf","--",Dir
,(char *)NULL
);
74 return ExecWait(Pid
,_config
->Find("dir::bin::rm","/bin/rm").c_str());
77 // DpkgDB::debDpkgDB - Constructor /*{{{*/
78 // ---------------------------------------------------------------------
80 debDpkgDB::debDpkgDB() : CacheMap(0), FileMap(0)
82 AdminDir
= flNotFile(_config
->Find("Dir::State::status"));
87 // DpkgDB::~debDpkgDB - Destructor /*{{{*/
88 // ---------------------------------------------------------------------
90 debDpkgDB::~debDpkgDB()
103 // DpkgDB::InitMetaTmp - Get the temp dir for meta information /*{{{*/
104 // ---------------------------------------------------------------------
105 /* This creats+empties the meta temporary directory /var/lib/dpkg/tmp.ci
106 Only one package at a time can be using the returned meta directory. */
107 bool debDpkgDB::InitMetaTmp(string
&Dir
)
109 string Tmp
= AdminDir
+ "tmp.ci/";
110 if (EraseDir(Tmp
.c_str()) == false)
111 return _error
->Error(_("Unable to create %s"),Tmp
.c_str());
112 if (mkdir(Tmp
.c_str(),0755) != 0)
113 return _error
->Errno("mkdir",_("Unable to create %s"),Tmp
.c_str());
115 // Verify it is on the same filesystem as the main info directory
118 if (stat((AdminDir
+ "info").c_str(),&St
) != 0)
119 return _error
->Errno("stat",_("Failed to stat %sinfo"),AdminDir
.c_str());
121 if (stat(Tmp
.c_str(),&St
) != 0)
122 return _error
->Errno("stat",_("Failed to stat %s"),Tmp
.c_str());
123 if (Dev
!= St
.st_dev
)
124 return _error
->Error(_("The info and temp directories need to be on the same filesystem"));
131 // DpkgDB::ReadyPkgCache - Prepare the cache with the current status /*{{{*/
132 // ---------------------------------------------------------------------
133 /* This reads in the status file into an empty cache. This really needs
134 to be somehow unified with the high level APT notion of the Database
135 directory, but there is no clear way on how to do that yet. */
136 bool debDpkgDB::ReadyPkgCache(OpProgress
&Progress
)
140 Progress
.OverallProgress(1,1,1,_("Reading package lists"));
150 if (pkgMakeOnlyStatusCache(Progress
,&CacheMap
) == false)
152 Cache
->DropProgress();
157 // DpkgDB::ReadFList - Read the File Listings in /*{{{*/
158 // ---------------------------------------------------------------------
159 /* This reads the file listing in from the state directory. This is a
160 performance critical routine, as it needs to parse about 50k lines of
161 text spread over a hundred or more files. For an initial cold start
162 most of the time is spent in reading file inodes and so on, not
164 bool debDpkgDB::ReadFList(OpProgress
&Progress
)
166 // Count the number of packages we need to read information for
167 unsigned long Total
= 0;
168 pkgCache
&Cache
= this->Cache
->GetCache();
169 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
171 // Only not installed packages have no files.
172 if (I
->CurrentState
== pkgCache::State::NotInstalled
)
177 /* Switch into the admin dir, this prevents useless lookups for the
179 string Cwd
= SafeGetCWD();
180 if (chdir((AdminDir
+ "info/").c_str()) != 0)
181 return _error
->Errno("chdir",_("Failed to change to the admin dir %sinfo"),AdminDir
.c_str());
183 // Allocate a buffer. Anything larger than this buffer will be mmaped
184 unsigned long BufSize
= 32*1024;
185 char *Buffer
= new char[BufSize
];
187 // Begin Loading them
188 unsigned long Count
= 0;
190 for (pkgCache::PkgIterator I
= Cache
.PkgBegin(); I
.end() == false; I
++)
192 /* Only not installed packages have no files. ConfFile packages have
193 file lists but we don't want to read them in */
194 if (I
->CurrentState
== pkgCache::State::NotInstalled
||
195 I
->CurrentState
== pkgCache::State::ConfigFiles
)
198 // Fetch a package handle to associate with the file
199 pkgFLCache::PkgIterator FlPkg
= FList
->GetPkg(I
.Name(),0,true);
200 if (FlPkg
.end() == true)
202 _error
->Error(_("Internal error getting a package name"));
206 Progress
.OverallProgress(Count
,Total
,1,_("Reading file listing"));
208 // Open the list file
209 snprintf(Name
,sizeof(Name
),"%s.list",I
.Name());
210 int Fd
= open(Name
,O_RDONLY
);
212 /* Okay this is very strange and bad.. Best thing is to bail and
213 instruct the user to look into it. */
215 if (Fd
== -1 || fstat(Fd
,&Stat
) != 0)
217 _error
->Errno("open",_("Failed to open the list file '%sinfo/%s'. If you "
218 "cannot restore this file then make it empty "
219 "and immediately re-install the same version of the package!"),
220 AdminDir
.c_str(),Name
);
224 // Set File to be a memory buffer containing the whole file
226 if ((unsigned)Stat
.st_size
< BufSize
)
228 if (read(Fd
,Buffer
,Stat
.st_size
) != Stat
.st_size
)
230 _error
->Errno("read",_("Failed reading the list file %sinfo/%s"),
231 AdminDir
.c_str(),Name
);
240 File
= (char *)mmap(0,Stat
.st_size
,PROT_READ
,MAP_PRIVATE
,Fd
,0);
241 if (File
== (char *)(-1))
243 _error
->Errno("mmap",_("Failed reading the list file %sinfo/%s"),
244 AdminDir
.c_str(),Name
);
251 const char *Start
= File
;
252 const char *End
= File
;
253 const char *Finish
= File
+ Stat
.st_size
;
254 for (; End
< Finish
; End
++)
256 // Not an end of line
257 if (*End
!= '\n' && End
+ 1 < Finish
)
263 pkgFLCache::NodeIterator Node
= FList
->GetNode(Start
,End
,
264 FlPkg
.Offset(),true,false);
265 if (Node
.end() == true)
267 _error
->Error(_("Internal error getting a node"));
272 // Skip past the end of line
273 for (; *End
== '\n' && End
< Finish
; End
++);
278 if ((unsigned)Stat
.st_size
>= BufSize
)
279 munmap((caddr_t
)File
,Stat
.st_size
);
289 if (chdir(Cwd
.c_str()) != 0)
292 return !_error
->PendingError();
295 // DpkgDB::ReadDiversions - Load the diversions file /*{{{*/
296 // ---------------------------------------------------------------------
297 /* Read the diversion file in from disk. This is usually invoked by
298 LoadChanges before performing an operation that uses the FLCache. */
299 bool debDpkgDB::ReadDiversions()
302 if (stat((AdminDir
+ "diversions").c_str(),&Stat
) != 0)
305 if (_error
->PendingError() == true)
308 FILE *Fd
= fopen((AdminDir
+ "diversions").c_str(),"r");
310 return _error
->Errno("fopen",_("Failed to open the diversions file %sdiversions"),AdminDir
.c_str());
312 FList
->BeginDiverLoad();
319 // Read the three lines in
320 if (fgets(From
,sizeof(From
),Fd
) == 0)
322 if (fgets(To
,sizeof(To
),Fd
) == 0 ||
323 fgets(Package
,sizeof(Package
),Fd
) == 0)
325 _error
->Error(_("The diversion file is corrupted"));
330 unsigned long Len
= strlen(From
);
331 if (Len
< 2 || From
[Len
-1] != '\n')
332 _error
->Error(_("Invalid line in the diversion file: %s"),From
);
336 if (Len
< 2 || To
[Len
-1] != '\n')
337 _error
->Error(_("Invalid line in the diversion file: %s"),To
);
340 Len
= strlen(Package
);
341 if (Len
< 2 || Package
[Len
-1] != '\n')
342 _error
->Error(_("Invalid line in the diversion file: %s"),Package
);
346 // Make sure the lines were parsed OK
347 if (_error
->PendingError() == true)
351 if (strcmp(Package
,":") == 0)
353 pkgFLCache::PkgIterator FlPkg
= FList
->GetPkg(Package
,0,true);
354 if (FlPkg
.end() == true)
356 _error
->Error(_("Internal error getting a package name"));
360 // Install the diversion
361 if (FList
->AddDiversion(FlPkg
,From
,To
) == false)
363 _error
->Error(_("Internal error adding a diversion"));
367 if (_error
->PendingError() == false)
368 FList
->FinishDiverLoad();
370 DiverInode
= Stat
.st_ino
;
371 DiverTime
= Stat
.st_mtime
;
374 return !_error
->PendingError();
377 // DpkgDB::ReadFileList - Read the file listing /*{{{*/
378 // ---------------------------------------------------------------------
379 /* Read in the file listing. The file listing is created from three
380 sources, *.list, Conffile sections and the Diversion table. */
381 bool debDpkgDB::ReadyFileList(OpProgress
&Progress
)
384 return _error
->Error(_("The pkg cache must be initialized first"));
387 Progress
.OverallProgress(1,1,1,_("Reading file listing"));
391 // Create the cache and read in the file listing
392 FileMap
= new DynamicMMap(MMap::Public
);
393 FList
= new pkgFLCache(*FileMap
);
394 if (_error
->PendingError() == true ||
395 ReadFList(Progress
) == false ||
396 ReadConfFiles() == false ||
397 ReadDiversions() == false)
406 cout
<< "Node: " << FList
->HeaderP
->NodeCount
<< ',' << FList
->HeaderP
->UniqNodes
<< endl
;
407 cout
<< "Dir: " << FList
->HeaderP
->DirCount
<< endl
;
408 cout
<< "Package: " << FList
->HeaderP
->PackageCount
<< endl
;
409 cout
<< "HashSize: " << FList
->HeaderP
->HashSize
<< endl
;
410 cout
<< "Size: " << FileMap
->Size() << endl
;
416 // DpkgDB::ReadConfFiles - Read the conf file sections from the s-file /*{{{*/
417 // ---------------------------------------------------------------------
418 /* Reading the conf files is done by reparsing the status file. This is
419 actually rather fast so it is no big deal. */
420 bool debDpkgDB::ReadConfFiles()
422 FileFd
File(_config
->FindFile("Dir::State::status"),FileFd::ReadOnly
);
423 pkgTagFile
Tags(&File
);
424 if (_error
->PendingError() == true)
427 pkgTagSection Section
;
430 // Skip to the next section
431 unsigned long Offset
= Tags
.Offset();
432 if (Tags
.Step(Section
) == false)
438 if (Section
.Find("Conffiles",Start
,Stop
) == false)
441 const char *PkgStart
;
443 if (Section
.Find("Package",PkgStart
,PkgEnd
) == false)
444 return _error
->Error(_("Failed to find a Package: header, offset %lu"),Offset
);
446 // Snag a package record for it
447 pkgFLCache::PkgIterator FlPkg
= FList
->GetPkg(PkgStart
,PkgEnd
,true);
448 if (FlPkg
.end() == true)
449 return _error
->Error(_("Internal error getting a package name"));
451 // Parse the conf file lines
454 for (; isspace(*Start
) != 0 && Start
< Stop
; Start
++);
458 // Split it into words
459 const char *End
= Start
;
460 for (; isspace(*End
) == 0 && End
< Stop
; End
++);
461 const char *StartMd5
= End
;
462 for (; isspace(*StartMd5
) != 0 && StartMd5
< Stop
; StartMd5
++);
463 const char *EndMd5
= StartMd5
;
464 for (; isspace(*EndMd5
) == 0 && EndMd5
< Stop
; EndMd5
++);
465 if (StartMd5
== EndMd5
|| Start
== End
)
466 return _error
->Error(_("Bad ConfFile section in the status file. Offset %lu"),Offset
);
468 // Insert a new entry
469 unsigned char MD5
[16];
470 if (Hex2Num(string(StartMd5
,EndMd5
-StartMd5
),MD5
,16) == false)
471 return _error
->Error(_("Error parsing MD5. Offset %lu"),Offset
);
473 if (FList
->AddConfFile(Start
,End
,FlPkg
,MD5
) == false)
482 // DpkgDB::LoadChanges - Read in any changed state files /*{{{*/
483 // ---------------------------------------------------------------------
484 /* The only file in the dpkg system that can change while packages are
485 unpacking is the diversions file. */
486 bool debDpkgDB::LoadChanges()
489 if (stat((AdminDir
+ "diversions").c_str(),&Stat
) != 0)
491 if (DiverInode
== Stat
.st_ino
&& DiverTime
== Stat
.st_mtime
)
493 return ReadDiversions();