]> git.saurik.com Git - apt.git/blob - apt-pkg/deb/debsystem.cc
Newer sub/guess scripts
[apt.git] / apt-pkg / deb / debsystem.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: debsystem.cc,v 1.2 2001/02/20 07:03:17 jgg Exp $
4 /* ######################################################################
5
6 System - Abstraction for running on different systems.
7
8 Basic general structure..
9
10 ##################################################################### */
11 /*}}}*/
12 // Include Files /*{{{*/
13 #ifdef __GNUG__
14 #pragma implementation "apt-pkg/debsystem.h"
15 #endif
16
17 #include <apt-pkg/debsystem.h>
18 #include <apt-pkg/debversion.h>
19 #include <apt-pkg/debindexfile.h>
20 #include <apt-pkg/dpkgpm.h>
21 #include <apt-pkg/configuration.h>
22 #include <apt-pkg/error.h>
23 #include <apt-pkg/fileutl.h>
24
25 #include <sys/types.h>
26 #include <unistd.h>
27 #include <dirent.h>
28 #include <errno.h>
29 /*}}}*/
30
31 debSystem debSys;
32
33 // System::debSystem - Constructor /*{{{*/
34 // ---------------------------------------------------------------------
35 /* */
36 debSystem::debSystem()
37 {
38 LockFD = -1;
39 LockCount = 0;
40
41 Label = "Debian dpkg interface";
42 VS = &debVS;
43 }
44 /*}}}*/
45 // System::Lock - Get the lock /*{{{*/
46 // ---------------------------------------------------------------------
47 /* This mirrors the operations dpkg does when it starts up. Note the
48 checking of the updates directory. */
49 bool debSystem::Lock()
50 {
51 // Disable file locking
52 if (_config->FindB("Debug::NoLocking",false) == true || LockCount > 1)
53 {
54 LockCount++;
55 return true;
56 }
57
58 // Create the lockfile
59 string AdminDir = flNotFile(_config->Find("Dir::State::status"));
60 LockFD = GetLock(AdminDir + "lock");
61 if (LockFD == -1)
62 {
63 if (errno == EACCES || errno == EAGAIN)
64 return _error->Error("Unable to lock the administration directory (%s), "
65 "is another process using it?",AdminDir.c_str());
66 else
67 return _error->Error("Unable to lock the administration directory (%s), "
68 "are you root?",AdminDir.c_str());
69 }
70
71 // See if we need to abort with a dirty journal
72 if (CheckUpdates() == true)
73 {
74 close(LockFD);
75 LockFD = -1;
76 return _error->Error("dpkg was interrupted, you must manually "
77 "run 'dpkg --configure -a' to correct the problem. ");
78 }
79
80 LockCount++;
81
82 return true;
83 }
84 /*}}}*/
85 // System::UnLock - Drop a lock /*{{{*/
86 // ---------------------------------------------------------------------
87 /* */
88 bool debSystem::UnLock(bool NoErrors)
89 {
90 if (LockCount == 0 && NoErrors == true)
91 return false;
92
93 if (LockCount < 1)
94 return _error->Error("Not locked");
95 if (--LockCount == 0)
96 {
97 close(LockFD);
98 LockCount = 0;
99 }
100
101 return true;
102 }
103 /*}}}*/
104 // System::CheckUpdates - Check if the updates dir is dirty /*{{{*/
105 // ---------------------------------------------------------------------
106 /* This does a check of the updates directory (dpkg journal) to see if it has
107 any entries in it. */
108 bool debSystem::CheckUpdates()
109 {
110 // Check for updates.. (dirty)
111 string File = flNotFile(_config->Find("Dir::State::status")) + "updates/";
112 DIR *DirP = opendir(File.c_str());
113 if (DirP == 0)
114 return false;
115
116 /* We ignore any files that are not all digits, this skips .,.. and
117 some tmp files dpkg will leave behind.. */
118 bool Damaged = false;
119 for (struct dirent *Ent = readdir(DirP); Ent != 0; Ent = readdir(DirP))
120 {
121 Damaged = true;
122 for (unsigned int I = 0; Ent->d_name[I] != 0; I++)
123 {
124 // Check if its not a digit..
125 if (isdigit(Ent->d_name[I]) == 0)
126 {
127 Damaged = false;
128 break;
129 }
130 }
131 if (Damaged == true)
132 break;
133 }
134 closedir(DirP);
135
136 return Damaged;
137 }
138 /*}}}*/
139 // System::CreatePM - Create the underlying package manager /*{{{*/
140 // ---------------------------------------------------------------------
141 /* */
142 pkgPackageManager *debSystem::CreatePM(pkgDepCache *Cache) const
143 {
144 return new pkgDPkgPM(Cache);
145 }
146 /*}}}*/
147 // System::Initialize - Setup the configuration space.. /*{{{*/
148 // ---------------------------------------------------------------------
149 /* These are the Debian specific configuration variables.. */
150 bool debSystem::Initialize(Configuration &Cnf)
151 {
152 /* These really should be jammed into a generic 'Local Database' engine
153 which is yet to be determined. The functions in pkgcachegen should
154 be the only users of these */
155 Cnf.CndSet("Dir::State::userstatus","status.user"); // Defunct
156 Cnf.CndSet("Dir::State::status","/var/lib/dpkg/status");
157 Cnf.CndSet("Dir::Bin::dpkg","/usr/bin/dpkg");
158
159 return true;
160 }
161 /*}}}*/
162 // System::ArchiveSupported - Is a file format supported /*{{{*/
163 // ---------------------------------------------------------------------
164 /* The standard name for a deb is 'deb'.. There are no seperate versions
165 of .deb to worry about.. */
166 bool debSystem::ArchiveSupported(const char *Type)
167 {
168 if (strcmp(Type,"deb") == 0)
169 return true;
170 return false;
171 }
172 /*}}}*/
173 // System::Score - Determine how 'Debiany' this sys is.. /*{{{*/
174 // ---------------------------------------------------------------------
175 /* We check some files that are sure tell signs of this being a Debian
176 System.. */
177 signed debSystem::Score(Configuration const &Cnf)
178 {
179 signed Score = 0;
180 if (FileExists(Cnf.FindFile("Dir::State::status","/var/lib/dpkg/status")) == true)
181 Score += 10;
182 if (FileExists(Cnf.FindFile("Dir::Bin::dpkg","/usr/bin/dpkg")) == true)
183 Score += 10;
184 if (FileExists("/etc/debian_version") == true)
185 Score += 10;
186 return Score;
187 }
188 /*}}}*/
189 // System::AddStatusFiles - Register the status files /*{{{*/
190 // ---------------------------------------------------------------------
191 /* */
192 bool debSystem::AddStatusFiles(vector<pkgIndexFile *> &List)
193 {
194 List.push_back(new debStatusIndex(_config->FindFile("Dir::State::status")));
195 return true;
196 }
197 /*}}}*/