]>
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 | #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 | static bool ShowHelp(CommandLine &) /*{{{*/ | |
207 | { | |
208 | std::cout << | |
209 | _("Usage: apt-cdrom [options] command\n" | |
210 | "\n" | |
211 | "apt-cdrom is used to add CDROM's, USB flashdrives and other removable\n" | |
212 | "media types as package sources to APT. The mount point and device\n" | |
213 | "information is taken from apt.conf(5), udev(7) and fstab(5).\n"); | |
214 | return true; | |
215 | } | |
216 | /*}}}*/ | |
217 | static std::vector<aptDispatchWithHelp> GetCommands() /*{{{*/ | |
218 | { | |
219 | return { | |
220 | {"add", &DoAdd, "Add a CDROM"}, | |
221 | {"ident", &DoIdent, "Report the identity of a CDROM"}, | |
222 | {nullptr, nullptr, nullptr} | |
223 | }; | |
224 | } | |
225 | /*}}}*/ | |
226 | int main(int argc,const char *argv[]) /*{{{*/ | |
227 | { | |
228 | InitLocale(); | |
229 | ||
230 | // Parse the command line and initialize the package library | |
231 | CommandLine CmdL; | |
232 | auto const Cmds = ParseCommandLine(CmdL, APT_CMD::APT_CDROM, &_config, &_system, argc, argv, &ShowHelp, &GetCommands); | |
233 | ||
234 | InitOutput(); | |
235 | ||
236 | return DispatchCommandLine(CmdL, Cmds); | |
237 | } | |
238 | /*}}}*/ |