]> git.saurik.com Git - apt.git/blob - cmdline/apt-cdrom.cc
* apt-pkg/aptconfiguration.cc:
[apt.git] / cmdline / apt-cdrom.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: apt-cdrom.cc,v 1.45 2003/11/19 23:50:51 mdz Exp $
4 /* ######################################################################
5
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.
10
11 ##################################################################### */
12 /*}}}*/
13 // Include Files /*{{{*/
14 #include<config.h>
15
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>
28
29 #include <locale.h>
30 #include <iostream>
31 #include <fstream>
32 #include <vector>
33 #include <algorithm>
34 #include <sys/stat.h>
35 #include <fcntl.h>
36 #include <dirent.h>
37 #include <unistd.h>
38 #include <stdio.h>
39
40 #include <apti18n.h>
41 /*}}}*/
42
43 using namespace std;
44
45 class pkgCdromTextStatus : public pkgCdromStatus /*{{{*/
46 {
47 protected:
48 OpTextProgress Progress;
49 void Prompt(const char *Text);
50 string PromptLine(const char *Text);
51 bool AskCdromName(string &name);
52
53 public:
54 virtual void Update(string text, int current);
55 virtual bool ChangeCdrom();
56 virtual OpProgress* GetOpProgress();
57 };
58
59 void pkgCdromTextStatus::Prompt(const char *Text)
60 {
61 char C;
62 cout << Text << ' ' << flush;
63 read(STDIN_FILENO,&C,1);
64 if (C != '\n')
65 cout << endl;
66 }
67
68 string pkgCdromTextStatus::PromptLine(const char *Text)
69 {
70 cout << Text << ':' << endl;
71
72 string Res;
73 getline(cin,Res);
74 return Res;
75 }
76
77 bool pkgCdromTextStatus::AskCdromName(string &name)
78 {
79 cout << _("Please provide a name for this Disc, such as 'Debian 5.0.3 Disk 1'") << flush;
80 name = PromptLine("");
81
82 return true;
83 }
84
85
86 void pkgCdromTextStatus::Update(string text, int current)
87 {
88 if(text.size() > 0)
89 cout << text << flush;
90 }
91
92 bool pkgCdromTextStatus::ChangeCdrom()
93 {
94 Prompt(_("Please insert a Disc in the drive and press enter"));
95 return true;
96 }
97
98 OpProgress* pkgCdromTextStatus::GetOpProgress()
99 {
100 return &Progress;
101 };
102 /*}}}*/
103 // SetupAutoDetect /*{{{*/
104 bool AutoDetectCdrom(pkgUdevCdromDevices &UdevCdroms, unsigned int &i)
105 {
106 bool Debug = _config->FindB("Debug::Acquire::cdrom", false);
107
108 vector<struct CdromDevice> v = UdevCdroms.Scan();
109 if (i >= v.size())
110 return false;
111
112 if (Debug)
113 clog << "Looking at devce " << i
114 << " DeviveName: " << v[i].DeviceName
115 << " IsMounted: '" << v[i].Mounted << "'"
116 << " MountPoint: '" << v[i].MountPath << "'"
117 << endl;
118
119 if (v[i].Mounted)
120 {
121 // set the right options
122 _config->Set("Acquire::cdrom::mount", v[i].MountPath);
123 _config->Set("APT::CDROM::NoMount", true);
124 } else {
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);
132 }
133 i++;
134
135 return true;
136 }
137 /*}}}*/
138
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 &)
146 {
147 pkgUdevCdromDevices UdevCdroms;
148 pkgCdromTextStatus log;
149 pkgCdrom cdrom;
150 bool res = true;
151
152 bool AutoDetect = _config->FindB("Acquire::cdrom::AutoDetect", true);
153 unsigned int count = 0;
154
155 if (AutoDetect && UdevCdroms.Dlopen())
156 {
157 while (AutoDetectCdrom(UdevCdroms, count))
158 res &= cdrom.Add(&log);
159 } else {
160 res = cdrom.Add(&log);
161 }
162
163 if(res)
164 cout << _("Repeat this process for the rest of the CDs in your set.") << endl;
165
166 return res;
167 }
168 /*}}}*/
169 // DoIdent - Ident a CDROM /*{{{*/
170 // ---------------------------------------------------------------------
171 /* */
172 bool DoIdent(CommandLine &)
173 {
174 pkgUdevCdromDevices UdevCdroms;
175 string ident;
176 pkgCdromTextStatus log;
177 pkgCdrom cdrom;
178 bool res = true;
179
180 bool AutoDetect = _config->FindB("Acquire::cdrom::AutoDetect");
181 unsigned int count = 0;
182
183 if (AutoDetect && UdevCdroms.Dlopen())
184 {
185 while (AutoDetectCdrom(UdevCdroms, count))
186 res &= cdrom.Ident(ident, &log);
187 } else {
188 return cdrom.Ident(ident, &log);
189 }
190
191 return res;
192 }
193 /*}}}*/
194 // ShowHelp - Show the help screen /*{{{*/
195 // ---------------------------------------------------------------------
196 /* */
197 int ShowHelp()
198 {
199 ioprintf(cout,_("%s %s for %s compiled on %s %s\n"),PACKAGE,VERSION,
200 COMMON_ARCH,__DATE__,__TIME__);
201 if (_config->FindB("version") == true)
202 return 0;
203
204 cout <<
205 "Usage: apt-cdrom [options] command\n"
206 "\n"
207 "apt-cdrom is a tool to add CDROM's to APT's source list. The\n"
208 "CDROM mount point and device information is taken from apt.conf\n"
209 "and /etc/fstab.\n"
210 "\n"
211 "Commands:\n"
212 " add - Add a CDROM\n"
213 " ident - Report the identity of a CDROM\n"
214 "\n"
215 "Options:\n"
216 " -h This help text\n"
217 " -d CD-ROM mount point\n"
218 " -r Rename a recognized CD-ROM\n"
219 " -m No mounting\n"
220 " -f Fast mode, don't check package files\n"
221 " -a Thorough scan mode\n"
222 " --auto-detect Auto detect drive and mount point\n"
223 " -c=? Read this configuration file\n"
224 " -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp\n"
225 "See fstab(5)\n";
226 return 0;
227 }
228 /*}}}*/
229 int main(int argc,const char *argv[]) /*{{{*/
230 {
231 CommandLine::Args Args[] = {
232 {'h',"help","help",0},
233 { 0,"auto-detect","Acquire::cdrom::AutoDetect",0},
234 {'v',"version","version",0},
235 {'d',"cdrom","Acquire::cdrom::mount",CommandLine::HasArg},
236 {'r',"rename","APT::CDROM::Rename",0},
237 {'m',"no-mount","APT::CDROM::NoMount",0},
238 {'f',"fast","APT::CDROM::Fast",0},
239 {'n',"just-print","APT::CDROM::NoAct",0},
240 {'n',"recon","APT::CDROM::NoAct",0},
241 {'n',"no-act","APT::CDROM::NoAct",0},
242 {'a',"thorough","APT::CDROM::Thorough",0},
243 {'c',"config-file",0,CommandLine::ConfigFile},
244 {'o',"option",0,CommandLine::ArbItem},
245 {0,0,0,0}};
246 CommandLine::Dispatch Cmds[] = {
247 {"add",&DoAdd},
248 {"ident",&DoIdent},
249 {0,0}};
250
251 // Set up gettext support
252 setlocale(LC_ALL,"");
253 textdomain(PACKAGE);
254
255 // Parse the command line and initialize the package library
256 CommandLine CmdL(Args,_config);
257 if (pkgInitConfig(*_config) == false ||
258 CmdL.Parse(argc,argv) == false ||
259 pkgInitSystem(*_config,_system) == false)
260 {
261 _error->DumpErrors();
262 return 100;
263 }
264
265 // See if the help should be shown
266 if (_config->FindB("help") == true || _config->FindB("version") == true ||
267 CmdL.FileSize() == 0)
268 return ShowHelp();
269
270 // Deal with stdout not being a tty
271 if (isatty(STDOUT_FILENO) && _config->FindI("quiet", -1) == -1)
272 _config->Set("quiet","1");
273
274 // Match the operation
275 CmdL.DispatchArg(Cmds);
276
277 // Print any errors or warnings found during parsing
278 bool const Errors = _error->PendingError();
279 if (_config->FindI("quiet",0) > 0)
280 _error->DumpErrors();
281 else
282 _error->DumpErrors(GlobalError::DEBUG);
283 return Errors == true ? 100 : 0;
284 }
285 /*}}}*/