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