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