* Sync up to trunk HEAD (r62975).
[reactos.git] / dll / win32 / ole32 / storage32.c
index 57a7fef..fd59674 100644 (file)
@@ -119,20 +119,20 @@ typedef struct TransactedDirEntry
   DirRef transactedParentEntry;
 
   /* True if this entry is being used. */
-  int inuse;
+  BOOL inuse;
 
   /* True if data is up to date. */
-  int read;
+  BOOL read;
 
   /* True if this entry has been modified. */
-  int dirty;
+  BOOL dirty;
 
   /* True if this entry's stream has been modified. */
-  int stream_dirty;
+  BOOL stream_dirty;
 
   /* True if this entry has been deleted in the transacted storage, but the
    * delete has not yet been committed. */
-  int deleted;
+  BOOL deleted;
 
   /* If this entry's stream has been modified, a reference to where the stream
    * is stored in the snapshot file. */
@@ -1404,7 +1404,7 @@ static HRESULT insertIntoTree(
      * The root storage contains some element, therefore, start the research
      * for the appropriate location.
      */
-    BOOL found = 0;
+    BOOL found = FALSE;
     DirRef current, next, previous, currentEntryId;
 
     /*
@@ -1423,7 +1423,7 @@ static HRESULT insertIntoTree(
     next     = currentEntry.rightChild;
     current  = currentEntryId;
 
-    while (found == 0)
+    while (!found)
     {
       LONG diff = entryNameCmp( newEntry.name, currentEntry.name);
 
@@ -1442,7 +1442,7 @@ static HRESULT insertIntoTree(
           StorageBaseImpl_WriteDirEntry(This,
                                         current,
                                         &currentEntry);
-          found = 1;
+          found = TRUE;
         }
       }
       else if (diff > 0)
@@ -1460,7 +1460,7 @@ static HRESULT insertIntoTree(
           StorageBaseImpl_WriteDirEntry(This,
                                         current,
                                         &currentEntry);
-          found = 1;
+          found = TRUE;
         }
       }
       else
@@ -2767,7 +2767,7 @@ static HRESULT StorageImpl_Construct(
   else
     This->base.lockingrole = SWMR_None;
 
-  This->base.reverted = 0;
+  This->base.reverted = FALSE;
 
   /*
    * Initialize the big block cache.
@@ -2997,7 +2997,7 @@ static void StorageImpl_Invalidate(StorageBaseImpl* iface)
 
   StorageBaseImpl_DeleteAll(&This->base);
 
-  This->base.reverted = 1;
+  This->base.reverted = TRUE;
 }
 
 static void StorageImpl_Destroy(StorageBaseImpl* iface)
@@ -4364,7 +4364,7 @@ static DirRef TransactedSnapshotImpl_FindFreeEntry(TransactedSnapshotImpl *This)
     This->entries_size = new_size;
   }
 
-  This->entries[result].inuse = 1;
+  This->entries[result].inuse = TRUE;
 
   This->firstFreeEntry = result+1;
 
@@ -4385,7 +4385,7 @@ static DirRef TransactedSnapshotImpl_CreateStubEntry(
 
     entry->newTransactedParentEntry = entry->transactedParentEntry = parentEntryRef;
 
-    entry->read = 0;
+    entry->read = FALSE;
   }
 
   return stubEntryRef;
@@ -4430,7 +4430,7 @@ static HRESULT TransactedSnapshotImpl_EnsureReadEntry(
     if (SUCCEEDED(hr))
     {
       memcpy(&This->entries[entry].data, &data, sizeof(DirEntry));
-      This->entries[entry].read = 1;
+      This->entries[entry].read = TRUE;
     }
   }
 
@@ -4469,7 +4469,7 @@ static HRESULT TransactedSnapshotImpl_MakeStreamDirty(
     }
 
     if (SUCCEEDED(hr))
-      This->entries[entry].stream_dirty = 1;
+      This->entries[entry].stream_dirty = TRUE;
 
     if (This->entries[entry].transactedParentEntry != DIRENTRY_NULL)
     {
@@ -4479,7 +4479,7 @@ static HRESULT TransactedSnapshotImpl_MakeStreamDirty(
       delete_ref = TransactedSnapshotImpl_CreateStubEntry(This, This->entries[entry].transactedParentEntry);
 
       if (delete_ref != DIRENTRY_NULL)
-        This->entries[delete_ref].deleted = 1;
+        This->entries[delete_ref].deleted = TRUE;
 
       This->entries[entry].transactedParentEntry = This->entries[entry].newTransactedParentEntry = DIRENTRY_NULL;
     }
@@ -4773,9 +4773,9 @@ static HRESULT WINAPI TransactedSnapshotImpl_Commit(
           {
             StorageBaseImpl_StreamSetSize(This->scratch, entry->stream_entry, zero);
             StorageBaseImpl_DestroyDirEntry(This->scratch, entry->stream_entry);
-            entry->stream_dirty = 0;
+            entry->stream_dirty = FALSE;
           }
-          entry->dirty = 0;
+          entry->dirty = FALSE;
           entry->transactedParentEntry = entry->newTransactedParentEntry;
         }
       }
@@ -4831,7 +4831,7 @@ static void TransactedSnapshotImpl_Invalidate(StorageBaseImpl* This)
   {
     TRACE("Storage invalidated (stg=%p)\n", This);
 
-    This->reverted = 1;
+    This->reverted = TRUE;
 
     StorageBaseImpl_DeleteAll(This);
   }
@@ -4875,8 +4875,8 @@ static HRESULT TransactedSnapshotImpl_CreateDirEntry(StorageBaseImpl *base,
   new_entry = &This->entries[new_ref];
 
   new_entry->newTransactedParentEntry = new_entry->transactedParentEntry = DIRENTRY_NULL;
-  new_entry->read = 1;
-  new_entry->dirty = 1;
+  new_entry->read = TRUE;
+  new_entry->dirty = TRUE;
   memcpy(&new_entry->data, newData, sizeof(DirEntry));
 
   *index = new_ref;
@@ -4901,7 +4901,7 @@ static HRESULT TransactedSnapshotImpl_WriteDirEntry(StorageBaseImpl *base,
 
   if (index != This->base.storageDirEntry)
   {
-    This->entries[index].dirty = 1;
+    This->entries[index].dirty = TRUE;
 
     if (data->size.QuadPart == 0 &&
         This->entries[index].transactedParentEntry != DIRENTRY_NULL)
@@ -4912,7 +4912,7 @@ static HRESULT TransactedSnapshotImpl_WriteDirEntry(StorageBaseImpl *base,
       delete_ref = TransactedSnapshotImpl_CreateStubEntry(This, This->entries[index].transactedParentEntry);
 
       if (delete_ref != DIRENTRY_NULL)
-        This->entries[delete_ref].deleted = 1;
+        This->entries[delete_ref].deleted = TRUE;
 
       This->entries[index].transactedParentEntry = This->entries[index].newTransactedParentEntry = DIRENTRY_NULL;
     }
@@ -4953,7 +4953,7 @@ static HRESULT TransactedSnapshotImpl_DestroyDirEntry(StorageBaseImpl *base,
   }
   else
   {
-    This->entries[index].deleted = 1;
+    This->entries[index].deleted = TRUE;
   }
 
   return S_OK;
@@ -5029,7 +5029,7 @@ static HRESULT TransactedSnapshotImpl_StreamSetSize(StorageBaseImpl *base,
         This->entries[index].stream_entry, zero);
       StorageBaseImpl_DestroyDirEntry(This->scratch,
         This->entries[index].stream_entry);
-      This->entries[index].stream_dirty = 0;
+      This->entries[index].stream_dirty = FALSE;
     }
     else if (This->entries[index].transactedParentEntry != DIRENTRY_NULL)
     {
@@ -5037,7 +5037,7 @@ static HRESULT TransactedSnapshotImpl_StreamSetSize(StorageBaseImpl *base,
       delete_ref = TransactedSnapshotImpl_CreateStubEntry(This, This->entries[index].transactedParentEntry);
 
       if (delete_ref != DIRENTRY_NULL)
-        This->entries[delete_ref].deleted = 1;
+        This->entries[delete_ref].deleted = TRUE;
 
       This->entries[index].transactedParentEntry = This->entries[index].newTransactedParentEntry = DIRENTRY_NULL;
     }
@@ -5236,7 +5236,7 @@ static void StorageInternalImpl_Invalidate( StorageBaseImpl *base )
   {
     TRACE("Storage invalidated (stg=%p)\n", This);
 
-    This->base.reverted = 1;
+    This->base.reverted = TRUE;
 
     This->parentStorage = NULL;
 
@@ -5721,7 +5721,7 @@ static StorageInternalImpl* StorageInternalImpl_Construct(
     newStorage->base.baseVtbl = &StorageInternalImpl_BaseVtbl;
     newStorage->base.openFlags = (openFlags & ~STGM_CREATE);
 
-    newStorage->base.reverted = 0;
+    newStorage->base.reverted = FALSE;
 
     newStorage->base.ref = 1;
 
@@ -5732,7 +5732,7 @@ static StorageInternalImpl* StorageInternalImpl_Construct(
      */
     newStorage->base.storageDirEntry = storageDirEntry;
 
-    newStorage->base.create = 0;
+    newStorage->base.create = FALSE;
 
     return newStorage;
   }
