Recent

Author Topic: [SOLVED] File Not Found by FPC Reset under WSL2 Ubuntu-20.04 on Windows 10  (Read 972 times)

MisterD

  • Newbie
  • Posts: 2
I'm running WSL2 with Ubuntu-20.04 on Windows 10.
The exe named chap1 is in my ~/pascal dir along with input file mod.txt.
When attempting to open mod.txt, it crashes with a file not found error on line 591 which is the Reset command (see Log of Run below).
Prior to the Reset command, the exe displays the current dir as ~/pascal.
What would cause the mod.txt file to not be found in that dir?

I do have rw access to mod.txt
-rw-r--r-- 1 dawsond dawsond 29 Jul 22  2016 mod.txt

Relevant Source Code
Code: Pascal  [Select][+][-]
  1.      BEGIN
  2.        infilename:=parseString;   (* parse filename parameter *)
  3.        Writeln ('Current Directory is : ',GetCurrentDir);
  4.        writeln(' Loading file : ',infilename);
  5.        Assign(infile,infilename);
  6.        RESET(infile);
  7.        readfile:=true;  (* set flag telling nextchar function to read from a file *)
  8.        if cmdnm = load then
  9.          echo:=true;
  10.      END;
  11.  

Log of Run
~/pascal$ ./chap1
-> )load mod.txt
Current Directory is : /home/dawsond/pascal
 Loading file : mod.txt
An unhandled exception occurred at $0000000000402994:
EInOutError: File not found
  $0000000000402994 line 591 of chap1.pas
  $00000000004026D5 line 616 of chap1.pas
  $00000000004025A3 line 633 of chap1.pas
  $0000000000404EA5 line 1548 of chap1.pas

Version Info
Lazarus IDE is not installed.
Ubuntu-20.04
WSL version: 2.0.14.0
Kernel version: 5.15.133.1-1
WSLg version: 1.0.59
MSRDC version: 1.2.4677
Direct3D version: 1.611.1-81528511
DXCore version: 10.0.25131.1002-220531-1700.rs-onecore-base2-hyp
Windows version: 10.0.19045.2913
Free Pascal Compiler version 3.0.4+dfsg-23 [2019/11/25] for x86_64

Attempted Solutions
- prefixed filename with full path
- added ~/pascal to $PATH
« Last Edit: February 14, 2024, 05:10:54 am by MisterD »

dbannon

  • Hero Member
  • *****
  • Posts: 2854
    • tomboy-ng, a rewrite of the classic Tomboy
Re: File Not Found by FPC Reset under WSL2 Ubuntu-20.04 on Windows 10
« Reply #1 on: February 14, 2024, 02:46:44 am »
MisterD, maybe a good a good idea to test if the file really is there before opening it ?

Code: Pascal  [Select][+][-]
  1.       if not FileExists(inifilename) then begin
  2.           writeln(ininfilename, ' does not seem to exist');
  3.           exit;
  4.       end
  5.        Assign(infile,infilename);
  6.        RESET(infile);

Certainly seems to be there, but that assumes inifilename really does contain the right name and its accessible.

Davo
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

TRon

  • Hero Member
  • *****
  • Posts: 2684
Re: File Not Found by FPC Reset under WSL2 Ubuntu-20.04 on Windows 10
« Reply #2 on: February 14, 2024, 03:20:12 am »
A couple of additional remarks:
- If you use old-style file operations then make sure to always check ioresult manually, see also example in manual
- Though your presented information do not seem to show any signs, realize that in Linux filenames and paths are case sensitive.
- Seeing your end statement ending in a semicolon (and not a dot) I can only assume that the snippet posted is part of a bigger code-base and one of the things missing in the presented snippet is the actual closing of the opened file. Note that omission of closing an opened file can lead to filehandles staying open and most file-systems really do not like that, and which can result in erroneous behaviour.
- Free Pascal 3.0.4 was released in nov 2017... almost 7 years ago. Please update.
« Last Edit: February 14, 2024, 03:35:22 am by TRon »

MisterD

  • Newbie
  • Posts: 2
Re: File Not Found by FPC Reset under WSL2 Ubuntu-20.04 on Windows 10
« Reply #3 on: February 14, 2024, 05:10:26 am »
Thank you Davo for your suggestion.
The spaces between the filename and message in Log 1 below made me realize that I
needed to pad infilename with trailing nulls instead of trailing spaces.
Now it works correctly as shown in Log 2.
TRon, thank you for your helpful comments as well.

Log 1 - erroneous exe with trailing spaces in file name
~/pascal$ ./chap1
-> )load mod.txt
Current Directory is : /home/dawsond/pascal
 Loading file : mod.txt
mod.txt                                                                          does not seem to exist
-> quit$

Log 2 - corrected exe with trailing nulls in filename
mod.txt is read and mod function is defined successfully
~/pascal$ ./chap1
-> )load mod.txt
Current Directory is : /home/dawsond/pascal
 Loading file : mod.txt
mod.txt does exist
 fun mod(m,n):=m-n*(m/n)nuf$
mod
-> quit$

PascalDragon

  • Hero Member
  • *****
  • Posts: 5526
  • Compiler Developer
Re: File Not Found by FPC Reset under WSL2 Ubuntu-20.04 on Windows 10
« Reply #4 on: February 14, 2024, 08:52:26 pm »
- If you use old-style file operations then make sure to always check ioresult manually, see also example in manual

This is not necessary if the SysUtils is used as by default (unless $I- is used) an I/O error will be converted to an exception. See also here.

TRon

  • Hero Member
  • *****
  • Posts: 2684
Re: File Not Found by FPC Reset under WSL2 Ubuntu-20.04 on Windows 10
« Reply #5 on: February 15, 2024, 11:28:12 am »
This is not necessary if the SysUtils is used as by default (unless $I- is used) an I/O error will be converted to an exception. See also here.
Thank you very much for the hint PascalDragon.

Yes, that is definitely true but I made my remark with OP's code in mind which seem to require some form of proper file error handling before actually attempting to process the file. Though dbannon's suggestion is perhaps more elegant it is (theoretically) still possible that something is amiss when attempting to open/access the file. The exception is nice to have (as a backup to know what is/went wrong).

@MisterD:
I know you marked the issue as being solved but
Thank you Davo for your suggestion.
The spaces between the filename and message in Log 1 below made me realize that I
needed to pad infilename with trailing nulls instead of trailing spaces.
..
When you write padding with trailing nulls then something tells me that there might be something wrong somewhere in your parsing routine. Are you using hard-coded length strings (shortstrings) or something ?

And are you aware of the function trim that automatically strips non visible characters from a string  ?

As a hint: when you write out filenames (but I personally also use it for displaying any string data) try to enclose the output into quotes (e.g. quotedstr) so that you are able to visually inspect the outputted string in a more detailed manner.
« Last Edit: February 15, 2024, 11:58:47 am by TRon »

 

TinyPortal © 2005-2018