Index: trunk/src/kernel32/HandleManager.cpp
===================================================================
--- trunk/src/kernel32/HandleManager.cpp	(revision 9910)
+++ trunk/src/kernel32/HandleManager.cpp	(revision 9911)
@@ -1,3 +1,3 @@
-/* $Id: HandleManager.cpp,v 1.94 2003-02-04 11:28:55 sandervl Exp $ */
+/* $Id: HandleManager.cpp,v 1.95 2003-03-06 10:44:32 sandervl Exp $ */
 
 /*
@@ -899,6 +899,5 @@
                        DWORD   fdwAccess,
                        BOOL    fInherit,
-                       DWORD   fdwOptions,
-                       DWORD   fdwOdinOptions)
+                       DWORD   fdwOptions)
 {
   int             iIndex;                     /* index into the handle table */
@@ -925,26 +924,10 @@
   /* initialize the complete HMHANDLEDATA structure */
   pHMHandleData                = &TabWin32Handles[iIndexNew].hmHandleData;
-  if (fdwOptions & DUPLICATE_SAME_ACCESS)
+  if (fdwOptions & DUPLICATE_SAME_ACCESS) 
       pHMHandleData->dwAccess    = TabWin32Handles[srchandle].hmHandleData.dwAccess;
   else
       pHMHandleData->dwAccess    = fdwAccess;
 
-  if((fdwOdinOptions & DUPLICATE_ACCESS_READWRITE) == DUPLICATE_ACCESS_READWRITE) {
-      pHMHandleData->dwAccess = GENERIC_READ | GENERIC_WRITE;
-  }
-  else
-  if(fdwOdinOptions & DUPLICATE_ACCESS_READ) {
-      pHMHandleData->dwAccess = GENERIC_READ;
-  }
-
-  if(fdwOdinOptions & DUPLICATE_SHARE_READ) {
-      pHMHandleData->dwShare = FILE_SHARE_READ;
-  }
-  else
-  if(fdwOdinOptions & DUPLICATE_SHARE_DENYNONE) {
-      pHMHandleData->dwShare = FILE_SHARE_READ | FILE_SHARE_WRITE;
-  }
-  else  pHMHandleData->dwShare = TabWin32Handles[srchandle].hmHandleData.dwShare;
-
+  pHMHandleData->dwShare       = TabWin32Handles[srchandle].hmHandleData.dwShare;
   pHMHandleData->dwCreation    = TabWin32Handles[srchandle].hmHandleData.dwCreation;
   pHMHandleData->dwFlags       = TabWin32Handles[srchandle].hmHandleData.dwFlags;
@@ -967,6 +950,5 @@
                                        fdwAccess,
                                        fInherit,
-                                       fdwOptions & ~DUPLICATE_CLOSE_SOURCE,
-                                       fdwOdinOptions);
+                                       fdwOptions & ~DUPLICATE_CLOSE_SOURCE, 0);
 
   //Don't let Open32 close it for us, but do it manually (regardless of error; see SDK docs))
