summaryrefslogtreecommitdiff
path: root/apt-inst/deb/dpkgdb.cc
blob: 85fec1ccd20cd6304d4ed1753faacb5a7e49295b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
// -*- mode: cpp; mode: fold -*-
// Description								/*{{{*/
// $Id: dpkgdb.cc,v 1.2 2001/02/20 07:03:17 jgg Exp $
/* ######################################################################

   DPKGv1 Database Implemenation
   
   This class provides parsers and other implementations for the DPKGv1
   database. It reads the diversion file, the list files and the status
   file to build both the list of currently installed files and the 
   currently installed package list.
   
   ##################################################################### */
									/*}}}*/
// Include Files							/*{{{*/
#ifdef __GNUG__
#pragma implementation "apt-pkg/dpkgdb.h"
#endif

#include <apt-pkg/dpkgdb.h>
#include <apt-pkg/configuration.h>
#include <apt-pkg/error.h>
#include <apt-pkg/progress.h>
#include <apt-pkg/tagfile.h>
#include <apt-pkg/strutl.h>

#include <stdio.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <unistd.h>
									/*}}}*/

// EraseDir - Erase A Directory						/*{{{*/
// ---------------------------------------------------------------------
/* This is necessary to create a new empty sub directory. The caller should
   invoke mkdir after this with the proper permissions and check for 
   error. Maybe stick this in fileutils */
static bool EraseDir(const char *Dir)
{
   // First we try a simple RM
   if (rmdir(Dir) == 0 ||
       errno == ENOENT)
      return true;
   
   // A file? Easy enough..
   if (errno == ENOTDIR)
   {
      if (unlink(Dir) != 0)
	 return _error->Errno("unlink","Failed to remove %s",Dir);
      return true;
   }
   
   // Should not happen
   if (errno != ENOTEMPTY)
      return _error->Errno("rmdir","Failed to remove %s",Dir);
   
   // Purge it using rm
   int Pid = ExecFork();

   // Spawn the subprocess
   if (Pid == 0)
   {
      execlp(_config->Find("Dir::Bin::rm","/bin/rm").c_str(),
	     "rm","-rf","--",Dir,0);
      _exit(100);
   }
   return ExecWait(Pid,_config->Find("dir::bin::rm","/bin/rm").c_str());
}
									/*}}}*/
// DpkgDB::debDpkgDB - Constructor					/*{{{*/
// ---------------------------------------------------------------------
/* */
debDpkgDB::debDpkgDB() : CacheMap(0), FileMap(0)
{
   AdminDir = flNotFile(_config->Find("Dir::State::status"));   
   DiverInode = 0;
   DiverTime = 0;
}
									/*}}}*/
// DpkgDB::~debDpkgDB - Destructor					/*{{{*/
// ---------------------------------------------------------------------
/* */
debDpkgDB::~debDpkgDB()
{
   delete Cache;
   Cache = 0;
   delete CacheMap;
   CacheMap = 0;
   
   delete FList;
   FList = 0;
   delete FileMap;
   FileMap = 0;
}
									/*}}}*/
// DpkgDB::InitMetaTmp - Get the temp dir for meta information		/*{{{*/
// ---------------------------------------------------------------------
/* This creats+empties the meta temporary directory /var/lib/dpkg/tmp.ci
   Only one package at a time can be using the returned meta directory. */
bool debDpkgDB::InitMetaTmp(string &Dir)
{
   string Tmp = AdminDir + "tmp.ci/";
   if (EraseDir(Tmp.c_str()) == false)
      return _error->Error("Unable to create %s",Tmp.c_str());
   if (mkdir(Tmp.c_str(),0755) != 0)
      return _error->Errno("mkdir","Unable to create %s",Tmp.c_str());
   
   // Verify it is on the same filesystem as the main info directory
   dev_t Dev;
   struct stat St;
   if (stat((AdminDir + "info").c_str(),&St) != 0)
      return _error->Errno("stat","Failed to stat %sinfo",AdminDir.c_str());
   Dev = St.st_dev;
   if (stat(Tmp.c_str(),&St) != 0)
      return _error->Errno("stat","Failed to stat %s",Tmp.c_str());
   if (Dev != St.st_dev)
      return _error->Error("The info and temp directories need to be on the same filesystem");
   
   // Done
   Dir = Tmp;
   return true;
}
									/*}}}*/
