Recent

Author Topic: FileExists doesn't seem to be working  (Read 1336 times)

CyberFilth

  • Jr. Member
  • **
  • Posts: 84
    • My github account
FileExists doesn't seem to be working
« on: September 28, 2021, 05:26:22 pm »
I work in cyber security and I've put together a small GUI to connect to a couple of API's, that detect whether an email address has had its passwords leaked.
The application uses 2 forms, when it first starts it looks to see if there is an XML file with the users credentials, and if not it opens up a second form for the user to enter their data.

The code that checks this is here
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormShow(Sender: TObject);
  2. begin
  3.   (* Check to see if a config file exists *)
  4.   if (FileExists('nergalConf.xml') = True) then
  5.     loadConfig
  6.   else
  7.   begin
  8.     try
  9.       deetsForm.ShowModal();
  10.     finally
  11.       deetsForm.Free();
  12.     end;
  13.   end;
  14. end;

(deetsForm is the second form that the user can enter their details on...)
I wrote and tested this on the latest version of Xubuntu Linux and it works fine. When I tried to run the program on Kali Linux though I got the error below about it not finding the config file.
Kali is really the target platform for this application, but I'm posting this under General in case the problem is due to my code rather than the OS, since it works on a different flavour of Linux.

The full code is at https://github.com/cyberfilth/nergal/
Running Windows 10 & Xubuntu 20.04 | Lazarus 2.0.12 | FPC 3.2.0

MarkMLl

  • Hero Member
  • *****
  • Posts: 3235
Re: FileExists doesn't seem to be working
« Reply #1 on: September 28, 2021, 05:29:53 pm »
That's an unqualified filename: what directory do you think it's in?

MarkMLl
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

CyberFilth

  • Jr. Member
  • **
  • Posts: 84
    • My github account
Re: FileExists doesn't seem to be working
« Reply #2 on: September 28, 2021, 06:08:26 pm »
I thought that without a path specified, the application would look in the directory that it's running from.
It's a portable application, although normally I'd save a config file in the users' home directory, for this application I'd like all associated files to be stored next to the app.

It does actually create the XML file in the same directory as the application. It just shows the attached warning when doing it.
Running Windows 10 & Xubuntu 20.04 | Lazarus 2.0.12 | FPC 3.2.0

Bart

  • Hero Member
  • *****
  • Posts: 4472
    • Bart en Mariska's Webstek
Re: FileExists doesn't seem to be working
« Reply #3 on: September 28, 2021, 06:29:57 pm »
What is the value of ExpandFilename('nergalConf.xml')?
And does that (now fully qualified) filename actually exist?

Bart

CyberFilth

  • Jr. Member
  • **
  • Posts: 84
    • My github account
Re: FileExists doesn't seem to be working
« Reply #4 on: September 28, 2021, 06:45:21 pm »
Yep, using ExpandFilename prints out the full path of where I expect the file to be '/home/cyberfilth/Development/Nergal/nergalConf.xml'

Whether the filename exists or not shouldn't matter. If the file is there then another procedure loads it, if it's not then it's created.
The line of code that's showing an error is the code that checks whether the file exists or not, and it gives the error whether the file is there or not (I've created it through the application and then manually deleted it to check).
Running Windows 10 & Xubuntu 20.04 | Lazarus 2.0.12 | FPC 3.2.0

Bart

  • Hero Member
  • *****
  • Posts: 4472
    • Bart en Mariska's Webstek
Re: FileExists doesn't seem to be working
« Reply #5 on: September 28, 2021, 07:35:05 pm »
Yep, using ExpandFilename prints out the full path of where I expect the file to be '/home/cyberfilth/Development/Nergal/nergalConf.xml'
And ls /home/cyberfilth/Development/Nergal/nergalConf.xml actually confirms the file is there?

Whether the filename exists or not shouldn't matter.

That's a rather remarkable statement given that you say FileExists() is False (and the filename is correct), but the file exists.

Bart

CyberFilth

  • Jr. Member
  • **
  • Posts: 84
    • My github account
Re: FileExists doesn't seem to be working
« Reply #6 on: September 28, 2021, 08:07:52 pm »
Quote
And ls /home/cyberfilth/Development/Nergal/nergalConf.xml actually confirms the file is there?
It isn't there the first time the application runs (and it gives an error), it is there the second time the application runs (and still gives the error)

Quote
Whether the filename exists or not shouldn't matter.
Let me clarify that, FileExists should just evaluate whether the file exists or not. The application then responds as required.
What's happening now is, the application checks if a file exists, then regardless whether it does or not, it's producing an error message, then continuing on as normal. So it is correctly evaluating whether the file exists or not, it's just halting program execution and showing an error when doing so.
Running Windows 10 & Xubuntu 20.04 | Lazarus 2.0.12 | FPC 3.2.0

MarkMLl

  • Hero Member
  • *****
  • Posts: 3235
Re: FileExists doesn't seem to be working
« Reply #7 on: September 28, 2021, 08:19:41 pm »
Let me clarify that, FileExists should just evaluate whether the file exists or not. The application then responds as required.
What's happening now is, the application checks if a file exists, then regardless whether it does or not, it's producing an error message, then continuing on as normal. So it is correctly evaluating whether the file exists or not, it's just halting program execution and showing an error when doing so.

Two points. First, FileExists() on Linux was recently changed so as to not respond to the presence of a directory, so I think you need to check your code on multiple versions of the compiler in case something nasty's crept in. Second, I wonder what the behaviour is if the current user doesn't have the right to read the directory in which he believe's the file's stored or the containing filesystem?

I've seen /something/ nasty in the past, but I think it was at the shell level and apart from that the detail escapes me. In general, if FileExists() doesn't work you should trust it and look /very/ carefully at the parameters and your surrounding code.

MarkMLl
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

GetMem

  • Hero Member
  • *****
  • Posts: 3491
Re: FileExists doesn't seem to be working
« Reply #8 on: September 28, 2021, 08:33:19 pm »
@CyberFilth

If the file exists you call loadConfig(good so far), however if the file does not exists you proceed to show deetsForm. On deetsForm FormShow, you assume that the config file exists anyways and you try to read its containt:
Code: Pascal  [Select][+][-]
  1.  
  2. try
  3.   dfileName := 'nergalConf.xml';
  4.   (* Read in xml file from disk *)
  5.   ReadXMLFile(Doc, dfileName);          
  6.   /...
  7. finally
  8.   (* free memory *)
  9.   Doc.Free;
  10. end;

Hence the error. It's nothing wrong with FileExists.
« Last Edit: September 28, 2021, 08:36:53 pm by GetMem »

CyberFilth

  • Jr. Member
  • **
  • Posts: 84
    • My github account
Re: FileExists doesn't seem to be working
« Reply #9 on: September 28, 2021, 08:57:30 pm »
You're right! I think that's it.

Originally the deetsForm didn't try and load the config file. Later on, I decided that I wanted the option to amend the config file so had it load up the existing values. I forgot to include a check to see if the file already existed at that point.

Thankyou. Now I have to got through the whole program flow again to see if any other late changes I made are causing issues.
Running Windows 10 & Xubuntu 20.04 | Lazarus 2.0.12 | FPC 3.2.0

 

TinyPortal © 2005-2018