]> git.saurik.com Git - apt.git/blob - cmdline/apt-cdrom.cc
add cacheset push_back wrapping for std::back_inserter
[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<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 #include <apt-private/private-output.h>
35
36 #include <apti18n.h>
37 /*}}}*/
38
39 using namespace std;
40
41 class pkgCdromTextStatus : public pkgCdromStatus /*{{{*/
42 {
43 protected:
44 OpTextProgress Progress;
45 void Prompt(const char *Text);
46 string PromptLine(const char *Text);
47 bool AskCdromName(string &name) APT_OVERRIDE;
48
49 public:
50 virtual void Update(string text, int current) APT_OVERRIDE;
51 virtual bool ChangeCdrom() APT_OVERRIDE;
52 virtual OpProgress* GetOpProgress() APT_OVERRIDE;
53 };
54
55 void pkgCdromTextStatus::Prompt(const char *Text)
56 {
57 char C;
58 cout << Text << ' ' << flush;
59 if (read(STDIN_FILENO,&C,1) < 0)
60 _error->Errno("pkgCdromTextStatus::Prompt",
61 "Failed to read from standard input (not a terminal?)");
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 APT_CONST OpProgress* pkgCdromTextStatus::GetOpProgress()
97 {
98 return &Progress;
99 }
100 /*}}}*/
101 // AddOrIdent - Add or Ident a CDROM /*{{{*/
102 static bool AddOrIdent(bool const Add)
103 {
104 pkgUdevCdromDevices UdevCdroms;
105 pkgCdromTextStatus log;
106 pkgCdrom cdrom;
107
108 bool oneSuccessful = false;
109 bool AutoDetect = _config->FindB("Acquire::cdrom::AutoDetect", true);
110 if (AutoDetect == true && UdevCdroms.Dlopen() == true)
111 {
112 bool const Debug = _config->FindB("Debug::Acquire::cdrom", false);
113 std::string const CDMount = _config->Find("Acquire::cdrom::mount");
114 bool const NoMount = _config->FindB("APT::CDROM::NoMount", false);
115 if (NoMount == false)
116 _config->Set("APT::CDROM::NoMount", true);
117
118 vector<struct CdromDevice> const v = UdevCdroms.Scan();
119 for (std::vector<struct CdromDevice>::const_iterator cd = v.begin(); cd != v.end(); ++cd)
120 {
121 if (Debug)
122 clog << "Looking at device:"
123 << "\tDeviveName: '" << cd->DeviceName << "'"
124 << "\tIsMounted: '" << cd->Mounted << "'"
125 << "\tMountPoint: '" << cd->MountPath << "'"
126 << endl;
127
128 std::string AptMountPoint;
129 if (cd->Mounted)
130 _config->Set("Acquire::cdrom::mount", cd->MountPath);
131 else if (NoMount == true)
132 continue;
133 else
134 {
135 AptMountPoint = _config->FindDir("Dir::Media::MountPath");
136 if (FileExists(AptMountPoint) == false)
137 mkdir(AptMountPoint.c_str(), 0750);
138 if(MountCdrom(AptMountPoint, cd->DeviceName) == false)
139 {
140 _error->Warning(_("Failed to mount '%s' to '%s'"), cd->DeviceName.c_str(), AptMountPoint.c_str());
141 continue;
142 }
143 _config->Set("Acquire::cdrom::mount", AptMountPoint);
144 }
145
146 _error->PushToStack();
147 if (Add == true)
148 oneSuccessful = cdrom.Add(&log);
149 else
150 {
151 std::string id;
152 oneSuccessful = cdrom.Ident(id, &log);
153 }
154 _error->MergeWithStack();
155
156 if (AptMountPoint.empty() == false)
157 UnmountCdrom(AptMountPoint);
158 }
159 if (NoMount == false)
160 _config->Set("APT::CDROM::NoMount", NoMount);
161 _config->Set("Acquire::cdrom::mount", CDMount);
162 }
163
164 // fallback if auto-detect didn't work
165 if (oneSuccessful == false)
166 {
167 _error->PushToStack();
168 if (Add == true)
169 oneSuccessful = cdrom.Add(&log);
170 else
171 {
172 std::string id;
173 oneSuccessful = cdrom.Ident(id, &log);
174 }
175 _error->MergeWithStack();
176 }
177
178 if (oneSuccessful == false)
179 _error->Error("%s", _("No CD-ROM could be auto-detected or found using the default mount point.\n"
180 "You may try the --cdrom option to set the CD-ROM mount point.\n"
181 "See 'man apt-cdrom' for more information about the CD-ROM auto-detection and mount point."));
182 else if (Add == true)
183 cout << _("Repeat this process for the rest of the CDs in your set.") << endl;
184
185 return oneSuccessful;
186 }
187 /*}}}*/
188 // DoAdd - Add a new CDROM /*{{{*/
189 // ---------------------------------------------------------------------
190 /* This does the main add bit.. We show some status and things. The
191 sequence is to mount/umount the CD, Ident it then scan it for package
192 files and reduce that list. Then we copy over the package files and
193 verify them. Then rewrite the database files */
194 static bool DoAdd(CommandLine &)
195 {
196 return AddOrIdent(true);
197 }
198 /*}}}*/
199 // DoIdent - Ident a CDROM /*{{{*/
200 static bool DoIdent(CommandLine &)
201 {
202 return AddOrIdent(false);
203 }
204 /*}}}*/
205 // ShowHelp - Show the help screen /*{{{*/
206 static bool ShowHelp(CommandLine &)
207 {
208 ioprintf(cout, "%s %s (%s)\n", PACKAGE, PACKAGE_VERSION, COMMON_ARCH);
209
210 if (_config->FindB("version") == true)
211 return true;
212
213 cout <<
214 "Usage: apt-cdrom [options] command\n"
215 "\n"
216 "apt-cdrom is a tool to add CDROM's to APT's source list. The\n"
217 "CDROM mount point and device information is taken from apt.conf\n"
218 "and /etc/fstab.\n"
219 "\n"
220 "Commands:\n"
221 " add - Add a CDROM\n"
222 " ident - Report the identity of a CDROM\n"
223 "\n"
224 "Options:\n"
225 " -h This help text\n"
226 " -d CD-ROM mount point\n"
227 " -r Rename a recognized CD-ROM\n"
228 " -m No mounting\n"
229 " -f Fast mode, don't check package files\n"
230 " -a Thorough scan mode\n"
231 " --no-auto-detect Do not try to auto detect drive and mount point\n"
232 " -c=? Read this configuration file\n"
233 " -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp\n"
234 "See fstab(5)\n";
235 return true;
236 }
237 /*}}}*/
238 int main(int argc,const char *argv[]) /*{{{*/
239 {
240 CommandLine::Dispatch Cmds[] = {
241 {"add",&DoAdd},
242 {"ident",&DoIdent},
243 {"help",&ShowHelp},
244 {0,0}};
245
246 std::vector<CommandLine::Args> Args = getCommandArgs("apt-cdrom", CommandLine::GetCommand(Cmds, argc, argv));
247
248 // Set up gettext support
249 setlocale(LC_ALL,"");
250 textdomain(PACKAGE);
251
252 // Parse the command line and initialize the package library
253 CommandLine CmdL;
254 ParseCommandLine(CmdL, Cmds, Args.data(), &_config, &_system, argc, argv, ShowHelp);
255
256 InitOutput();
257
258 // Match the operation
259 bool returned = CmdL.DispatchArg(Cmds);
260
261 if (_config->FindI("quiet",0) > 0)
262 _error->DumpErrors();
263 else
264 _error->DumpErrors(GlobalError::DEBUG);
265 return returned == true ? 0 : 100;
266 }
267 /*}}}*/