Dokan簡介[轉]

來源:互聯網
上載者:User

標籤:

1.      Dokan Library 簡介 

Dokan Library 協助程式員在windows系統下輕鬆建立使用者級檔案系統,不需要寫裝置驅動,其與FUSE(Linux user mode file system)類似。

2.      Dokan原理 

Dokan Library包含以下部分:   
- user-mode library (dokan.dll)  LGPL    
- driver (dokan.sys)             LGPL    
- control program (dokanctl.exe) MIT    
- mount service (mouter.exe)     MIT    
- samples (mirror.c)             MIT

dokan.sys是核心態檔案系統驅動,當此驅動安裝之後,就可以建立自己的檔案系統了。而我們建立檔案系統(實現Dokan庫的API)的程式稱為檔案系統程式。這時,程式提出的檔案操作請求(例如CreateFile, ReadFile, WriteFile)就會送到Windows I/O subsystem (核心中運行),後者會繼續將請求送到dokan.sys。檔案系統程式可以利用dokan.dll提供的函數將我們的回呼函數(操作實現函數)註冊到dokan.sys中,後者會在收到請求之後調用這些我們提供的函數。回呼函數的結果會送回提出請求的程式。(可參見圖一)

dokan.sys 相當於一個運行在核心態的代理,作為提出請求的程式和我們實現各種操作的檔案系統程式的橋樑。至此,我們程式員就可以在使用者態輕鬆的建立調試檔案系統程式,大大提高了開發效率。

 

3.      建立檔案系統 

與FUSE類似,我們的檔案系統程式需要實現一個結構體中的各個操作DOKAN_OPERATIONS(聲明在dokan.h中),然後該結構體作為參數調用DokanMain掛載檔案系統。這些函數的參數與Windows APIs 一致,但必須做到安全執行緒,因為有可能有多個線程調用。

這些函數有個典型的調用順序:   
1. CreateFile(OpenDirectory, CreateFile)    
2. Other functions    
3. Cleanup    
4. CloseFile

file creation functions (OpenDirectory, CreateFile,…)總是在file access operations (listing directory, reading file attributes, …)之前調用。當檔案被CloseFile Windows API關閉時,Cleanup程式總是被dokan.sys調用。

傳回值為0時表示操作成功。

每個函數的最後一個參數是DOKAN_FILE_INFO structure

typedef struct _DOKAN_FILE_INFO {

ULONG64 Context;     //檔案系統程式維護,可作為檔案控制代碼

ULONG64 DokanContext;  //Dokan Library維護

ULONG ProcessId;   //操作ID

BOOL IsDirectory;  //目錄= TRUE

} DOKAN_FILE_INFO, *PDOKAN_FILE_INFO;

每一個檔案控制代碼都與一個DOKAN_FILE_INFO struct對應。該結構建立在檔案被CreateFile系統調用開啟時,回收在檔案被CloseFile系統調用關閉時。

下面是幾個操作的聲明:

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); //注意設定IsDirectory = TRUE

//CloseHandle ( Windows API)執行之後調用,如果檔案控制代碼在//CreateFile中建立,應該在此釋放,而不是在CloseFile。

//如果使用者在記憶體中緩衝了檔案,調用Cleanup之後還有可能調用讀寫//操作。

int (*Cleanup) (

LPCWSTR,      // FileName

PDOKAN_FILE_INFO);

//如果使用者調用CloseHandle後再開啟相同檔案,CreateFile之前可能//不會再調用CloseFile,這可能會出共用錯誤。

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 FindFires or FindFilesWithPattern

int (*FindFilesWithPattern) (

LPCWSTR,           // PathName

LPCWSTR,           // SearchPattern

PFillFindData,     // call this function with PWIN32_FIND_DATAW

PDOKAN_FILE_INFO);

上面兩個函數是回應列目錄項操作請求的。對每一個目錄項,檔案系統程式都會調用函數FillFindData( &win32FindDataw,  DokanFileInfo )。由於Windows的shell對於模式比對不支援,檔案系統程式就要執行通配模式。當檔案系統程式實現FindFiles,DokanLibrary會自動添加通配模式,我們也可以自己實現FindFilesWithPattern來加以控制。DokanIsNameInExpression (dokan.dll)函數就是用來實現模式比對的。

4.      掛載 

前面已經提到,可以調用DokanMain函數來掛載檔案系統。該程式會阻塞到檔案系統被卸載。

我們的檔案系統要做兩件事,一是為Dokan運行庫填寫DokanOptions,二是填寫帶各個操作函數指標的DokanOperations作為DokanMain的參數。

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;

  5.      卸載 

調用 DokanUnmount進行檔案系統的卸載。使用者也可以使用 DokanCtl 像這樣進行卸載:

> dokanctl.exe /u DriveLetter

  

Dokan簡介[轉]

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.