FileSize() to identify if no read-access to a file

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

FileSize() to identify if no read-access to a file

Post by Mistrel »

Currently this function only identifies whether the file exists (and its size), whether it doesn't exist, or whether the path is a directory. If the user has no read access it will return -1, as if the file doesn't exist.

The reason this fails is because obtaining attributes about the file would be a security vulnerability. I believe the reason this fails is because this function wraps GetFileSize from the Win32 API.

Checking for the file existence specifically can be done using the C++ sys/stat.h library (I believe this is part of the STL) and should work even if the user has no access:

Code: Select all

bool fileExist(const string fileName)
{
   struct stat fileInfo;
   int statistics;

   // Attempt to get the file attributes
   statistics = stat(fileName.c_str(), &fileInfo);
   
   return !statistics; // Returns true/false if file exists
}
rsts
Addict
Addict
Posts: 2736
Joined: Wed Aug 24, 2005 8:39 am
Location: Southwest OH - USA

Post by rsts »

As an option, not a replacement.

cheers
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Post by Mistrel »

I would expect a different return value like "-3". I can't imagine why a replacement would be necessary here. What I'm asking for would be for an improvement of the current functionality.
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: FileSize() to identify if no read-access to a file

Post by PB »

There was a snippet posted here once that showed how to use FileSize
with locked files, but I can't find it now. I know it worked because I used
it with pagefile.sys successfully. Don't know where it's gone now.
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Post by Mistrel »

This is for files the user has no access to, not for locked files.
Post Reply