]> git.saurik.com Git - apt.git/blame - cmdline/apt-cdrom.cc
if file is inaccessible for _apt, disable privilege drop in acquire
[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 23#include <apt-pkg/cdrom.h>
472ff00e
DK
24#include <apt-pkg/configuration.h>
25#include <apt-pkg/pkgsystem.h>
143abaeb 26
83d89a9f
AL
27#include <iostream>
28#include <vector>
453b82a3 29#include <string>
83d89a9f 30#include <sys/stat.h>
83d89a9f 31#include <unistd.h>
ea542140 32
b9179170 33#include <apt-private/private-cmndline.h>
d9e518c6 34#include <apt-private/private-output.h>
b9179170 35
ea542140 36#include <apti18n.h>
83d89a9f
AL
37 /*}}}*/
38
076d01b0
AL
39using namespace std;
40
92fcbfc1 41class pkgCdromTextStatus : public pkgCdromStatus /*{{{*/
83d89a9f 42{
a75c6a6e
MZ
43protected:
44 OpTextProgress Progress;
d3e8fbb3 45 void Prompt(const char *Text);
a75c6a6e 46 string PromptLine(const char *Text);
3b302846 47 bool AskCdromName(string &name) APT_OVERRIDE;
a75c6a6e
MZ
48
49public:
3b302846
DK
50 virtual void Update(string text, int current) APT_OVERRIDE;
51 virtual bool ChangeCdrom() APT_OVERRIDE;
52 virtual OpProgress* GetOpProgress() APT_OVERRIDE;
8a579291 53};
83d89a9f 54
d3e8fbb3 55void pkgCdromTextStatus::Prompt(const char *Text)
83d89a9f 56{
a75c6a6e
MZ
57 char C;
58 cout << Text << ' ' << flush;
f52037d6 59 if (read(STDIN_FILENO,&C,1) < 0)
d3e8fbb3 60 _error->Errno("pkgCdromTextStatus::Prompt",
7dd5854b 61 "Failed to read from standard input (not a terminal?)");
a75c6a6e
MZ
62 if (C != '\n')
63 cout << endl;
83d89a9f 64}
4dfaa253 65
a75c6a6e 66string pkgCdromTextStatus::PromptLine(const char *Text)
83d89a9f 67{
a75c6a6e 68 cout << Text << ':' << endl;
d3e8fbb3 69
a75c6a6e
MZ
70 string Res;
71 getline(cin,Res);
83d89a9f
AL
72 return Res;
73}
18444708 74
d3e8fbb3 75bool pkgCdromTextStatus::AskCdromName(string &name)
83d89a9f 76{
cdd5a135 77 cout << _("Please provide a name for this Disc, such as 'Debian 5.0.3 Disk 1'") << flush;
a75c6a6e 78 name = PromptLine("");
d3e8fbb3 79
83d89a9f
AL
80 return true;
81}
83d89a9f 82
d3e8fbb3 83
65512241 84void pkgCdromTextStatus::Update(string text, int /*current*/)
18444708 85{
a75c6a6e
MZ
86 if(text.size() > 0)
87 cout << text << flush;
18444708 88}
4dfaa253 89
d3e8fbb3 90bool pkgCdromTextStatus::ChangeCdrom()
83d89a9f 91{
94171725 92 Prompt(_("Please insert a Disc in the drive and press [Enter]"));
18444708
AL
93 return true;
94}
83d89a9f 95
a02db58f 96APT_CONST OpProgress* pkgCdromTextStatus::GetOpProgress()
d3e8fbb3
DK
97{
98 return &Progress;
99}
83d89a9f 100 /*}}}*/
b5ee78ae
DK
101// AddOrIdent - Add or Ident a CDROM /*{{{*/
102static bool AddOrIdent(bool const Add)
169413dc 103{
b5ee78ae
DK
104 pkgUdevCdromDevices UdevCdroms;
105 pkgCdromTextStatus log;
106 pkgCdrom cdrom;
169413dc 107
b5ee78ae
DK
108 bool oneSuccessful = false;
109 bool AutoDetect = _config->FindB("Acquire::cdrom::AutoDetect", true);
110 if (AutoDetect == true && UdevCdroms.Dlopen() == true)
111 {
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);
117
118 vector<struct CdromDevice> const v = UdevCdroms.Scan();
119 for (std::vector<struct CdromDevice>::const_iterator cd = v.begin(); cd != v.end(); ++cd)
120 {
121 if (Debug)
122 clog << "Looking at device:"
123 << "\tDeviveName: '" << cd->DeviceName << "'"
124 << "\tIsMounted: '" << cd->Mounted << "'"
125 << "\tMountPoint: '" << cd->MountPath << "'"
126 << endl;
127
128 std::string AptMountPoint;
129 if (cd->Mounted)
130 _config->Set("Acquire::cdrom::mount", cd->MountPath);
131 else if (NoMount == true)
132 continue;
133 else
134 {
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)
139 {
140 _error->Warning(_("Failed to mount '%s' to '%s'"), cd->DeviceName.c_str(), AptMountPoint.c_str());
141 continue;
142 }
143 _config->Set("Acquire::cdrom::mount", AptMountPoint);
144 }
62dcbf84 145
b5ee78ae
DK
146 _error->PushToStack();
147 if (Add == true)
148 oneSuccessful = cdrom.Add(&log);
149 else
150 {
151 std::string id;
152 oneSuccessful = cdrom.Ident(id, &log);
153 }
154 _error->MergeWithStack();
169413dc 155
b5ee78ae
DK
156 if (AptMountPoint.empty() == false)
157 UnmountCdrom(AptMountPoint);
158 }
159 if (NoMount == false)
160 _config->Set("APT::CDROM::NoMount", NoMount);
161 _config->Set("Acquire::cdrom::mount", CDMount);
162 }
169413dc 163
b5ee78ae
DK
164 // fallback if auto-detect didn't work
165 if (oneSuccessful == false)
169413dc 166 {
b5ee78ae
DK
167 _error->PushToStack();
168 if (Add == true)
169 oneSuccessful = cdrom.Add(&log);
62dcbf84 170 else
b5ee78ae
DK
171 {
172 std::string id;
173 oneSuccessful = cdrom.Ident(id, &log);
174 }
175 _error->MergeWithStack();
169413dc 176 }
169413dc 177
b5ee78ae
DK
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;
184
185 return oneSuccessful;
169413dc
MV
186}
187 /*}}}*/
83d89a9f
AL
188// DoAdd - Add a new CDROM /*{{{*/
189// ---------------------------------------------------------------------
4dfaa253 190/* This does the main add bit.. We show some status and things. The
b5ee78ae 191 sequence is to mount/umount the CD, Ident it then scan it for package
4dfaa253
AL
192 files and reduce that list. Then we copy over the package files and
193 verify them. Then rewrite the database files */
c3ccac92 194static bool DoAdd(CommandLine &)
83d89a9f 195{
b5ee78ae 196 return AddOrIdent(true);
83d89a9f
AL
197}
198 /*}}}*/
b2e465d6 199// DoIdent - Ident a CDROM /*{{{*/
c3ccac92 200static bool DoIdent(CommandLine &)
b2e465d6 201{
b5ee78ae 202 return AddOrIdent(false);
b2e465d6
AL
203}
204 /*}}}*/
83d89a9f 205// ShowHelp - Show the help screen /*{{{*/
c3ccac92 206static bool ShowHelp(CommandLine &)
83d89a9f 207{
249aec3b
JB
208 ioprintf(cout, "%s %s (%s)\n", PACKAGE, PACKAGE_VERSION, COMMON_ARCH);
209
04aa15a8 210 if (_config->FindB("version") == true)
b9179170 211 return true;
b2e465d6
AL
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 234 "See fstab(5)\n";
b9179170 235 return true;
83d89a9f
AL
236}
237 /*}}}*/
92fcbfc1 238int main(int argc,const char *argv[]) /*{{{*/
83d89a9f 239{
83d89a9f
AL
240 CommandLine::Dispatch Cmds[] = {
241 {"add",&DoAdd},
b2e465d6 242 {"ident",&DoIdent},
b9179170 243 {"help",&ShowHelp},
83d89a9f 244 {0,0}};
67111687 245
b9179170
MV
246 std::vector<CommandLine::Args> Args = getCommandArgs("apt-cdrom", CommandLine::GetCommand(Cmds, argc, argv));
247
67111687
AL
248 // Set up gettext support
249 setlocale(LC_ALL,"");
250 textdomain(PACKAGE);
251
83d89a9f 252 // Parse the command line and initialize the package library
ad7e0941
DK
253 CommandLine CmdL;
254 ParseCommandLine(CmdL, Cmds, Args.data(), &_config, &_system, argc, argv, ShowHelp);
a9a5908d 255
d9e518c6
DK
256 InitOutput();
257
83d89a9f 258 // Match the operation
b5ee78ae 259 bool returned = CmdL.DispatchArg(Cmds);
83d89a9f 260
65beb572 261 if (_config->FindI("quiet",0) > 0)
83d89a9f 262 _error->DumpErrors();
65beb572
DK
263 else
264 _error->DumpErrors(GlobalError::DEBUG);
b5ee78ae 265 return returned == true ? 0 : 100;
83d89a9f 266}
92fcbfc1 267 /*}}}*/