关于文件共享打开时,share权限的判断

只要是调用了下面的检测函数。

第4个参数ShareAccess来源于scb(也可以说是FCB)
NTSTATUS
IoCheckShareAccess(
IN ACCESS_MASK DesiredAccess,
IN ULONG DesiredShareAccess,
IN OUT PFILE_OBJECT FileObject,
IN OUT PSHARE_ACCESS ShareAccess,
IN BOOLEAN Update
)

/*++

Routine Description:

This routine is invoked to determine whether or not a new accessor to
a file actually has shared access to it. The check is made according
to:

1) How the file is currently opened.

2) What types of shared accesses are currently specified.

3) The desired and shared accesses that the new open is requesting.

If the open should succeed, then the access information about how the
file is currently opened is updated, according to the Update parameter.

Arguments:

DesiredAccess - Desired access of current open request.

DesiredShareAccess - Shared access requested by current open request.

FileObject - Pointer to the file object of the current open request.

ShareAccess - Pointer to the share access structure that describes how
the file is currently being accessed.

Update - Specifies whether or not the share access information for the
file is to be updated.

Return Value:

The final status of the access check is the function value. If the
accessor has access to the file, STATUS_SUCCESS is returned. Otherwise,
STATUS_SHARING_VIOLATION is returned.

Note:

Note that the ShareAccess parameter must be locked against other accesses
from other threads while this routine is executing. Otherwise the counts
will be out-of-synch.

--*/

{
ULONG ocount;

PAGED_CODE();

//
// Set the access type in the file object for the current accessor.
// Note that reading and writing attributes are not included in the
// access check.
//

FileObject->ReadAccess = (BOOLEAN) ((DesiredAccess & (FILE_EXECUTE
| FILE_READ_DATA)) != 0);
FileObject->WriteAccess = (BOOLEAN) ((DesiredAccess & (FILE_WRITE_DATA
| FILE_APPEND_DATA)) != 0);
FileObject->DeleteAccess = (BOOLEAN) ((DesiredAccess & DELETE) != 0);


//
// There is no more work to do unless the user specified one of the
// sharing modes above.
//

if (FileObject->ReadAccess ||
FileObject->WriteAccess ||
FileObject->DeleteAccess) {

FileObject->SharedRead = (BOOLEAN) ((DesiredShareAccess & FILE_SHARE_READ) != 0);
FileObject->SharedWrite = (BOOLEAN) ((DesiredShareAccess & FILE_SHARE_WRITE) != 0);
FileObject->SharedDelete = (BOOLEAN) ((DesiredShareAccess & FILE_SHARE_DELETE) != 0);

//
// If this is a special filter fileobject ignore share access check if necessary.
//

if (FileObject->Flags & FO_FILE_OBJECT_HAS_EXTENSION) {
PIOP_FILE_OBJECT_EXTENSION fileObjectExtension =(PIOP_FILE_OBJECT_EXTENSION)(FileObject + 1);

if (fileObjectExtension->FileObjectExtensionFlags & FO_EXTENSION_IGNORE_SHARE_ACCESS_CHECK) {
return STATUS_SUCCESS;
}
}

//
// Now check to see whether or not the desired accesses are compatible
// with the way that the file is currently open.
//

ocount = ShareAccess->OpenCount;

if ( (FileObject->ReadAccess && (ShareAccess->SharedRead < ocount))
||
(FileObject->WriteAccess && (ShareAccess->SharedWrite < ocount))
||
(FileObject->DeleteAccess && (ShareAccess->SharedDelete < ocount))
||
((ShareAccess->Readers != 0) && !FileObject->SharedRead)
||
((ShareAccess->Writers != 0) && !FileObject->SharedWrite)
||
((ShareAccess->Deleters != 0) && !FileObject->SharedDelete)
) {

//
// The check failed. Simply return to the caller indicating that the
// current open cannot access the file.
//

return STATUS_SHARING_VIOLATION;

//
// The check was successful. Update the counter information in the
// shared access structure for this open request if the caller
// specified that it should be updated.
//

} else if (Update) {

ShareAccess->OpenCount++;

ShareAccess->Readers += FileObject->ReadAccess;
ShareAccess->Writers += FileObject->WriteAccess;
ShareAccess->Deleters += FileObject->DeleteAccess;

ShareAccess->SharedRead += FileObject->SharedRead;
ShareAccess->SharedWrite += FileObject->SharedWrite;
ShareAccess->SharedDelete += FileObject->SharedDelete;
}
}
return STATUS_SUCCESS;
}

 

里面总结的意思就是:

1.打开文件时候希望得到权限X1,那么只要之前有人打开过这个文件时没指定share x1权限就失败

2.打开文件时候,如果前面有人用Y1打开过,那么就一定要指定share Y1权限, 所以使用的时候可以说share这个权限是多多益善!!

posted @ 2012-09-03 10:43  kkindof  阅读(834)  评论(0)    收藏  举报