]> git.saurik.com Git - apt.git/blame - cmdline/apt-cdrom.cc
cleanup the hash iteration. unfortunately there is no 1:1 mapping from Hashes::Suppor...
[apt.git] / cmdline / apt-cdrom.cc
CommitLineData
83d89a9f
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
a3f6ea20 3// $Id: apt-cdrom.cc,v 1.45 2003/11/19 23:50:51 mdz Exp $
83d89a9f
AL
4/* ######################################################################
5
18444708
AL
6 APT CDROM - Tool for handling APT's CDROM database.
7
8 Currently the only option is 'add' which will take the current CD
9 in the drive and add it into the database.
83d89a9f
AL
10
11 ##################################################################### */
12 /*}}}*/
13// Include Files /*{{{*/
ea542140
DK
14#include<config.h>
15
83d89a9f
AL
16#include <apt-pkg/cmndline.h>
17#include <apt-pkg/error.h>
18#include <apt-pkg/init.h>
83d89a9f
AL
19#include <apt-pkg/fileutl.h>
20#include <apt-pkg/progress.h>
65ae8fab 21#include <apt-pkg/cdromutl.h>
cdcc6d34 22#include <apt-pkg/strutl.h>
a75c6a6e
MZ
23#include <apt-pkg/acquire.h>
24#include <apt-pkg/acquire-item.h>
25#include <apt-pkg/cdrom.h>
472ff00e
DK
26#include <apt-pkg/configuration.h>
27#include <apt-pkg/pkgsystem.h>
143abaeb 28
233c2b66 29#include <locale.h>
83d89a9f 30#include <iostream>
18444708 31#include <fstream>
83d89a9f
AL
32#include <vector>
33#include <algorithm>
83d89a9f
AL
34#include <sys/stat.h>
35#include <fcntl.h>
36#include <dirent.h>
37#include <unistd.h>
38#include <stdio.h>
ea542140
DK
39
40#include <apti18n.h>
83d89a9f
AL
41 /*}}}*/
42
076d01b0
AL
43using namespace std;
44
92fcbfc1 45class pkgCdromTextStatus : public pkgCdromStatus /*{{{*/
83d89a9f 46{
a75c6a6e
MZ
47protected:
48 OpTextProgress Progress;
49 void Prompt(const char *Text);
50 string PromptLine(const char *Text);
51 bool AskCdromName(string &name);
52
53public:
54 virtual void Update(string text, int current);
55 virtual bool ChangeCdrom();
56 virtual OpProgress* GetOpProgress();
8a579291 57};
83d89a9f 58
a75c6a6e 59void pkgCdromTextStatus::Prompt(const char *Text)
83d89a9f 60{
a75c6a6e
MZ
61 char C;
62 cout << Text << ' ' << flush;
63 read(STDIN_FILENO,&C,1);
64 if (C != '\n')
65 cout << endl;
83d89a9f 66}
4dfaa253 67
a75c6a6e 68string pkgCdromTextStatus::PromptLine(const char *Text)
83d89a9f 69{
a75c6a6e 70 cout << Text << ':' << endl;
83d89a9f 71
a75c6a6e
MZ
72 string Res;
73 getline(cin,Res);
83d89a9f
AL
74 return Res;
75}
18444708 76
a75c6a6e 77bool pkgCdromTextStatus::AskCdromName(string &name)
83d89a9f 78{
cdd5a135 79 cout << _("Please provide a name for this Disc, such as 'Debian 5.0.3 Disk 1'") << flush;
a75c6a6e 80 name = PromptLine("");
83d89a9f 81
83d89a9f
AL
82 return true;
83}
83d89a9f 84
83d89a9f 85
a75c6a6e 86void pkgCdromTextStatus::Update(string text, int current)
18444708 87{
a75c6a6e
MZ
88 if(text.size() > 0)
89 cout << text << flush;
18444708 90}
4dfaa253 91
8a579291 92bool pkgCdromTextStatus::ChangeCdrom()
83d89a9f 93{
4a5e5089 94 Prompt(_("Please insert a Disc in the drive and press enter"));
18444708
AL
95 return true;
96}
83d89a9f 97
8a579291 98OpProgress* pkgCdromTextStatus::GetOpProgress()
a75c6a6e
MZ
99{
100 return &Progress;
101};
83d89a9f 102 /*}}}*/
169413dc 103// SetupAutoDetect /*{{{*/
fb503892 104bool AutoDetectCdrom(pkgUdevCdromDevices &UdevCdroms, unsigned int &i)
169413dc
MV
105{
106 bool Debug = _config->FindB("Debug::Acquire::cdrom", false);
107
108 vector<struct CdromDevice> v = UdevCdroms.Scan();
109 if (i >= v.size())
110 return false;
111
112 if (Debug)
113 clog << "Looking at devce " << i
114 << " DeviveName: " << v[i].DeviceName
115 << " IsMounted: '" << v[i].Mounted << "'"
116 << " MountPoint: '" << v[i].MountPath << "'"
117 << endl;
118
119 if (v[i].Mounted)
120 {
121 // set the right options
122 _config->Set("Acquire::cdrom::mount", v[i].MountPath);
123 _config->Set("APT::CDROM::NoMount", true);
124 } else {
ffee221b 125 string AptMountPoint = _config->FindDir("Dir::Media::MountPath");
fb503892 126 if (!FileExists(AptMountPoint))
ffee221b 127 mkdir(AptMountPoint.c_str(), 0750);
fb503892 128 if(MountCdrom(AptMountPoint, v[i].DeviceName) == false)
ffee221b 129 _error->Warning(_("Failed to mount '%s' to '%s'"), v[i].DeviceName.c_str(), AptMountPoint.c_str());
fb503892 130 _config->Set("Acquire::cdrom::mount", AptMountPoint);
169413dc
MV
131 _config->Set("APT::CDROM::NoMount", true);
132 }
133 i++;
134
135 return true;
136}
137 /*}}}*/
138
83d89a9f
AL
139// DoAdd - Add a new CDROM /*{{{*/
140// ---------------------------------------------------------------------
4dfaa253
AL
141/* This does the main add bit.. We show some status and things. The
142 sequence is to mount/umount the CD, Ident it then scan it for package
143 files and reduce that list. Then we copy over the package files and
144 verify them. Then rewrite the database files */
83d89a9f
AL
145bool DoAdd(CommandLine &)
146{
169413dc 147 pkgUdevCdromDevices UdevCdroms;
a75c6a6e
MZ
148 pkgCdromTextStatus log;
149 pkgCdrom cdrom;
fb503892 150 bool res = true;
169413dc 151
4df70e75 152 bool AutoDetect = _config->FindB("Acquire::cdrom::AutoDetect", true);
169413dc
MV
153 if (AutoDetect && UdevCdroms.Dlopen())
154 {
785412cf 155 unsigned int count = 0;
169413dc
MV
156 while (AutoDetectCdrom(UdevCdroms, count))
157 res &= cdrom.Add(&log);
158 } else {
159 res = cdrom.Add(&log);
160 }
161
a75c6a6e 162 if(res)
4a5e5089 163 cout << _("Repeat this process for the rest of the CDs in your set.") << endl;
169413dc 164
a75c6a6e 165 return res;
83d89a9f
AL
166}
167 /*}}}*/
b2e465d6
AL
168// DoIdent - Ident a CDROM /*{{{*/
169// ---------------------------------------------------------------------
170/* */
171bool DoIdent(CommandLine &)
172{
169413dc 173 pkgUdevCdromDevices UdevCdroms;
a75c6a6e
MZ
174 string ident;
175 pkgCdromTextStatus log;
176 pkgCdrom cdrom;
169413dc
MV
177 bool res = true;
178
179 bool AutoDetect = _config->FindB("Acquire::cdrom::AutoDetect");
785412cf 180
169413dc
MV
181 if (AutoDetect && UdevCdroms.Dlopen())
182 {
785412cf 183 unsigned int count = 0;
169413dc
MV
184 while (AutoDetectCdrom(UdevCdroms, count))
185 res &= cdrom.Ident(ident, &log);
186 } else {
187 return cdrom.Ident(ident, &log);
188 }
189
190 return res;
b2e465d6
AL
191}
192 /*}}}*/
83d89a9f
AL
193// ShowHelp - Show the help screen /*{{{*/
194// ---------------------------------------------------------------------
195/* */
196int ShowHelp()
197{
9179f697 198 ioprintf(cout,_("%s %s for %s compiled on %s %s\n"),PACKAGE,PACKAGE_VERSION,
5b28c804 199 COMMON_ARCH,__DATE__,__TIME__);
04aa15a8 200 if (_config->FindB("version") == true)
b2e465d6
AL
201 return 0;
202
203 cout <<
204 "Usage: apt-cdrom [options] command\n"
205 "\n"
206 "apt-cdrom is a tool to add CDROM's to APT's source list. The\n"
207 "CDROM mount point and device information is taken from apt.conf\n"
208 "and /etc/fstab.\n"
209 "\n"
210 "Commands:\n"
211 " add - Add a CDROM\n"
212 " ident - Report the identity of a CDROM\n"
213 "\n"
214 "Options:\n"
215 " -h This help text\n"
216 " -d CD-ROM mount point\n"
217 " -r Rename a recognized CD-ROM\n"
218 " -m No mounting\n"
219 " -f Fast mode, don't check package files\n"
220 " -a Thorough scan mode\n"
470221bb 221 " --auto-detect Auto detect drive and mount point\n"
b2e465d6 222 " -c=? Read this configuration file\n"
a2884e32 223 " -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp\n"
b2e465d6
AL
224 "See fstab(5)\n";
225 return 0;
83d89a9f
AL
226}
227 /*}}}*/
92fcbfc1 228int main(int argc,const char *argv[]) /*{{{*/
83d89a9f
AL
229{
230 CommandLine::Args Args[] = {
231 {'h',"help","help",0},
470221bb 232 { 0,"auto-detect","Acquire::cdrom::AutoDetect",0},
04aa15a8 233 {'v',"version","version",0},
83d89a9f
AL
234 {'d',"cdrom","Acquire::cdrom::mount",CommandLine::HasArg},
235 {'r',"rename","APT::CDROM::Rename",0},
236 {'m',"no-mount","APT::CDROM::NoMount",0},
237 {'f',"fast","APT::CDROM::Fast",0},
c60d151b 238 {'n',"just-print","APT::CDROM::NoAct",0},
18444708 239 {'n',"recon","APT::CDROM::NoAct",0},
c60d151b
AL
240 {'n',"no-act","APT::CDROM::NoAct",0},
241 {'a',"thorough","APT::CDROM::Thorough",0},
83d89a9f
AL
242 {'c',"config-file",0,CommandLine::ConfigFile},
243 {'o',"option",0,CommandLine::ArbItem},
244 {0,0,0,0}};
245 CommandLine::Dispatch Cmds[] = {
246 {"add",&DoAdd},
b2e465d6 247 {"ident",&DoIdent},
83d89a9f 248 {0,0}};
67111687
AL
249
250 // Set up gettext support
251 setlocale(LC_ALL,"");
252 textdomain(PACKAGE);
253
83d89a9f
AL
254 // Parse the command line and initialize the package library
255 CommandLine CmdL(Args,_config);
b2e465d6
AL
256 if (pkgInitConfig(*_config) == false ||
257 CmdL.Parse(argc,argv) == false ||
258 pkgInitSystem(*_config,_system) == false)
83d89a9f
AL
259 {
260 _error->DumpErrors();
261 return 100;
262 }
263
264 // See if the help should be shown
5633a7c2 265 if (_config->FindB("help") == true || _config->FindB("version") == true ||
83d89a9f
AL
266 CmdL.FileSize() == 0)
267 return ShowHelp();
a9a5908d
AL
268
269 // Deal with stdout not being a tty
c340d185 270 if (isatty(STDOUT_FILENO) && _config->FindI("quiet", -1) == -1)
a9a5908d 271 _config->Set("quiet","1");
83d89a9f
AL
272
273 // Match the operation
274 CmdL.DispatchArg(Cmds);
275
276 // Print any errors or warnings found during parsing
65beb572
DK
277 bool const Errors = _error->PendingError();
278 if (_config->FindI("quiet",0) > 0)
83d89a9f 279 _error->DumpErrors();
65beb572
DK
280 else
281 _error->DumpErrors(GlobalError::DEBUG);
282 return Errors == true ? 100 : 0;
83d89a9f 283}
92fcbfc1 284 /*}}}*/