// -*- mode: cpp; mode: fold -*-
// Description /*{{{*/
-// $Id: apt-cdrom.cc,v 1.3 1998/11/28 00:00:36 jgg Exp $
+// $Id: apt-cdrom.cc,v 1.22 1999/04/07 06:00:20 jgg Exp $
/* ######################################################################
APT CDROM - Tool for handling APT's CDROM database.
#include <apt-pkg/cmndline.h>
#include <apt-pkg/error.h>
#include <apt-pkg/init.h>
-#include <apt-pkg/md5.h>
#include <apt-pkg/fileutl.h>
#include <apt-pkg/progress.h>
#include <apt-pkg/tagfile.h>
-#include <strutl.h>
+#include <apt-pkg/cdromutl.h>
+#include <apt-pkg/strutl.h>
#include <config.h>
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
-#include <sys/wait.h>
-#include <sys/errno.h>
-#include <sys/vfs.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h>
#include <stdio.h>
/*}}}*/
-// UnmountCdrom - Unmount a cdrom /*{{{*/
-// ---------------------------------------------------------------------
-/* */
-bool UnmountCdrom(string Path)
-{
- int Child = fork();
- if (Child < -1)
- return _error->Errno("fork","Failed to fork");
-
- // The child
- if (Child == 0)
- {
- // Make all the fds /dev/null
- for (int I = 0; I != 10; I++)
- close(I);
- for (int I = 0; I != 3; I++)
- dup2(open("/dev/null",O_RDWR),I);
-
- const char *Args[10];
- Args[0] = "umount";
- Args[1] = Path.c_str();
- Args[2] = 0;
- execvp(Args[0],(char **)Args);
- exit(100);
- }
-
- // Wait for mount
- int Status = 0;
- while (waitpid(Child,&Status,0) != Child)
- {
- if (errno == EINTR)
- continue;
- return _error->Errno("waitpid","Couldn't wait for subprocess");
- }
-
- // Check for an error code.
- if (WIFEXITED(Status) == 0 || WEXITSTATUS(Status) != 0)
- return false;
- return true;
-}
- /*}}}*/
-// MountCdrom - Mount a cdrom /*{{{*/
-// ---------------------------------------------------------------------
-/* We fork mount.. */
-bool MountCdrom(string Path)
-{
- int Child = fork();
- if (Child < -1)
- return _error->Errno("fork","Failed to fork");
-
- // The child
- if (Child == 0)
- {
- // Make all the fds /dev/null
- for (int I = 0; I != 10; I++)
- close(I);
- for (int I = 0; I != 3; I++)
- dup2(open("/dev/null",O_RDWR),I);
-
- const char *Args[10];
- Args[0] = "mount";
- Args[1] = Path.c_str();
- Args[2] = 0;
- execvp(Args[0],(char **)Args);
- exit(100);
- }
-
- // Wait for mount
- int Status = 0;
- while (waitpid(Child,&Status,0) != Child)
- {
- if (errno == EINTR)
- continue;
- return _error->Errno("waitpid","Couldn't wait for subprocess");
- }
-
- // Check for an error code.
- if (WIFEXITED(Status) == 0 || WEXITSTATUS(Status) != 0)
- return false;
- return true;
-}
- /*}}}*/
-// IdentCdrom - Generate a unique string for this CD /*{{{*/
-// ---------------------------------------------------------------------
-/* We convert everything we hash into a string, this prevents byte size/order
- from effecting the outcome. */
-bool IdentCdrom(string CD,string &Res)
-{
- MD5Summation Hash;
-
- string StartDir = SafeGetCWD();
- if (chdir(CD.c_str()) != 0)
- return _error->Errno("chdir","Unable to change to %s",CD.c_str());
-
- DIR *D = opendir(".");
- if (D == 0)
- return _error->Errno("opendir","Unable to read %s",CD.c_str());
-
- // Run over the directory
- char S[300];
- for (struct dirent *Dir = readdir(D); Dir != 0; Dir = readdir(D))
- {
- // Skip some files..
- if (strcmp(Dir->d_name,".") == 0 ||
- strcmp(Dir->d_name,"..") == 0)
- continue;
-
- sprintf(S,"%lu",Dir->d_ino);
- Hash.Add(S);
- Hash.Add(Dir->d_name);
- };
-
- chdir(StartDir.c_str());
- closedir(D);
-
- // Some stats from the fsys
- struct statfs Buf;
- if (statfs(CD.c_str(),&Buf) != 0)
- return _error->Errno("statfs","Failed to stat the cdrom");
-
- sprintf(S,"%u %u",Buf.f_blocks,Buf.f_bfree);
- Hash.Add(S);
-
- Res = Hash.Result().Value();
- return true;
-}
- /*}}}*/
-
// FindPackages - Find the package files on the CDROM /*{{{*/
// ---------------------------------------------------------------------
/* We look over the cdrom for package files. This is a recursive
search that short circuits when it his a package file in the dir.
This speeds it up greatly as the majority of the size is in the
binary-* sub dirs. */
-bool FindPackages(string CD,vector<string> &List, int Depth = 0)
+bool FindPackages(string CD,vector<string> &List,string &InfoDir,
+ unsigned int Depth = 0)
{
+ static ino_t Inodes[9];
if (Depth >= 7)
return true;
this dir is controlled by those package files so we don't look down
anymore */
struct stat Buf;
- if (stat("Packages",&Buf) == 0 ||
- stat("Packages.gz",&Buf) == 0)
+ if (stat("Packages",&Buf) == 0)
{
List.push_back(CD);
- return true;
+
+ // Continue down if thorough is given
+ if (_config->FindB("APT::CDROM::Thorough",false) == false)
+ return true;
}
+ // Look for a .disk subdirectory
+ if (stat(".disk",&Buf) == 0)
+ {
+ if (InfoDir.empty() == true)
+ InfoDir = CD + ".disk/";
+ }
+
DIR *D = opendir(".");
if (D == 0)
return _error->Errno("opendir","Unable to read %s",CD.c_str());
if (S_ISDIR(Buf.st_mode) == 0)
continue;
+ unsigned int I;
+ for (I = 0; I != Depth; I++)
+ if (Inodes[I] == Buf.st_ino)
+ break;
+ if (I != Depth)
+ continue;
+
+ // Store the inodes weve seen
+ Inodes[Depth] = Buf.st_ino;
+
// Descend
- if (FindPackages(CD + Dir->d_name,List,Depth+1) == false)
+ if (FindPackages(CD + Dir->d_name,List,InfoDir,Depth+1) == false)
break;
if (chdir(CD.c_str()) != 0)
int Res = 0;
if (Path.find("stable/") != string::npos)
Res += 2;
+ if (Path.find("/binary-") != string::npos)
+ Res += 2;
if (Path.find("frozen/") != string::npos)
Res += 2;
if (Path.find("/dists/") != string::npos)
for (unsigned int I = 0; I != List.size(); I++)
{
struct stat Buf;
- if (stat(List[I].c_str(),&Buf) != 0)
- _error->Errno("stat","Failed to stat %s",List[I].c_str());
+ if (stat((List[I] + "Packages").c_str(),&Buf) != 0)
+ _error->Errno("stat","Failed to stat %sPackages",List[I].c_str());
Inodes[I] = Buf.st_ino;
}
+ if (_error->PendingError() == true)
+ return false;
+
// Look for dups
for (unsigned int I = 0; I != List.size(); I++)
{
// Strip the cdrom base path
Path = string(Path,CD.length());
+ if (Path.empty() == true)
+ Path = "/";
// Too short to be a dists/ type
if (Path.length() < strlen("dists/"))
return true;
}
/*}}}*/
+// ChopDirs - Chop off the leading directory components /*{{{*/
+// ---------------------------------------------------------------------
+/* */
+string ChopDirs(string Path,unsigned int Depth)
+{
+ string::size_type I = 0;
+ do
+ {
+ I = Path.find('/',I+1);
+ Depth--;
+ }
+ while (I != string::npos && Depth != 0);
+
+ if (I == string::npos)
+ return string();
+
+ return string(Path,I+1);
+}
+ /*}}}*/
+// ReconstructPrefix - Fix strange prefixing /*{{{*/
+// ---------------------------------------------------------------------
+/* This prepends dir components from the path to the package files to
+ the path to the deb until it is found */
+bool ReconstructPrefix(string &Prefix,string OrigPath,string CD,
+ string File)
+{
+ bool Debug = _config->FindB("Debug::aptcdrom",false);
+ unsigned int Depth = 1;
+ string MyPrefix = Prefix;
+ while (1)
+ {
+ struct stat Buf;
+ if (stat(string(CD + MyPrefix + File).c_str(),&Buf) != 0)
+ {
+ if (Debug == true)
+ cout << "Failed, " << CD + MyPrefix + File << endl;
+ if (GrabFirst(OrigPath,MyPrefix,Depth++) == true)
+ continue;
+
+ return false;
+ }
+ else
+ {
+ Prefix = MyPrefix;
+ return true;
+ }
+ }
+ return false;
+}
+ /*}}}*/
+// ReconstructChop - Fixes bad source paths /*{{{*/
+// ---------------------------------------------------------------------
+/* This removes path components from the filename and prepends the location
+ of the package files until a file is found */
+bool ReconstructChop(unsigned long &Chop,string Dir,string File)
+{
+ // Attempt to reconstruct the filename
+ unsigned long Depth = 0;
+ while (1)
+ {
+ struct stat Buf;
+ if (stat(string(Dir + File).c_str(),&Buf) != 0)
+ {
+ File = ChopDirs(File,1);
+ Depth++;
+ if (File.empty() == false)
+ continue;
+ return false;
+ }
+ else
+ {
+ Chop = Depth;
+ return true;
+ }
+ }
+ return false;
+}
+ /*}}}*/
+
// CopyPackages - Copy the package files from the CD /*{{{*/
// ---------------------------------------------------------------------
/* */
OpTextProgress Progress;
bool NoStat = _config->FindB("APT::CDROM::Fast",false);
+ bool Debug = _config->FindB("Debug::aptcdrom",false);
// Prepare the progress indicator
unsigned long TotalSize = 0;
pkgTagSection Section;
string Prefix;
unsigned long Hits = 0;
+ unsigned long Chop = 0;
while (Parser.Step(Section) == true)
{
Progress.Progress(Parser.Offset());
if (File.empty() || Size == 0)
return _error->Error("Cannot find filename or size tag");
+ if (Chop != 0)
+ File = OrigPath + ChopDirs(File,Chop);
+
// See if the file exists
if (NoStat == false || Hits < 10)
{
- struct stat Buf;
- unsigned int Depth = 1;
- string MyPrefix = Prefix;
- while (1)
+ // Attempt to fix broken structure
+ if (Hits == 0)
{
- if (stat(string(CDROM + MyPrefix + File).c_str(),&Buf) != 0)
+ if (ReconstructPrefix(Prefix,OrigPath,CDROM,File) == false &&
+ ReconstructChop(Chop,*I,File) == false)
{
- if (Prefix.empty() == true)
- {
- if (GrabFirst(OrigPath,MyPrefix,Depth++) == true)
- continue;
- }
-
NotFound++;
- Depth = 0;
- break;
+ continue;
}
- else
- break;
+ if (Chop != 0)
+ File = OrigPath + ChopDirs(File,Chop);
}
- // Failed
- if (Depth == 0)
+ // Get the size
+ struct stat Buf;
+ if (stat(string(CDROM + Prefix + File).c_str(),&Buf) != 0)
+ {
+ NotFound++;
continue;
-
- // Store the new prefix
- if (Depth != 1)
- Prefix = MyPrefix;
-
+ }
+
// Size match
if ((unsigned)Buf.st_size != Size)
{
// Copy it to the target package file
const char *Start;
const char *Stop;
- Section.GetSection(Start,Stop);
- if (Target.Write(Start,Stop-Start) == false)
- return false;
+ if (Chop != 0)
+ {
+ // Mangle the output filename
+ const char *Filename;
+ Section.Find("Filename",Filename,Stop);
+
+ /* We need to rewrite the filename field so we emit
+ all fields except the filename file and rewrite that one */
+ for (unsigned int I = 0; I != Section.Count(); I++)
+ {
+ Section.Get(Start,Stop,I);
+ if (Start <= Filename && Stop > Filename)
+ {
+ char S[500];
+ sprintf(S,"Filename: %s\n",File.c_str());
+ if (I + 1 == Section.Count())
+ strcat(S,"\n");
+ if (Target.Write(S,strlen(S)) == false)
+ return false;
+ }
+ else
+ if (Target.Write(Start,Stop-Start) == false)
+ return false;
+ }
+ if (Target.Write("\n",1) == false)
+ return false;
+ }
+ else
+ {
+ Section.GetSection(Start,Stop);
+ if (Target.Write(Start,Stop-Start) == false)
+ return false;
+ }
}
+ if (Debug == true)
+ cout << " Processed by using Prefix '" << Prefix << "' and chop " << Chop << endl;
+
if (_config->FindB("APT::CDROM::NoAct",false) == false)
{
// Move out of the partial directory
if (NotFound + WrongSize > 10)
cout << "Alot of package entires were discarded, perhaps this CD is funny?" << endl;
+
+ return true;
}
/*}}}*/
{
// Startup
string CDROM = _config->FindDir("Acquire::cdrom::mount","/cdrom/");
- cout << "Using CD-ROM mount point " << CDROM << endl;
+ if (CDROM[0] == '.')
+ CDROM= SafeGetCWD() + '/' + CDROM;
+ cout << "Using CD-ROM mount point " << CDROM << endl;
+
// Read the database
Configuration Database;
string DFile = _config->FindFile("Dir::State::cdroms");
UnmountCdrom(CDROM);
// Mount the new CDROM
- Prompt("Please insert a Disc in the drive and press any key");
+ Prompt("Please insert a Disc in the drive and press enter");
cout << "Mounting CD-ROM" << endl;
if (MountCdrom(CDROM) == false)
- {
- cout << "Failed to mount the cdrom." << endl;
- return false;
- }
+ return _error->Error("Failed to mount the cdrom.");
}
// Hash the CD to get an ID
cout << "Identifying.. " << flush;
string ID;
if (IdentCdrom(CDROM,ID) == false)
+ {
+ cout << endl;
return false;
+ }
+
cout << '[' << ID << ']' << endl;
cout << "Scanning Disc for index files.. " << flush;
// Get the CD structure
vector<string> List;
string StartDir = SafeGetCWD();
- if (FindPackages(CDROM,List) == false)
+ string InfoDir;
+ if (FindPackages(CDROM,List,InfoDir) == false)
+ {
+ cout << endl;
return false;
+ }
+
chdir(StartDir.c_str());
if (_config->FindB("Debug::aptcdrom",false) == true)
_config->FindB("APT::CDROM::Rename",false) == true)
{
// Try to use the CDs label if at all possible
- if (FileExists(CDROM + "/.disk/info") == true)
+ if (InfoDir.empty() == false &&
+ FileExists(InfoDir + "/info") == true)
{
- ifstream F(string(CDROM+ "/.disk/info").c_str());
+ ifstream F(string(InfoDir + "/info").c_str());
if (!F == 0)
getline(F,Name);
}
if (_config->FindB("APT::CDROM::Rename",false) == true ||
- Name.empty() == false)
+ Name.empty() == true)
{
cout << "Please provide a name for this Disc, such as 'Debian 2.1r1 Disk 1'";
while (1)
{
Name = PromptLine("");
if (Name.empty() == false &&
+ Name.find('"') == string::npos &&
+ Name.find(':') == string::npos &&
Name.find('/') == string::npos)
break;
cout << "That is not a valid name, try again " << endl;
- }
-
+ }
}
}
else
Name = Database.Find("CD::" + ID);
+
+ string::iterator J = Name.begin();
+ for (; J != Name.end(); J++)
+ if (*J == '/' || *J == '"' || *J == ':')
+ *J = '_';
+
Database.Set("CD::" + ID,Name);
cout << "This Disc is called '" << Name << "'" << endl;
}
// Print the sourcelist entries
- cout << "Source List entires for this Disc are:" << endl;
+ cout << "Source List entries for this Disc are:" << endl;
for (vector<string>::iterator I = List.begin(); I != List.end(); I++)
{
string::size_type Space = (*I).find(' ');
{
cout << PACKAGE << ' ' << VERSION << " for " << ARCHITECTURE <<
" compiled on " << __DATE__ << " " << __TIME__ << endl;
+ if (_config->FindB("version") == true)
+ return 100;
cout << "Usage: apt-cdrom [options] command" << endl;
cout << endl;
cout << " -r Rename a recognized CD-ROM" << endl;
cout << " -m No mounting" << endl;
cout << " -f Fast mode, don't check package files" << endl;
+ cout << " -a Thorough scan mode" << endl;
cout << " -c=? Read this configuration file" << endl;
- cout << " -o=? Set an arbitary configuration option, ie -o dir::cache=/tmp" << endl;
+ cout << " -o=? Set an arbitary configuration option, eg -o dir::cache=/tmp" << endl;
cout << "See fstab(5)" << endl;
return 100;
}
{
CommandLine::Args Args[] = {
{'h',"help","help",0},
+ {'v',"version","version",0},
{'d',"cdrom","Acquire::cdrom::mount",CommandLine::HasArg},
{'r',"rename","APT::CDROM::Rename",0},
{'m',"no-mount","APT::CDROM::NoMount",0},
{'f',"fast","APT::CDROM::Fast",0},
- {'n',"just-print","APT::CDROM::NoAct",0},
+ {'n',"just-print","APT::CDROM::NoAct",0},
{'n',"recon","APT::CDROM::NoAct",0},
- {'n',"no-act","APT::CDROM::NoAct",0},
+ {'n',"no-act","APT::CDROM::NoAct",0},
+ {'a',"thorough","APT::CDROM::Thorough",0},
{'c',"config-file",0,CommandLine::ConfigFile},
{'o',"option",0,CommandLine::ArbItem},
{0,0,0,0}};
if (_config->FindB("help") == true ||
CmdL.FileSize() == 0)
return ShowHelp();
+
+ // Deal with stdout not being a tty
+ if (ttyname(STDOUT_FILENO) == 0 && _config->FindI("quiet",0) < 1)
+ _config->Set("quiet","1");
// Match the operation
CmdL.DispatchArg(Cmds);