X-Git-Url: https://git.saurik.com/apt.git/blobdiff_plain/2a001232db431d995281c2bead32ed9423721447..4e1c86a6c0cf9a6e51ec4f9b9ad9271547fa036d:/apt-pkg/contrib/cdromutl.cc diff --git a/apt-pkg/contrib/cdromutl.cc b/apt-pkg/contrib/cdromutl.cc index 129e4a9f6..187f6bd59 100644 --- a/apt-pkg/contrib/cdromutl.cc +++ b/apt-pkg/contrib/cdromutl.cc @@ -10,24 +10,28 @@ ##################################################################### */ /*}}}*/ // Include Files /*{{{*/ +#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 @@ -156,15 +160,18 @@ bool MountCdrom(string Path, string DeviceName) 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"))) { - CD = CD+string("/.disk"); + 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.c_str() << std::endl; + std::clog << "Found writable cdrom, using alternative path: " << CD + << std::endl; } string StartDir = SafeGetCWD(); @@ -202,8 +209,11 @@ bool IdentCdrom(string CD,string &Res,unsigned int Version) Hash.Add(Dir->d_name); }; - if (chdir(StartDir.c_str()) != 0) - return _error->Errno("chdir",_("Unable to change to %s"),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 @@ -212,10 +222,15 @@ bool IdentCdrom(string CD,string &Res,unsigned int Version) 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 - sprintf(S,"%lu %lu",(long)(Buf.f_blocks*(Buf.f_bsize/1024)), - (long)(Buf.f_bfree*(Buf.f_bsize/1024))); + 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); } @@ -226,3 +241,37 @@ bool IdentCdrom(string CD,string &Res,unsigned int Version) return true; } /*}}}*/ + +// FindMountPointForDevice - Find mountpoint for the given device /*{{{*/ +string FindMountPointForDevice(const char *devnode) +{ + char buf[255]; + char *out[10]; + int i=0; + + // this is the order that mount uses as well + const char *mount[] = { "/etc/mtab", + "/proc/mount", + NULL }; + + for (i=0; mount[i] != NULL; i++) { + if (FileExists(mount[i])) { + FILE *f=fopen(mount[i], "r"); + while ( fgets(buf, sizeof(buf), f) != NULL) { + if (strncmp(buf, devnode, strlen(devnode)) == 0) { + if(TokSplitString(' ', buf, out, 10)) + { + fclose(f); + // unescape the \0XXX chars in the path + string mount_point = out[1]; + return DeEscapeString(mount_point); + } + } + } + fclose(f); + } + } + + return string(); +} + /*}}}*/