Search code examples
c++winapifilesystemsdriverdokan

Dokan: Should I handle file sharing and access rights myself


I am trying to create a file system driver with Dokan. But now I got confused about if I should handle the complex stuff myself.

Some examples:

  • Two consecutive calls to CreateFile() with sharing mode set to 0.
  • Delete a opened file(without FILE_SHARE_DELETE set).
  • Request FILE_GENERIC_WRITE access when the file has been opened with FIlE_SHARE_READ.

In these conditions, Windows will obviously return an error. But I'm not sure it will be automatically handled by the Windows kernel or is the duty of my file system application.


Solution

  • when kernel handle create/open file request (say IoCreateFile\[Ex\]) it check only that ShareAccess have valid mask:

    if (ShareAccess & ~FILE_SHARE_VALID_FLAGS) return STATUS_INVALID_PARAMETER;

    and store ShareAccess in IO_STACK_LOCATION.Parameters.Create.ShareAccess then already FS driver task use/check it. however usually FS drivers use system supplied IoCheckShareAccess routine for this (look for example FatCheckShareAccess)

    for delete file - we open file with option FILE_DELETE_ON_CLOSE. kernel only check that in this case we request DELETE in DesiredAccess

    if ((CreateOptions & FILE_DELETE_ON_CLOSE) && !(DesiredAccess & DELETE)) return STATUS_INVALID_PARAMETER;

    but main task handle this is FS responsibility - search for DeleteOnClose in create.c

    Request FILE_GENERIC_WRITE access when the file has been opened with FIlE_SHARE_READ

    this check exactly done in IoCheckShareAccess (look for it code in iosubs.c ) but FS must direct call this routine with IrpSp->Parameters.Create.ShareAccess