1 // -*- mode: cpp; mode: fold -*- 
   3 // $Id: apt-cdrom.cc,v 1.45 2003/11/19 23:50:51 mdz Exp $ 
   4 /* ###################################################################### 
   6    APT CDROM - Tool for handling APT's CDROM database. 
   8    Currently the only option is 'add' which will take the current CD 
   9    in the drive and add it into the database. 
  11    ##################################################################### */ 
  13 // Include Files                                                        /*{{{*/ 
  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/acquire.h> 
  24 #include <apt-pkg/acquire-item.h> 
  25 #include <apt-pkg/cdrom.h> 
  26 #include <apt-pkg/configuration.h> 
  27 #include <apt-pkg/pkgsystem.h> 
  42 static const char *W_NO_CDROM_FOUND 
= \
 
  43    N_("No CD-ROM could be auto-detected or found using " 
  44       "the default mount point.\n" 
  45       "You may try the --cdrom option to set the CD-ROM mount point. " 
  46       "See 'man apt-cdrom' for more " 
  47       "information about the CD-ROM auto-detection and mount point."); 
  51 class pkgCdromTextStatus 
: public pkgCdromStatus                        
/*{{{*/ 
  54    OpTextProgress Progress
; 
  55    void Prompt(const char *Text
);  
  56    string 
PromptLine(const char *Text
); 
  57    bool AskCdromName(string 
&name
); 
  60    virtual void Update(string text
, int current
); 
  61    virtual bool ChangeCdrom(); 
  62    virtual OpProgress
* GetOpProgress(); 
  65 void pkgCdromTextStatus::Prompt(const char *Text
)  
  68    cout 
<< Text 
<< ' ' << flush
; 
  69    read(STDIN_FILENO
,&C
,1); 
  74 string 
pkgCdromTextStatus::PromptLine(const char *Text
) 
  76    cout 
<< Text 
<< ':' << endl
; 
  83 bool pkgCdromTextStatus::AskCdromName(string 
&name
)  
  85    cout 
<< _("Please provide a name for this Disc, such as 'Debian 5.0.3 Disk 1'") << flush
; 
  86    name 
= PromptLine(""); 
  92 void pkgCdromTextStatus::Update(string text
, int current
)  
  95       cout 
<< text 
<< flush
; 
  98 bool pkgCdromTextStatus::ChangeCdrom()  
 100    Prompt(_("Please insert a Disc in the drive and press enter")); 
 104 OpProgress
* pkgCdromTextStatus::GetOpProgress()  
 109 // SetupAutoDetect                                                      /*{{{*/ 
 110 bool AutoDetectCdrom(pkgUdevCdromDevices 
&UdevCdroms
, unsigned int &i
) 
 112    bool Debug 
=  _config
->FindB("Debug::Acquire::cdrom", false); 
 114    vector
<struct CdromDevice
> v 
= UdevCdroms
.Scan(); 
 119       clog 
<< "Looking at devce " << i
 
 120            << " DeviveName: " << v
[i
].DeviceName 
 
 121            << " IsMounted: '" << v
[i
].Mounted 
<< "'" 
 122            << " MountPoint: '" << v
[i
].MountPath 
<< "'" 
 127       // set the right options 
 128       _config
->Set("Acquire::cdrom::mount", v
[i
].MountPath
); 
 129       _config
->Set("APT::CDROM::NoMount", true); 
 131       string AptMountPoint 
= _config
->FindDir("Dir::Media::MountPath"); 
 132       if (!FileExists(AptMountPoint
)) 
 133          mkdir(AptMountPoint
.c_str(), 0750); 
 134       if(MountCdrom(AptMountPoint
, v
[i
].DeviceName
) == false) 
 135          _error
->Warning(_("Failed to mount '%s' to '%s'"), v
[i
].DeviceName
.c_str(), AptMountPoint
.c_str()); 
 136       _config
->Set("Acquire::cdrom::mount", AptMountPoint
); 
 137       _config
->Set("APT::CDROM::NoMount", true); 
 144 // DoAdd - Add a new CDROM                                              /*{{{*/ 
 145 // --------------------------------------------------------------------- 
 146 /* This does the main add bit.. We show some status and things. The 
 147    sequence is to mount/umount the CD, Ident it then scan it for package  
 148    files and reduce that list. Then we copy over the package files and 
 149    verify them. Then rewrite the database files */ 
 150 bool DoAdd(CommandLine 
&) 
 152    pkgUdevCdromDevices UdevCdroms
; 
 153    pkgCdromTextStatus log
; 
 157    bool AutoDetect 
= _config
->FindB("Acquire::cdrom::AutoDetect", true); 
 158    unsigned int count 
