]> git.saurik.com Git - apt.git/blob - methods/cdrom.cc
apt-pkg/cdrom.cc: make cdrom.Mounted property reliable
[apt.git] / methods / cdrom.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: cdrom.cc,v 1.20.2.1 2004/01/16 18:58:50 mdz Exp $
4 /* ######################################################################
5
6 CDROM URI method for APT
7
8 ##################################################################### */
9 /*}}}*/
10 // Include Files /*{{{*/
11 #include <apt-pkg/acquire-method.h>
12 #include <apt-pkg/cdromutl.h>
13 #include <apt-pkg/error.h>
14 #include <apt-pkg/configuration.h>
15 #include <apt-pkg/fileutl.h>
16 #include <apt-pkg/hashes.h>
17
18 #include <sys/stat.h>
19 #include <unistd.h>
20 #include <dlfcn.h>
21
22 #include <iostream>
23 #include <apti18n.h>
24 /*}}}*/
25
26 using namespace std;
27
28 class CDROMMethod : public pkgAcqMethod
29 {
30 bool DatabaseLoaded;
31 ::Configuration Database;
32 string CurrentID;
33 string CDROM;
34 bool MountedByApt;
35
36 virtual bool Fetch(FetchItem *Itm);
37 string GetID(string Name);
38 virtual void Exit();
39
40 public:
41
42 CDROMMethod();
43 };
44
45 // CDROMMethod::CDROMethod - Constructor /*{{{*/
46 // ---------------------------------------------------------------------
47 /* */
48 CDROMMethod::CDROMMethod() : pkgAcqMethod("1.0",SingleInstance | LocalOnly |
49 SendConfig | NeedsCleanup |
50 Removable),
51 DatabaseLoaded(false),
52 MountedByApt(false)
53 {
54
55
56 };
57 /*}}}*/
58 // CDROMMethod::Exit - Unmount the disc if necessary /*{{{*/
59 // ---------------------------------------------------------------------
60 /* */
61 void CDROMMethod::Exit()
62 {
63 if (MountedByApt == true)
64 UnmountCdrom(CDROM);
65 }
66 /*}}}*/
67 // CDROMMethod::GetID - Search the database for a matching string /*{{{*/
68 // ---------------------------------------------------------------------
69 /* */
70 string CDROMMethod::GetID(string Name)
71 {
72 // Search for an ID
73 const Configuration::Item *Top = Database.Tree("CD");
74 if (Top != 0)
75 Top = Top->Child;
76
77 for (; Top != 0;)
78 {
79 if (Top->Value == Name)
80 return Top->Tag;
81
82 Top = Top->Next;
83 }
84 return string();
85 }
86 /*}}}*/
87 // CDROMMethod::Fetch - Fetch a file /*{{{*/
88 // ---------------------------------------------------------------------
89 /* */
90 bool CDROMMethod::Fetch(FetchItem *Itm)
91 {
92 URI Get = Itm->Uri;
93 string File = Get.Path;
94 FetchResult Res;
95
96 bool Debug = _config->FindB("Debug::Acquire::cdrom",false);
97
98 /* All IMS queries are returned as a hit, CDROMs are readonly so
99 time stamps never change */
100 if (Itm->LastModified != 0)
101 {
102 Res.LastModified = Itm->LastModified;
103 Res.IMSHit = true;
104 Res.Filename = Itm->DestFile;
105 URIDone(Res);
106 return true;
107 }
108
109 // Load the database
110 if (DatabaseLoaded == false)
111 {
112 // Read the database
113 string DFile = _config->FindFile("Dir::State::cdroms");
114 if (FileExists(DFile) == true)
115 {
116 if (ReadConfigFile(Database,DFile) == false)
117 return _error->Error(_("Unable to read the cdrom database %s"),
118 DFile.c_str());
119 }
120 DatabaseLoaded = true;
121 }
122
123 // All non IMS queries for package files fail.
124 if (Itm->IndexFile == true || GetID(Get.Host).empty() == true)
125 {
126 Fail(_("Please use apt-cdrom to make this CD-ROM recognized by APT."
127 " apt-get update cannot be used to add new CD-ROMs"));
128 return true;
129 }
130
131 // We already have a CD inserted, but it is the wrong one
132 if (CurrentID.empty() == false && Database.Find("CD::" + CurrentID) != Get.Host)
133 {
134 Fail(_("Wrong CD-ROM"),true);
135 return true;
136 }
137
138 CDROM = _config->FindDir("Acquire::cdrom::mount","/cdrom/");
139 if (CDROM[0] == '.')
140 CDROM= SafeGetCWD() + '/' + CDROM;
141 string NewID;
142 while (CurrentID.empty() == true)
143 {
144 bool Hit = false;
145 if(!IsMounted(CDROM))
146 MountedByApt = MountCdrom(CDROM);
147 for (unsigned int Version = 2; Version != 0; Version--)
148 {
149 if (IdentCdrom(CDROM,NewID,Version) == false)
150 return false;
151
152 if (Debug == true)
153 clog << "ID " << Version << " " << NewID << endl;
154
155 // A hit
156 if (Database.Find("CD::" + NewID) == Get.Host)
157 {
158 Hit = true;
159 break;
160 }
161 }
162
163 if (Hit == true)
164 break;
165
166 // I suppose this should prompt somehow?
167 if (_config->FindB("APT::CDROM::NoMount",false) == false &&
168 UnmountCdrom(CDROM) == false)
169 return _error->Error(_("Unable to unmount the CD-ROM in %s, it may still be in use."),
170 CDROM.c_str());
171 if (MediaFail(Get.Host,CDROM) == false)
172 {
173 CurrentID = "FAIL";
174 return _error->Error(_("Disk not found."));
175 }
176 }
177
178 // Found a CD
179 Res.Filename = CDROM + File;
180 struct stat Buf;
181 if (stat(Res.Filename.c_str(),&Buf) != 0)
182 return _error->Error(_("File not found"));
183
184 if (NewID.empty() == false)
185 CurrentID = NewID;
186 Res.LastModified = Buf.st_mtime;
187 Res.Size = Buf.st_size;
188
189 Hashes Hash;
190 FileFd Fd(Res.Filename, FileFd::ReadOnly);
191 Hash.AddFD(Fd.Fd(), Fd.Size());
192 Res.TakeHashes(Hash);
193
194 URIDone(Res);
195 return true;
196 }
197 /*}}}*/
198
199 int main()
200 {
201 setlocale(LC_ALL, "");
202
203 CDROMMethod Mth;
204 return Mth.Run();
205 }