[fpc-pascal] TFileStream.Create and hidden files on Windows
Bart
bartjunk64 at gmail.com
Thu Jan 15 17:28:28 CET 2015
Hi,
I have some old tools lying around that are still in use, and I
modernized them a bit.
One part of that is to replace file handling with FileStreams whenever
apllicable.
This all went without major hickups, until one of my tools suddenly crashed.
Where in the past it did a Rewrite(Filevar,1), it now uses
TFileStream.Create with appropriate parameters.
The file in question turned out to have the hidden attribute set.
While this poses no problem to rewrite/reset, TFileStream.Create does
not like it and fails.
MS explains why:
http://msdn.microsoft.com/en-us/library/windows/desktop/aa363858%28v=vs.85%29.aspx
Of course it was a simple fix.
But I found it a little bit strange and asked myself why we did not
handle this inside FileCreate?
An obvious reason might be that Delphi (7) behaves the same.
But from a cross-platform standpoint the same code runs without
problems on hidden file in linux (and yes, I know hidden files on
linus is a different concept).
But now I have to fiddle with the attribute (twice) and ifdef it for
windows in all of my code using TFileStream, which looks ugly.
So why not have something like:
function FileCreate(Const FileName : string; ShareMode: Integer;
Rights: Integer) : THandle;
var
dwFlagAndAttributes: DWORD;
Attr: LongInt;
begin
dwFlagAndAttributes := FILE_ATTRIBUTE_NORMAL;
Attr := FileGetAttrUtf8(FileName);
if (Attr <> LongInt(feInvalidHandle)) and ((Attr and faHidden) > 0) then
begin
//If a file is hidden than CreateFile fails if it does not have
FILE_ATTRIBUTE_HIDDEN set
//http://msdn.microsoft.com/en-us/library/windows/desktop/aa363858%28v=vs.85%29.aspx
dwFlagAndAttributes := dwFlagAndAttributes or FILE_ATTRIBUTE_HIDDEN;
end;
Result := CreateFileW(PWideChar(UTF8Decode(FileName)), GENERIC_READ
or GENERIC_WRITE,
dword(ShareModes[(ShareMode and $F0) shr 4]),
nil, CREATE_ALWAYS, dwFlagAndAttributes, 0);
end;
Or, alternatively handle it in TFileStream.Create?
Bart
More information about the fpc-pascal
mailing list