]>
Commit | Line | Data |
---|---|---|
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 /*{{{*/ | |
ea542140 DK |
14 | #include<config.h> |
15 | ||
83d89a9f AL |
16 | #include <apt-pkg/cmndline.h> |
17 | #include <apt-pkg/error.h> | |
18 | #include <apt-pkg/init.h> | |
83d89a9f AL |
19 | #include <apt-pkg/fileutl.h> |
20 | #include <apt-pkg/progress.h> | |
65ae8fab | 21 | #include <apt-pkg/cdromutl.h> |
cdcc6d34 | 22 | #include <apt-pkg/strutl.h> |
a75c6a6e | 23 | #include <apt-pkg/cdrom.h> |
472ff00e DK |
24 | #include <apt-pkg/configuration.h> |
25 | #include <apt-pkg/pkgsystem.h> | |
143abaeb | 26 | |
83d89a9f AL |
27 | #include <iostream> |
28 | #include <vector> | |
453b82a3 | 29 | #include <string> |
83d89a9f | 30 | #include <sys/stat.h> |
83d89a9f | 31 | #include <unistd.h> |
ea542140 | 32 | |
b9179170 MV |
33 | #include <apt-private/private-cmndline.h> |
34 | ||
ea542140 | 35 | #include <apti18n.h> |
83d89a9f | 36 | /*}}}*/ |
0a83556b MV |
37 | static const char *W_NO_CDROM_FOUND = \ |
38 | N_("No CD-ROM could be auto-detected or found using " | |
9d0c9e70 MV |
39 | "the default mount point.\n" |
40 | "You may try the --cdrom option to set the CD-ROM mount point. " | |
0a83556b | 41 | "See 'man apt-cdrom' for more " |
9d0c9e70 | 42 | "information about the CD-ROM auto-detection and mount point."); |
83d89a9f | 43 | |
076d01b0 AL |
44 | using namespace std; |
45 | ||
92fcbfc1 | 46 | class pkgCdromTextStatus : public pkgCdromStatus /*{{{*/ |
83d89a9f | 47 | { |
a75c6a6e MZ |
48 | protected: |
49 | OpTextProgress Progress; | |
d3e8fbb3 | 50 | void Prompt(const char *Text); |
a75c6a6e MZ |
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(); | |
8a579291 | 58 | }; |
83d89a9f | 59 | |
d3e8fbb3 | 60 | void pkgCdromTextStatus::Prompt(const char *Text) |
83d89a9f | 61 | { |
a75c6a6e MZ |
62 | char C; |
63 | cout << Text << ' ' << flush; | |
f52037d6 | 64 | if (read(STDIN_FILENO,&C,1) < 0) |
d3e8fbb3 | 65 | _error->Errno("pkgCdromTextStatus::Prompt", |
7dd5854b | 66 | "Failed to read from standard input (not a terminal?)"); |
a75c6a6e MZ |
67 | if (C != '\n') |
68 | cout << endl; | |
83d89a9f | 69 | } |
4dfaa253 | 70 | |
a75c6a6e | 71 | string pkgCdromTextStatus::PromptLine(const char *Text) |
83d89a9f | 72 | { |
a75c6a6e | 73 | cout << Text << ':' << endl; |
d3e8fbb3 | 74 | |
a75c6a6e MZ |
75 | string Res; |
76 | getline(cin,Res); | |
83d89a9f AL |
77 | return Res; |
78 | } | |
18444708 | 79 | |
d3e8fbb3 | 80 | bool pkgCdromTextStatus::AskCdromName(string &name) |
83d89a9f | 81 | { |
cdd5a135 | 82 | cout << _("Please provide a name for this Disc, such as 'Debian 5.0.3 Disk 1'") << flush; |
a75c6a6e | 83 | name = PromptLine(""); |
d3e8fbb3 | 84 | |
83d89a9f AL |
85 | return true; |
86 | } | |
83d89a9f | 87 | |
d3e8fbb3 | 88 | |
65512241 | 89 | void pkgCdromTextStatus::Update(string text, int /*current*/) |
18444708 | 90 | { |
a75c6a6e MZ |
91 | if(text.size() > 0) |
92 | cout << text << flush; | |
18444708 | 93 | } |
4dfaa253 | 94 | |
d3e8fbb3 | 95 | bool pkgCdromTextStatus::ChangeCdrom() |
83d89a9f | 96 | { |
4a5e5089 | 97 | Prompt(_("Please insert a Disc in the drive and press enter")); |
18444708 AL |
98 | return true; |
99 | } | |
83d89a9f | 100 | |
a02db58f | 101 | APT_CONST OpProgress* pkgCdromTextStatus::GetOpProgress() |
d3e8fbb3 DK |
102 | { |
103 | return &Progress; | |
104 | } | |
83d89a9f | 105 | /*}}}*/ |
169413dc | 106 | // SetupAutoDetect /*{{{*/ |
c3ccac92 | 107 | static bool AutoDetectCdrom(pkgUdevCdromDevices &UdevCdroms, unsigned int &i, bool &automounted) |
169413dc MV |
108 | { |
109 | bool Debug = _config->FindB("Debug::Acquire::cdrom", false); | |
110 | ||
62dcbf84 JO |
111 | automounted = false; |
112 | ||
169413dc MV |
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 { | |
ffee221b | 130 | string AptMountPoint = _config->FindDir("Dir::Media::MountPath"); |
fb503892 | 131 | if (!FileExists(AptMountPoint)) |
ffee221b | 132 | mkdir(AptMountPoint.c_str(), 0750); |
fb503892 | 133 | if(MountCdrom(AptMountPoint, v[i].DeviceName) == false) |
ffee221b | 134 | _error->Warning(_("Failed to mount '%s' to '%s'"), v[i].DeviceName.c_str(), AptMountPoint.c_str()); |
62dcbf84 JO |
135 | else |
136 | automounted = true; | |
fb503892 | 137 | _config->Set("Acquire::cdrom::mount", AptMountPoint); |
169413dc MV |
138 | _config->Set("APT::CDROM::NoMount", true); |
139 | } | |
140 | i++; | |
141 | ||
142 | return true; | |
143 | } | |
144 | /*}}}*/ | |
83d89a9f AL |
145 | // DoAdd - Add a new CDROM /*{{{*/ |
146 | // --------------------------------------------------------------------- | |
4dfaa253 AL |
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 */ | |
c3ccac92 | 151 | static bool DoAdd(CommandLine &) |
83d89a9f | 152 | { |
169413dc | 153 | pkgUdevCdromDevices UdevCdroms; |
a75c6a6e MZ |
154 | pkgCdromTextStatus log; |
155 | pkgCdrom cdrom; | |
fb503892 | 156 | bool res = true; |
169413dc | 157 | |
4df70e75 | 158 | bool AutoDetect = _config->FindB("Acquire::cdrom::AutoDetect", true); |
2ecf54d8 | 159 | unsigned int count = 0; |
62dcbf84 JO |
160 | string AptMountPoint = _config->FindDir("Dir::Media::MountPath"); |
161 | bool automounted = false; | |
169413dc | 162 | if (AutoDetect && UdevCdroms.Dlopen()) |
62dcbf84 JO |
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(); | |
0a83556b | 181 | } |
169413dc | 182 | |
62dcbf84 JO |
183 | if (count == 0) |
184 | res = cdrom.Add(&log); | |
185 | ||
186 | if (res == false) | |
187 | _error->Error("%s", _(W_NO_CDROM_FOUND)); | |
188 | else | |
4a5e5089 | 189 | cout << _("Repeat this process for the rest of the CDs in your set.") << endl; |
169413dc | 190 | |
a75c6a6e | 191 | return res; |
83d89a9f AL |
192 | } |
193 | /*}}}*/ | |
b2e465d6 AL |
194 | // DoIdent - Ident a CDROM /*{{{*/ |
195 | // --------------------------------------------------------------------- | |
196 | /* */ | |
c3ccac92 | 197 | static bool DoIdent(CommandLine &) |
b2e465d6 | 198 | { |
169413dc | 199 | pkgUdevCdromDevices UdevCdroms; |
a75c6a6e MZ |
200 | string ident; |
201 | pkgCdromTextStatus log; | |
202 | pkgCdrom cdrom; | |
169413dc MV |
203 | bool res = true; |
204 | ||
62dcbf84 | 205 | bool AutoDetect = _config->FindB("Acquire::cdrom::AutoDetect", true); |
785412cf | 206 | |
2ecf54d8 | 207 | unsigned int count = 0; |
62dcbf84 JO |
208 | string AptMountPoint = _config->FindDir("Dir::Media::MountPath"); |
209 | bool automounted = false; | |
169413dc | 210 | if (AutoDetect && UdevCdroms.Dlopen()) |
62dcbf84 JO |
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(); | |
0a83556b | 229 | } |
62dcbf84 JO |
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 | ||
169413dc | 237 | return res; |
b2e465d6 AL |
238 | } |
239 | /*}}}*/ | |
83d89a9f AL |
240 | // ShowHelp - Show the help screen /*{{{*/ |
241 | // --------------------------------------------------------------------- | |
242 | /* */ | |
c3ccac92 | 243 | static bool ShowHelp(CommandLine &) |
83d89a9f | 244 | { |
9179f697 | 245 | ioprintf(cout,_("%s %s for %s compiled on %s %s\n"),PACKAGE,PACKAGE_VERSION, |
5b28c804 | 246 | COMMON_ARCH,__DATE__,__TIME__); |
04aa15a8 | 247 | if (_config->FindB("version") == true) |
b9179170 | 248 | return true; |
b2e465d6 AL |
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" | |
0a83556b | 268 | " --no-auto-detect Do not try to auto detect drive and mount point\n" |
b2e465d6 | 269 | " -c=? Read this configuration file\n" |
a2884e32 | 270 | " -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp\n" |
b2e465d6 | 271 | "See fstab(5)\n"; |
b9179170 | 272 | return true; |
83d89a9f AL |
273 | } |
274 | /*}}}*/ | |
92fcbfc1 | 275 | int main(int argc,const char *argv[]) /*{{{*/ |
83d89a9f | 276 | { |
83d89a9f AL |
277 | CommandLine::Dispatch Cmds[] = { |
278 | {"add",&DoAdd}, | |
b2e465d6 | 279 | {"ident",&DoIdent}, |
b9179170 | 280 | {"help",&ShowHelp}, |
83d89a9f | 281 | {0,0}}; |
67111687 | 282 | |
b9179170 MV |
283 | std::vector<CommandLine::Args> Args = getCommandArgs("apt-cdrom", CommandLine::GetCommand(Cmds, argc, argv)); |
284 | ||
67111687 AL |
285 | // Set up gettext support |
286 | setlocale(LC_ALL,""); | |
287 | textdomain(PACKAGE); | |
288 | ||
83d89a9f | 289 | // Parse the command line and initialize the package library |
b9179170 | 290 | CommandLine CmdL(Args.data(),_config); |
b2e465d6 AL |
291 | if (pkgInitConfig(*_config) == false || |
292 | CmdL.Parse(argc,argv) == false || | |
293 | pkgInitSystem(*_config,_system) == false) | |
83d89a9f AL |
294 | { |
295 | _error->DumpErrors(); | |
296 | return 100; | |
297 | } | |
298 | ||
299 | // See if the help should be shown | |
5633a7c2 | 300 | if (_config->FindB("help") == true || _config->FindB("version") == true || |
83d89a9f | 301 | CmdL.FileSize() == 0) |
b9179170 | 302 | return ShowHelp(CmdL); |
a9a5908d AL |
303 | |
304 | // Deal with stdout not being a tty | |
c340d185 | 305 | if (isatty(STDOUT_FILENO) && _config->FindI("quiet", -1) == -1) |
a9a5908d | 306 | _config->Set("quiet","1"); |
83d89a9f AL |
307 | |
308 | // Match the operation | |
309 | CmdL.DispatchArg(Cmds); | |
310 | ||
311 | // Print any errors or warnings found during parsing | |
65beb572 DK |
312 | bool const Errors = _error->PendingError(); |
313 | if (_config->FindI("quiet",0) > 0) | |
83d89a9f | 314 | _error->DumpErrors(); |
65beb572 DK |
315 | else |
316 | _error->DumpErrors(GlobalError::DEBUG); | |
317 | return Errors == true ? 100 : 0; | |
83d89a9f | 318 | } |
92fcbfc1 | 319 | /*}}}*/ |