Index: trunk/src/kernel32/hmdisk.cpp
===================================================================
--- trunk/src/kernel32/hmdisk.cpp	(revision 9910)
+++ trunk/src/kernel32/hmdisk.cpp	(revision 9911)
@@ -1,3 +1,3 @@
-/* $Id: hmdisk.cpp,v 1.61 2003-02-18 18:48:54 sandervl Exp $ */
+/* $Id: hmdisk.cpp,v 1.62 2003-03-06 10:44:33 sandervl Exp $ */
 
 /*
@@ -1839,11 +1839,9 @@
     if(map) {
         lpRealBuf = (LPVOID)((ULONG)map->getMappingAddr() + offset);
-        DWORD nrpages = nNumberOfBytesToRead/4096;
-        if(offset & 0xfff)
-            nrpages++;
-        if(nNumberOfBytesToRead & 0xfff)
+        DWORD nrpages = (nNumberOfBytesToRead+offset)/4096;
+        if((nNumberOfBytesToRead+offset) & 0xfff)
             nrpages++;
     
-        map->commitPage(offset & ~0xfff, TRUE, nrpages);
+        map->commitRange((ULONG)lpBuffer, offset & ~0xfff, TRUE, nrpages);
         map->Release();
     }
@@ -2105,11 +2103,9 @@
     if(map) {
         lpRealBuf = (LPVOID)((ULONG)map->getMappingAddr() + offset);
-        DWORD nrpages = nNumberOfBytesToWrite/4096;
-        if(offset & 0xfff)
-            nrpages++;
-        if(nNumberOfBytesToWrite & 0xfff)
+        DWORD nrpages = (nNumberOfBytesToWrite+offset)/4096;
+        if((nNumberOfBytesToWrite+offset) & 0xfff)
             nrpages++;
     
-        map->commitPage(offset & ~0xfff, TRUE, nrpages);
+        map->commitRange((ULONG)lpBuffer, offset & ~0xfff, TRUE, nrpages);
         map->Release();
     }
Index: trunk/src/kernel32/hmfile.cpp
===================================================================
--- trunk/src/kernel32/hmfile.cpp	(revision 9910)
+++ trunk/src/kernel32/hmfile.cpp	(revision 9911)
@@ -1,3 +1,3 @@
-/* $Id: hmfile.cpp,v 1.40 2003-02-18 18:48:54 sandervl Exp $ */
+/* $Id: hmfile.cpp,v 1.41 2003-03-06 10:44:33 sandervl Exp $ */
 
 /*
@@ -527,14 +527,22 @@
   if(map) {
        lpRealBuf = (LPVOID)((ULONG)map->getMappingAddr() + offset);
-       DWORD nrpages = nNumberOfBytesToRead/4096;
-       if(offset & 0xfff)
+       DWORD nrpages = (nNumberOfBytesToRead+offset)/4096;
+       if((nNumberOfBytesToRead+offset) & 0xfff)
            nrpages++;
-       if(nNumberOfBytesToRead & 0xfff)
-           nrpages++;
-
-       map->commitPage(offset & ~0xfff, TRUE, nrpages);
+
+       map->commitRange((ULONG)lpBuffer, offset & ~0xfff, TRUE, nrpages);
        map->Release();
   }
   else lpRealBuf = (LPVOID)lpBuffer;
+
+  //If this file is also used in a memory map somewhere, then we need
+  //to tell the map to flush all modified contents to disk right NOW
+  map = Win32MemMap::findMapByFile(pHMHandleData->hWin32Handle);
+  if(map) {
+      DWORD curpos = SetFilePointer(pHMHandleData, 0, NULL, FILE_CURRENT);
+
+      dprintf(("Flush memory maps to disk before reading!!"));
+      map->flushView(MMAP_FLUSHVIEW_ALL, curpos, nNumberOfBytesToRead);
+  }
 
   if(pHMHandleData->dwFlags & FILE_FLAG_OVERLAPPED) {
@@ -615,14 +623,23 @@
   if(map) {
        lpRealBuf = (LPVOID)((ULONG)map->getMappingAddr() + offset);
-       DWORD nrpages = nNumberOfBytesToWrite/4096;
-       if(offset & 0xfff)
-           nrpages++;
-       if(nNumberOfBytesToWrite & 0xfff)
+       DWORD nrpages = (nNumberOfBytesToWrite+offset)/4096;
+       if((nNumberOfBytesToWrite+offset) & 0xfff)
            nrpages++;
  
-       map->commitPage(offset & ~0xfff, TRUE, nrpages);
+       map->commitRange((ULONG)lpBuffer, offset & ~0xfff, TRUE, nrpages);
        map->Release();
   }
   else lpRealBuf = (LPVOID)lpBuffer;
+
+  //If this file is also used in a memory map somewhere, then we need
+  //to tell the map to flush all modified contents to disk right NOW
+  DWORD curfilepos;
+  map = Win32MemMap::findMapByFile(pHMHandleData->hWin32Handle);
+  if(map) {
+      curfilepos = SetFilePointer(pHMHandleData, 0, NULL, FILE_CURRENT);
+
+      dprintf(("Flush memory maps to disk before writing!!"));
+      map->flushView(MMAP_FLUSHVIEW_ALL, curfilepos, nNumberOfBytesToWrite);
+  }
 
   if(pHMHandleData->dwFlags & FILE_FLAG_OVERLAPPED) {
@@ -635,4 +652,9 @@
                             lpNumberOfBytesWritten);
 //  }
+
+  if(map && bRC) {
+      dprintf(("Invalidate memory map after file writing!!"));
+      map->invalidatePages(curfilepos, *lpNumberOfBytesWritten);
+  }
 
   dprintf(("KERNEL32: HMDeviceFileClass::WriteFile returned %08xh\n",
Index: trunk/src/kernel32/kernel32.mak
===================================================================
--- trunk/src/kernel32/kernel32.mak	(revision 9910)
+++ trunk/src/kernel32/kernel32.mak	(revision 9911)
@@ -1,3 +1,3 @@
-# $Id: kernel32.mak,v 1.41 2003-03-06 10:22:27 sandervl Exp $
+# $Id: kernel32.mak,v 1.42 2003-03-06 10:44:33 sandervl Exp $
 
 #
@@ -100,6 +100,4 @@
 $(OBJDIR)\time.obj \
 $(OBJDIR)\mmap.obj \
-$(OBJDIR)\mmapview.obj \
-$(OBJDIR)\mmapdup.obj \
 $(OBJDIR)\winimagepe2lx.obj \
 $(OBJDIR)\winimagepeldr.obj \
Index: trunk/src/kernel32/mmap.cpp
===================================================================
--- trunk/src/kernel32/mmap.cpp	(revision 9910)
+++ trunk/src/kernel32/mmap.cpp	(revision 9911)
@@ -1,3 +1,3 @@
-/* $Id: mmap.cpp,v 1.62 2003-02-18 18:58:47 sandervl Exp $ */
+/* $Id: mmap.cpp,v 1.63 2003-03-06 10:44:34 sandervl Exp $ */
 
 /*
@@ -126,13 +126,6 @@
                            &hMemFile, 0, FALSE, DUPLICATE_SAME_ACCESS) == FALSE)
 #else
-        DWORD dwOdinOptions;
-
-        if(!(mProtFlags & PAGE_READWRITE)) {
-                dwOdinOptions = DUPLICATE_ACCESS_READ | DUPLICATE_SHARE_DENYNONE;
-        }
-        else    dwOdinOptions = DUPLICATE_ACCESS_READWRITE | DUPLICATE_SHARE_DENYNONE;
-
-        if(HMDuplicateHandleOdin(GetCurrentProcess(), hMemFile, GetCurrentProcess(),
-                           &hMemFile, 0, FALSE, DUPLICATE_SAME_ACCESS, dwOdinOptions) == FALSE)
+        if(HMDuplicateHandle(GetCurrentProcess(), hMemFile, GetCurrentProcess(),
+                           &hMemFile, 0, FALSE, DUPLICATE_SAME_ACCESS) == FALSE)
 #endif
         {
@@ -265,9 +258,69 @@
 }
 //******************************************************************************
-//We determine whether a page has been modified by checking it's protection flags
-//If the write flag is set, this means commitPage had to enable this due to a pagefault
-//(all pages are readonly until the app tries to write to it)
-//******************************************************************************
-BOOL Win32MemMap::commitPage(ULONG offset, BOOL fWriteAccess, int nrpages)
+// Win32MemMap::commitRange
+//
+// Commit a range of pages
+//
+// Parameters:
+//
+//   ULONG ulFaultAddr          - exception address
+//   ULONG ulOffset             - offset in memory map
+//   BOOL  fWriteAccess         - TRUE  -> write exception
+//                                FALSE -> read exception
+//   int   nrpages              - number of pages
+//
+// Returns:
+//   TRUE                       - success
+//   FALSE                      - failure
+//
+//******************************************************************************
+BOOL Win32MemMap::commitRange(ULONG ulFaultAddr, ULONG offset, BOOL fWriteAccess, int nrpages)
+{
+    LPVOID lpPageFaultAddr = (LPVOID)((ULONG)pMapping + offset);
+    DWORD  pageAddr        = (DWORD)lpPageFaultAddr & ~0xFFF;
+
+    dprintf(("Win32MemMap::commitRange %x (faultaddr %x)", pageAddr, lpPageFaultAddr));
+
+    if(fWriteAccess) 
+    {//writes are handled on a per-page basis
+        for(int i=i;i<nrpages;i++) 
+        {
+            if(commitPage(ulFaultAddr, offset, TRUE, 1) == FALSE) {
+                dprintf(("Win32MemMap::commit: commitPage failed!!"));
+                return FALSE;
+            }
+            ulFaultAddr += PAGE_SIZE;
+            offset      += PAGE_SIZE;
+        }
+        return TRUE;
+    }
+    else    return commitPage(ulFaultAddr, offset, FALSE, nrpages);
+}
+//******************************************************************************
+// Win32MemMap::commitPage
+//
+// Handle a pagefault for a memory map
+//
+// Parameters:
+//
+//   ULONG ulFaultAddr          - exception address
+//   ULONG ulOffset             - offset in memory map
+//   BOOL  fWriteAccess         - TRUE  -> write exception
+//                                FALSE -> read exception
+//   int   nrpages              - number of pages
+//
+// Returns:
+//   TRUE                       - success
+//   FALSE                      - failure
+//
+// NOTE: 
+//   We handle only one pages for write access!
+//
+// REMARKS:
+//   We determine whether a page has been modified by checking it's protection flags
+//   If the write flag is set, this means commitPage had to enable this due to a pagefault
+//   (all pages are readonly until the app tries to write to it)
+//******************************************************************************
+BOOL Win32MemMap::commitPage(ULONG ulFaultAddr, ULONG offset, BOOL fWriteAccess, int nrpages)
 {
  MEMORY_BASIC_INFORMATION memInfo;
@@ -365,4 +418,44 @@
 }
 //******************************************************************************
+// Win32MemMap::commitGuardPage
+//
+// Handle a guard page exception for a copy-on-write view (one page only)
+//
+// Parameters:
+//
+//   ULONG ulFaultAddr          - exception address
+//   ULONG ulOffset             - offset in memory map
+//   BOOL  fWriteAccess         - TRUE  -> write exception
+//                                FALSE -> read exception
+//
+// Returns:
+//   TRUE                       - success
+//   FALSE                      - failure
+//
+//******************************************************************************
+BOOL Win32MemMap::commitGuardPage(ULONG ulFaultAddr, ULONG ulOffset, BOOL fWriteAccess)
+{
+    return FALSE;
+}
+//******************************************************************************
+// Win32MemMap::invalidatePages
+//
+// Invalidate map pages. (called by WriteFile)
+//
+// Parameters:
+//
+//   ULONG offset               - offset in memory map
+//   ULONG size                 - invalid range size
+//
+// Returns:
+//   TRUE                       - success
+//   FALSE                      - failure
+//
+//******************************************************************************
+BOOL Win32MemMap::invalidatePages(ULONG offset, ULONG size)
+{
+    return FALSE;
+}
+//******************************************************************************
 // Win32MemMap::unmapViewOfFile
 //
@@ -517,5 +610,5 @@
 //      I'm assuming they aren't for now.
 //******************************************************************************
-BOOL Win32MemMap::flushView(ULONG offset, ULONG cbFlush)
+BOOL Win32MemMap::flushView(ULONG viewaddr, ULONG offset, ULONG cbFlush)
 {
  LPVOID lpvBase = (LPVOID)((ULONG)pMapping+offset);
@@ -767,5 +860,5 @@
 
     if(mfAccess & MEMMAP_ACCESS_WRITE)
-        mParentMap->flushView(mOffset, mSize);
+        mParentMap->flushView(MMAP_FLUSHVIEW_ALL, mOffset, mSize);
 
     //Don't free memory for executable image map views (only used internally)
Index: trunk/src/kernel32/mmap.h
===================================================================
--- trunk/src/kernel32/mmap.h	(revision 9910)
+++ trunk/src/kernel32/mmap.h	(revision 9911)
@@ -1,3 +1,3 @@
-/* $Id: mmap.h,v 1.25 2003-02-18 18:48:55 sandervl Exp $ */
+/* $Id: mmap.h,v 1.26 2003-03-06 10:44:34 sandervl Exp $ */
 
 /*
@@ -28,7 +28,11 @@
 #define NRPAGES_TOCOMMIT        16
 
-#define MEMMAP_ACCESS_READ      1
-#define MEMMAP_ACCESS_WRITE     2
-#define MEMMAP_ACCESS_EXECUTE   4
+#define MEMMAP_ACCESS_INVALID           0
+#define MEMMAP_ACCESS_READ      	1
+#define MEMMAP_ACCESS_WRITE     	2
+#define MEMMAP_ACCESS_EXECUTE   	4
+#define MEMMAP_ACCESS_COPYONWRITE	8
+
+#define MMAP_FLUSHVIEW_ALL		0xFFFFFFFF
 
 class Win32MemMapView;
@@ -45,8 +49,8 @@
   ~Win32MemMap();
 
-   BOOL   Init(DWORD aMSize=0);
-   BOOL   flushView(ULONG offset, ULONG cbFlush);
-   LPVOID mapViewOfFile(ULONG size, ULONG offset, ULONG fdwAccess);
-   BOOL   unmapViewOfFile(LPVOID addr);
+virtual BOOL   Init(DWORD aMSize=0);
+virtual BOOL   flushView(ULONG viewaddr, ULONG offset, ULONG cbFlush);
+virtual LPVOID mapViewOfFile(ULONG size, ULONG offset, ULONG fdwAccess);
+virtual BOOL   unmapViewOfFile(LPVOID addr);
 
    HFILE  getFileHandle()                { return hMemFile; };
@@ -63,5 +67,8 @@
    void   Release();
 
-   BOOL   commitPage(ULONG offset, BOOL fWriteAccess, int nrpages = NRPAGES_TOCOMMIT);
+virtual BOOL   invalidatePages(ULONG offset, ULONG size);
+virtual BOOL   commitPage(ULONG ulFaultAddr, ULONG offset, BOOL fWriteAccess, int nrpages = NRPAGES_TOCOMMIT);
+virtual BOOL   commitGuardPage(ULONG ulFaultAddr, ULONG offset, BOOL fWriteAccess);
+        BOOL   commitRange(ULONG ulFaultAddr, ULONG offset, BOOL fWriteAccess, int nrpages);
 
 static Win32MemMap *findMap(LPSTR lpszName);
Index: trunk/src/kernel32/oslibmem.cpp
===================================================================
--- trunk/src/kernel32/oslibmem.cpp	(revision 9910)
+++ trunk/src/kernel32/oslibmem.cpp	(revision 9911)
@@ -1,3 +1,3 @@
-/* $Id: oslibmem.cpp,v 1.5 2002-09-24 15:15:27 sandervl Exp $ */
+/* $Id: oslibmem.cpp,v 1.6 2003-03-06 10:44:34 sandervl Exp $ */
 /*
  * Wrappers for OS/2 Dos* API
@@ -43,5 +43,4 @@
 
 //******************************************************************************
-//TODO: Assumes entire memory range has the same protection flags!
 //TODO: Check if this works for code aliases...
 //******************************************************************************
@@ -55,26 +54,4 @@
     cb+= PAGE_SIZE;
 
-    rc = DosQueryMem(pb, &size, &attr);
-    if(rc) {
-        dprintf(("!ERROR!: OSLibDosAliasMem: DosQueryMem %x %x return %d", pb, size, rc));
-        return rc;
-    }
-    size = (size-1) & ~0xfff;
-    size+= PAGE_SIZE;
-    if(size != cb) {
-        dprintf(("!WARNING!: OSLibDosAliasMem: size != cb (%x!=%x)!!!!!!!!", size, cb));
-        //ignore this and continue return 5;
-        attr = fl; //just use original protection flags (NOT CORRECT)
-    }
-    attr &= (PAG_READ|PAG_WRITE|PAG_EXECUTE|PAG_GUARD|PAG_DEFAULT);
-    if(attr != fl) {
-        rc = DosSetMem(pb, size, fl);
-        if(rc) {
-                dprintf(("!ERROR!: OSLibDosAliasMem: DosSetMem %x %x return %d", pb, size, rc));
-                attr = fl;
-                //just continue for now
-                //return rc;
-        }
-    }
     rc = DosAliasMem(pb, cb, ppbAlias, 2);
     if(rc) {
@@ -82,10 +59,26 @@
         return rc;
     }
-    if(attr != fl) {
-        rc = DosSetMem(pb, size, attr);
-        if(rc) {
-            dprintf(("!ERROR!: OSLibDosAliasMem: DosSetMem (2) %x %x return %d", pb, size, rc));
+    //Now try to change the protection flags of all pages in the aliased range
+    DWORD pAlias = (DWORD)*ppbAlias;
+
+    while(pAlias < (DWORD)*ppbAlias + cb) 
+    {
+        rc = DosQueryMem((PVOID)pAlias, &size, &attr);
+        if(rc != NO_ERROR) {
+            dprintf(("!ERROR!: OSLibDosAliasMem: DosQueryMem %x returned %d", pAlias, rc));
+            DebugInt3();
             return rc;
         }
+        //Don't bother if the pages are not committed. DosSetMem will return 
+        //ERROR_ACCESS_DENIED. 
+        if(attr & PAG_COMMIT) {
+            rc = DosSetMem((PVOID)pAlias, size, fl);
+            if(rc) {
+                dprintf(("!ERROR!: OSLibDosAliasMem: DosSetMem %x %x return %d", *ppbAlias, size, rc));
+                DebugInt3();
+                return rc;
+            }
+        }
+        pAlias += size;
     }
     return 0;
Index: trunk/src/kernel32/profile.h
===================================================================
--- trunk/src/kernel32/profile.h	(revision 9910)
+++ trunk/src/kernel32/profile.h	(revision 9911)
@@ -1,3 +1,3 @@
-/* $Id: profile.h,v 1.8 2000-12-04 12:41:48 bird Exp $ */
+/* $Id: profile.h,v 1.9 2003-03-06 10:44:34 sandervl Exp $ */
 /*
  * Profile header for initterm
@@ -14,34 +14,4 @@
 #define ODINININAME "ODIN.INI"
 
-void WINAPI WriteOutProfiles(void);
-int WINAPI PROFILE_LoadOdinIni(void);
-
-#ifdef DEBUG
-
-INT ODIN_EXTERN(GetPrivateProfileStringA)(LPCSTR section, LPCSTR entry,
-                                          LPCSTR def_val, LPSTR buffer,
-                                          UINT len, LPCSTR filename);
-
-int ODIN_EXTERN(PROFILE_GetOdinIniString)(LPCSTR section, LPCSTR entry,
-                                          LPCSTR def_val, LPSTR buffer,
-                                          UINT len);
-
-int ODIN_EXTERN(PROFILE_SetOdinIniString)(LPCSTR section_name, LPCSTR key_name,
-                                          LPCSTR value);
-
-int ODIN_EXTERN(PROFILE_GetOdinIniInt)(LPCSTR section_name, LPCSTR key_name,
-                                          int value);
-
-int ODIN_EXTERN(PROFILE_GetOdinIniBool)(LPCSTR section, LPCSTR key_name,
-                                          int def);
-
-UINT ODIN_EXTERN(GetPrivateProfileIntA)(LPCSTR, LPCSTR, INT, LPCSTR);
-UINT ODIN_EXTERN(GetPrivateProfileIntW)(LPCWSTR, LPCWSTR, INT, LPCWSTR);
-INT  ODIN_EXTERN(GetPrivateProfileStringW)(LPCWSTR, LPCWSTR, LPCWSTR, LPWSTR, UINT, LPCWSTR);
-BOOL ODIN_EXTERN(WritePrivateProfileStringA)(LPCSTR, LPCSTR, LPCSTR, LPCSTR);
-BOOL ODIN_EXTERN(WritePrivateProfileStringW)(LPCWSTR, LPCWSTR, LPCWSTR, LPCWSTR);
-
-#endif
-
 #include <win\options.h> //for odin profile apis
 
Index: trunk/src/kernel32/virtual.cpp
===================================================================
--- trunk/src/kernel32/virtual.cpp	(revision 9910)
+++ trunk/src/kernel32/virtual.cpp	(revision 9911)
@@ -1,3 +1,3 @@
-/* $Id: virtual.cpp,v 1.53 2003-03-03 16:30:44 sandervl Exp $ */
+/* $Id: virtual.cpp,v 1.54 2003-03-06 10:44:34 sandervl Exp $ */
 
 /*
@@ -180,5 +180,5 @@
         return FALSE;
     }
-    ret = map->flushView(offset, cbFlush);
+    ret = map->flushView((ULONG)base, offset, cbFlush);
     map->Release();
     return ret;
@@ -365,5 +365,5 @@
         if(map) {
             //TODO: We don't allow protection flag changes for mmaped files now
-            map->commitPage(offset, FALSE, nrpages);
+            map->commitPage((ULONG)lpvAddress, offset, FALSE, nrpages);
             map->Release();
             return lpvAddress;
