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