Deleting an opened memory mapped file?

Why do I get “Access is denied” error when I try to delete a memory mapped
file?
The file is opened with the FILE_SHARE_DELETE flag.

DeleteFile() would succeed if it is called BEFORE calling
CreateFileMapping().

Here is the code:

fh = CreateFile(
“testfile”, // file name
GENERIC_READ | GENERIC_WRITE, // access mode
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, // share mode
NULL, // SD
CREATE_ALWAYS, // how to create
FILE_ATTRIBUTE_NORMAL, // file attributes
NULL // handle to template file
);

//DeleteFile(“testfile”); succeeds here, CreateFileMapping() would succeed
too

fileMappingHandle = CreateFileMapping(
fh, // handle to file
NULL, // security
PAGE_READWRITE, // protection
0, // high-order DWORD of size
64*1024, // low-order DWORD of size
“DelFileTest” // object name
);

//DeleteFile(“testfile”); fails here

Dmitriy Budko, VMware

File systems explicitly check if the file is memory-mapped when processing
IRP_SET_INFORMATION/FileDispositionInformation and denies deletion if there
is memory mapping. File system will not be able to truncate file when handle
is closed if it allow deletion to proceed.

Alexei.

“Dmitriy Budko” wrote in message news:xxxxx@ntfsd…
Why do I get “Access is denied” error when I try to delete a memory mapped
file?
The file is opened with the FILE_SHARE_DELETE flag.

DeleteFile() would succeed if it is called BEFORE calling
CreateFileMapping().

Here is the code:

fh = CreateFile(
“testfile”, // file name
GENERIC_READ | GENERIC_WRITE, // access mode
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, // share mode
NULL, // SD
CREATE_ALWAYS, // how to create
FILE_ATTRIBUTE_NORMAL, // file attributes
NULL // handle to template file
);

//DeleteFile(“testfile”); succeeds here, CreateFileMapping() would
succeed
too

fileMappingHandle = CreateFileMapping(
fh, // handle to file
NULL, // security
PAGE_READWRITE, // protection
0, // high-order DWORD of size
64*1024, // low-order DWORD of size
“DelFileTest” // object name
);

//DeleteFile(“testfile”); fails here

Dmitriy Budko, VMware

> fh = CreateFile(

“testfile”, // file name
GENERIC_READ | GENERIC_WRITE, // access mode
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, // share mode
NULL, // SD
CREATE_ALWAYS, // how to create
FILE_ATTRIBUTE_NORMAL, // file attributes
NULL // handle to template file
);

//DeleteFile(“testfile”); succeeds here, CreateFileMapping() would succeed
too

fileMappingHandle = CreateFileMapping(
fh, // handle to file
NULL, // security
PAGE_READWRITE, // protection
0, // high-order DWORD of size
64*1024, // low-order DWORD of size
“DelFileTest” // object name
);

I guess, by default when u create a file mapping the same file be used
for fetching contents or flushing into disk. So, it doesnt allow to
delete it. try with “PAGE_READWRITE | SEC_NOCACHE” it will allow you
to delete the file. ( Last call of your code will get success ).

Regards,
Yash K.S