strange: findfirst / find next (

Antworten
d.oertel
Beiträge: 5
Registriert: Fr 7. Jan 2011, 14:00

strange: findfirst / find next (

Beitrag von d.oertel »

Hi to all,

first of all my program runs with 'system' privileges (started from a service).
what i do is:
creating a symbolik link
try fileexist on this symbolik link (will fail - why ever)
try findfirst on this symbolik link (will fail)
try "cmd.exe /C dir" on this symbolik link (will find it)
now try findfirst on this symbolik link again (will now find it)

the code is:

Code: Alles auswählen

s1 := ExpandFileName(s1);
      LogDatei.log ('  Starting query if file exist ...', LLNotice);
      s2 := s1;
      if (length(s1) > 0) and (s1[length(s1)] = PATHSEPARATOR)
       then s2 := copy(s1,1,length(s1)-1);
      {$IFDEF WINDOWS}
      // disable  critical-error-handler message box. (Drive not ready)
      OldWinapiErrorMode := SetErrorMode(SEM_FAILCRITICALERRORS);
      try
        try
          s2 := trim(s2);
          BooleanResult := FileExists(s2) or DirectoryExists(s2);
          if (not BooleanResult) and (not (trim(s2) = '')) then
          begin
            LogDatei.log ('File: '+s2+' not found via FileExists', LLDebug3);
            // search for s2*
            LogDatei.log ('Looking for: '+ExtractFilePath(s2)+'*'+' via FindFirst', LLDebug3);
            FindResultcode := FindFirst(ExtractFilePath(s2)+'*',faAnyFile or faSymlink, FileRecord);
            while FindResultcode = 0 do
            begin
              LogDatei.log ('found: '+ExtractFilePath(s2)+ FileRecord.Name+' via FindFirst/Next', LLDebug3);
              if ExtractFilePath(s2)+ FileRecord.Name = s2 then
              begin
                BooleanResult := true;
                LogDatei.log ('File: '+s2+' found via FindFirst/Next', LLDebug3);
              end;
              FindResultcode := sysutils.FindNext(FileRecord);
            end;
            SysUtils.findclose(FileRecord);
            if not BooleanResult then LogDatei.log ('File: '+s2+' not found via FindFirst/next', LLDebug3);
            list1.Clear;
            list1.Text := execShellCall('dir '+s2, '32',4, false,false).Text;
            //calling shellCall with FetchExitCodePublic=false result is on FLastPrivateExitCode
            if (0 = FLastPrivateExitCode) then BooleanResult := true;
            LogDatei.log ('Looking for: '+ExtractFilePath(s2)+'*'+' via FindFirst', LLDebug3);
            FindResultcode := FindFirst(ExtractFilePath(s2)+'*',faAnyFile or faSymlink, FileRecord);
            while FindResultcode = 0 do
            begin
              LogDatei.log ('found: '+ExtractFilePath(s2)+ FileRecord.Name+' via FindFirst/Next', LLDebug3);
              if ExtractFilePath(s2)+ FileRecord.Name = s2 then
              begin
                BooleanResult := true;
                LogDatei.log ('File: '+s2+' found via FindFirst/Next', LLDebug3);
              end;
              FindResultcode := sysutils.FindNext(FileRecord);
            end;
            SysUtils.findclose(FileRecord);
            if not BooleanResult then LogDatei.log ('File: '+s2+' not found via FindFirst/next', LLDebug3);
            (*
            if FindResultcode = 0 then
            begin
              BooleanResult := true;
              LogDatei.log ('File: '+s2+' found via FindFirst', LLDebug3);
            end
            else LogDatei.log ('File: '+s2+' not found via FindFirst', LLDebug3);
            list1.Clear;
            list1.Text := execShellCall('dir '+s2, '32',4, false,false).Text;
            //calling shellCall with FetchExitCodePublic=false result is on FLastPrivateExitCode
            if (0 = FLastPrivateExitCode) then BooleanResult := true;
            *)

          end;
        except
          BooleanResult := false;
        end;
      finally
        setErrorMode(OldWinapiErrorMode);
      end;                 


the log of my program is:

Code: Alles auswählen

  Starting query if file exist ...
File: C:\tmp\testFiles\files\symlink.txt not found via FileExists
Looking for: C:\tmp\testFiles\files\* via FindFirst
found: C:\tmp\testFiles\files\. via FindFirst/Next
found: C:\tmp\testFiles\files\.. via FindFirst/Next
found: C:\tmp\testFiles\files\dummy.txt via FindFirst/Next
found: C:\tmp\testFiles\files\dummy2.txt via FindFirst/Next
found: C:\tmp\testFiles\files\hardlink.txt via FindFirst/Next
File: C:\tmp\testFiles\files\symlink.txt not found via FindFirst/next
Executing "cmd.exe" /C "dir C:\tmp\testFiles\files\symlink.txt"
ExitCode 0
output:
--------------
 Datenträger in Laufwerk C: ist WINDOWS
 Volumeseriennummer: 0202-3EB5
 
 Verzeichnis von C:\tmp\testFiles\files
 
19.12.2016  18:49    <SYMLINK>      symlink.txt [C:\tmp\testFiles\files\dummy.txt]
               1 Datei(en),              0 Bytes
               0 Verzeichnis(se), 380.498.264.064 Bytes frei
 
Looking for: C:\tmp\testFiles\files\* via FindFirst
found: C:\tmp\testFiles\files\. via FindFirst/Next
found: C:\tmp\testFiles\files\.. via FindFirst/Next
found: C:\tmp\testFiles\files\dummy.txt via FindFirst/Next
found: C:\tmp\testFiles\files\dummy2.txt via FindFirst/Next
found: C:\tmp\testFiles\files\hardlink.txt via FindFirst/Next
found: C:\tmp\testFiles\files\symlink.txt via FindFirst/Next
File: C:\tmp\testFiles\files\symlink.txt found via FindFirst/Next
 


Perhaps I'm blind but it seems for me that two identical findfirst/findnext block gives a different result if I called a dir command between.
I see the same if I make some other operations on the disk and than call findfirst/findnext I will find my symlink directly.
Is there any (API-) Call to Refresh / ReRead something like a Directory Cache ?
Any other tip ?

regards

d.oertel
lazarus 1.6.2 (32 Bit) on win8.1 64

Benutzeravatar
m.fuchs
Lazarusforum e. V.
Beiträge: 2639
Registriert: Fr 22. Sep 2006, 19:32
OS, Lazarus, FPC: Winux (Lazarus 2.0.10, FPC 3.2.0)
CPU-Target: x86, x64, arm
Wohnort: Berlin
Kontaktdaten:

Re: strange: findfirst / find next (

Beitrag von m.fuchs »

Hi, ein Hinweis: Das ist das deutschsprachige Lazarusforum, bitte daher auch bei dieser Sprache bleiben.
Software, Bibliotheken, Vorträge und mehr: https://www.ypa-software.de

Antworten