FileExists wrong ouput

Linux specific questions.
Post Reply
Dinosaur
Posts: 1478
Joined: Jul 24, 2005 1:13
Location: Hervey Bay (.au)

FileExists wrong ouput

Post by Dinosaur »

Hi All

Is there a history of permission problems with Fileexists ? (my browsing can't locate any)
In my earlier apps it appeared to work, but recompiling earlier apps creates a problem.
Tested on Mint 19.3 with latest FB and tested on Debian with FB version 1.05 on arm 32 bit.

Code: Select all

#include once "File.bi"
If FileExists("/dev/ttyUSB2") Then 
Test running the app as root gives correct result.
Does this mean that the file exists depending on the permissions ??

Regards

EDIT: ttyUSB2 is created by smstools and has permissions crw-rw
adeyblue
Posts: 299
Joined: Nov 07, 2019 20:08

Re: FileExists wrong ouput

Post by adeyblue »

These things are usually implemented as
Is it readable?
instead of actually
Does it exist?

And it's no different for FB. I think primarily because the next operation after knowing it exists is to start accessing it. it doesn't seem partiularly often you want to know the status of a file you have no access to.

You might have better luck making your own function that uses stat() in sys/stat.bi or Dir() to more accurately answer the question for inaccessible but extant files.

I don't know how Linux works too well, so it may be possible that it is in fact only visible to the root user and not to any normal users.
Dinosaur
Posts: 1478
Joined: Jul 24, 2005 1:13
Location: Hervey Bay (.au)

Re: FileExists wrong ouput

Post by Dinosaur »

Hi All

Thanks adeyblue for your response.
Sounds to me like we need a "DeviceExists" routine.
However using Dir becomes to long winded, so solved by doing:

Code: Select all

    Dim as Integer TF = Freefile
    Open Com "/dev/ttyUSB2:115200,n,8,1" for Input as #TF
    If Err = 0 Then
            Close #TF
            etc,etc
Regards
Laurens
Posts: 17
Joined: Mar 16, 2022 9:16
Location: Flevoland, the Netherlands

Re: FileExists wrong ouput

Post by Laurens »

Hi Dinosaur,

The problem is in the permission to access the device directly. In order to get access to a device, the user must be a member of the group corresponding the use of it.

Serial ports usually belong to the dialout or modem group, depending on your distro. Just edit the /etc/group file and add with your favourite text editor your name to the corresponding group in /etc/group. For example the line for dialout then will look like this:

Code: Select all

dialout:x:20:dinosaur
After re-logging in your user, the FileExists module should be able to access the port without root permissions.
caseih
Posts: 2157
Joined: Feb 26, 2007 5:32

Re: FileExists wrong ouput

Post by caseih »

If you really want to find out if you have proper permissions to open a file, you can use the stat() function from the C standard library. Also stat() can tell you whether it's a symbolic link or an actual file. However using stat() also means you need to use C runtime library functions to get your own uid and gid, and then interrogate the group entities to find out if you're a member of the required group.

In actual practice, no one does this. Just open the file and report a problem if it occurs. No matter how you cut it, if you lack permission to open a file your program cannot succeed, so it does not matter if you know that before or after you try to open the file.

If your goal is to get a list of serial ports that your user can choose from, if the user does not have permissions to open a serial port then it doesn't matter if it exists or not, and if FileExists() cannot see it, it may as well not be there. So you might want to just inform the user that no accessible serial port was found and recommend they check their permissions and try again. Most distros use the "dialout" group to control access to serial ports.
Post Reply