@@ -6019,10 +6019,10 @@ HRESULT BlockChainStream_GetBlockAtOffset(BlockChainStream *This,
     {
       if (!StorageImpl_WriteBigBlock(This->parentStorage, result->sector, result->data))
         return STG_E_WRITEFAULT;
-      result->dirty = 0;
+      result->dirty = FALSE;
     }
 
-    result->read = 0;
+    result->read = FALSE;
     result->index = index;
     result->sector = *sector;
   }
@@ -6047,9 +6047,9 @@ BlockChainStream* BlockChainStream_Construct(
   newStream->indexCacheLen           = 0;
   newStream->indexCacheSize          = 0;
   newStream->cachedBlocks[0].index = 0xffffffff;
-  newStream->cachedBlocks[0].dirty = 0;
+  newStream->cachedBlocks[0].dirty = FALSE;
   newStream->cachedBlocks[1].index = 0xffffffff;
-  newStream->cachedBlocks[1].dirty = 0;
+  newStream->cachedBlocks[1].dirty = FALSE;
   newStream->blockToEvict          = 0;
 
   if (FAILED(BlockChainStream_UpdateIndexCache(newStream)))
@@ -6071,7 +6071,7 @@ HRESULT BlockChainStream_Flush(BlockChainStream* This)
     if (This->cachedBlocks[i].dirty)
     {
       if (StorageImpl_WriteBigBlock(This->parentStorage, This->cachedBlocks[i].sector, This->cachedBlocks[i].data))
-        This->cachedBlocks[i].dirty = 0;
+        This->cachedBlocks[i].dirty = FALSE;
       else
         return STG_E_WRITEFAULT;
     }
@@ -6211,7 +6211,7 @@ HRESULT BlockChainStream_ReadAt(BlockChainStream* This,
         if (FAILED(StorageImpl_ReadBigBlock(This->parentStorage, cachedBlock->sector, cachedBlock->data, &read)) && !read)
           return STG_E_READFAULT;
 
-        cachedBlock->read = 1;
+        cachedBlock->read = TRUE;
       }
 
       memcpy(bufferWalker, cachedBlock->data+offsetInBlock, bytesToReadInBuffer);
@@ -6299,8 +6299,8 @@ HRESULT BlockChainStream_WriteAt(BlockChainStream* This,
 
       memcpy(cachedBlock->data+offsetInBlock, bufferWalker, bytesToWrite);
       bytesWrittenAt = bytesToWrite;
-      cachedBlock->read = 1;
-      cachedBlock->dirty = 1;
+      cachedBlock->read = TRUE;
+      cachedBlock->dirty = TRUE;
     }
 
     blockNoInSequence++;
@@ -6402,7 +6402,7 @@ static BOOL BlockChainStream_Shrink(BlockChainStream* This,
     if (This->cachedBlocks[i].index >= numBlocks)
     {
       This->cachedBlocks[i].index = 0xffffffff;
-      This->cachedBlocks[i].dirty = 0;
+      This->cachedBlocks[i].dirty = FALSE;
     }
   }
 
@@ -7590,11 +7590,21 @@ HRESULT WINAPI StgOpenStorage(
   HANDLE         hFile = 0;
   DWORD          shareMode;
   DWORD          accessMode;
+  LPWSTR         temp_name = NULL;
 
   TRACE("(%s, %p, %x, %p, %d, %p)\n",
        debugstr_w(pwcsName), pstgPriority, grfMode,
        snbExclude, reserved, ppstgOpen);
 
+  if (pstgPriority)
+  {
+    /* FIXME: Copy ILockBytes instead? But currently for STGM_PRIORITY it'll be read-only. */
+    hr = StorageBaseImpl_GetFilename((StorageBaseImpl*)pstgPriority, &temp_name);
+    if (FAILED(hr)) goto end;
+    pwcsName = temp_name;
+    TRACE("using filename %s\n", debugstr_w(temp_name));
+  }
+
   if (pwcsName == 0)
   {
     hr = STG_E_INVALIDNAME;
@@ -7756,6 +7766,8 @@ HRESULT WINAPI StgOpenStorage(
   *ppstgOpen = &newStorage->IStorage_iface;
 
 end:
+  CoTaskMemFree(temp_name);
+  if (pstgPriority) IStorage_Release(pstgPriority);
   TRACE("<-- %08x, IStorage %p\n", hr, ppstgOpen ? *ppstgOpen : NULL);
   return hr;
 }
@@ -8667,7 +8679,10 @@ static HRESULT STREAM_ReadString( IStream *stm, LPWSTR *string )
     len = MultiByteToWideChar( CP_ACP, 0, str, count, NULL, 0 );
     wstr = CoTaskMemAlloc( (len + 1)*sizeof (WCHAR) );
     if( wstr )
+    {
          MultiByteToWideChar( CP_ACP, 0, str, count, wstr, len );
+         wstr[len] = 0;
+    }
     CoTaskMemFree( str );
 
     *string = wstr;
@@ -8726,32 +8741,37 @@ static HRESULT STORAGE_WriteCompObj( LPSTORAGE pstg, CLSID *clsid,
 HRESULT WINAPI WriteFmtUserTypeStg(
          LPSTORAGE pstg, CLIPFORMAT cf, LPOLESTR lpszUserType)
 {
+    STATSTG stat;
     HRESULT r;
     WCHAR szwClipName[0x40];
-    CLSID clsid = CLSID_NULL;
+    CLSID clsid;
     LPWSTR wstrProgID = NULL;
     DWORD n;
 
     TRACE("(%p,%x,%s)\n",pstg,cf,debugstr_w(lpszUserType));
 
     /* get the clipboard format name */
-    n = GetClipboardFormatNameW( cf, szwClipName, sizeof(szwClipName)/sizeof(szwClipName[0]) );
-    szwClipName[n]=0;
+    if( cf )
+    {
+        n = GetClipboardFormatNameW( cf, szwClipName,
+                sizeof(szwClipName)/sizeof(szwClipName[0]) );
+        szwClipName[n]=0;
+    }
 
     TRACE("Clipboard name is %s\n", debugstr_w(szwClipName));
 
-    /* FIXME: There's room to save a CLSID and its ProgID, but
-       the CLSID is not looked up in the registry and in all the
-       tests I wrote it was CLSID_NULL.  Where does it come from?
-    */
+    r = IStorage_Stat(pstg, &stat, STATFLAG_NONAME);
+    if(SUCCEEDED(r))
+        clsid = stat.clsid;
+    else
+        clsid = CLSID_NULL;
 
-    /* get the real program ID.  This may fail, but that's fine */
     ProgIDFromCLSID(&clsid, &wstrProgID);
 
     TRACE("progid is %s\n",debugstr_w(wstrProgID));
 
-    r = STORAGE_WriteCompObj( pstg, &clsid, 
-                              lpszUserType, szwClipName, wstrProgID );
+    r = STORAGE_WriteCompObj( pstg, &clsid, lpszUserType,
+            cf ? szwClipName : NULL, wstrProgID );
 
     CoTaskMemFree(wstrProgID);
 
@@ -9472,51 +9492,52 @@ HRESULT WINAPI GetConvertStg(IStorage *stg)
  */
 HRESULT WINAPI SetConvertStg(IStorage *storage, BOOL convert)
 {
+    static const WCHAR stream_1oleW[] = {1,'O','l','e',0};
     DWORD flags = convert ? OleStream_Convert : 0;
+    IStream *stream;
+    DWORD header[2];
     HRESULT hr;
 
     TRACE("(%p, %d)\n", storage, convert);
 
-    hr = STORAGE_CreateOleStream(storage, flags);
-    if (hr == STG_E_FILEALREADYEXISTS)
+    hr = IStorage_OpenStream(storage, stream_1oleW, NULL, STGM_READWRITE | STGM_SHARE_EXCLUSIVE, 0, &stream);
+    if (FAILED(hr))
     {
-        static const WCHAR stream_1oleW[] = {1,'O','l','e',0};
-        IStream *stream;
-        DWORD header[2];
+        if (hr != STG_E_FILENOTFOUND)
+            return hr;
 
-        hr = IStorage_OpenStream(storage, stream_1oleW, NULL, STGM_READWRITE | STGM_SHARE_EXCLUSIVE, 0, &stream);
-        if (FAILED(hr)) return hr;
+        return STORAGE_CreateOleStream(storage, flags);
+    }
 
-        hr = IStream_Read(stream, header, sizeof(header), NULL);
+    hr = IStream_Read(stream, header, sizeof(header), NULL);
+    if (FAILED(hr))
+    {
+        IStream_Release(stream);
+        return hr;
+    }
+
+    /* update flag if differs */
+    if ((header[1] ^ flags) & OleStream_Convert)
+    {
+        LARGE_INTEGER pos = {{0}};
+
+        if (header[1] & OleStream_Convert)
+            flags = header[1] & ~OleStream_Convert;
+        else
+            flags = header[1] |  OleStream_Convert;
+
+        pos.QuadPart = sizeof(DWORD);
+        hr = IStream_Seek(stream, pos, STREAM_SEEK_SET, NULL);
         if (FAILED(hr))
         {
             IStream_Release(stream);
             return hr;
         }
 
-        /* update flag if differs */
-        if ((header[1] ^ flags) & OleStream_Convert)
-        {
-            LARGE_INTEGER pos;
-
-            if (header[1] & OleStream_Convert)
-                flags = header[1] & ~OleStream_Convert;
-            else
-                flags = header[1] |  OleStream_Convert;
-
-            pos.QuadPart = sizeof(DWORD);
-            hr = IStream_Seek(stream, pos, STREAM_SEEK_SET, NULL);
-            if (FAILED(hr))
-            {
-                IStream_Release(stream);
-                return hr;
-            }
-
-            hr = IStream_Write(stream, &flags, sizeof(flags), NULL);
-        }
-        IStream_Release(stream);
+        hr = IStream_Write(stream, &flags, sizeof(flags), NULL);
     }
 
+    IStream_Release(stream);
     return hr;
 }