]> git.saurik.com Git - apt.git/blame - methods/cdrom.cc
Multiple different versions support
[apt.git] / methods / cdrom.cc
CommitLineData
f46e7681
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
e42eb508 3// $Id: cdrom.cc,v 1.9 1999/05/23 05:45:13 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{
23 Configuration Database;
f631d1ba 24 bool DatabaseLoaded;
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;
71
72 /* All IMS queries are returned as a hit, CDROMs are readonly so
73 time stamps never change */
74 if (Itm->LastModified != 0)
75 {
76 Res.LastModified = Itm->LastModified;
77 Res.IMSHit = true;
78 URIDone(Res);
79 return true;
80 }
e42eb508
AL
81
82 // Load the database
83 if (DatabaseLoaded == false)
84 {
85 // Read the database
86 string DFile = _config->FindFile("Dir::State::cdroms");
87 if (FileExists(DFile) == true)
88 {
89 if (ReadConfigFile(Database,DFile) == false)
90 return _error->Error("Unable to read the cdrom database %s",
91 DFile.c_str());
92 }
93 DatabaseLoaded = true;
94 }
95
f46e7681 96 // All non IMS queries for package files fail.
e42eb508 97 if (Itm->IndexFile == true || GetID(Get.Host).empty() == true)
f46e7681
AL
98 {
99 Fail("Please use apt-cdrom to make this CD recognized by APT."
100 " apt-get update cannot be used to add new CDs");
9e0349cc 101 return true;
f46e7681
AL
102 }
103
104 // We already have a CD inserted, but it is the wrong one
e42eb508 105 if (CurrentID.empty() == false && Database.Find("CD::" + CurrentID) != Get.Host)
f46e7681
AL
106 {
107 Fail("Wrong CD",true);
9e0349cc 108 return true;
f46e7681
AL
109 }
110
111 string CDROM = _config->FindDir("Acquire::cdrom::mount","/cdrom/");
76d97c26
AL
112 if (CDROM[0] == '.')
113 CDROM= SafeGetCWD() + '/' + CDROM;
f46e7681
AL
114 string NewID;
115 while (1)
116 {
117 if (IdentCdrom(CDROM,NewID) == false)
118 return false;
119
120 // A hit
e42eb508 121 if (Database.Find("CD::" + NewID) == Get.Host)
f46e7681 122 break;
4df0b629
AL
123
124 // I suppose this should prompt somehow?
125 if (UnmountCdrom(CDROM) == false)
126 return _error->Error("Unable to unmount the CD-ROM in %s, it may still be in use.",
127 CDROM.c_str());
018f1533
AL
128 if (MediaFail(Get.Host,CDROM) == false)
129 {
76d97c26
AL
130 CurrentID = "FAIL";
131 Fail("Wrong CD",true);
9e0349cc 132 return true;
018f1533
AL
133 }
134
f46e7681
AL
135 MountCdrom(CDROM);
136 }
137
e42eb508
AL
138 // Found a CD
139 Res.Filename = CDROM + File;
140 struct stat Buf;
141 if (stat(Res.Filename.c_str(),&Buf) != 0)
142 return _error->Error("File not found");
f46e7681 143
e42eb508
AL
144 CurrentID = NewID;
145 Res.LastModified = Buf.st_mtime;
146 Res.IMSHit = true;
147 Res.Size = Buf.st_size;
148 URIDone(Res);
149 return true;
f46e7681
AL
150}
151 /*}}}*/
152
153int main()
154{
155 CDROMMethod Mth;
156 return Mth.Run();
157}