= 0; 
 159    if (AutoDetect 
&& UdevCdroms
.Dlopen()) 
 160       while (AutoDetectCdrom(UdevCdroms
, count
)) 
 161          res 
&= cdrom
.Add(&log
); 
 163       res 
= cdrom
.Add(&log
); 
 165          _error
->Error("%s", _(W_NO_CDROM_FOUND
)); 
 170       cout 
<< _("Repeat this process for the rest of the CDs in your set.") << endl
; 
 175 // DoIdent - Ident a CDROM                                              /*{{{*/ 
 176 // --------------------------------------------------------------------- 
 178 bool DoIdent(CommandLine 
&) 
 180    pkgUdevCdromDevices UdevCdroms
; 
 182    pkgCdromTextStatus log
; 
 186    bool AutoDetect 
= _config
->FindB("Acquire::cdrom::AutoDetect"); 
 188    unsigned int count 
= 0; 
 189    if (AutoDetect 
&& UdevCdroms
.Dlopen()) 
 190       while (AutoDetectCdrom(UdevCdroms
, count
)) 
 191          res 
&= cdrom
.Ident(ident
, &log
); 
 193       res 
= cdrom
.Ident(ident
, &log
); 
 195          _error
->Error("%s", _(W_NO_CDROM_FOUND
)); 
 201 // ShowHelp - Show the help screen                                      /*{{{*/ 
 202 // --------------------------------------------------------------------- 
 206    ioprintf(cout
,_("%s %s for %s compiled on %s %s\n"),PACKAGE
,PACKAGE_VERSION
, 
 207             COMMON_ARCH
,__DATE__
,__TIME__
); 
 208    if (_config
->FindB("version") == true) 
 212       "Usage: apt-cdrom [options] command\n" 
 214       "apt-cdrom is a tool to add CDROM's to APT's source list. The\n" 
 215       "CDROM mount point and device information is taken from apt.conf\n" 
 219       "   add - Add a CDROM\n" 
 220       "   ident - Report the identity of a CDROM\n" 
 223       "  -h   This help text\n" 
 224       "  -d   CD-ROM mount point\n" 
 225       "  -r   Rename a recognized CD-ROM\n" 
 227       "  -f   Fast mode, don't check package files\n" 
 228       "  -a   Thorough scan mode\n" 
 229       "  --no-auto-detect Do not try to auto detect drive and mount point\n" 
 230       "  -c=? Read this configuration file\n" 
 231       "  -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp\n" 
 236 int main(int argc
,const char *argv
[])                                   /*{{{*/ 
 238    CommandLine::Args Args
[] = { 
 239       {'h',"help","help",0}, 
 240       {  0,"auto-detect","Acquire::cdrom::AutoDetect", CommandLine::Boolean
}, 
 241       {'v',"version","version",0}, 
 242       {'d',"cdrom","Acquire::cdrom::mount",CommandLine::HasArg
}, 
 243       {'r',"rename","APT::CDROM::Rename",0}, 
 244       {'m',"no-mount","APT::CDROM::NoMount",0}, 
 245       {'f',"fast","APT::CDROM::Fast",0}, 
 246       {'n',"just-print","APT::CDROM::NoAct",0}, 
 247       {'n',"recon","APT::CDROM::NoAct",0},       
 248       {'n',"no-act","APT::CDROM::NoAct",0}, 
 249       {'a',"thorough","APT::CDROM::Thorough",0}, 
 250       {'c',"config-file",0,CommandLine::ConfigFile
}, 
 251       {'o',"option",0,CommandLine::ArbItem
}, 
 253    CommandLine::Dispatch Cmds
[] = { 
 258    // Set up gettext support 
 259    setlocale(LC_ALL
,""); 
 262    // Parse the command line and initialize the package library 
 263    CommandLine 
CmdL(Args
,_config
); 
 264    if (pkgInitConfig(*_config
) == false || 
 265        CmdL
.Parse(argc
,argv
) == false || 
 266        pkgInitSystem(*_config
,_system
) == false) 
 268       _error
->DumpErrors(); 
 272    // See if the help should be shown 
 273    if (_config
->FindB("help") == true || _config
->FindB("version") == true || 
 274        CmdL
.FileSize() == 0) 
 277    // Deal with stdout not being a tty 
 278    if (isatty(STDOUT_FILENO
) && _config
->FindI("quiet", -1) == -1) 
 279       _config
->Set("quiet","1"); 
 281    // Match the operation 
 282    CmdL
.DispatchArg(Cmds
); 
 284    // Print any errors or warnings found during parsing 
 285    bool const Errors 
= _error
->PendingError(); 
 286    if (_config
->FindI("quiet",0) > 0) 
 287       _error
->DumpErrors(); 
 289       _error
->DumpErrors(GlobalError::DEBUG
); 
 290    return Errors 
== true ? 100 : 0;