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