* Applied patch from Aurelien Jarno <aurel32@debian.org> to fix wrong
[ntk/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 /*{{{*/
44 class pkgCdromTextStatus : public pkgCdromStatus
45 {
46 protected:
47 OpTextProgress Progress;
48 void Prompt(const char *Text);
49 string PromptLine(const char *Text);
50 bool AskCdromName(string &name);
51
52 public:
53 virtual void Update(string text, int current);
54 virtual bool ChangeCdrom();
55 virtual OpProgress* GetOpProgress();
56 };
57
58 void pkgCdromTextStatus::Prompt(const char *Text)
59 {
60 char C;
61 cout << Text << ' ' << flush;
62 read(STDIN_FILENO,&C,1);
63 if (C != '\n')
64 cout << endl;
65 }
66
67 string pkgCdromTextStatus::PromptLine(const char *Text)
68 {
69 cout << Text << ':' << endl;
70
71 string Res;
72 getline(cin,Res);
73 return Res;
74 }
75
76 bool pkgCdromTextStatus::AskCdromName(string &name)
77 {
78 cout << _("Please provide a name for this Disc, such as 'Debian 2.1r1 Disk 1'") << flush;
79 name = PromptLine("");
80
81 return true;
82 }
83
84
85 void pkgCdromTextStatus::Update(string text, int current)
86 {
87 if(text.size() > 0)
88 cout << text << flush;
89 }
90
91 bool pkgCdromTextStatus::ChangeCdrom()
92 {
93 Prompt(_("Please insert a Disc in the drive and press enter"));
94 return true;
95 }
96
97 OpProgress* pkgCdromTextStatus::GetOpProgress()
98 {
99 return &Progress;
100 };
101
102 /*}}}*/
103
104 // DoAdd - Add a new CDROM /*{{{*/
105 // ---------------------------------------------------------------------
106 /* This does the main add bit.. We show some status and things. The
107 sequence is to mount/umount the CD, Ident it then scan it for package
108 files and reduce that list. Then we copy over the package files and
109 verify them. Then rewrite the database files */
110 bool DoAdd(CommandLine &)
111 {
112 bool res = false;
113 pkgCdromTextStatus log;
114 pkgCdrom cdrom;
115 res = cdrom.Add(&log);
116 if(res)
117 cout << _("Repeat this process for the rest of the CDs in your set.") << endl;
118 return res;
119 }
120 /*}}}*/
121 // DoIdent - Ident a CDROM /*{{{*/
122 // ---------------------------------------------------------------------
123 /* */
124 bool DoIdent(CommandLine &)
125 {
126 string ident;
127 pkgCdromTextStatus log;
128 pkgCdrom cdrom;
129 return cdrom.Ident(ident, &log);
130 }
131 /*}}}*/
132
133 // ShowHelp - Show the help screen /*{{{*/
134 // ---------------------------------------------------------------------
135 /* */
136 int ShowHelp()
137 {
138 ioprintf(cout,_("%s %s for %s compiled on %s %s\n"),PACKAGE,VERSION,
139 COMMON_ARCH,__DATE__,__TIME__);
140 if (_config->FindB("version") == true)
141 return 0;
142
143 cout <<
144 "Usage: apt-cdrom [options] command\n"
145 "\n"
146 "apt-cdrom is a tool to add CDROM's to APT's source list. The\n"
147 "CDROM mount point and device information is taken from apt.conf\n"
148 "and /etc/fstab.\n"
149 "\n"
150 "Commands:\n"
151 " add - Add a CDROM\n"
152 " ident - Report the identity of a CDROM\n"
153 "\n"
154 "Options:\n"
155 " -h This help text\n"
156 " -d CD-ROM mount point\n"
157 " -r Rename a recognized CD-ROM\n"
158 " -m No mounting\n"
159 " -f Fast mode, don't check package files\n"
160 " -a Thorough scan mode\n"
161 " -c=? Read this configuration file\n"
162 " -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp\n"
163 "See fstab(5)\n";
164 return 0;
165 }
166 /*}}}*/
167
168 int main(int argc,const char *argv[])
169 {
170 CommandLine::Args Args[] = {
171 {'h',"help","help",0},
172 {'v',"version","version",0},
173 {'d',"cdrom","Acquire::cdrom::mount",CommandLine::HasArg},
174 {'r',"rename","APT::CDROM::Rename",0},
175 {'m',"no-mount","APT::CDROM::NoMount",0},
176 {'f',"fast","APT::CDROM::Fast",0},
177 {'n',"just-print","APT::CDROM::NoAct",0},
178 {'n',"recon","APT::CDROM::NoAct",0},
179 {'n',"no-act","APT::CDROM::NoAct",0},
180 {'a',"thorough","APT::CDROM::Thorough",0},
181 {'c',"config-file",0,CommandLine::ConfigFile},
182 {'o',"option",0,CommandLine::ArbItem},
183 {0,0,0,0}};
184 CommandLine::Dispatch Cmds[] = {
185 {"add",&DoAdd},
186 {"ident",&DoIdent},
187 {0,0}};
188
189 // Set up gettext support
190 setlocale(LC_ALL,"");
191 textdomain(PACKAGE);
192
193 // Parse the command line and initialize the package library
194 CommandLine CmdL(Args,_config);
195 if (pkgInitConfig(*_config) == false ||
196 CmdL.Parse(argc,argv) == false ||
197 pkgInitSystem(*_config,_system) == false)
198 {
199 _error->DumpErrors();
200 return 100;
201 }
202
203 // See if the help should be shown
204 if (_config->FindB("help") == true || _config->FindB("version") == true ||
205 CmdL.FileSize() == 0)
206 return ShowHelp();
207
208 // Deal with stdout not being a tty
209 if (isatty(STDOUT_FILENO) && _config->FindI("quiet",0) < 1)
210 _config->Set("quiet","1");
211
212 // Match the operation
213 CmdL.DispatchArg(Cmds);
214
215 // Print any errors or warnings found during parsing
216 if (_error->empty() == false)
217 {
218 bool Errors = _error->PendingError();
219 _error->DumpErrors();
220 return Errors == true?100:0;
221 }
222
223 return 0;
224 }