// DpkgDB::ReadyPkgCache - Prepare the cache with the current status	/*{{{*/
// ---------------------------------------------------------------------
/* This reads in the status file into an empty cache. This really needs 
   to be somehow unified with the high level APT notion of the Database
   directory, but there is no clear way on how to do that yet. */
bool debDpkgDB::ReadyPkgCache(OpProgress &Progress)
{
   if (Cache != 0)
   {  
      Progress.OverallProgress(1,1,1,"Reading Package Lists");      
      return true;
   }
   
   if (CacheMap != 0)
   {
      delete CacheMap;
      CacheMap = 0;
   }
   
   if (pkgMakeOnlyStatusCache(Progress,&CacheMap) == false)
      return false;
   Cache->DropProgress();
   
   return true;
}
									/*}}}*/
// DpkgDB::ReadFList - Read the File Listings in 			/*{{{*/
// ---------------------------------------------------------------------
/* This reads the file listing in from the state directory. This is a 
   performance critical routine, as it needs to parse about 50k lines of
   text spread over a hundred or more files. For an initial cold start
   most of the time is spent in reading file inodes and so on, not 
   actually parsing. */
bool debDpkgDB::ReadFList(OpProgress &Progress)
{
   // Count the number of packages we need to read information for
   unsigned long Total = 0;
   pkgCache &Cache = this->Cache->GetCache();
   for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
   {
      // Only not installed packages have no files.
      if (I->CurrentState == pkgCache::State::NotInstalled)
	 continue;
      Total++;
   }

   /* Switch into the admin dir, this prevents useless lookups for the 
      path components */
   string Cwd = SafeGetCWD();
   if (chdir((AdminDir + "info/").c_str()) != 0)
      return _error->Errno("chdir","Failed to change to the admin dir %sinfo",AdminDir.c_str());
   
   // Allocate a buffer. Anything larger than this buffer will be mmaped
   unsigned long BufSize = 32*1024;
   char *Buffer = new char[BufSize];

   // Begin Loading them
   unsigned long Count = 0;
   char Name[300];
   for (pkgCache::PkgIterator I = Cache.PkgBegin(); I.end() == false; I++)
   {
      /* Only not installed packages have no files. ConfFile packages have
         file lists but we don't want to read them in */
      if (I->CurrentState == pkgCache::State::NotInstalled ||
	  I->CurrentState == pkgCache::State::ConfigFiles)
	 continue;

      // Fetch a package handle to associate with the file
      pkgFLCache::PkgIterator FlPkg = FList->GetPkg(I.Name(),0,true);
      if (FlPkg.end() == true)
      {
	 _error->Error("Internal Error getting a Package Name");
	 break;
      }
      
      Progress.OverallProgress(Count,Total,1,"Reading File Listing");
     
      // Open the list file
      snprintf(Name,sizeof(Name),"%s.list",I.Name());
      int Fd = open(Name,O_RDONLY);
      
      /* Okay this is very strange and bad.. Best thing is to bail and
         instruct the user to look into it. */
      struct stat Stat;
      if (Fd == -1 || fstat(Fd,&Stat) != 0)
      {
	 _error->Errno("open","Failed to open the list file '%sinfo/%s'. If you "
		       "cannot restore this file then make it empty "
		       "and immediately re-install the same version of the package!",
		       AdminDir.c_str(),Name);
	 break;
      }
      
      // Set File to be a memory buffer containing the whole file
      char *File;
      if ((unsigned)Stat.st_size < BufSize)
      {
	 if (read(Fd,Buffer,Stat.st_size) != Stat.st_size)
	 {
	    _error->Errno("read","Failed reading the list file %sinfo/%s",
			  AdminDir.c_str(),Name);
	    close(Fd);
	    break;
	 }
	 File = Buffer;
      }
      else
      {
	 // Use mmap
	 File = (char *)mmap(0,Stat.st_size,PROT_READ,MAP_PRIVATE,Fd,0);
	 if (File == (char *)(-1))
	 {
	    _error->Errno("mmap","Failed reading the list file %sinfo/%s",
			  AdminDir.c_str(),Name);
	    close(Fd);
	    break;
	 }	    
      }
      
      // Parse it
      const char *Start = File;
      const char *End = File;
      const char *Finish = File + Stat.st_size;
      for (; End < Finish; End++)
      {
	 // Not an end of line
	 if (*End != '\n' && End + 1 < Finish)
	    continue;

	 // Skip blank lines
	 if (End - Start > 1)
	 {
	    pkgFLCache::NodeIterator Node = FList->GetNode(Start,End,
 					      FlPkg.Offset(),true,false);
	    if (Node.end() == true)
	    {
	       _error->Error("Internal Error getting a Node");
	       break;
	    }
	 }
	 
	 // Skip past the end of line
	 for (; *End == '\n' && End < Finish; End++);
	 Start = End;
      }      
      
      close(Fd);
      if ((unsigned)Stat.st_size >= BufSize)
	 munmap((caddr_t)File,Stat.st_size);
      
      // Failed
      if (End < Finish)
	 break;
      
      Count++;
   }

   delete [] Buffer;
   if (chdir(Cwd.c_str()) != 0)
      chdir("/");
   
   return !_error->PendingError();
}
									/*}}}*/
