]> git.saurik.com Git - apt.git/blobdiff - apt-pkg/contrib/cdromutl.cc
Make private sha2 functions static
[apt.git] / apt-pkg / contrib / cdromutl.cc
index ab170ec5a8b0c17f2c3bfae51ea9542201fd9d4e..821e6d688e45b2fd083ffea530de738dd0101dbd 100644 (file)
    ##################################################################### */
                                                                        /*}}}*/
 // Include Files                                                       /*{{{*/
-#ifdef __GNUG__
-#pragma implementation "apt-pkg/cdromutl.h"
-#endif
 #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>
@@ -67,41 +64,50 @@ bool UnmountCdrom(string Path)
 {
    if (IsMounted(Path) == 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
+        for (int I = 0; I != 3; I++)
+           dup2(open("/dev/null",O_RDWR),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);
-      }      
+
+      // 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);
    }
 
-   // Wait for mount
-   return ExecWait(Child,"umount",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)
       return true;
@@ -125,8 +131,15 @@ 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);
       }      
@@ -143,6 +156,19 @@ bool MountCdrom(string Path)
 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)
@@ -179,7 +205,11 @@ bool IdentCdrom(string CD,string &Res,unsigned int Version)
       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
@@ -188,10 +218,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);
    }
@@ -202,3 +237,35 @@ 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);
+                  return string(out[1]);
+               }
+            }
+         }
+         fclose(f);
+      }
+   }
+   
+   return string();
+}
+                                                                       /*}}}*/