summaryrefslogtreecommitdiff
path: root/apt-pkg/deb/dpkginit.cc
blob: 5327de0e1d439451719f9606b014149df0728068 (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
// -*- mode: cpp; mode: fold -*-
// Description								/*{{{*/
// $Id: dpkginit.cc,v 1.3 1999/07/26 17:46:08 jgg Exp $
/* ######################################################################

   DPKG init - Initialize the dpkg stuff

   This class provides the locking mechanism used by dpkg for its 
   database area. It does the proper consistency checks and acquires the
   correct kind of lock.
   
   ##################################################################### */
									/*}}}*/
// Includes								/*{{{*/
#ifdef __GNUG__
#pragma implementation "apt-pkg/dpkginit.h"
#endif
#include <apt-pkg/dpkginit.h>
#include <apt-pkg/error.h>
#include <apt-pkg/configuration.h>
#include <apt-pkg/fileutl.h>

#include <sys/types.h>
#include <unistd.h>
#include <dirent.h>
									/*}}}*/

// DpkgLock::pkgDpkgLock - Constructor					/*{{{*/
// ---------------------------------------------------------------------
/* */
pkgDpkgLock::pkgDpkgLock(bool WithUpdates)
{
   LockFD = -1;
   GetLock(WithUpdates);
}
									/*}}}*/
// DpkgLock::~pkgDpkgLock - Destructor					/*{{{*/
// ---------------------------------------------------------------------
/* */
pkgDpkgLock::~pkgDpkgLock()
{
   Close();
}
									/*}}}*/
// DpkgLock::GetLock - Get the lock					/*{{{*/
// ---------------------------------------------------------------------
/* This mirrors the operations dpkg does when it starts up. Note the
   checking of the updates directory. */
bool pkgDpkgLock::GetLock(bool WithUpdates)
{
   // Disable file locking
   if (_config->FindB("Debug::NoLocking",false) == true)
      return true;
   
   Close();
   
   // Create the lockfile
   string AdminDir = flNotFile(_config->Find("Dir::State::status"));
   LockFD = ::GetLock(AdminDir + "lock");
   if (LockFD == -1)
      return _error->Error("Unable to lock the administration directory, "
			   "are you root?");

   // See if we need to abort with a dirty journal
   if (WithUpdates == true && CheckUpdates() == false)
   {
      Close();
      return _error->Error("dpkg was interrupted, you must manually "
			   "run 'dpkg --configure -a' to correct the problem. ");
   }
      
   return true;
}
									/*}}}*/
// DpkgLock::Close - Close the lock					/*{{{*/
// ---------------------------------------------------------------------
/* */
void pkgDpkgLock::Close()
{
   close(LockFD);
   LockFD = -1;
}
									/*}}}*/
// DpkgLock::CheckUpdates - Check if the updates dir is dirty		/*{{{*/
// ---------------------------------------------------------------------
/* This does a check of the updates directory to see if it has any entries
   in it. */
bool pkgDpkgLock::CheckUpdates()
{
   // Check for updates.. (dirty)
   string File = flNotFile(_config->Find("Dir::State::status")) + "updates/";
   DIR *DirP = opendir(File.c_str());
   if (DirP == 0)
      return true;
   
   /* We ignore any files that are not all digits, this skips .,.. and 
      some tmp files dpkg will leave behind.. */
   bool Damaged = false;
   for (struct dirent *Ent = readdir(DirP); Ent != 0; Ent = readdir(DirP))
   {
      Damaged = true;
      for (unsigned int I = 0; Ent->d_name[I] != 0; I++)
      {
	 // Check if its not a digit..
	 if (isdigit(Ent->d_name[I]) == 0)
	 {
	    Damaged = false;
	    break;
	 }
      }
      if (Damaged == true)
	 break;
   }
   closedir(DirP);

   return Damaged;
}
									/*}}}*/