dokan--the file system in user mode, the network disk can be developed into virtual Disk cloud storage (similar to Jinshan fast disk)

Source: Internet
Author: User
Tags ssh


refer:http://www.aiseminar.cn/bbs/forum.php?mod=viewthread&tid=1825











The Dokan Library helps programmers easily build user-level file systems under Windows systems, without writing device drivers, similar to fuse (Linux user mode file system).
Dokan official website: http://dokan-dev.net/en/

First, Dokan Library
If you want to create a new file system on a Windows system, such as improving the FAT or NTFS file system, you need to develop a file system driver yourself. It is extremely difficult to develop device drivers that work in kernel mode on Windows systems. With the Dokan library, you can easily create your own file system without having to write device drivers. The Dokan library is similar to the fuse user-space File system under Linux, but it works under Windows.

Dokan principle
The Dokan library contains the following sections:
-User-mode Library (Dokan.dll) LGPL
-Driver (Dokan.sys) LGPL
-Control Program (Dokanctl.exe) MIT
-Mount Service (Mouter.exe) MIT
-Samples (MIRROR.C) MIT



The Dokan library contains a user-mode DLL file (dokan.dll) and a kernel-mode file system driver (Dokan.sys). Dokan file System drivers once installed, you can create file systems like normal file systems on Windows. The application of a file system created using the Dokan library is called a file system application. The file operation request from the user program (for example: Createfile,readfile,writefile, etc.) will be sent to the Windows Input/output subsystem (running in kernel mode), After the request is sent to the Dokan file system driver (Dokan.sys). By using the functions provided by the Dokan user mode library file (dokan.dll), the file system application is able to register the callback function with the file system driver. The file system driver invokes the registered callback function routine to respond to the request after it receives the request. The processing result of the callback function routine is returned to the user program. For example, when Windows Explorer requests that a directory be created, the request "Opendirectory" will be sent to the Dokan file system driver, and then the driver will invoke the Opendirectory callback function routine provided by the file system application. The processing result of the function routine is returned to the Windows Resource browser as a response to the opendirectory request. As a result, the Dokan file system driver is like an agent between the user program and the file system program. The advantage of using the Dokan library is that it allows programmers to develop user-space file systems that are more secure and easier to debug.

Second, Dokan SSHFS
The SSHFS (SSH file system) is a file system client program that allows you to mount directories on a remote server directly to local users. The previous version was used to output the directories and files provided by SFTP, while the current version is primarily for systems with fuse installed. In the process of accessing server resources, data is encrypted and transmitted via SSH, which is safe and efficient.
Dokan is a Windows implementation of fuse that uses Dokan SSHFS to mount directories on a Linux server to local use, similar to samba. To use Dokan SSHFS you need to first install the Dokan Library, which can be downloaded on the Dokan official website.


Third. creating a file system with Dokan
Similar to fuse, our file system programs need to implement each operation Dokan_operations (declared in dokan.h) in a struct, and the struct is then called Dokanmain to mount the filesystem as a parameter. The parameters of these functions are consistent with Windows APIs, but must be thread-safe because there may be multiple threads called.

These functions have a typical invocation order:
1. CreateFile (opendirectory, CreateFile)
2. Other functions
3. Cleanup
4. CloseFile

File creation functions (Opendirectory, CreateFile,...) Always in file access operations (listing directory, reading file attributes, ...) Called before. When a file is closed by CloseFile Windows API, the cleanup program is always called by Dokan.sys. A return value of 0 indicates that the operation was successful.

The last parameter of each function is Dokan_file_info structure
typedef struct _DOKAN_FILE_INFO
{
ULONG64 Context; File System program maintenance, can be used as a file handle
ULONG64 Dokancontext; Dokan Library Maintenance
ULONG ProcessId; Action ID
BOOL isdirectory; Directory = TRUE
} dokan_file_info, *pdokan_file_info; Copy Code
Each of the file handles corresponds to a dokan_file_info struct. The structure is created when the file is opened by the CreateFile system call, and the recycle occurs when the file is closed by the CloseFile system call.

Here are the declarations of several operations:
Int (*createfile) (
LPCWSTR,//FileName
DWORD,//Desiredaccess
DWORD,//ShareMode
DWORD,//Creationdisposition
DWORD,//Flagsandattributes
Pdokan_file_info);

Int (*opendirectory) (
LPCWSTR,//FileName
Pdokan_file_info);

Int (*createdirectory) (
LPCWSTR,//FileName
Pdokan_file_info); Note Set isdirectory = TRUE

CloseHandle (Windows API) is called after execution, if the file handle is in the
Created in CreateFile, should be released here instead of CloseFile.
If the user caches the file in memory, it is possible to invoke the read-write after calling cleanup
Operation.
Int (*cleanup) (
LPCWSTR,//FileName
Pdokan_file_info);

If the user calls CloseHandle and then opens the same file, CreateFile may
No more calls to CloseFile, which may cause a sharing error.
Int (*closefile) (
LPCWSTR,//FileName
Pdokan_file_info);

Int (*findfiles) (
LPCWSTR,//PathName
Pfillfinddata,//Call this function with Pwin32_find_dataw
Pdokan_file_info); (see Pfillfinddata definition)

You should implement either FindFiles or Findfileswithpattern
Int (*findfileswithpattern) (
LPCWSTR,//PathName
LPCWSTR,//searchpattern
Pfillfinddata,//Call this function with Pwin32_find_dataw pdokan_file_info); Copy Code

The above two functions are requested in response to a column directory entry operation. For each directory entry, the file system program calls the function Fillfinddata (&win32finddataw, Dokanfileinfo). Because the shell of Windows does not support pattern matching, file system programs perform a wildcard mode. When the file System program implementation findfiles,dokanlibrary will automatically add a wildcard mode, we can also implement Findfileswithpattern to control. The dokanisnameinexpression (dokan.dll) function is used to implement pattern matching.

Fourth. mounting the disk
You can call the Dokanmain function to mount the file system, and the program will block the file system from being uninstalled. Our file system to do two things, one for the Dokan runtime to fill in the dokanoptions, and the other is to fill the dokanoperations with each operator function pointer as the Dokanmain parameters.
int Dokanapi Dokanmain (
Pdokan_options Dokanoptions,
Pdokan_operations dokanoperations);

typedef struct _DOKAN_OPTIONS {
USHORT Version; Supported Dokan Version, ex. "530" (Dokan ver 0.5.3)
ULONG ThreadCount; Number of threads to be used
ULONG Options; Combination of dokan_options_*
ULONG64 Globalcontext; FileSystem can use this variable
LPCWSTR Mountpoint; mount point "M:\" (drive letter) or//' C:\mount\dokan ' (path in NTFS)
} dokan_options, *pdokan_options; Copy Code

Fifth, uninstall
Call Dokanunmount to unmount the file system. Users can also use Dokanctl to uninstall like this:
> dokanctl.exe/u driveletter

Refered to:
New ways to share dokan:windows and Linux files
Http://www.cnblogs.com/dengke/archive/2010/02/22/1670858.html
Using Dokan for Virtual disk development
Http://www.fqyy.org/sunu/archives/tag/dokan
Dokan (Windows FUSE) Learning notes
Http://my.debugman.net/program-198.html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.