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