]> git.saurik.com Git - apt.git/blobdiff - apt-pkg/contrib/cdromutl.cc
apt-pkg:contrib Avoid compiler warning about sign-compare
[apt.git] / apt-pkg / contrib / cdromutl.cc
index 6dce82fe1b757f4983798b50c56d93ff0963bfab..afa01a56224d790f0cea9242f1976d78a83ff490 100644 (file)
    ##################################################################### */
                                                                        /*}}}*/
 // Include Files                                                       /*{{{*/
+#include<config.h>
+
 #include <apt-pkg/cdromutl.h>
 #include <apt-pkg/error.h>
 #include <apt-pkg/md5.h>
 #include <apt-pkg/fileutl.h>
 #include <apt-pkg/configuration.h>
+#include <apt-pkg/strutl.h>
 
-#include <apti18n.h>
-    
 #include <sys/wait.h>
-#include <sys/errno.h>
 #include <sys/statvfs.h>
 #include <dirent.h>
 #include <fcntl.h>
 #include <sys/stat.h>
 #include <unistd.h>
 #include <stdio.h>
+
+#include <apti18n.h>
                                                                        /*}}}*/
 
+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
@@ -118,8 +122,9 @@ bool MountCdrom(string Path, string DeviceName)
    if (Child == 0)
    {
       // Make all the fds /dev/null
+      int null_fd = open("/dev/null",O_RDWR);
       for (int I = 0; I != 3; I++)
-        dup2(open("/dev/null",O_RDWR),I);
+        dup2(null_fd, I);
       
       if (_config->Exists("Acquire::cdrom::"+Path+"::Mount") == true)
       {
@@ -156,6 +161,19 @@ 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"))) 
+   {
+      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)
@@ -192,8 +210,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
@@ -202,10 +223,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);
    }
@@ -216,3 +242,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();
+}
+                                                                       /*}}}*/