News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

setting end of mapped file

Started by gelatine1, February 01, 2016, 04:41:22 AM

Previous topic - Next topic

gelatine1

So I have this piece of code that opens a file and writes some data to it. Although when the program first opens the file it does not know how much data is going to be written to the file. But when the program closes and the file gets closed too I don't want all the unused bytes to stay in the file so I want to set the end of the file right after the last written byte. An attempt below but it doesn't work as expected. what did I do wrong ? what would be the best way to do this ?


.data
pos dd 0
.data?
fhandle dd ?
fmap dd ?
hmap dd ?

.code
invoke CreateFile,addr filename,GENERIC_WRITE or GENERIC_READ,0,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0
mov fhandle,eax
invoke CreateFileMapping,fhandle,0,PAGE_READWRITE,0,20000,0
mov hmap,eax
invoke MapViewOfFile,eax,FILE_MAP_WRITE,0,0,20000
mov fmap,eax
     
mov dword ptr [eax],01234567h ;random data
add pos,4 ;4 bytes written to the file

invoke SetFilePointer,fhandle,pos,0,FILE_BEGIN
invoke SetEndOfFile,fhandle
invoke SetFileValidData,fhandle,0,pos

invoke UnmapViewOfFile,fmap
invoke CloseHandle,hmap
invoke CloseHandle,fhandle

dedndave

i would keep track of data written in a QWORD
then, at exit, use SetEndOfFile

https://msdn.microsoft.com/en-us/library/windows/desktop/aa365531%28v=vs.85%29.aspx

(you set the file pointer to the EOF, then call SetEndOfFile)

fearless

https://msdn.microsoft.com/en-us/library/windows/desktop/aa365531%28v=vs.85%29.aspx
QuoteIf CreateFileMapping is called to create a file mapping object for hFile, UnmapViewOfFile must be called first to unmap all views and call CloseHandle to close the file mapping object before you can call SetEndOfFile.

Makes sense that you have to unmap the view of the file first as setendoffile along with read/write file api's are not guarenteed coherent.

https://msdn.microsoft.com/en-ie/library/windows/desktop/aa366537%28v=vs.85%29.aspx
QuoteA mapped file and a file that is accessed by using the input and output (I/O) functions (ReadFile and WriteFile) are not necessarily coherent.

hutch--

If you are not sharing the file, why do you need it as a memory mapped file ? Using a QWORD file pointer you can write a normal file the length of your free disk space on the HDD you are writing it to.

gelatine1

Quote from: fearless on February 01, 2016, 05:32:51 AM
https://msdn.microsoft.com/en-us/library/windows/desktop/aa365531%28v=vs.85%29.aspx
QuoteIf CreateFileMapping is called to create a file mapping object for hFile, UnmapViewOfFile must be called first to unmap all views and call CloseHandle to close the file mapping object before you can call SetEndOfFile.

Makes sense that you have to unmap the view of the file first as setendoffile along with read/write file api's are not guarenteed coherent.


Okay changing the order did the trick. thanks for helping me out.. I should've been able to figure that out myself sorry.

Quote from: hutch-- on February 01, 2016, 10:39:25 AM
If you are not sharing the file, why do you need it as a memory mapped file ? Using a QWORD file pointer you can write a normal file the length of your free disk space on the HDD you are writing it to.

I use mapped files for any file operation I do. Is it bad/slow in some cases ? It has some advantages too in some cases so if it is not very slow in some cases then I don't see why I wouldn't use memory mapped files ?

hutch--

gelatine1,

its basically that they do 2 different things, a normal file can be written to any length that the disk supports and you can move back and forth in the file address space easily where a memory mapped file is designed for a more special purpose, that of sharing data between 2 or more running executable file. Now among the conditions of a memory mapped file is its length must be set BEFORE you use it and it cannot be changed after.

Memory mapped files are really useful for inter application data sharing but conventional files are more flexible and open ended in their size.

dedndave

i have an application that uses memory-mapped files
the incoming data stream is of undetermined length (potentially huge)
the user may view any given section
not wanting to be tied down to memory constraints, it was best to stream data into the file
and view in different mappings