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