CDROM method
[ntk/apt.git] / methods / cdrom.cc
CommitLineData
f46e7681
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
3// $Id: cdrom.cc,v 1.1 1998/12/03 07:29:21 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
21class CDROMMethod : public pkgAcqMethod
22{
23 Configuration Database;
24 string CurrentID;
25
26 virtual bool Fetch(FetchItem *Itm);
27 string GetID(string Name);
28
29 public:
30
31 CDROMMethod();
32};
33
34// CDROMMethod::CDROMethod - Constructor /*{{{*/
35// ---------------------------------------------------------------------
36/* */
37CDROMMethod::CDROMMethod() : pkgAcqMethod("1.0",SingleInstance | LocalOnly)
38{
39 // Read the database
40 string DFile = _config->FindFile("Dir::State::cdroms");
41 if (FileExists(DFile) == true)
42 {
43 if (ReadConfigFile(Database,DFile) == false)
44 {
45 _error->Error("Unable to read the cdrom database %s",
46 DFile.c_str());
47 Fail();
48 }
49 }
50};
51 /*}}}*/
52// CDROMMethod::GetID - Get the ID hash for /*{{{*/
53// ---------------------------------------------------------------------
54/* We search the configuration space for the name and then return the ID
55 tag associated with it. */
56string CDROMMethod::GetID(string Name)
57{
58 const Configuration::Item *Top = Database.Tree(0);
59 for (; Top != 0;)
60 {
61 if (Top->Value == Name)
62 return Top->Tag;
63
64 Top = Top->Next;
65 }
66
67 return string();
68}
69 /*}}}*/
70// CDROMMethod::Fetch - Fetch a file /*{{{*/
71// ---------------------------------------------------------------------
72/* */
73bool CDROMMethod::Fetch(FetchItem *Itm)
74{
75 URI Get = Itm->Uri;
76 string File = Get.Path;
77 FetchResult Res;
78
79 /* All IMS queries are returned as a hit, CDROMs are readonly so
80 time stamps never change */
81 if (Itm->LastModified != 0)
82 {
83 Res.LastModified = Itm->LastModified;
84 Res.IMSHit = true;
85 URIDone(Res);
86 return true;
87 }
88
89 string ID = GetID(Get.Host);
90
91 // All non IMS queries for package files fail.
92 if (Itm->IndexFile == true || ID.empty() == false)
93 {
94 Fail("Please use apt-cdrom to make this CD recognized by APT."
95 " apt-get update cannot be used to add new CDs");
96 return true;
97 }
98
99 // We already have a CD inserted, but it is the wrong one
100 if (CurrentID.empty() == false && ID != CurrentID)
101 {
102 Fail("Wrong CD",true);
103 return true;
104 }
105
106 string CDROM = _config->FindDir("Acquire::cdrom::mount","/cdrom/");
107 string NewID;
108 while (1)
109 {
110 if (IdentCdrom(CDROM,NewID) == false)
111 return false;
112
113 // A hit
114 if (NewID == ID)
115 break;
116
117 UnmountCdrom(CDROM);
118 MediaFail(Get.Host,CDROM);
119 MountCdrom(CDROM);
120 }
121
122 // ID matches
123 if (NewID == ID)
124 {
125 Res.Filename = CDROM + File;
126 if (FileExists(Res.Filename) == false)
127 return _error->Error("File not found");
128
129 CurrentID = ID;
130 Res.LastModified = Itm->LastModified;
131 Res.IMSHit = true;
132 URIDone(Res);
133 return true;
134 }
135
136 return _error->Error("CDROM not found");
137}
138 /*}}}*/
139
140int main()
141{
142 CDROMMethod Mth;
143 return Mth.Run();
144}