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