X-Git-Url: https://git.saurik.com/apt.git/blobdiff_plain/d6e79b75582b25065e2e1e843191c93d1f56ec3a..a5bb5e1e747ceb7b5a9defb6b1a8d9282a6e0957:/apt-pkg/contrib/cdromutl.cc?ds=sidebyside diff --git a/apt-pkg/contrib/cdromutl.cc b/apt-pkg/contrib/cdromutl.cc index d0d810c5d..936e377fb 100644 --- a/apt-pkg/contrib/cdromutl.cc +++ b/apt-pkg/contrib/cdromutl.cc @@ -1,6 +1,6 @@ // -*- mode: cpp; mode: fold -*- // Description /*{{{*/ -// $Id: cdromutl.cc,v 1.5 1999/04/20 05:11:17 jgg Exp $ +// $Id: cdromutl.cc,v 1.12 2001/02/20 07:03:17 jgg Exp $ /* ###################################################################### CDROM Utilities - Some functions to manipulate CDROM mounts. @@ -10,25 +10,32 @@ ##################################################################### */ /*}}}*/ // Include Files /*{{{*/ -#ifdef __GNUG__ -#pragma implementation "apt-pkg/cdromutl.h" -#endif +#include + #include #include #include #include #include +#include -#include -#include -#include +#include +#include +#include +#include +#include +#include #include #include #include #include #include + +#include /*}}}*/ +using std::string; + // IsMounted - Returns true if the mount point is mounted /*{{{*/ // --------------------------------------------------------------------- /* This is a simple algorithm that should always work, we stat the mount point @@ -39,18 +46,23 @@ bool IsMounted(string &Path) { if (Path.empty() == true) return false; - + // Need that trailing slash for directories if (Path[Path.length() - 1] != '/') Path += '/'; - - /* First we check if the path is actualy mounted, we do this by - stating the path and the previous directory (carefull of links!) + + // if the path has a ".disk" directory we treat it as mounted + // this way even extracted copies of disks are recognized + if (DirectoryExists(Path + ".disk/") == true) + return true; + + /* First we check if the path is actually mounted, we do this by + stating the path and the previous directory (careful of links!) and comparing their device fields. */ struct stat Buf,Buf2; if (stat(Path.c_str(),&Buf) != 0 || stat((Path + "../").c_str(),&Buf2) != 0) - return _error->Errno("stat","Unable to stat the mount point %s",Path.c_str()); + return _error->Errno("stat",_("Unable to stat the mount point %s"),Path.c_str()); if (Buf.st_dev == Buf2.st_dev) return false; @@ -63,67 +75,78 @@ bool IsMounted(string &Path) leave /etc/mtab inconsitant. We drop all messages this produces. */ bool UnmountCdrom(string Path) { - if (IsMounted(Path) == false) + // do not generate errors, even if the mountpoint does not exist + // the mountpoint might be auto-created by the mount command + // and a non-existing mountpoint is surely not mounted + _error->PushToStack(); + bool const mounted = IsMounted(Path); + _error->RevertToStack(); + if (mounted == false) return true; - - int Child = ExecFork(); - // The child - if (Child == 0) + for (int i=0;i<3;i++) { - // Make all the fds /dev/null - for (int I = 0; I != 3; I++) - dup2(open("/dev/null",O_RDWR),I); + + int Child = ExecFork(); - if (_config->Exists("Acquire::cdrom::"+Path+"::UMount") == true) + // The child + if (Child == 0) { - if (system(_config->Find("Acquire::cdrom::"+Path+"::UMount").c_str()) != 0) + // Make all the fds /dev/null + int const null_fd = open("/dev/null",O_RDWR); + for (int I = 0; I != 3; ++I) + dup2(null_fd, I); + + if (_config->Exists("Acquire::cdrom::"+Path+"::UMount") == true) + { + if (system(_config->Find("Acquire::cdrom::"+Path+"::UMount").c_str()) != 0) + _exit(100); + _exit(0); + } + else + { + const char *Args[10]; + Args[0] = "umount"; + Args[1] = Path.c_str(); + Args[2] = 0; + execvp(Args[0],(char **)Args); _exit(100); - _exit(0); + } } - else - { - 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"); + // if it can not be umounted, give it a bit more time + // this can happen when auto-mount magic or fs/cdrom prober attack + if (ExecWait(Child,"umount",true) == true) + return true; + sleep(1); } - - // Check for an error code. - if (WIFEXITED(Status) == 0 || WEXITSTATUS(Status) != 0) - return false; - return true; + + return false; } /*}}}*/ // MountCdrom - Mount a cdrom /*{{{*/ // --------------------------------------------------------------------- /* We fork mount and drop all messages */ -bool MountCdrom(string Path) +bool MountCdrom(string Path, string DeviceName) { - if (IsMounted(Path) == true) + // do not generate errors, even if the mountpoint does not exist + // the mountpoint might be auto-created by the mount command + _error->PushToStack(); + bool const mounted = IsMounted(Path); + _error->RevertToStack(); + if (mounted == true) return true; - + int Child = ExecFork(); // The child if (Child == 0) { // Make all the fds /dev/null - for (int I = 0; I != 3; I++) - dup2(open("/dev/null",O_RDWR),I); - + int const null_fd = open("/dev/null",O_RDWR); + for (int I = 0; I != 3; ++I) + dup2(null_fd, I); + if (_config->Exists("Acquire::cdrom::"+Path+"::Mount") == true) { if (system(_config->Find("Acquire::cdrom::"+Path+"::Mount").c_str()) != 0) @@ -134,43 +157,52 @@ bool MountCdrom(string Path) { const char *Args[10]; Args[0] = "mount"; - Args[1] = Path.c_str(); - Args[2] = 0; + if (DeviceName == "") + { + Args[1] = Path.c_str(); + Args[2] = 0; + } else { + Args[1] = DeviceName.c_str(); + Args[2] = Path.c_str(); + Args[3] = 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; + return ExecWait(Child,"mount",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) +bool IdentCdrom(string CD,string &Res,unsigned int Version) { MD5Summation Hash; + bool writable_media = false; + + // if we are on a writable medium (like a usb-stick) that is just + // used like a cdrom don't use "." as it will constantly change, + // use .disk instead + if (access(CD.c_str(), W_OK) == 0 && DirectoryExists(CD+string("/.disk"))) + { + writable_media = true; + CD = CD.append("/.disk"); + if (_config->FindB("Debug::aptcdrom",false) == true) + std::clog << "Found writable cdrom, using alternative path: " << CD + << std::endl; + } string StartDir = SafeGetCWD(); if (chdir(CD.c_str()) != 0) - return _error->Errno("chdir","Unable to change to %s",CD.c_str()); + 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()); + return _error->Errno("opendir",_("Unable to read %s"),CD.c_str()); /* Run over the directory, we assume that the reader order will never change as the media is read-only. In theory if the kernel did @@ -182,26 +214,83 @@ bool IdentCdrom(string CD,string &Res) if (strcmp(Dir->d_name,".") == 0 || strcmp(Dir->d_name,"..") == 0) continue; - - sprintf(S,"%lu",Dir->d_ino); + + if (Version <= 1) + { + sprintf(S,"%lu",(unsigned long)Dir->d_ino); + } + else + { + struct stat Buf; + if (stat(Dir->d_name,&Buf) != 0) + continue; + sprintf(S,"%lu",(unsigned long)Buf.st_mtime); + } + Hash.Add(S); Hash.Add(Dir->d_name); }; - chdir(StartDir.c_str()); + if (chdir(StartDir.c_str()) != 0) { + _error->Errno("chdir",_("Unable to change to %s"),StartDir.c_str()); + closedir(D); + return false; + } 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"); - - // We use a kilobyte block size to advoid overflow - sprintf(S,"%lu %lu",(long)(Buf.f_blocks*(Buf.f_bsize/1024)), - (long)(Buf.f_bfree*(Buf.f_bsize/1024))); - Hash.Add(S); + if (_config->FindB("Debug::identcdrom",false) == false) + { + struct statvfs Buf; + if (statvfs(CD.c_str(),&Buf) != 0) + return _error->Errno("statfs",_("Failed to stat the cdrom")); + + // We use a kilobyte block size to advoid overflow + if (writable_media) + { + sprintf(S,"%lu",(long)(Buf.f_blocks*(Buf.f_bsize/1024))); + } else { + sprintf(S,"%lu %lu",(long)(Buf.f_blocks*(Buf.f_bsize/1024)), + (long)(Buf.f_bfree*(Buf.f_bsize/1024))); + } + Hash.Add(S); + sprintf(S,"-%u",Version); + } + else + sprintf(S,"-%u.debug",Version); - Res = Hash.Result().Value(); + Res = Hash.Result().Value() + S; return true; } /*}}}*/ +// FindMountPointForDevice - Find mountpoint for the given device /*{{{*/ +string FindMountPointForDevice(const char *devnode) +{ + // this is the order that mount uses as well + std::vector const mounts = _config->FindVector("Dir::state::MountPoints", "/etc/mtab,/proc/mount"); + + for (std::vector::const_iterator m = mounts.begin(); m != mounts.end(); ++m) + if (FileExists(*m) == true) + { + char * line = NULL; + size_t line_len = 0; + FILE * f = fopen(m->c_str(), "r"); + while(getline(&line, &line_len, f) != -1) + { + char * out[] = { NULL, NULL, NULL }; + TokSplitString(' ', line, out, 3); + if (out[2] != NULL || out[1] == NULL || out[0] == NULL) + continue; + if (strcmp(out[0], devnode) != 0) + continue; + fclose(f); + // unescape the \0XXX chars in the path + string mount_point = out[1]; + return DeEscapeString(mount_point); + } + fclose(f); + } + + return string(); +} + /*}}}*/