]> git.saurik.com Git - apt.git/blame - cmdline/apt-cdrom.cc
eipp: let apt make a plan, not make stuff plane
[apt.git] / cmdline / apt-cdrom.cc
CommitLineData
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 33#include <apt-private/private-cmndline.h>
d9e518c6 34#include <apt-private/private-output.h>
e7e10e47 35#include <apt-private/private-main.h>
b9179170 36
ea542140 37#include <apti18n.h>
83d89a9f
AL
38 /*}}}*/
39
076d01b0
AL
40using namespace std;
41
92fcbfc1 42class pkgCdromTextStatus : public pkgCdromStatus /*{{{*/
83d89a9f 43{
a75c6a6e
MZ
44protected:
45 OpTextProgress Progress;
d3e8fbb3 46 void Prompt(const char *Text);
a75c6a6e 47 string PromptLine(const char *Text);
3b302846 48 bool AskCdromName(string &name) APT_OVERRIDE;
a75c6a6e
MZ
49
50public:
3b302846
DK
51 virtual void Update(string text, int current) APT_OVERRIDE;
52 virtual bool ChangeCdrom() APT_OVERRIDE;
53 virtual OpProgress* GetOpProgress() APT_OVERRIDE;
8a579291 54};
83d89a9f 55
d3e8fbb3 56void pkgCdromTextStatus::Prompt(const char *Text)
83d89a9f 57{
a75c6a6e
MZ
58 char C;
59 cout << Text << ' ' << flush;
f52037d6 60 if (read(STDIN_FILENO,&C,1) < 0)
d3e8fbb3 61 _error->Errno("pkgCdromTextStatus::Prompt",
7dd5854b 62 "Failed to read from standard input (not a terminal?)");
a75c6a6e
MZ
63 if (C != '\n')
64 cout << endl;
83d89a9f 65}
4dfaa253 66
a75c6a6e 67string pkgCdromTextStatus::PromptLine(const char *Text)
83d89a9f 68{
a75c6a6e 69 cout << Text << ':' << endl;
d3e8fbb3 70
a75c6a6e
MZ
71 string Res;
72 getline(cin,Res);
83d89a9f
AL
73 return Res;
74}
18444708 75
d3e8fbb3 76bool pkgCdromTextStatus::AskCdromName(string &name)
83d89a9f 77{
cdd5a135 78 cout << _("Please provide a name for this Disc, such as 'Debian 5.0.3 Disk 1'") << flush;
a75c6a6e 79 name = PromptLine("");
d3e8fbb3 80
83d89a9f
AL
81 return true;
82}
83d89a9f 83
d3e8fbb3 84
65512241 85void pkgCdromTextStatus::Update(string text, int /*current*/)
18444708 86{
a75c6a6e
MZ
87 if(text.size() > 0)
88 cout << text << flush;
18444708 89}
4dfaa253 90
d3e8fbb3 91bool pkgCdromTextStatus::ChangeCdrom()
83d89a9f 92{
94171725 93 Prompt(_("Please insert a Disc in the drive and press [Enter]"));
18444708
AL
94 return true;
95}
83d89a9f 96
a02db58f 97APT_CONST OpProgress* pkgCdromTextStatus::GetOpProgress()
d3e8fbb3
DK
98{
99 return &Progress;
100}
83d89a9f 101 /*}}}*/
b5ee78ae
DK
102// AddOrIdent - Add or Ident a CDROM /*{{{*/
103static bool AddOrIdent(bool const Add)
169413dc 104{
b5ee78ae
DK
105 pkgUdevCdromDevices UdevCdroms;
106 pkgCdromTextStatus log;
107 pkgCdrom cdrom;
169413dc 108
b5ee78ae
DK
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 }
62dcbf84 146
b5ee78ae
DK
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();
169413dc 156
b5ee78ae
DK
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 }
169413dc 164
b5ee78ae
DK
165 // fallback if auto-detect didn't work
166 if (oneSuccessful == false)
169413dc 167 {
b5ee78ae
DK
168 _error->PushToStack();
169 if (Add == true)
170 oneSuccessful = cdrom.Add(&log);
62dcbf84 171 else
b5ee78ae
DK
172 {
173 std::string id;
174 oneSuccessful = cdrom.Ident(id, &log);
175 }
176 _error->MergeWithStack();
169413dc 177 }
169413dc 178
b5ee78ae
DK
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;
169413dc
MV
187}
188 /*}}}*/
83d89a9f
AL
189// DoAdd - Add a new CDROM /*{{{*/
190// ---------------------------------------------------------------------
4dfaa253 191/* This does the main add bit.. We show some status and things. The
b5ee78ae 192 sequence is to mount/umount the CD, Ident it then scan it for package
4dfaa253
AL
193 files and reduce that list. Then we copy over the package files and
194 verify them. Then rewrite the database files */
c3ccac92 195static bool DoAdd(CommandLine &)
83d89a9f 196{
b5ee78ae 197 return AddOrIdent(true);
83d89a9f
AL
198}
199 /*}}}*/
b2e465d6 200// DoIdent - Ident a CDROM /*{{{*/
c3ccac92 201static bool DoIdent(CommandLine &)
b2e465d6 202{
b5ee78ae 203 return AddOrIdent(false);
b2e465d6
AL
204}
205 /*}}}*/
f6777222 206static bool ShowHelp(CommandLine &) /*{{{*/
83d89a9f 207{
cbbee23e
DK
208 std::cout <<
209 _("Usage: apt-cdrom [options] command\n"
b2e465d6 210 "\n"
8561c2fe
DK
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");
b9179170 214 return true;
83d89a9f
AL
215}
216 /*}}}*/
f6777222 217static std::vector<aptDispatchWithHelp> GetCommands() /*{{{*/
83d89a9f 218{
011188e3 219 return {
cbbee23e
DK
220 {"add", &DoAdd, "Add a CDROM"},
221 {"ident", &DoIdent, "Report the identity of a CDROM"},
222 {nullptr, nullptr, nullptr}
223 };
011188e3
DK
224}
225 /*}}}*/
226int main(int argc,const char *argv[]) /*{{{*/
227{
83d89a9f 228 // Parse the command line and initialize the package library
ad7e0941 229 CommandLine CmdL;
90986d4d 230 auto const Cmds = ParseCommandLine(CmdL, APT_CMD::APT_CDROM, &_config, &_system, argc, argv, &ShowHelp, &GetCommands);
a9a5908d 231
d9e518c6
DK
232 InitOutput();
233
e7e10e47 234 return DispatchCommandLine(CmdL, Cmds);
83d89a9f 235}
92fcbfc1 236 /*}}}*/