// DpkgDB::ReadDiversions - Load the diversions file			/*{{{*/
// ---------------------------------------------------------------------
/* Read the diversion file in from disk. This is usually invoked by 
   LoadChanges before performing an operation that uses the FLCache. */
bool debDpkgDB::ReadDiversions()
{
   struct stat Stat;
   if (stat((AdminDir + "diversions").c_str(),&Stat) != 0)
      return true;
   
   if (_error->PendingError() == true)
      return false;
   
   FILE *Fd = fopen((AdminDir + "diversions").c_str(),"r");
   if (Fd == 0)
      return _error->Errno("fopen","Failed to open the diversions file %sdiversions",AdminDir.c_str());
	
   FList->BeginDiverLoad();
   while (1)
   {
      char From[300];
      char To[300];
      char Package[100];
   
      // Read the three lines in
      if (fgets(From,sizeof(From),Fd) == 0)
	 break;
      if (fgets(To,sizeof(To),Fd) == 0 ||
	  fgets(Package,sizeof(Package),Fd) == 0)
      {
	 _error->Error("The diversion file is corrupted");
	 break;
      }
      
      // Strip the \ns
      unsigned long Len = strlen(From);
      if (Len < 2 || From[Len-1] != '\n')
	 _error->Error("Invalid line in the diversion file: %s",From);
      else
	 From[Len-1] = 0;
      Len = strlen(To);
      if (Len < 2 || To[Len-1] != '\n')
	 _error->Error("Invalid line in the diversion file: %s",To);
      else
	 To[Len-1] = 0;     
      Len = strlen(Package);
      if (Len < 2 || Package[Len-1] != '\n')
	 _error->Error("Invalid line in the diversion file: %s",Package);
      else
	 Package[Len-1] = 0;
      
      // Make sure the lines were parsed OK
      if (_error->PendingError() == true)
	 break;
      
      // Fetch a package
      if (strcmp(Package,":") == 0)
	 Package[0] = 0;
      pkgFLCache::PkgIterator FlPkg = FList->GetPkg(Package,0,true);
      if (FlPkg.end() == true)
      {
	 _error->Error("Internal Error getting a Package Name");
	 break;
      }
      
      // Install the diversion
      if (FList->AddDiversion(FlPkg,From,To) == false)
      {
	 _error->Error("Internal Error adding a diversion");
	 break;
      }
   }
   if (_error->PendingError() == false)
      FList->FinishDiverLoad();
   
   DiverInode = Stat.st_ino;
   DiverTime = Stat.st_mtime;
   
   fclose(Fd);
   return !_error->PendingError();
}
									/*}}}*/
// DpkgDB::ReadFileList - Read the file listing				/*{{{*/
// ---------------------------------------------------------------------
/* Read in the file listing. The file listing is created from three
   sources, *.list, Conffile sections and the Diversion table. */
