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