]> git.saurik.com Git - apt.git/blob - cmdline/apt-cdrom.cc
fc949ce7b3155ce1e0ed2dd376faa7a87fa5f062
[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 <apt-pkg/cmndline.h>
15 #include <apt-pkg/error.h>
16 #include <apt-pkg/init.h>
17 #include <apt-pkg/fileutl.h>
18 #include <apt-pkg/progress.h>
19 #include <apt-pkg/cdromutl.h>
20 #include <apt-pkg/strutl.h>
21 #include <apt-pkg/acquire.h>
22 #include <apt-pkg/acquire-item.h>
23 #include <apt-pkg/cdrom.h>
24 #include <config.h>
25 #include <apti18n.h>
26
27 //#include "indexcopy.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
41 using namespace std;
42
43 class pkgCdromTextStatus : public pkgCdromStatus /*{{{*/
44 {
45 protected:
46 OpTextProgress Progress;
47 void Prompt(const char *Text);
48 string PromptLine(const char *Text);
49 bool AskCdromName(string &name);
50
51 public:
52 virtual void Update(string text, int current);
53 virtual bool ChangeCdrom();
54 virtual OpProgress* GetOpProgress();
55 };
56
57 void pkgCdromTextStatus::Prompt(const char *Text)
58 {
59 char C;
60 cout << Text << ' ' << flush;
61 read(STDIN_FILENO,&C,1);
62 if (C != '\n')
63 cout << endl;
64 }
65
66 string pkgCdromTextStatus::PromptLine(const char *Text)
67 {
68 cout << Text << ':' << endl;
69
70 string Res;
71 getline(cin,Res);
72 return Res;
73 }
74
75 bool pkgCdromTextStatus::AskCdromName(string &name)
76 {
77 cout << _("Please provide a name for this Disc, such as 'Debian 5.0.3 Disk 1'") << flush;
78 name = PromptLine("");
79
80 return true;
81 }
82
83
84 void pkgCdromTextStatus::Update(string text, int current)
85 {
86 if(text.size() > 0)
87 cout << text << flush;
88 }
89
90 bool pkgCdromTextStatus::ChangeCdrom()
91 {
92 Prompt(_("Please insert a Disc in the drive and press enter"));
93 return true;
94 }
95
96 OpProgress* pkgCdromTextStatus::GetOpProgress()
97 {
98 return &Progress;
99 };
100 /*}}}*/
101 // SetupAutoDetect /*{{{*/
102 bool AutoDetectCdrom(pkgUdevCdromDevices &UdevCdroms, int &i)
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 {
123 MountCdrom("/var/lib/apt/media", v[i].DeviceName);
124 MountCdrom("/var/lib/apt/media", v[i].DeviceName);
125 _config->Set("Acquire::cdrom::mount", "/var/lib/apt/media");
126 _config->Set("APT::CDROM::NoMount", true);
127 }
128 i++;
129
130 return true;
131 }
132 /*}}}*/
133
134 // DoAdd - Add a new CDROM /*{{{*/
135 // ---------------------------------------------------------------------
136 /* This does the main add bit.. We show some status and things. The
137 sequence is to mount/umount the CD, Ident it then scan it for package
138 files and reduce that list. Then we copy over the package files and
139 verify them. Then rewrite the database files */
140 bool DoAdd(CommandLine &)
141 {
142 pkgUdevCdromDevices UdevCdroms;
143 pkgCdromTextStatus log;
144 pkgCdrom cdrom;
145 bool res = false;
146
147 bool AutoDetect = _config->FindB("Acquire::cdrom::AutoDetect");
148 int count = 0;
149
150 if (AutoDetect && UdevCdroms.Dlopen())
151 {
152 while (AutoDetectCdrom(UdevCdroms, count))
153 res &= cdrom.Add(&log);
154 } else {
155 res = cdrom.Add(&log);
156 }
157
158 if(res)
159 cout << _("Repeat this process for the rest of the CDs in your set.") << endl;
160
161 return res;
162 }
163 /*}}}*/
164 // DoIdent - Ident a CDROM /*{{{*/
165 // ---------------------------------------------------------------------
166 /* */
167 bool DoIdent(CommandLine &)
168 {
169 pkgUdevCdromDevices UdevCdroms;
170 string ident;
171 pkgCdromTextStatus log;
172 pkgCdrom cdrom;
173 bool res = true;
174
175 bool AutoDetect = _config->FindB("Acquire::cdrom::AutoDetect");
176 int count = 0;
177
178 if (AutoDetect && UdevCdroms.Dlopen())
179 {
180 while (AutoDetectCdrom(UdevCdroms, count))
181 res &= cdrom.Ident(ident, &log);
182 } else {
183 return cdrom.Ident(ident, &log);
184 }
185
186 return res;
187 }
188 /*}}}*/
189 // ShowHelp - Show the help screen /*{{{*/
190 // ---------------------------------------------------------------------
191 /* */
192 int ShowHelp()
193 {
194 ioprintf(cout,_("%s %s for %s compiled on %s %s\n"),PACKAGE,VERSION,
195 COMMON_ARCH,__DATE__,__TIME__);
196 if (_config->FindB("version") == true)
197 return 0;
198
199 cout <<
200 "Usage: apt-cdrom [options] command\n"
201 "\n"
202 "apt-cdrom is a tool to add CDROM's to APT's source list. The\n"
203 "CDROM mount point and device information is taken from apt.conf\n"
204 "and /etc/fstab.\n"
205 "\n"
206 "Commands:\n"
207 " add - Add a CDROM\n"
208 " ident - Report the identity of a CDROM\n"
209 "\n"
210 "Options:\n"
211 " -h This help text\n"
212 " -d CD-ROM mount point\n"
213 " -r Rename a recognized CD-ROM\n"
214 " -m No mounting\n"
215 " -f Fast mode, don't check package files\n"
216 " -a Thorough scan mode\n"
217 " -c=? Read this configuration file\n"
218 " -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp\n"
219 "See fstab(5)\n";
220 return 0;
221 }
222 /*}}}*/
223 int main(int argc,const char *argv[]) /*{{{*/
224 {
225 CommandLine::Args Args[] = {
226 {'h',"help","help",0},
227 {'a',"auto-detect","Acquire::cdrom::AutoDetect",0},
228 {'v',"version","version",0},
229 {'d',"cdrom","Acquire::cdrom::mount",CommandLine::HasArg},
230 {'r',"rename","APT::CDROM::Rename",0},
231 {'m',"no-mount","APT::CDROM::NoMount",0},
232 {'f',"fast","APT::CDROM::Fast",0},
233 {'n',"just-print","APT::CDROM::NoAct",0},
234 {'n',"recon","APT::CDROM::NoAct",0},
235 {'n',"no-act","APT::CDROM::NoAct",0},
236 {'a',"thorough","APT::CDROM::Thorough",0},
237 {'c',"config-file",0,CommandLine::ConfigFile},
238 {'o',"option",0,CommandLine::ArbItem},
239 {0,0,0,0}};
240 CommandLine::Dispatch Cmds[] = {
241 {"add",&DoAdd},
242 {"ident",&DoIdent},
243 {0,0}};
244
245 // Set up gettext support
246 setlocale(LC_ALL,"");
247 textdomain(PACKAGE);
248
249 // Parse the command line and initialize the package library
250 CommandLine CmdL(Args,_config);
251 if (pkgInitConfig(*_config) == false ||
252 CmdL.Parse(argc,argv) == false ||
253 pkgInitSystem(*_config,_system) == false)
254 {
255 _error->DumpErrors();
256 return 100;
257 }
258
259 // See if the help should be shown
260 if (_config->FindB("help") == true || _config->FindB("version") == true ||
261 CmdL.FileSize() == 0)
262 return ShowHelp();
263
264 // Deal with stdout not being a tty
265 if (isatty(STDOUT_FILENO) && _config->FindI("quiet",0) < 1)
266 _config->Set("quiet","1");
267
268 // Match the operation
269 CmdL.DispatchArg(Cmds);
270
271 // Print any errors or warnings found during parsing
272 if (_error->empty() == false)
273 {
274 bool Errors = _error->PendingError();
275 _error->DumpErrors();
276 return Errors == true?100:0;
277 }
278
279 return 0;
280 }
281 /*}}}*/