]> git.saurik.com Git - apt.git/blame - cmdline/apt-cdrom.cc
release 1.0.9.8
[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
MV
33#include <apt-private/private-cmndline.h>
34
ea542140 35#include <apti18n.h>
83d89a9f
AL
36 /*}}}*/
37
076d01b0
AL
38using namespace std;
39
92fcbfc1 40class pkgCdromTextStatus : public pkgCdromStatus /*{{{*/
83d89a9f 41{
a75c6a6e
MZ
42protected:
43 OpTextProgress Progress;
d3e8fbb3 44 void Prompt(const char *Text);
a75c6a6e
MZ
45 string PromptLine(const char *Text);
46 bool AskCdromName(string &name);
47
48public:
49 virtual void Update(string text, int current);
50 virtual bool ChangeCdrom();
51 virtual OpProgress* GetOpProgress();
8a579291 52};
83d89a9f 53
d3e8fbb3 54void pkgCdromTextStatus::Prompt(const char *Text)
83d89a9f 55{
a75c6a6e
MZ
56 char C;
57 cout << Text << ' ' << flush;
f52037d6 58 if (read(STDIN_FILENO,&C,1) < 0)
d3e8fbb3 59 _error->Errno("pkgCdromTextStatus::Prompt",
7dd5854b 60 "Failed to read from standard input (not a terminal?)");
a75c6a6e
MZ
61 if (C != '\n')
62 cout << endl;
83d89a9f 63}
4dfaa253 64
a75c6a6e 65string pkgCdromTextStatus::PromptLine(const char *Text)
83d89a9f 66{
a75c6a6e 67 cout << Text << ':' << endl;
d3e8fbb3 68
a75c6a6e
MZ
69 string Res;
70 getline(cin,Res);
83d89a9f
AL
71 return Res;
72}
18444708 73
d3e8fbb3 74bool pkgCdromTextStatus::AskCdromName(string &name)
83d89a9f 75{
cdd5a135 76 cout << _("Please provide a name for this Disc, such as 'Debian 5.0.3 Disk 1'") << flush;
a75c6a6e 77 name = PromptLine("");
d3e8fbb3 78
83d89a9f
AL
79 return true;
80}
83d89a9f 81
d3e8fbb3 82
65512241 83void pkgCdromTextStatus::Update(string text, int /*current*/)
18444708 84{
a75c6a6e
MZ
85 if(text.size() > 0)
86 cout << text << flush;
18444708 87}
4dfaa253 88
d3e8fbb3 89bool pkgCdromTextStatus::ChangeCdrom()
83d89a9f 90{
4a5e5089 91 Prompt(_("Please insert a Disc in the drive and press enter"));
18444708
AL
92 return true;
93}
83d89a9f 94
a02db58f 95APT_CONST OpProgress* pkgCdromTextStatus::GetOpProgress()
d3e8fbb3
DK
96{
97 return &Progress;
98}
83d89a9f 99 /*}}}*/
b5ee78ae
DK
100// AddOrIdent - Add or Ident a CDROM /*{{{*/
101static bool AddOrIdent(bool const Add)
169413dc 102{
b5ee78ae
DK
103 pkgUdevCdromDevices UdevCdroms;
104 pkgCdromTextStatus log;
105 pkgCdrom cdrom;
169413dc 106
b5ee78ae
DK
107 bool oneSuccessful = false;
108 bool AutoDetect = _config->FindB("Acquire::cdrom::AutoDetect", true);
109 if (AutoDetect == true && UdevCdroms.Dlopen() == true)
110 {
111 bool const Debug = _config->FindB("Debug::Acquire::cdrom", false);
112 std::string const CDMount = _config->Find("Acquire::cdrom::mount");
113 bool const NoMount = _config->FindB("APT::CDROM::NoMount", false);
114 if (NoMount == false)
115 _config->Set("APT::CDROM::NoMount", true);
116
117 vector<struct CdromDevice> const v = UdevCdroms.Scan();
118 for (std::vector<struct CdromDevice>::const_iterator cd = v.begin(); cd != v.end(); ++cd)
119 {
120 if (Debug)
121 clog << "Looking at device:"
122 << "\tDeviveName: '" << cd->DeviceName << "'"
123 << "\tIsMounted: '" << cd->Mounted << "'"
124 << "\tMountPoint: '" << cd->MountPath << "'"
125 << endl;
126
127 std::string AptMountPoint;
128 if (cd->Mounted)
129 _config->Set("Acquire::cdrom::mount", cd->MountPath);
130 else if (NoMount == true)
131 continue;
132 else
133 {
134 AptMountPoint = _config->FindDir("Dir::Media::MountPath");
135 if (FileExists(AptMountPoint) == false)
136 mkdir(AptMountPoint.c_str(), 0750);
137 if(MountCdrom(AptMountPoint, cd->DeviceName) == false)
138 {
139 _error->Warning(_("Failed to mount '%s' to '%s'"), cd->DeviceName.c_str(), AptMountPoint.c_str());
140 continue;
141 }
142 _config->Set("Acquire::cdrom::mount", AptMountPoint);
143 }
62dcbf84 144
b5ee78ae
DK
145 _error->PushToStack();
146 if (Add == true)
147 oneSuccessful = cdrom.Add(&log);
148 else
149 {
150 std::string id;
151 oneSuccessful = cdrom.Ident(id, &log);
152 }
153 _error->MergeWithStack();
169413dc 154
b5ee78ae
DK
155 if (AptMountPoint.empty() == false)
156 UnmountCdrom(AptMountPoint);
157 }
158 if (NoMount == false)
159 _config->Set("APT::CDROM::NoMount", NoMount);
160 _config->Set("Acquire::cdrom::mount", CDMount);
161 }
169413dc 162
b5ee78ae
DK
163 // fallback if auto-detect didn't work
164 if (oneSuccessful == false)
169413dc 165 {
b5ee78ae
DK
166 _error->PushToStack();
167 if (Add == true)
168 oneSuccessful = cdrom.Add(&log);
62dcbf84 169 else
b5ee78ae
DK
170 {
171 std::string id;
172 oneSuccessful = cdrom.Ident(id, &log);
173 }
174 _error->MergeWithStack();
169413dc 175 }
169413dc 176
b5ee78ae
DK
177 if (oneSuccessful == false)
178 _error->Error("%s", _("No CD-ROM could be auto-detected or found using the default mount point.\n"
179 "You may try the --cdrom option to set the CD-ROM mount point.\n"
180 "See 'man apt-cdrom' for more information about the CD-ROM auto-detection and mount point."));
181 else if (Add == true)
182 cout << _("Repeat this process for the rest of the CDs in your set.") << endl;
183
184 return oneSuccessful;
169413dc
MV
185}
186 /*}}}*/
83d89a9f
AL
187// DoAdd - Add a new CDROM /*{{{*/
188// ---------------------------------------------------------------------
4dfaa253 189/* This does the main add bit.. We show some status and things. The
b5ee78ae 190 sequence is to mount/umount the CD, Ident it then scan it for package
4dfaa253
AL
191 files and reduce that list. Then we copy over the package files and
192 verify them. Then rewrite the database files */
c3ccac92 193static bool DoAdd(CommandLine &)
83d89a9f 194{
b5ee78ae 195 return AddOrIdent(true);
83d89a9f
AL
196}
197 /*}}}*/
b2e465d6 198// DoIdent - Ident a CDROM /*{{{*/
c3ccac92 199static bool DoIdent(CommandLine &)
b2e465d6 200{
b5ee78ae 201 return AddOrIdent(false);
b2e465d6
AL
202}
203 /*}}}*/
83d89a9f 204// ShowHelp - Show the help screen /*{{{*/
c3ccac92 205static bool ShowHelp(CommandLine &)
83d89a9f 206{
9179f697 207 ioprintf(cout,_("%s %s for %s compiled on %s %s\n"),PACKAGE,PACKAGE_VERSION,
5b28c804 208 COMMON_ARCH,__DATE__,__TIME__);
04aa15a8 209 if (_config->FindB("version") == true)
b9179170 210 return true;
b2e465d6
AL
211
212 cout <<
213 "Usage: apt-cdrom [options] command\n"
214 "\n"
215 "apt-cdrom is a tool to add CDROM's to APT's source list. The\n"
216 "CDROM mount point and device information is taken from apt.conf\n"
217 "and /etc/fstab.\n"
218 "\n"
219 "Commands:\n"
220 " add - Add a CDROM\n"
221 " ident - Report the identity of a CDROM\n"
222 "\n"
223 "Options:\n"
224 " -h This help text\n"
225 " -d CD-ROM mount point\n"
226 " -r Rename a recognized CD-ROM\n"
227 " -m No mounting\n"
228 " -f Fast mode, don't check package files\n"
229 " -a Thorough scan mode\n"
0a83556b 230 " --no-auto-detect Do not try to auto detect drive and mount point\n"
b2e465d6 231 " -c=? Read this configuration file\n"
a2884e32 232 " -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp\n"
b2e465d6 233 "See fstab(5)\n";
b9179170 234 return true;
83d89a9f
AL
235}
236 /*}}}*/
92fcbfc1 237int main(int argc,const char *argv[]) /*{{{*/
83d89a9f 238{
83d89a9f
AL
239 CommandLine::Dispatch Cmds[] = {
240 {"add",&DoAdd},
b2e465d6 241 {"ident",&DoIdent},
b9179170 242 {"help",&ShowHelp},
83d89a9f 243 {0,0}};
67111687 244
b9179170
MV
245 std::vector<CommandLine::Args> Args = getCommandArgs("apt-cdrom", CommandLine::GetCommand(Cmds, argc, argv));
246
67111687
AL
247 // Set up gettext support
248 setlocale(LC_ALL,"");
249 textdomain(PACKAGE);
250
83d89a9f 251 // Parse the command line and initialize the package library
b9179170 252 CommandLine CmdL(Args.data(),_config);
b2e465d6
AL
253 if (pkgInitConfig(*_config) == false ||
254 CmdL.Parse(argc,argv) == false ||
255 pkgInitSystem(*_config,_system) == false)
83d89a9f
AL
256 {
257 _error->DumpErrors();
258 return 100;
259 }
260
261 // See if the help should be shown
5633a7c2 262 if (_config->FindB("help") == true || _config->FindB("version") == true ||
83d89a9f 263 CmdL.FileSize() == 0)
b9179170 264 return ShowHelp(CmdL);
a9a5908d
AL
265
266 // Deal with stdout not being a tty
c340d185 267 if (isatty(STDOUT_FILENO) && _config->FindI("quiet", -1) == -1)
a9a5908d 268 _config->Set("quiet","1");
83d89a9f
AL
269
270 // Match the operation
b5ee78ae 271 bool returned = CmdL.DispatchArg(Cmds);
83d89a9f 272
65beb572 273 if (_config->FindI("quiet",0) > 0)
83d89a9f 274 _error->DumpErrors();
65beb572
DK
275 else
276 _error->DumpErrors(GlobalError::DEBUG);
b5ee78ae 277 return returned == true ? 0 : 100;
83d89a9f 278}
92fcbfc1 279 /*}}}*/