Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is difference between _SH_SECURE and _SH_DENYWR

_SH_DENYWR denies any other attempt to open a file with write permissions (share violation) _SH_SECURE Sets secure mode (shared read, exclusive write access)

The _SH_SECURE seems to be newer, based on the fact that the docs seem to gloss over it or omit it depending on where you look. There's almost no info on the 'net that I could find on it.

How are those any different?

like image 465
Mordachai Avatar asked Mar 16 '12 14:03

Mordachai


1 Answers

The behavior of _SH_SECURE depends on the access requested in the mode argument to _fsopen()/_wfsopen(). If only read access is requested, then _SH_SECURE maps to FILE_SHARE_READ. Otherwise, it maps to 0 (exclusive access).

Contrast _SH_DENYWR, which always maps to FILE_SHARE_READ.

The relevant part of the CRT source code (lines 269-301 of open.c in Visual Studio 2010) is as follows:

/*
 * decode sharing flags
 */
switch ( shflag ) {

    case _SH_DENYRW:        /* exclusive access */
        fileshare = 0L;
        break;

    case _SH_DENYWR:        /* share read access */
        fileshare = FILE_SHARE_READ;
        break;

    case _SH_DENYRD:        /* share write access */
        fileshare = FILE_SHARE_WRITE;
        break;

    case _SH_DENYNO:        /* share read and write access */
        fileshare = FILE_SHARE_READ | FILE_SHARE_WRITE;
        break;

    case _SH_SECURE:       /* share read access only if read-only */
        if (fileaccess == GENERIC_READ)
            fileshare = FILE_SHARE_READ;
        else
            fileshare = 0L;
        break;

    default:                /* error, bad shflag */
        _doserrno = 0L; /* not an OS error */
        *pfh = -1;
        _VALIDATE_RETURN_ERRCODE(( "Invalid sharing flag" , 0 ), EINVAL);
}
like image 180
Frédéric Hamidi Avatar answered Nov 12 '22 07:11

Frédéric Hamidi