Support for memory-only caching
[ntk/apt.git] / apt-pkg / deb / dpkginit.cc
CommitLineData
d38b7b3d
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
2d11135a 3// $Id: dpkginit.cc,v 1.2 1999/04/18 06:36:36 jgg Exp $
d38b7b3d
AL
4/* ######################################################################
5
6 DPKG init - Initialize the dpkg stuff
7
8 ##################################################################### */
9 /*}}}*/
10// Includes /*{{{*/
11#ifdef __GNUG__
12#pragma implementation "apt-pkg/dpkginit.h"
13#endif
14#include <apt-pkg/dpkginit.h>
15#include <apt-pkg/error.h>
16#include <apt-pkg/configuration.h>
17#include <apt-pkg/fileutl.h>
18
19#include <sys/types.h>
20#include <unistd.h>
21#include <dirent.h>
22 /*}}}*/
23
24// DpkgLock::pkgDpkgLock - Constructor /*{{{*/
25// ---------------------------------------------------------------------
26/* */
27pkgDpkgLock::pkgDpkgLock()
28{
29 LockFD = -1;
30 GetLock();
31}
32 /*}}}*/
33// DpkgLock::~pkgDpkgLock - Destructor /*{{{*/
34// ---------------------------------------------------------------------
35/* */
36pkgDpkgLock::~pkgDpkgLock()
37{
38 Close();
39}
40 /*}}}*/
41// DpkgLock::GetLock - Get the lock /*{{{*/
42// ---------------------------------------------------------------------
43/* This mirrors the operations dpkg does when it starts up. Note the
44 checking of the updates directory. */
45bool pkgDpkgLock::GetLock()
46{
47 // Disable file locking
48 if (_config->FindB("Debug::NoLocking",false) == true)
49 return true;
50
51 Close();
52
53 // Create the lockfile
54 string AdminDir = flNotFile(_config->Find("Dir::State::status"));
55 LockFD = ::GetLock(AdminDir + "lock");
56 if (LockFD == -1)
2d11135a
AL
57 return _error->Error("Unable to lock the administration directory, "
58 "are you root?");
d38b7b3d
AL
59
60 // Check for updates.. (dirty)
61 string File = AdminDir + "updates/";
62 DIR *DirP = opendir(File.c_str());
63 if (DirP != 0)
64 {
65 /* We ignore any files that are not all digits, this skips .,.. and
66 some tmp files dpkg will leave behind.. */
67 bool Damaged = false;
68 for (struct dirent *Ent = readdir(DirP); Ent != 0; Ent = readdir(DirP))
69 {
70 Damaged = true;
71 for (unsigned int I = 0; Ent->d_name[I] != 0; I++)
72 {
73 // Check if its not a digit..
74 if (isdigit(Ent->d_name[I]) == 0)
75 {
76 Damaged = false;
77 break;
78 }
79 }
80 if (Damaged == true)
81 break;
82 }
83 closedir(DirP);
84
85 // Woops, we have to run dpkg to rewrite the status file
86 if (Damaged == true)
87 {
88 Close();
89 return _error->Error("dpkg was interrupted, you must manually "
90 "run 'dpkg --configure -a' to correct the problem. ");
91 }
92 }
93
94 return true;
95}
96 /*}}}*/
97// DpkgLock::Close - Close the lock /*{{{*/
98// ---------------------------------------------------------------------
99/* */
100void pkgDpkgLock::Close()
101{
102 close(LockFD);
103 LockFD = -1;
104}
105 /*}}}*/