Recent

Author Topic: [SOLVED] FileExists doesn't always work in Windows  (Read 24812 times)

ASerge

  • Hero Member
  • *****
  • Posts: 2212
Re: FileExists doesn't always work in Windows
« Reply #15 on: August 16, 2018, 01:41:49 am »
My function contains an error: the last line should be like this:
Code: Pascal  [Select][+][-]
  1. Result := (Attr and FILE_ATTRIBUTE_DIRECTORY) = 0;
And now I'll try to explain why the (my) function is better than just FindFirst, FindClose.
1. GetFileAttributes is one system call, while FindFirst, FindClose is two.
2. GetFileAttributes exclude the names of such '*'.
3. GetFileAttributes finds a file in a directory, even if the user does not have permission to list the directory.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5444
  • Compiler Developer
Re: FileExists doesn't always work in Windows
« Reply #16 on: August 21, 2018, 04:21:09 pm »
Quote
In the case where GetFileAttribute() succeeds, Delphi has an option to handle FILE_ATTRIBUTE_REPARSE_POINT before checking for FILE_ATTRIBUTE_DIRECTORY.
Delphi has FollowLink parameter. FPC not. I tried to add features with FollowLink support, added patch on mantis, but the administration wasn't interested.
Which bug report? Was it resolved as "won't fix" or merely ignored? (the later more likely means that it was missed)

Bart

  • Hero Member
  • *****
  • Posts: 5265
    • Bart en Mariska's Webstek
Re: FileExists doesn't always work in Windows
« Reply #17 on: August 21, 2018, 04:27:08 pm »
Which bug report? Was it resolved as "won't fix" or merely ignored? (the later more likely means that it was missed)

A quick search for "followlink" in the bugtracker finds one entry: Issue #32370.

Bart

Thaddy

  • Hero Member
  • *****
  • Posts: 14169
  • Probably until I exterminate Putin.
Re: FileExists doesn't always work in Windows
« Reply #18 on: August 21, 2018, 07:01:42 pm »
Microsoft had itself some issues with that implementation: there were insufficient checks for validity, you could by-pass some levels.
By now (fixed in July 2018) this is fixed, but only if your system is up-to-date.
There is no reason not to have a naive implementation of "follow link". Just be careful with the OS calls that support it.
Specialize a type, not a var.

ASerge

  • Hero Member
  • *****
  • Posts: 2212
Re: FileExists doesn't always work in Windows
« Reply #19 on: August 23, 2018, 12:51:25 am »
Microsoft had itself some issues with that implementation: there were insufficient checks for validity, you could by-pass some levels.
By now (fixed in July 2018) this is fixed, but only if your system is up-to-date.
There is no reason not to have a naive implementation of "follow link". Just be careful with the OS calls that support it.
In my patch uses information from the Microsoft documentation about a reparse points of NTFS. This behavior has not changed since Vista.
I add new FileExists in issue 32370.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5444
  • Compiler Developer
Re: FileExists doesn't always work in Windows
« Reply #20 on: August 23, 2018, 07:23:27 am »
Which bug report? Was it resolved as "won't fix" or merely ignored? (the later more likely means that it was missed)

A quick search for "followlink" in the bugtracker finds one entry: Issue #32370.

Thanks. So my suspicion that it was mainly ignored/missed seems to be right. I'll try to give it a go...

DJMaster

  • New Member
  • *
  • Posts: 44
    • DJMaster on GitHub
Re: [SOLVED] FileExists doesn't always work in Windows
« Reply #21 on: August 31, 2020, 06:25:11 pm »
Hello,
I'd like to report a wrong behaviour of the FileExists function with OneDrive for Business files (I'm not having problems with OneDrive standard).

