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