[maemo-commits] [maemo-commits] r9918 - projects/haf/trunk/apt/apt-pkg/contrib

From: subversion at stage.maemo.org subversion at stage.maemo.org
Date: Thu Feb 15 18:27:14 EET 2007
Author: marivoll
Date: 2007-02-15 18:27:13 +0200 (Thu, 15 Feb 2007)
New Revision: 9918

Modified:
   projects/haf/trunk/apt/apt-pkg/contrib/mmap.cc
   projects/haf/trunk/apt/apt-pkg/contrib/mmap.h
Log:
  * Applied mmap.patch that allows us to work on file systems that don't
    support read-write mmaps such as JFFS2.


Modified: projects/haf/trunk/apt/apt-pkg/contrib/mmap.cc
===================================================================
--- projects/haf/trunk/apt/apt-pkg/contrib/mmap.cc	2007-02-15 15:49:47 UTC (rev 9917)
+++ projects/haf/trunk/apt/apt-pkg/contrib/mmap.cc	2007-02-15 16:27:13 UTC (rev 9918)
@@ -35,13 +35,15 @@
 #include <sys/stat.h>
 #include <unistd.h>
 #include <fcntl.h>
+#include <errno.h>
+
    									/*}}}*/
 
 // MMap::MMap - Constructor						/*{{{*/
 // ---------------------------------------------------------------------
 /* */
 MMap::MMap(FileFd &F,unsigned long Flags) : Flags(Flags), iSize(0),
-                     Base(0)
+					    Base(0), fd(0)
 {
    if ((Flags & NoImmMap) != NoImmMap)
       Map(F);
@@ -51,7 +53,7 @@
 // ---------------------------------------------------------------------
 /* */
 MMap::MMap(unsigned long Flags) : Flags(Flags), iSize(0),
-                     Base(0)
+                     Base(0), fd(0)
 {
 }
 									/*}}}*/
@@ -84,7 +86,26 @@
    // Map it.
    Base = mmap(0,iSize,Prot,Map,Fd.Fd(),0);
    if (Base == (void *)-1)
-      return _error->Errno("mmap",_("Couldn't make mmap of %lu bytes"),iSize);
+     {
+       if (errno == ENODEV || errno == EINVAL)
+        {
+          // The filesystem doesn't support this particular kind of
+          // mmap.  So we allocate a buffer and read the whole file
+          // into it.
+          //
+	  int dupped_fd = dup (Fd.Fd());
+	  if (dupped_fd == -1)
+	    return _error->Errno("mmap",_("Couldn't dup filedescriptor"));
+	    
+	  Base = new unsigned char[iSize];
+          fd = new FileFd (dupped_fd);
+          if (!fd->Seek(0L) || !fd->Read(Base, iSize))
+	    return false;
+        }
+       else
+        return _error->Errno("mmap",_("Couldn't make mmap of %lu bytes"),
+                             iSize);
+     }
 
    return true;
 }
@@ -99,10 +120,19 @@
    
    if (DoSync == true)
       Sync();
+
+   if (fd)
+     {
+       delete[] (char *)Base;
+       delete fd;
+       fd = NULL;
+     }
+   else
+     {
+       if (munmap((char *)Base,iSize) != 0)
+	 _error->Warning("Unable to munmap");
+     }
    
-   if (munmap((char *)Base,iSize) != 0)
-      _error->Warning("Unable to munmap");
-   
    iSize = 0;
    Base = 0;
    return true;
@@ -113,14 +143,24 @@
 /* This is done in syncronous mode - the docs indicate that this will 
    not return till all IO is complete */
 bool MMap::Sync()
-{   
+{
    if ((Flags & UnMapped) == UnMapped)
       return true;
    
 #ifdef _POSIX_SYNCHRONIZED_IO   
    if ((Flags & ReadOnly) != ReadOnly)
-      if (msync((char *)Base,iSize,MS_SYNC) != 0)
-	 return _error->Errno("msync","Unable to write mmap");
+     {
+       if (fd)
+	 {
+	   if (!fd->Seek (0) || !fd->Write (Base, iSize))
+	     return false;
+	 }
+       else
+	 {
+	   if (msync((char *)Base,iSize,MS_SYNC) != 0)
+	     return _error->Errno("msync","Unable to write mmap");
+	 }
+     }
 #endif   
    return true;
 }
@@ -136,8 +176,20 @@
 #ifdef _POSIX_SYNCHRONIZED_IO
    unsigned long PSize = sysconf(_SC_PAGESIZE);
    if ((Flags & ReadOnly) != ReadOnly)
-      if (msync((char *)Base+(int)(Start/PSize)*PSize,Stop - Start,MS_SYNC) != 0)
-	 return _error->Errno("msync","Unable to write mmap");
+     {
+       if (fd)
+	 {
+	   if (!fd->Seek (Start)
+	       || !fd->Write (((char *)Base)+Start, Stop-Start))
+	     return false;
+	 }
+       else
+	 {
+	   if (msync((char *)Base+(int)(Start/PSize)*PSize,Stop - Start,
+		     MS_SYNC) != 0)
+	     return _error->Errno("msync","Unable to write mmap");
+	 }
+     }
 #endif   
    return true;
 }

Modified: projects/haf/trunk/apt/apt-pkg/contrib/mmap.h
===================================================================
--- projects/haf/trunk/apt/apt-pkg/contrib/mmap.h	2007-02-15 15:49:47 UTC (rev 9917)
+++ projects/haf/trunk/apt/apt-pkg/contrib/mmap.h	2007-02-15 16:27:13 UTC (rev 9918)
@@ -47,6 +47,12 @@
    unsigned long iSize;
    void *Base;
 
+   // In case mmap can not be used, we keep a dup of the file
+   // descriptor that should have been mmaped so that we can write to
+   // the file in Sync().
+   //
+   FileFd *fd;
+
    bool Map(FileFd &Fd);
    bool Close(bool DoSync = true);
    


More information about the maemo-commits mailing list