Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2015-01

TFileStream.Create and hidden files on Windows

4 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Bart

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

Reply by email

Michael Van Canneyt
to Bart
Bart wrote 14 lines
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.

It seems strange that rewrite/reset handles it, because a quick search reveals that there is no handling of FILE_ATTRIBUTE_HIDDEN in the RTL.

Michael.

Reply by email

Bart
to Michael Van Canneyt
Michael Van Canneyt wrote
It seems strange that rewrite/reset handles it, because a quick search reveals that there is no handling of FILE_ATTRIBUTE_HIDDEN in the RTL.

Reset handles it, rewrite not (access denied (GetLastOSError = 5)). TFileStream.Create with fmCreate also fails, but with GetLasOSError = 6 (invalid entry).

Chances are I replaced some Resets with TFileStream.Create when saving the file in question.

So, in the end it's all my own doing.

Sorry for wasting your time.

Bart

Reply by email

Mark Morgan Lloyd
to Bart
Bart wrote 16 lines
On 1/15/15, Michael Van Canneyt wrote: > It seems strange that rewrite/reset handles it, because a quick search > reveals > that there is no handling of FILE_ATTRIBUTE_HIDDEN in the RTL. Reset handles it, rewrite not (access denied (GetLastOSError = 5)). TFileStream.Create with fmCreate also fails, but with GetLasOSError = 6 (invalid entry). Chances are I replaced some Resets with TFileStream.Create when saving the file in question. So, in the end it's all my own doing. Sorry for wasting your time.

Although it also affects TInifile on Windows, which has made your comments interesting.

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]

Reply by email