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/cdrom.h>
24 #include <apt-pkg/configuration.h>
25 #include <apt-pkg/pkgsystem.h>
33 #include <apt-private/private-cmndline.h>
34 #include <apt-private/private-output.h>
41 class pkgCdromTextStatus
: public pkgCdromStatus
/*{{{*/
44 OpTextProgress Progress
;
45 void Prompt(const char *Text
);
46 string
PromptLine(const char *Text
);
47 bool AskCdromName(string
&name
) APT_OVERRIDE
;
50 virtual void Update(string text
, int current
) APT_OVERRIDE
;
51 virtual bool ChangeCdrom() APT_OVERRIDE
;
52 virtual OpProgress
* GetOpProgress() APT_OVERRIDE
;
55 void pkgCdromTextStatus::Prompt(const char *Text
)
58 cout
<< Text
<< ' ' << flush
;
59 if (read(STDIN_FILENO
,&C
,1) < 0)
60 _error
->Errno("pkgCdromTextStatus::Prompt",
61 "Failed to read from standard input (not a terminal?)");
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 APT_CONST OpProgress
* pkgCdromTextStatus::GetOpProgress()
101 // AddOrIdent - Add or Ident a CDROM /*{{{*/
102 static bool AddOrIdent(bool const Add
)
104 pkgUdevCdromDevices UdevCdroms
;
105 pkgCdromTextStatus log
;
108 bool oneSuccessful
= false;
109 bool AutoDetect
= _config
->FindB("Acquire::cdrom::AutoDetect", true);
110 if (AutoDetect
== true && UdevCdroms
.Dlopen() == true)
112 bool const Debug
= _config
->FindB("Debug::Acquire::cdrom", false);
113 std::string
const CDMount
= _config
->Find("Acquire::cdrom::mount");
114 bool const NoMount
= _config
->FindB("APT::CDROM::NoMount", false);
115 if (NoMount
== false)
116 _config
->Set("APT::CDROM::NoMount", true);
118 vector
<struct CdromDevice
> const v
= UdevCdroms
.Scan();
119 for (std::vector
<struct CdromDevice
>::const_iterator cd
= v
.begin(); cd
!= v
.end(); ++cd
)
122 clog
<< "Looking at device:"
123 << "\tDeviveName: '" << cd
->DeviceName
<< "'"
124 << "\tIsMounted: '" << cd
->Mounted
<< "'"
125 << "\tMountPoint: '" << cd
->MountPath
<< "'"
128 std::string AptMountPoint
;
130 _config
->Set("Acquire::cdrom::mount", cd
->MountPath
);
131 else if (NoMount
== true)
135 AptMountPoint
= _config
->FindDir("Dir::Media::MountPath");
136 if (FileExists(AptMountPoint
) == false)
137 mkdir(AptMountPoint
.c_str(), 0750);
138 if(MountCdrom(AptMountPoint
, cd
->DeviceName
) == false)
140 _error
->Warning(_("Failed to mount '%s' to '%s'"), cd
->DeviceName
.c_str(), AptMountPoint
.c_str());
143 _config
->Set("Acquire::cdrom::mount", AptMountPoint
);
146 _error
->PushToStack();
148 oneSuccessful
= cdrom
.Add(&log
);
152 oneSuccessful
= cdrom
.Ident(id
, &log
);
154 _error
->MergeWithStack();
156 if (AptMountPoint
.empty() == false)
157 UnmountCdrom(AptMountPoint
);
159 if (NoMount
== false)
160 _config
->Set("APT::CDROM::NoMount", NoMount
);
161 _config
->Set("Acquire::cdrom::mount", CDMount
);
164 // fallback if auto-detect didn't work
165 if (oneSuccessful
== false)
167 _error
->PushToStack();
169 oneSuccessful
= cdrom
.Add(&log
);
173 oneSuccessful
= cdrom
.Ident(id
, &log
);
175 _error
->MergeWithStack();
178 if (oneSuccessful
== false)
179 _error
->Error("%s", _("No CD-ROM could be auto-detected or found using the default mount point.\n"
180 "You may try the --cdrom option to set the CD-ROM mount point.\n"
181 "See 'man apt-cdrom' for more information about the CD-ROM auto-detection and mount point."));
182 else if (Add
== true)
183 cout
<< _("Repeat this process for the rest of the CDs in your set.") << endl
;
185 return oneSuccessful
;
188 // DoAdd - Add a new CDROM /*{{{*/
189 // ---------------------------------------------------------------------
190 /* This does the main add bit.. We show some status and things. The
191 sequence is to mount/umount the CD, Ident it then scan it for package
192 files and reduce that list. Then we copy over the package files and
193 verify them. Then rewrite the database files */
194 static bool DoAdd(CommandLine
&)
196 return AddOrIdent(true);
199 // DoIdent - Ident a CDROM /*{{{*/
200 static bool DoIdent(CommandLine
&)
202 return AddOrIdent(false);
205 // ShowHelp - Show the help screen /*{{{*/
206 static bool ShowHelp(CommandLine
&)
208 ioprintf(cout
, "%s %s (%s)\n", PACKAGE
, PACKAGE_VERSION
, COMMON_ARCH
);
210 if (_config
->FindB("version") == true)
214 "Usage: apt-cdrom [options] command\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"
221 " add - Add a CDROM\n"
222 " ident - Report the identity of a CDROM\n"
225 " -h This help text\n"
226 " -d CD-ROM mount point\n"
227 " -r Rename a recognized CD-ROM\n"
229 " -f Fast mode, don't check package files\n"
230 " -a Thorough scan mode\n"
231 " --no-auto-detect Do not try to auto detect drive and mount point\n"
232 " -c=? Read this configuration file\n"
233 " -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp\n"
238 int main(int argc
,const char *argv
[]) /*{{{*/
240 CommandLine::Dispatch Cmds
[] = {
246 std::vector
<CommandLine::Args
> Args
= getCommandArgs("apt-cdrom", CommandLine::GetCommand(Cmds
, argc
, argv
));
248 // Set up gettext support
249 setlocale(LC_ALL
,"");
252 // Parse the command line and initialize the package library
254 ParseCommandLine(CmdL
, Cmds
, Args
.data(), &_config
, &_system
, argc
, argv
, ShowHelp
);
258 // Match the operation
259 bool returned
= CmdL
.DispatchArg(Cmds
);
261 if (_config
->FindI("quiet",0) > 0)
262 _error
->DumpErrors();
264 _error
->DumpErrors(GlobalError::DEBUG
);
265 return returned
== true ? 0 : 100;