]> git.saurik.com Git - apt.git/blame - cmdline/apt-cdrom.cc
prefer the Policy if it is built instead of the DepCache and
[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 /*{{{*/
14#include <apt-pkg/cmndline.h>
15#include <apt-pkg/error.h>
16#include <apt-pkg/init.h>
83d89a9f
AL
17#include <apt-pkg/fileutl.h>
18#include <apt-pkg/progress.h>
65ae8fab 19#include <apt-pkg/cdromutl.h>
cdcc6d34 20#include <apt-pkg/strutl.h>
a75c6a6e
MZ
21#include <apt-pkg/acquire.h>
22#include <apt-pkg/acquire-item.h>
23#include <apt-pkg/cdrom.h>
83d89a9f 24#include <config.h>
b2e465d6
AL
25#include <apti18n.h>
26
a75c6a6e 27//#include "indexcopy.h"
143abaeb 28
233c2b66 29#include <locale.h>
83d89a9f 30#include <iostream>
18444708 31#include <fstream>
83d89a9f
AL
32#include <vector>
33#include <algorithm>
83d89a9f
AL
34#include <sys/stat.h>
35#include <fcntl.h>
36#include <dirent.h>
37#include <unistd.h>
38#include <stdio.h>
39 /*}}}*/
40
076d01b0
AL
41using namespace std;
42
92fcbfc1 43class pkgCdromTextStatus : public pkgCdromStatus /*{{{*/
83d89a9f 44{
a75c6a6e
MZ
45protected:
46 OpTextProgress Progress;
47 void Prompt(const char *Text);
48 string PromptLine(const char *Text);
49 bool AskCdromName(string &name);
50
51public:
52 virtual void Update(string text, int current);
53 virtual bool ChangeCdrom();
54 virtual OpProgress* GetOpProgress();
8a579291 55};
83d89a9f 56
a75c6a6e 57void pkgCdromTextStatus::Prompt(const char *Text)
83d89a9f 58{
a75c6a6e
MZ
59 char C;
60 cout << Text << ' ' << flush;
61 read(STDIN_FILENO,&C,1);
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;
83d89a9f 69
a75c6a6e
MZ
70 string Res;
71 getline(cin,Res);
83d89a9f
AL
72 return Res;
73}
18444708 74
a75c6a6e 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("");
83d89a9f 79
83d89a9f
AL
80 return true;
81}
83d89a9f 82
83d89a9f 83
a75c6a6e 84void pkgCdromTextStatus::Update(string text, int current)
18444708 85{
a75c6a6e
MZ
86 if(text.size() > 0)
87 cout << text << flush;
18444708 88}
4dfaa253 89
8a579291 90bool pkgCdromTextStatus::ChangeCdrom()
83d89a9f 91{
4a5e5089 92 Prompt(_("Please insert a Disc in the drive and press enter"));
18444708
AL
93 return true;
94}
83d89a9f 95
8a579291 96OpProgress* pkgCdromTextStatus::GetOpProgress()
a75c6a6e
MZ
97{
98 return &Progress;
99};
83d89a9f 100 /*}}}*/
169413dc 101// SetupAutoDetect /*{{{*/
fb503892 102bool AutoDetectCdrom(pkgUdevCdromDevices &UdevCdroms, unsigned int &i)
169413dc
MV
103{
104 bool Debug = _config->FindB("Debug::Acquire::cdrom", false);
105
106 vector<struct CdromDevice> v = UdevCdroms.Scan();
107 if (i >= v.size())
108 return false;
109
110 if (Debug)
111 clog << "Looking at devce " << i
112 << " DeviveName: " << v[i].DeviceName
113 << " IsMounted: '" << v[i].Mounted << "'"
114 << " MountPoint: '" << v[i].MountPath << "'"
115 << endl;
116
117 if (v[i].Mounted)
118 {
119 // set the right options
120 _config->Set("Acquire::cdrom::mount", v[i].MountPath);
121 _config->Set("APT::CDROM::NoMount", true);
122 } else {
ffee221b 123 string AptMountPoint = _config->FindDir("Dir::Media::MountPath");
fb503892 124 if (!FileExists(AptMountPoint))
ffee221b 125 mkdir(AptMountPoint.c_str(), 0750);
fb503892 126 if(MountCdrom(AptMountPoint, v[i].DeviceName) == false)
ffee221b 127 _error->Warning(_("Failed to mount '%s' to '%s'"), v[i].DeviceName.c_str(), AptMountPoint.c_str());
fb503892 128 _config->Set("Acquire::cdrom::mount", AptMountPoint);
169413dc
MV
129 _config->Set("APT::CDROM::NoMount", true);
130 }
131 i++;
132
133 return true;
134}
135 /*}}}*/
136
83d89a9f
AL
137// DoAdd - Add a new CDROM /*{{{*/
138// ---------------------------------------------------------------------
4dfaa253
AL
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 */
83d89a9f
AL
143bool DoAdd(CommandLine &)
144{
169413dc 145 pkgUdevCdromDevices UdevCdroms;
a75c6a6e
MZ
146 pkgCdromTextStatus log;
147 pkgCdrom cdrom;
fb503892 148 bool res = true;
169413dc 149
4df70e75 150 bool AutoDetect = _config->FindB("Acquire::cdrom::AutoDetect", true);
fb503892 151 unsigned int count = 0;
169413dc
MV
152
153 if (AutoDetect && UdevCdroms.Dlopen())
154 {
155 while (AutoDetectCdrom(UdevCdroms, count))
156 res &= cdrom.Add(&log);
157 } else {
158 res = cdrom.Add(&log);
159 }
160
a75c6a6e 161 if(res)
4a5e5089 162 cout << _("Repeat this process for the rest of the CDs in your set.") << endl;
169413dc 163
a75c6a6e 164 return res;
83d89a9f
AL
165}
166 /*}}}*/
b2e465d6
AL
167// DoIdent - Ident a CDROM /*{{{*/
168// ---------------------------------------------------------------------
169/* */
170bool DoIdent(CommandLine &)
171{
169413dc 172 pkgUdevCdromDevices UdevCdroms;
a75c6a6e
MZ
173 string ident;
174 pkgCdromTextStatus log;
175 pkgCdrom cdrom;
169413dc
MV
176 bool res = true;
177
178 bool AutoDetect = _config->FindB("Acquire::cdrom::AutoDetect");
fb503892 179 unsigned int count = 0;
169413dc
MV
180
181 if (AutoDetect && UdevCdroms.Dlopen())
182 {
183 while (AutoDetectCdrom(UdevCdroms, count))
184 res &= cdrom.Ident(ident, &log);
185 } else {
186 return cdrom.Ident(ident, &log);
187 }
188
189 return res;
b2e465d6
AL
190}
191 /*}}}*/
83d89a9f
AL
192// ShowHelp - Show the help screen /*{{{*/
193// ---------------------------------------------------------------------
194/* */
195int ShowHelp()
196{
5b28c804
OS
197 ioprintf(cout,_("%s %s for %s compiled on %s %s\n"),PACKAGE,VERSION,
198 COMMON_ARCH,__DATE__,__TIME__);
04aa15a8 199 if (_config->FindB("version") == true)
b2e465d6
AL
200 return 0;
201
202 cout <<
203 "Usage: apt-cdrom [options] command\n"
204 "\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"
207 "and /etc/fstab.\n"
208 "\n"
209 "Commands:\n"
210 " add - Add a CDROM\n"
211 " ident - Report the identity of a CDROM\n"
212 "\n"
213 "Options:\n"
214 " -h This help text\n"
215 " -d CD-ROM mount point\n"
216 " -r Rename a recognized CD-ROM\n"
217 " -m No mounting\n"
218 " -f Fast mode, don't check package files\n"
219 " -a Thorough scan mode\n"
470221bb 220 " --auto-detect Auto detect drive and mount point\n"
b2e465d6 221 " -c=? Read this configuration file\n"
a2884e32 222 " -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp\n"
b2e465d6
AL
223 "See fstab(5)\n";
224 return 0;
83d89a9f
AL
225}
226 /*}}}*/
92fcbfc1 227int main(int argc,const char *argv[]) /*{{{*/
83d89a9f
AL
228{
229 CommandLine::Args Args[] = {
230 {'h',"help","help",0},
470221bb 231 { 0,"auto-detect","Acquire::cdrom::AutoDetect",0},
04aa15a8 232 {'v',"version","version",0},
83d89a9f
AL
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},
c60d151b 237 {'n',"just-print","APT::CDROM::NoAct",0},
18444708 238 {'n',"recon","APT::CDROM::NoAct",0},
c60d151b
AL
239 {'n',"no-act","APT::CDROM::NoAct",0},
240 {'a',"thorough","APT::CDROM::Thorough",0},
83d89a9f
AL
241 {'c',"config-file",0,CommandLine::ConfigFile},
242 {'o',"option",0,CommandLine::ArbItem},
243 {0,0,0,0}};
244 CommandLine::Dispatch Cmds[] = {
245 {"add",&DoAdd},
b2e465d6 246 {"ident",&DoIdent},
83d89a9f 247 {0,0}};
67111687
AL
248
249 // Set up gettext support
250 setlocale(LC_ALL,"");
251 textdomain(PACKAGE);
252
83d89a9f
AL
253 // Parse the command line and initialize the package library
254 CommandLine CmdL(Args,_config);
b2e465d6
AL
255 if (pkgInitConfig(*_config) == false ||
256 CmdL.Parse(argc,argv) == false ||
257 pkgInitSystem(*_config,_system) == false)
83d89a9f
AL
258 {
259 _error->DumpErrors();
260 return 100;
261 }
262
263 // See if the help should be shown
5633a7c2 264 if (_config->FindB("help") == true || _config->FindB("version") == true ||
83d89a9f
AL
265 CmdL.FileSize() == 0)
266 return ShowHelp();
a9a5908d
AL
267
268 // Deal with stdout not being a tty
c340d185 269 if (isatty(STDOUT_FILENO) && _config->FindI("quiet", -1) == -1)
a9a5908d 270 _config->Set("quiet","1");
83d89a9f
AL
271
272 // Match the operation
273 CmdL.DispatchArg(Cmds);
274
275 // Print any errors or warnings found during parsing
65beb572
DK
276 bool const Errors = _error->PendingError();
277 if (_config->FindI("quiet",0) > 0)
83d89a9f 278 _error->DumpErrors();
65beb572
DK
279 else
280 _error->DumpErrors(GlobalError::DEBUG);
281 return Errors == true ? 100 : 0;
83d89a9f 282}
92fcbfc1 283 /*}}}*/