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