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