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