]> git.saurik.com Git - apt.git/blame - methods/cdrom.cc
Fixes
[apt.git] / methods / cdrom.cc
CommitLineData
f46e7681
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
33abc0e5 3// $Id: cdrom.cc,v 1.5 1998/12/22 07:41:25 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 /*}}}*/
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. */
47string CDROMMethod::GetID(string Name)
48{
f631d1ba
AL
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 Fail();
33abc0e5 60 return string();
f631d1ba
AL
61 }
62 }
63 DatabaseLoaded = true;
64 }
65
66 const Configuration::Item *Top = Database.Tree("CD");
f46e7681
AL
67 for (; Top != 0;)
68 {
69 if (Top->Value == Name)
70 return Top->Tag;
71
72 Top = Top->Next;
73 }
74
75 return string();
76}
77 /*}}}*/
78// CDROMMethod::Fetch - Fetch a file /*{{{*/
79// ---------------------------------------------------------------------
80/* */
81bool CDROMMethod::Fetch(FetchItem *Itm)
82{
83 URI Get = Itm->Uri;
84 string File = Get.Path;
85 FetchResult Res;
86
87 /* All IMS queries are returned as a hit, CDROMs are readonly so
88 time stamps never change */
89 if (Itm->LastModified != 0)
90 {
91 Res.LastModified = Itm->LastModified;
92 Res.IMSHit = true;
93 URIDone(Res);
94 return true;
95 }
96
97 string ID = GetID(Get.Host);
98
99 // All non IMS queries for package files fail.
f631d1ba 100 if (Itm->IndexFile == true || ID.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");
33abc0e5 104 return false;
f46e7681
AL
105 }
106
107 // We already have a CD inserted, but it is the wrong one
108 if (CurrentID.empty() == false && ID != CurrentID)
109 {
110 Fail("Wrong CD",true);
33abc0e5 111 return false;
f46e7681
AL
112 }
113
114 string CDROM = _config->FindDir("Acquire::cdrom::mount","/cdrom/");
76d97c26
AL
115 if (CDROM[0] == '.')
116 CDROM= SafeGetCWD() + '/' + CDROM;
f46e7681
AL
117 string NewID;
118 while (1)
119 {
120 if (IdentCdrom(CDROM,NewID) == false)
121 return false;
122
123 // A hit
124 if (NewID == ID)
125 break;
126
127 UnmountCdrom(CDROM);
018f1533
AL
128 if (MediaFail(Get.Host,CDROM) == false)
129 {
76d97c26
AL
130 CurrentID = "FAIL";
131 Fail("Wrong CD",true);
33abc0e5 132 return false;
018f1533
AL
133 }
134
f46e7681
AL
135 MountCdrom(CDROM);
136 }
137
138 // ID matches
139 if (NewID == ID)
140 {
141 Res.Filename = CDROM + File;
142 if (FileExists(Res.Filename) == false)
143 return _error->Error("File not found");
144
145 CurrentID = ID;
146 Res.LastModified = Itm->LastModified;
147 Res.IMSHit = true;
148 URIDone(Res);
149 return true;
150 }
151
152 return _error->Error("CDROM not found");
153}
154 /*}}}*/
155
156int main()
157{
158 CDROMMethod Mth;
159 return Mth.Run();
160}