We create our file system in Windows OS on .Net

There are so many file systems. These are file systems for storage media (FAT *, NTFS, ext *, etc.), and network file systems (NFS, CIFS, etc.), and virtual file systems, and a great many others . And did you,% habrauser%, have a need for your own, non-existent file system? How to make it for Windows OS on managed-code (.net) will be discussed.

In general, to create your own file system (hereinafter referred to as the FS), you need to write a driver for this FS and register it in the OS. Because Since the driver interacts with the OS kernel, its creation on the managed code is not trivial, and the performance of such a driver will be at a low level. In this regard, it is more expedient to have a FS driver written in native code and some intermediate layer between this driver and managed code. Such a driver exists in the Dokan project , as well as a set of DokanNet classes for interacting with this driver in managed code. Both projects are licensed under GPLv3.

Let's try to figure out how it works.



Dokan- This is a driver that runs at the kernel level of Windows, with which any application from userspace can interact. The driver exists for both 32-bit systems and 64-bit ones. The driver does not carry in itself the implementation of any FS, but only acts as a proxy, passing through itself all the input-output (IO) operations from the OS kernel in userspace. The implementation of the FS functionality lies on our shoulders, we create basic FS functions, such as opening / closing a file, reading from a file, writing to a file, etc., which will be called by the Dokan driver when the corresponding events occur in the system. At the time of registering our FS in the Dokan driver, we indicate some parameters of our FS operation (number of I / O operations flow handlers, mount point of our FS (Dokan supports mounting as a removable or network drive and only to the root of the FS), automatic unmounting of our FS in case of errors in operation, and some other parameters). After that, a new disk appears in the OS at the root of the FS, with which any applications and the OS itself interacts, as with a regular disk. What operations are allowed to be performed in this FS depends only on the developer of this FS, that is, on us.

DokanNet is a wrapper for the Dokan driver. Allows you to develop your own FS using managed code. DokanNet describes the DokanOperations interface, which must be implemented to register our FS in the Dokan driver. The author of DokanNet, along with the source code of the project itself, put 2 examples of working with this wrapper:
  • DokanNetMirror - FS mirror of an existing directory
  • RegistoryFS - FS representing the structure of the Windows registry in directories and files

The sample codes are easy to read and allow you to familiarize yourself with the features provided by the Dokan driver.

The implementation of the DokanOperations interface comes down to the implementation of the following functions:
  • CreateFile - create files / directories
  • OpenDirectory - open directory
  • CreateDirectory - create a directory
  • Cleanup - delete a file / empty directory
  • CloseFile - close file descriptor
  • ReadFile - reading a piece of a file of a specified length with a specified offset
  • WriteFile - write data to a file with the specified offset
  • FlushFileBuffers - flush file buffers
  • GetFileInformation - getting information about size, attributes, creation time / last access / file / directory modification
  • FindFiles - getting a list of files / directories in the specified directory
  • SetFileAttributes - setting file / directory attributes
  • SetFileTime - setting the time of creation / last access / modification of a file / directory
  • DeleteFile - mark file for deletion (deletion is performed in Cleanup)
  • DeleteDirectory - mark the directory for deletion (deletion is performed in Cleanup)
  • MoveFile - move / rename file / directory
  • SetEndOfFile - setting the file size (used when creating an empty file of a certain length)
  • SetAllocationSize - the author did not indicate why this function is necessary, in practice, the transfer of control to it was not noticed
  • LockFile - lock file in single-access
  • UnlockFile - unlocking
  • GetDiskFreeSpace - getting information about the number of available / shared / free space in the FS
  • Unmount - unmount / disable FS


As we can see, the functionality provided by the Dokan driver and the DokanNet wrapper is very rich. Based on this project, FS SSHFS was developed . Everyone can develop an FS that meets their requirements. I settled on the development of HttpFS, which will allow you to mount files on remote Http servers into the system. But more about that in the next article ...

Also popular now: