Boost logo

Boost :

From: Reece Dunn (msclrhd_at_[hidden])
Date: 2005-09-28 17:42:55


Angus Leeming wrote:
> John Maddock wrote:
>
>>>The cure to my immediate problem is to have the actual error that my
>>>user is experiencing added to the list of known errors. My intuition
>>>tells me to expect ERROR_INVALID_DRIVE but I know we shouldn't write
>>>code based on intuition :) I'll post back the actual error as and when
>>>my bug reporter gets back to me with the results from a modified
>>>version of the code. (I got it to throw_exception instead of return
>>>true.)
>
> Looks like my intuition was wrong:
>
> Exception caught:
> boost::filesystem::exists:
> "J:\MinSYS\home\Angus\lyx\devel\build-tex2lyx\LyX-1.4\Resources\locale":
> Access is denied.
> Assertion triggered in void boost::throw_exception(const std::exception&)
> by failing check "false" in file ../../../src/tex2lyx/boost.C:28
>
> I guess that's the message associated with ERROR_ACCESS_DENIED, no? Having
> said that, I can't see the harm in testing for ERROR_INVALID_DRIVE too.

"Access is denied." is the description for ERROR_ACCESS_DENIED, yes.

> This was on a machine running Win98SE. The machine has no J drive.
>
> Now I can imagine some people saying, hold on. Access denied doesn't mean
> that the file doesn't exist. However, I'd say that *as far as my program
> is concerned* that's exactly what it means...

Consider:

    if( !exists( "j:\\foo.txt" ))
    {
       std::ofstream out( "j:\\foo.txt" ); // oops!
    }

if "j:\\foo.txt" exists, but you get an Access is denied error.

> Hmmm. Sometimes the actual error is the worst possible news, no :(
>
> Any chance of this change going into the sources?

The proper way to implement exists( ... ) on Windows is to do:

    #include "shlwapi.h"
    // link to shlwapi.lib

    bool exists( const path & ph )
    {
       return ::PathFileExistsA( ph.string().c_str()) == TRUE;
    }

MSDN: Function Information (PathFileExists)

Minimum DLL Version shlwapi.dll version 4.71 or later
Custom Implementation No
Header shlwapi.h
Import library shlwapi.lib
Minimum operating systems Windows 2000, Windows NT 4.0 with Internet
Explorer 4.0, Windows 98, Windows 95 with Internet Explorer 4.0
Unicode Implemented as ANSI and Unicode versions.

or, as a fallback (for WinNT4/Win95 with IE3):

    bool exists( const path & ph )
    {
       HANDLE file = CreateFileA( ph.string().c_str(),
             GENERIC_READ, FILE_SHARE_READ, NULL,
             OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL
          );
       bool exist = file != INVALID_HANDLE_VALUE;
       ::CloseHandle( file );
       return exist;
    }

This should work for all cases.

- Reece


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk