]> git.saurik.com Git - apt.git/blob - methods/cdrom.cc
Fixed cisco cacheengine lameness
[apt.git] / methods / cdrom.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: cdrom.cc,v 1.8 1999/03/28 01:37:26 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 Configuration Database;
24 bool DatabaseLoaded;
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 /* */
38 CDROMMethod::CDROMMethod() : pkgAcqMethod("1.0",SingleInstance | LocalOnly |
39 SendConfig), DatabaseLoaded(false)
40 {
41 };
42 /*}}}*/
43 // CDROMMethod::GetID - Get the ID hash for /*{{{*/
44 // ---------------------------------------------------------------------
45 /* We search the configuration space for the name and then return the ID
46 tag associated with it. */
47 string CDROMMethod::GetID(string Name)
48 {
49 if (DatabaseLoaded == false)
50 {
51 // Read the database
52 string DFile = _config->FindFile("Dir::State::cdroms");
53 if (FileExists(DFile) == true)
54 {
55 if (ReadConfigFile(Database,DFile) == false)
56 {
57 _error->Error("Unable to read the cdrom database %s",
58 DFile.c_str());
59 return string();
60 }
61 }
62 DatabaseLoaded = true;
63 }
64
65 const Configuration::Item *Top = Database.Tree("CD");
66 if (Top != 0)
67 Top = Top->Child;
68
69 for (; Top != 0;)
70 {
71 if (Top->Value == Name)
72 return Top->Tag;
73
74 Top = Top->Next;
75 }
76
77 return string();
78 }
79 /*}}}*/
80 // CDROMMethod::Fetch - Fetch a file /*{{{*/
81 // ---------------------------------------------------------------------
82 /* */
83 bool CDROMMethod::Fetch(FetchItem *Itm)
84 {
85 URI Get = Itm->Uri;
86 string File = Get.Path;
87 FetchResult Res;
88
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;
95 URIDone(Res);
96 return true;
97 }
98
99 string ID = GetID(Get.Host);
100 if (_error->PendingError() == true)
101 return false;
102
103 // All non IMS queries for package files fail.
104 if (Itm->IndexFile == true || ID.empty() == true)
105 {
106 Fail("Please use apt-cdrom to make this CD recognized by APT."
107 " apt-get update cannot be used to add new CDs");
108 return true;
109 }
110
111 // We already have a CD inserted, but it is the wrong one
112 if (CurrentID.empty() == false && ID != CurrentID)
113 {
114 Fail("Wrong CD",true);
115 return true;
116 }
117
118 string CDROM = _config->FindDir("Acquire::cdrom::mount","/cdrom/");
119 if (CDROM[0] == '.')
120 CDROM= SafeGetCWD() + '/' + CDROM;
121 string NewID;
122 while (1)
123 {
124 if (IdentCdrom(CDROM,NewID) == false)
125 return false;
126
127 // A hit
128 if (NewID == ID)
129 break;
130
131 // I suppose this should prompt somehow?
132 if (UnmountCdrom(CDROM) == false)
133 return _error->Error("Unable to unmount the CD-ROM in %s, it may still be in use.",
134 CDROM.c_str());
135 if (MediaFail(Get.Host,CDROM) == false)
136 {
137 CurrentID = "FAIL";
138 Fail("Wrong CD",true);
139 return true;
140 }
141
142 MountCdrom(CDROM);
143 }
144
145 // ID matches
146 if (NewID == ID)
147 {
148 Res.Filename = CDROM + File;
149 struct stat Buf;
150 if (stat(Res.Filename.c_str(),&Buf) != 0)
151 return _error->Error("File not found");
152
153 CurrentID = ID;
154 Res.LastModified = Buf.st_mtime;
155 Res.IMSHit = true;
156 Res.Size = Buf.st_size;
157 URIDone(Res);
158 return true;
159 }
160
161 return _error->Error("CDROM not found");
162 }
163 /*}}}*/
164
165 int main()
166 {
167 CDROMMethod Mth;
168 return Mth.Run();
169 }