]> git.saurik.com Git - apt.git/blob - methods/cdrom.cc
CDROMs are unmounted
[apt.git] / methods / cdrom.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: cdrom.cc,v 1.16 1999/10/18 00:37:36 jgg Exp $
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
21 class CDROMMethod : public pkgAcqMethod
22 {
23 bool DatabaseLoaded;
24 ::Configuration Database;
25 string CurrentID;
26 string CDROM;
27 bool Mounted;
28
29 virtual bool Fetch(FetchItem *Itm);
30 string GetID(string Name);
31 virtual void Exit();
32
33 public:
34
35 CDROMMethod();
36 };
37
38 // CDROMMethod::CDROMethod - Constructor /*{{{*/
39 // ---------------------------------------------------------------------
40 /* */
41 CDROMMethod::CDROMMethod() : pkgAcqMethod("1.0",SingleInstance | LocalOnly |
42 SendConfig | NeedsCleanup),
43 DatabaseLoaded(false),
44 Mounted(false)
45 {
46 };
47 /*}}}*/
48 // CDROMMethod::Exit - Unmount the disc if necessary /*{{{*/
49 // ---------------------------------------------------------------------
50 /* */
51 void CDROMMethod::Exit()
52 {
53 if (Mounted == true)
54 UnmountCdrom(CDROM);
55 }
56 /*}}}*/
57 // CDROMMethod::GetID - Search the database for a matching string /*{{{*/
58 // ---------------------------------------------------------------------
59 /* */
60 string CDROMMethod::GetID(string Name)
61 {
62 // Search for an ID
63 const Configuration::Item *Top = Database.Tree("CD");
64 if (Top != 0)
65 Top = Top->Child;
66
67 for (; Top != 0;)
68 {
69 if (Top->Value == Name)
70 return Top->Tag;
71
72 Top = Top->Next;
73 }
74 return string();
75 }
76 /*}}}*/
77 // CDROMMethod::Fetch - Fetch a file /*{{{*/
78 // ---------------------------------------------------------------------
79 /* */
80 bool CDROMMethod::Fetch(FetchItem *Itm)
81 {
82 URI Get = Itm->Uri;
83 string File = Get.Path;
84 FetchResult Res;
85
86 bool Debug = _config->FindB("Debug::Acquire::cdrom",false);
87
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;
94 Res.Filename = File;
95 URIDone(Res);
96 return true;
97 }
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
113 // All non IMS queries for package files fail.
114 if (Itm->IndexFile == true || GetID(Get.Host).empty() == true)
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");
118 return true;
119 }
120
121 // We already have a CD inserted, but it is the wrong one
122 if (CurrentID.empty() == false && Database.Find("CD::" + CurrentID) != Get.Host)
123 {
124 Fail("Wrong CD",true);
125 return true;
126 }
127
128 CDROM = _config->FindDir("Acquire::cdrom::mount","/cdrom/");
129 if (CDROM[0] == '.')
130 CDROM= SafeGetCWD() + '/' + CDROM;
131 string NewID;
132 while (CurrentID.empty() == true)
133 {
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 }
150
151 if (Hit == true)
152 break;
153
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());
158 if (MediaFail(Get.Host,CDROM) == false)
159 {
160 CurrentID = "FAIL";
161 Fail("Wrong CD",true);
162 return true;
163 }
164
165 MountCdrom(CDROM);
166 Mounted = true;
167 }
168
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");
174
175 if (NewID.empty() == false)
176 CurrentID = NewID;
177 Res.LastModified = Buf.st_mtime;
178 Res.IMSHit = true;
179 Res.Size = Buf.st_size;
180 URIDone(Res);
181 return true;
182 }
183 /*}}}*/
184
185 int main()
186 {
187 CDROMMethod Mth;
188 return Mth.Run();
189 }