As reported in this thread (https://forum.lazarus.freepascal.org/index.php/topic,49564.msg359694.html#msg359694) I encountered problems accessing files stored into OneDrive for Business folders. WP solved the fpexif issue applying the following patch: https://sourceforge.net/p/lazarus-ccr/svn/7416/

Today I decided to investigate a bit more about this problem because I encountered the same issue with INI files and realized that it is a general FPC 3.2.0+ RTL source issue... every unit that uses FileExists is affected.

I created the attached test program (test.pas) to see the differences between FPC 3.0.4 and FPC 3.2.0. Results are displayed into the attached ALP_error.jpg image. The issue is caused by the L attribute applied to files under OneDrive for Business folder after online synchronization.

Before synchronization, files are marked with AP attributes only, and the issue is not present (look at the attached AP_ok.jpg image), after a few seconds synchronization ends, attribute is set to ALP, and the issue appears (look at the attached ALP_error.jpg image).

I think the problem is inside the new FPC 3.2.0+ FileExists/FileOrDirExists implementation and I'd like to help solving this issue, you can consider me available for testings with OneDrive for Business.

Thank you.

jamie

  • Hero Member
  • *****
  • Posts: 6077
Re: [SOLVED] FileExists doesn't always work in Windows
« Reply #22 on: August 31, 2020, 07:17:19 pm »
I think I ran into something like that before and ended up using FindFirstFile instead which worked.

But I don't think it was OneDrive about another situation

The only true wisdom is knowing you know nothing

DJMaster

  • New Member
  • *
  • Posts: 44
    • DJMaster on GitHub
Re: [SOLVED] FileExists doesn't always work in Windows
« Reply #23 on: August 31, 2020, 08:19:58 pm »
I think I ran into something like that before and ended up using FindFirstFile instead which worked.

But I don't think it was OneDrive about another situation

FileExists is used widely within a lot of packages (I mentioned RTL, but I was thinking about FPC Packages and Lazarus LCL, etc.)... It can't be fixed everywhere... there must be a global fix for this issue within RTL.

ASerge

  • Hero Member
  • *****
  • Posts: 2212
Re: [SOLVED] FileExists doesn't always work in Windows
« Reply #24 on: August 31, 2020, 09:08:27 pm »
I think the problem is inside the new FPC 3.2.0+ FileExists/FileOrDirExists implementation and I'd like to help solving this issue, you can consider me available for testings with OneDrive for Business.
Do it if you think so. Extract FileOrDirExists and related functions and test them in the debugger to find where they fail.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11352
  • FPC developer.
Re: [SOLVED] FileExists doesn't always work in Windows
« Reply #25 on: August 31, 2020, 11:31:43 pm »
I'm not sure why you need that patch?

If they are links, the links should be resolvable ? Or are the links only resolvable under certain conditions?

Anyway, trying to pull this issue (with onedrive internal implementation) to a broad RTL issue is wrong.

The fpexif patch now fails to resolve links to jpgs if some other package decides to follow links (and thus is IMHO also wrong, the follow link default is true for good reasons and as per MS advice).

If it is *really* important to let fpexif access onedrive, then a property that can be set/reset for microsoft business should be added, leaving it up to the programmers discretion.

But turning off all support for following symlinks in the RTL because of a onedrive problem is definitely not the way to go.
« Last Edit: August 31, 2020, 11:33:36 pm by marcov »

DJMaster

  • New Member
  • *
  • Posts: 44
    • DJMaster on GitHub
Re: [SOLVED] FileExists doesn't always work in Windows
« Reply #26 on: September 01, 2020, 12:19:56 am »
As far as I can see, the issue is generated by FPC 3.2.0+ implementation of FileExists / FileGetSymLinkTargetInt with OneDrive for Business files (with ALP attributes). It is clear that all software developed with Lazarus in combination with FPC 3.2.0+ is affected by this issue. I moved back to FPC 3.0.4 just in time.

Thaddy

  • Hero Member
  • *****
  • Posts: 14169
  • Probably until I exterminate Putin.
Re: [SOLVED] FileExists doesn't always work in Windows
« Reply #27 on: September 01, 2020, 07:12:09 am »
Well, there are already overloads in sysutils. See the docs:
https://www.freepascal.org/docs-html/rtl/sysutils/fileexists.html
Specialize a type, not a var.

fred

  • Full Member
  • ***
  • Posts: 201
Re: [SOLVED] FileExists doesn't always work in Windows
« Reply #28 on: September 01, 2020, 08:49:36 am »
Yesterday I found out that files did not seem to exists on a network share even when they are there.
Before the files could be found by FileExists(), I have read+write access to the files.
I could not quickly find an answer why, as a quick workaround I made a FileExists using FileAge() >= 0 and this worked as expected.
I had no time to look into it yet but the only changes are the new Laz 2.0.10 and fpc 3.2.0.

jamie

  • Hero Member
  • *****
  • Posts: 6077
Re: [SOLVED] FileExists doesn't always work in Windows
« Reply #29 on: September 01, 2020, 03:45:47 pm »
I wanted to note that "GetFileAttributeW" requires a prepending "\\?\" to the file path for the longer than MAX Path values..

 Only recent Windows 10 updates have allowed optionally to not need this..

 Just a thought..
The only true wisdom is knowing you know nothing

 

TinyPortal © 2005-2018