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 /*{{{*/
16 #include <apt-pkg/cmndline.h>
17 #include <apt-pkg/error.h>
18 #include <apt-pkg/init.h>
19 #include <apt-pkg/fileutl.h>
20 #include <apt-pkg/progress.h>
21 #include <apt-pkg/cdromutl.h>
22 #include <apt-pkg/strutl.h>
23 #include <apt-pkg/acquire.h>
24 #include <apt-pkg/acquire-item.h>
25 #include <apt-pkg/cdrom.h>
26 #include <apt-pkg/configuration.h>
27 #include <apt-pkg/pkgsystem.h>
45 class pkgCdromTextStatus
: public pkgCdromStatus
/*{{{*/
48 OpTextProgress Progress
;
49 void Prompt(const char *Text
);
50 string
PromptLine(const char *Text
);
51 bool AskCdromName(string
&name
);
54 virtual void Update(string text
, int current
);
55 virtual bool ChangeCdrom();
56 virtual OpProgress
* GetOpProgress();
59 void pkgCdromTextStatus::Prompt(const char *Text
)
62 cout
<< Text
<< ' ' << flush
;
63 read(STDIN_FILENO
,&C
,1);
68 string
pkgCdromTextStatus::PromptLine(const char *Text
)
70 cout
<< Text
<< ':' << endl
;
77 bool pkgCdromTextStatus::AskCdromName(string
&name
)
79 cout
<< _("Please provide a name for this Disc, such as 'Debian 5.0.3 Disk 1'") << flush
;
80 name
= PromptLine("");
86 void pkgCdromTextStatus::Update(string text
, int current
)
89 cout
<< text
<< flush
;
92 bool pkgCdromTextStatus::ChangeCdrom()
94 Prompt(_("Please insert a Disc in the drive and press enter"));
98 OpProgress
* pkgCdromTextStatus::GetOpProgress()
103 // SetupAutoDetect /*{{{*/
104 bool AutoDetectCdrom(pkgUdevCdromDevices
&UdevCdroms
, unsigned int &i
)
106 bool Debug
= _config
->FindB("Debug::Acquire::cdrom", false);
108 vector
<struct CdromDevice
> v
= UdevCdroms
.Scan();
113 clog
<< "Looking at devce " << i
114 << " DeviveName: " << v
[i
].DeviceName
115 << " IsMounted: '" << v
[i
].Mounted
<< "'"
116 << " MountPoint: '" << v
[i
].MountPath
<< "'"
121 // set the right options
122 _config
->Set("Acquire::cdrom::mount", v
[i
].MountPath
);
123 _config
->Set("APT::CDROM::NoMount", true);
125 string AptMountPoint
= _config
->FindDir("Dir::Media::MountPath");
126 if (!FileExists(AptMountPoint
))
127 mkdir(AptMountPoint
.c_str(), 0750);
128 if(MountCdrom(AptMountPoint
, v
[i
].DeviceName
) == false)
129 _error
->Warning(_("Failed to mount '%s' to '%s'"), v
[i
].DeviceName
.c_str(), AptMountPoint
.c_str());
130 _config
->Set("Acquire::cdrom::mount", AptMountPoint
);
131 _config
->Set("APT::CDROM::NoMount", true);
139 // DoAdd - Add a new CDROM /*{{{*/
140 // ---------------------------------------------------------------------
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 */
145 bool DoAdd(CommandLine
&)
147 pkgUdevCdromDevices UdevCdroms
;
148 pkgCdromTextStatus log
;
152 bool AutoDetect
= _config
->FindB("Acquire::cdrom::AutoDetect", true);
153 unsigned int count
= 0;
154 if (AutoDetect
&& UdevCdroms
.Dlopen())
155 while (AutoDetectCdrom(UdevCdroms
, count
))
156 res
&= cdrom
.Add(&log
);
158 res
= cdrom
.Add(&log
);
161 cout
<< _("Repeat this process for the rest of the CDs in your set.") << endl
;
166 // DoIdent - Ident a CDROM /*{{{*/
167 // ---------------------------------------------------------------------
169 bool DoIdent(CommandLine
&)
171 pkgUdevCdromDevices UdevCdroms
;
173 pkgCdromTextStatus log
;
177 bool AutoDetect
= _config
->FindB("Acquire::cdrom::AutoDetect");
179 unsigned int count
= 0;
180 if (AutoDetect
&& UdevCdroms
.Dlopen())
181 while (AutoDetectCdrom(UdevCdroms
, count
))
182 res
&= cdrom
.Ident(ident
, &log
);
184 return cdrom
.Ident(ident
, &log
);
188 // ShowHelp - Show the help screen /*{{{*/
189 // ---------------------------------------------------------------------
193 ioprintf(cout
,_("%s %s for %s compiled on %s %s\n"),PACKAGE
,PACKAGE_VERSION
,
194 COMMON_ARCH
,__DATE__
,__TIME__
);
195 if (_config
->FindB("version") == true)
199 "Usage: apt-cdrom [options] command\n"
201 "apt-cdrom is a tool to add CDROM's to APT's source list. The\n"
202 "CDROM mount point and device information is taken from apt.conf\n"
206 " add - Add a CDROM\n"
207 " ident - Report the identity of a CDROM\n"
210 " -h This help text\n"
211 " -d CD-ROM mount point\n"
212 " -r Rename a recognized CD-ROM\n"
214 " -f Fast mode, don't check package files\n"
215 " -a Thorough scan mode\n"
216 " --auto-detect Auto detect drive and mount point\n"
217 " -c=? Read this configuration file\n"
218 " -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp\n"
223 int main(int argc
,const char *argv
[]) /*{{{*/
225 CommandLine::Args Args
[] = {
226 {'h',"help","help",0},
227 { 0,"auto-detect","Acquire::cdrom::AutoDetect",0},
228 {'v',"version","version",0},
229 {'d',"cdrom","Acquire::cdrom::mount",CommandLine::HasArg
},
230 {'r',"rename","APT::CDROM::Rename",0},
231 {'m',"no-mount","APT::CDROM::NoMount",0},
232 {'f',"fast","APT::CDROM::Fast",0},
233 {'n',"just-print","APT::CDROM::NoAct",0},
234 {'n',"recon","APT::CDROM::NoAct",0},
235 {'n',"no-act","APT::CDROM::NoAct",0},
236 {'a',"thorough","APT::CDROM::Thorough",0},
237 {'c',"config-file",0,CommandLine::ConfigFile
},
238 {'o',"option",0,CommandLine::ArbItem
},
240 CommandLine::Dispatch Cmds
[] = {
245 // Set up gettext support
246 setlocale(LC_ALL
,"");
249 // Parse the command line and initialize the package library
250 CommandLine
CmdL(Args
,_config
);
251 if (pkgInitConfig(*_config
) == false ||
252 CmdL
.Parse(argc
,argv
) == false ||
253 pkgInitSystem(*_config
,_system
) == false)
255 _error
->DumpErrors();
259 // See if the help should be shown
260 if (_config
->FindB("help") == true || _config
->FindB("version") == true ||
261 CmdL
.FileSize() == 0)
264 // Deal with stdout not being a tty
265 if (isatty(STDOUT_FILENO
) && _config
->FindI("quiet", -1) == -1)
266 _config
->Set("quiet","1");
268 // Match the operation
269 CmdL
.DispatchArg(Cmds
);
271 // Print any errors or warnings found during parsing
272 bool const Errors
= _error
->PendingError();
273 if (_config
->FindI("quiet",0) > 0)
274 _error
->DumpErrors();
276 _error
->DumpErrors(GlobalError::DEBUG
);
277 return Errors
== true ? 100 : 0;