bool debDpkgDB::ReadyFileList(OpProgress &Progress)
{
   if (Cache == 0)
      return _error->Error("The pkg cache must be initialize first");
   if (FList != 0)
   {
      Progress.OverallProgress(1,1,1,"Reading File List");
      return true;
   }
   
   // Create the cache and read in the file listing
   FileMap = new DynamicMMap(MMap::Public);
   FList = new pkgFLCache(*FileMap);
   if (_error->PendingError() == true || 
       ReadFList(Progress) == false ||
       ReadConfFiles() == false || 
       ReadDiversions() == false)
   {
      delete FList;
      delete FileMap;
      FileMap = 0;
      FList = 0;
      return false;
   }
      
   cout << "Node: " << FList->HeaderP->NodeCount << ',' << FList->HeaderP->UniqNodes << endl;
   cout << "Dir: " << FList->HeaderP->DirCount << endl;
   cout << "Package: " << FList->HeaderP->PackageCount << endl;
   cout << "HashSize: " << FList->HeaderP->HashSize << endl;
   cout << "Size: " << FileMap->Size() << endl;
   cout << endl;

   return true;
}
									/*}}}*/
// DpkgDB::ReadConfFiles - Read the conf file sections from the s-file	/*{{{*/
// ---------------------------------------------------------------------
/* Reading the conf files is done by reparsing the status file. This is
   actually rather fast so it is no big deal. */
bool debDpkgDB::ReadConfFiles()
{
   FileFd File(_config->FindFile("Dir::State::status"),FileFd::ReadOnly);
   pkgTagFile Tags(&File);
   if (_error->PendingError() == true)
      return false;
   
   pkgTagSection Section;   
   while (1)
   {
      // Skip to the next section
      unsigned long Offset = Tags.Offset();
      if (Tags.Step(Section) == false)
	 break;
	 
      // Parse the line
      const char *Start;
      const char *Stop;
      if (Section.Find("Conffiles",Start,Stop) == false)
	 continue;

      const char *PkgStart;
      const char *PkgEnd;
      if (Section.Find("Package",PkgStart,PkgEnd) == false)
	 return _error->Error("Failed to find a Package: Header, offset %lu",Offset);

      // Snag a package record for it
      pkgFLCache::PkgIterator FlPkg = FList->GetPkg(PkgStart,PkgEnd,true);
      if (FlPkg.end() == true)
	 return _error->Error("Internal Error getting a Package Name");

      // Parse the conf file lines
      while (1)
      {
	 for (; isspace(*Start) != 0 && Start < Stop; Start++);
	 if (Start == Stop)
	    break;

	 // Split it into words
	 const char *End = Start;
	 for (; isspace(*End) == 0 && End < Stop; End++);
	 const char *StartMd5 = End;
	 for (; isspace(*StartMd5) != 0 && StartMd5 < Stop; StartMd5++);
	 const char *EndMd5 = StartMd5;
	 for (; isspace(*EndMd5) == 0 && EndMd5 < Stop; EndMd5++);
	 if (StartMd5 == EndMd5 || Start == End)
	    return _error->Error("Bad ConfFile section in the status file. Offset %lu",Offset);
	 	 
	 // Insert a new entry
	 unsigned char MD5[16];
	 if (Hex2Num(StartMd5,EndMd5,MD5,16) == false)
	    return _error->Error("Error parsing MD5. Offset %lu",Offset);
	 
	 if (FList->AddConfFile(Start,End,FlPkg,MD5) == false)
	    return false;
	 Start = EndMd5;
      }      
   }   
   
   return true;
}
									/*}}}*/
// DpkgDB::LoadChanges - Read in any changed state files		/*{{{*/
// ---------------------------------------------------------------------
/* The only file in the dpkg system that can change while packages are
   unpacking is the diversions file. */
bool debDpkgDB::LoadChanges()
{
   struct stat Stat;
   if (stat((AdminDir + "diversions").c_str(),&Stat) != 0)
      return true;
   if (DiverInode == Stat.st_ino && DiverTime == Stat.st_mtime)
      return true;
   return ReadDiversions();
}
									/*}}}*/