Page 1 of 1

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

Posted: Wed Apr 08, 2009 9:30 pm
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
}

Posted: Wed Apr 08, 2009 9:51 pm
by rsts
As an option, not a replacement.

cheers

Posted: Wed Apr 08, 2009 10:17 pm
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.

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

Posted: Thu Apr 09, 2009 11:17 am
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.

Posted: Thu Apr 09, 2009 5:48 pm
by Mistrel
This is for files the user has no access to, not for locked files.