1 // -*- mode: cpp; mode: fold -*-
3 // $Id: apt-cdrom.cc,v 1.45 2003/11/19 23:50:51 mdz Exp $
4 /* ######################################################################
6 APT CDROM - Tool for handling APT's CDROM database.
8 Currently the only option is 'add' which will take the current CD
9 in the drive and add it into the database.
11 ##################################################################### */
13 // Include Files /*{{{*/
14 #include <apt-pkg/cmndline.h>
15 #include <apt-pkg/error.h>
16 #include <apt-pkg/init.h>
17 #include <apt-pkg/fileutl.h>
18 #include <apt-pkg/progress.h>
19 #include <apt-pkg/cdromutl.h>
20 #include <apt-pkg/strutl.h>
21 #include <apt-pkg/acquire.h>
22 #include <apt-pkg/acquire-item.h>
23 #include <apt-pkg/cdrom.h>
27 //#include "indexcopy.h"
43 class pkgCdromTextStatus
: public pkgCdromStatus
/*{{{*/
46 OpTextProgress Progress
;
47 void Prompt(const char *Text
);
48 string
PromptLine(const char *Text
);
49 bool AskCdromName(string
&name
);
52 virtual void Update(string text
, int current
);
53 virtual bool ChangeCdrom();
54 virtual OpProgress
* GetOpProgress();
57 void pkgCdromTextStatus::Prompt(const char *Text
)
60 cout
<< Text
<< ' ' << flush
;
61 read(STDIN_FILENO
,&C
,1);
66 string
pkgCdromTextStatus::PromptLine(const char *Text
)
68 cout
<< Text
<< ':' << endl
;
75 bool pkgCdromTextStatus::AskCdromName(string
&name
)
77 cout
<< _("Please provide a name for this Disc, such as 'Debian 5.0.3 Disk 1'") << flush
;
78 name
= PromptLine("");
84 void pkgCdromTextStatus::Update(string text
, int current
)
87 cout
<< text
<< flush
;
90 bool pkgCdromTextStatus::ChangeCdrom()
92 Prompt(_("Please insert a Disc in the drive and press enter"));
96 OpProgress
* pkgCdromTextStatus::GetOpProgress()
101 // SetupAutoDetect /*{{{*/
102 bool AutoDetectCdrom(pkgUdevCdromDevices
&UdevCdroms
, unsigned int &i
)
104 bool Debug
= _config
->FindB("Debug::Acquire::cdrom", false);
106 vector
<struct CdromDevice
> v
= UdevCdroms
.Scan();
111 clog
<< "Looking at devce " << i
112 << " DeviveName: " << v
[i
].DeviceName
113 << " IsMounted: '" << v
[i
].Mounted
<< "'"
114 << " MountPoint: '" << v
[i
].MountPath
<< "'"
119 // set the right options
120 _config
->Set("Acquire::cdrom::mount", v
[i
].MountPath
);
121 _config
->Set("APT::CDROM::NoMount", true);
123 string AptMountPoint
= _config
->FindDir("Dir::Media::MountPath");
124 if (!FileExists(AptMountPoint
))
125 mkdir(AptMountPoint
.c_str(), 0750);
126 if(MountCdrom(AptMountPoint
, v
[i
].DeviceName
) == false)
127 _error
->Warning(_("Failed to mount '%s' to '%s'"), v
[i
].DeviceName
.c_str(), AptMountPoint
.c_str());
128 _config
->Set("Acquire::cdrom::mount", AptMountPoint
);
129 _config
->Set("APT::CDROM::NoMount", true);
137 // DoAdd - Add a new CDROM /*{{{*/
138 // ---------------------------------------------------------------------
139 /* This does the main add bit.. We show some status and things. The
140 sequence is to mount/umount the CD, Ident it then scan it for package
141 files and reduce that list. Then we copy over the package files and
142 verify them. Then rewrite the database files */
143 bool DoAdd(CommandLine
&)
145 pkgUdevCdromDevices UdevCdroms
;
146 pkgCdromTextStatus log
;
150 bool AutoDetect
= _config
->FindB("Acquire::cdrom::AutoDetect");
151 unsigned int count
= 0;
153 if (AutoDetect
&& UdevCdroms
.Dlopen())
155 while (AutoDetectCdrom(UdevCdroms
, count
))
156 res
&= cdrom
.Add(&log
);
158 res
= cdrom
.Add(&log
);
162 cout
<< _("Repeat this process for the rest of the CDs in your set.") << endl
;
167 // DoIdent - Ident a CDROM /*{{{*/
168 // ---------------------------------------------------------------------
170 bool DoIdent(CommandLine
&)
172 pkgUdevCdromDevices UdevCdroms
;
174 pkgCdromTextStatus log
;
178 bool AutoDetect
= _config
->FindB("Acquire::cdrom::AutoDetect");
179 unsigned int count
= 0;
181 if (AutoDetect
&& UdevCdroms
.Dlopen())
183 while (AutoDetectCdrom(UdevCdroms
, count
))
184 res
&= cdrom
.Ident(ident
, &log
);
186 return cdrom
.Ident(ident
, &log
);
192 // ShowHelp - Show the help screen /*{{{*/
193 // ---------------------------------------------------------------------
197 ioprintf(cout
,_("%s %s for %s compiled on %s %s\n"),PACKAGE
,VERSION
,
198 COMMON_ARCH
,__DATE__
,__TIME__
);
199 if (_config
->FindB("version") == true)
203 "Usage: apt-cdrom [options] command\n"
205 "apt-cdrom is a tool to add CDROM's to APT's source list. The\n"
206 "CDROM mount point and device information is taken from apt.conf\n"
210 " add - Add a CDROM\n"
211 " ident - Report the identity of a CDROM\n"
214 " -h This help text\n"
215 " -d CD-ROM mount point\n"
216 " -r Rename a recognized CD-ROM\n"
218 " -f Fast mode, don't check package files\n"
219 " -a Thorough scan mode\n"
220 " --auto-detect Auto detect drive and mount point\n"
221 " -c=? Read this configuration file\n"
222 " -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp\n"
227 int main(int argc
,const char *argv
[]) /*{{{*/
229 CommandLine::Args Args
[] = {
230 {'h',"help","help",0},
231 { 0,"auto-detect","Acquire::cdrom::AutoDetect",0},
232 {'v',"version","version",0},
233 {'d',"cdrom","Acquire::cdrom::mount",CommandLine::HasArg
},
234 {'r',"rename","APT::CDROM::Rename",0},
235 {'m',"no-mount","APT::CDROM::NoMount",0},
236 {'f',"fast","APT::CDROM::Fast",0},
237 {'n',"just-print","APT::CDROM::NoAct",0},
238 {'n',"recon","APT::CDROM::NoAct",0},
239 {'n',"no-act","APT::CDROM::NoAct",0},
240 {'a',"thorough","APT::CDROM::Thorough",0},
241 {'c',"config-file",0,CommandLine::ConfigFile
},
242 {'o',"option",0,CommandLine::ArbItem
},
244 CommandLine::Dispatch Cmds
[] = {
249 // Set up gettext support
250 setlocale(LC_ALL
,"");
253 // Parse the command line and initialize the package library
254 CommandLine
CmdL(Args
,_config
);
255 if (pkgInitConfig(*_config
) == false ||
256 CmdL
.Parse(argc
,argv
) == false ||
257 pkgInitSystem(*_config
,_system
) == false)
259 _error
->DumpErrors();
263 // See if the help should be shown
264 if (_config
->FindB("help") == true || _config
->FindB("version") == true ||
265 CmdL
.FileSize() == 0)
268 // Deal with stdout not being a tty
269 if (isatty(STDOUT_FILENO
) && _config
->FindI("quiet",0) < 1)
270 _config
->Set("quiet","1");
272 // Match the operation
273 CmdL
.DispatchArg(Cmds
);
275 // Print any errors or warnings found during parsing
276 if (_error
->empty() == false)
278 bool Errors
= _error
->PendingError();
279 _error
->DumpErrors();
280 return Errors
== true?100:0;