這是一個建立於
的文章,其中的資訊可能已經有所發展或是發生改變。
起初,做了個小程式,用來檢測磁碟中所有的檔案
package mainimport( "fmt" "io/ioutil" "os")var dirpath ="D:\\"func main(){ CheckDir(dirpath)}func CheckDir(dirpath string){ dirs, err := ioutil.ReadDir(dirpath) if err !=nil{ panic("目錄輸入有誤!") } for _, dir := range dirs { if dir.IsDir(){ if dir.Name()=="System Volume Information"{ fmt.Println("檢測目錄:", dirpath+"\\"+dir.Name(),"sys", dir.Sys()) return } fmt.Println("檢測目錄:", dirpath+"\\"+dir.Name(),"sys", dir.Sys()) CheckDir(dirpath +"\\"+ dir.Name()) }else{ fmt.Println("檔案:", dirpath+"\\"+dir.Name(),"大小:", dir.Size()) if dir.Size()==0{ fmt.Println("刪除檔案:", dirpath+"\\"+dir.Name(), dir.Size()) } } }}
輸出結果為:
檔案: D:\\My Documents\Downloads\wcftestclient_exe.rar 大小: 110608
檢測目錄: D:\\SoftDown sys &{16 {2081520578 30419524} {1134116594 30422735} {1134116594 30422735} 0 0}
檔案: D:\\SoftDown\Sublime Text Build 3012 Setup.exe 大小: 7051120
檢測目錄: D:\\SoftDown\Vice.2015.720p.WEB-DL.DD5.1.H.264-PLAYNOW sys &{16 {753370423 30421925} {753410426 30421925} {753420426 30421925} 0 0}
檔案: D:\\SoftDown\Vice.2015.720p.WEB-DL.DD5.1.H.264-PLAYNOW\Vice.2015.720p.WEB-DL.DD5.1.H.264-PLAYNOW.mkv.tdl 大小: 3255167129
檔案: D:\\SoftDown\Vice.2015.720p.WEB-DL.DD5.1.H.264-PLAYNOW\f7a8aa8e2082c4ebe28f2c26cf16e4b08a27d5c1.torrent 大小: 31751
檔案: D:\\SoftDown\Vice.2015.720p.WEB-DL.DD5.1.H.264-PLAYNOW.qud.cfg 大小: 556
檔案: D:\\SoftDown\[iPlaySoft.com]VS2013_RTM_ULT_CHS.iso 大小: 3077509120
檢測目錄: D:\\System Volume Information sys &{22 {1206047926 30310737} {1206983927 30310737} {1206983927 30310737} 0 0}
panic: 目錄輸入有誤!
goroutine 1 [running]:
main.CheckDir(0xc0820585c0, 0x1d)
F:/goproj/GitTest.git/trunk/src/WebSite/main.go:22 +0xdemain.CheckDir(0x4f3890, 0x3)
F:/goproj/GitTest.git/trunk/src/WebSite/main.go:28 +0x46amain.main()
F:/goproj/GitTest.git/trunk/src/WebSite/main.go:15 +0xfc
這裡會報一個異常
D:\System Volume Information Microsoft的解答:
本文介紹如何訪問 System Volume Information 檔案夾。System Volume Information 檔案夾是一個隱藏的系統檔案夾,系統還原工具使用此檔案夾來儲存它的資訊和還原點。電腦的每個分區上都有一個 System Volume Information 檔案夾。為了進行故障排除,可能需要訪問此檔案夾。
於是就要判斷檔案是否是隱藏檔案,但是Golang api中並未直接給出這個IsHidden屬性
調式源碼得知:
os.Stat方法可以擷取到一個FileInfo,於是寫了如下代碼 fileinfo, _ := os.Stat(dirpath) sysifno := fileinfo.Sys() fmt.Println(sysifno)
- os.Stat
- os包中的func Lstat(name string) (fi FileInfo, err error)
- 通過
fs,err:=&fileStat{name: basename(name)} 這個初始化得到了一個FileInfo對象。
代碼:
type fileStat struct { name string sys syscall.Win32FileAttributeData // used to implement SameFile sync.Mutex path string vol uint32 idxhi uint32 idxlo uint32}type Win32FileAttributeData struct { FileAttributes uint32 CreationTimeFiletime LastAccessTimeFiletime LastWriteTimeFiletime FileSizeHigh uint32 FileSizeLow uint32}
然後調用 syscall.GetFileAttributesEx(namep, syscall.GetFileExInfoStandard, (*byte)(unsafe.Pointer(&fs.sys))) 將第3步中的fs傳入次方法中,調用kernel32.dll的GetFileAttributes方法擷取了檔案屬性
kernel32.dll的GetFileAttributes方法傳回值如下:
在MSDN中,檔案總共有15種屬性,根據磁碟的分區格式不同,檔案的屬性也會不同。
現在針對GetFileAttributes函數的傳回值做以下整理
| 返回欄位 |
傳回值 |
屬性類型 |
| FILE_ATTRIBUTE_READONLY |
1 |
唯讀 |
| FILE_ATTRIBUTE_HIDDEN |
2 |
隱藏 |
| FILE_ATTRIBUTE_SYSTEM |
4 |
系統 |
| FILE_ATTRIBUTE_DIRECTORY |
16 |
目錄 |
| FILE_ATTRIBUTE_ARCHIVE |
32 |
存檔 |
| FILE_ATTRIBUTE_DEVICE |
64 |
保留 |
| FILE_ATTRIBUTE_NORMAL |
128 |
正常 |
| FILE_ATTRIBUTE_TEMPORARY |
256 |
臨時 |
| FILE_ATTRIBUTE_SPARSE_FILE |
512 |
疏鬆檔案 |
| FILE_ATTRIBUTE_REPARSE_POINT |
1024 |
超連結或捷徑 |
| FILE_ATTRIBUTE_COMPRESSED |
2048 |
壓縮 |
| FILE_ATTRIBUTE_OFFLINE |
4096 |
離線 |
| FILE_ATTRIBUTE_NOT_CONTENT_INDEXED |
8192 |
索引 |
| FILE_ATTRIBUTE_ENCRYPTED |
16384 |
加密 |
| FILE_ATTRIBUTE_VIRTUAL |
65536 |
虛擬 |
橙色標記的屬性為Windows系統中檔案的公有屬性,其中“唯讀”、“隱藏”、“系統”、“存檔”為檔案的四種基本屬性。compressed,content_indexed,encrypted只存在於NTFS分區中。
檔案去掉全部屬性後(四種基本屬性),將自動標籤為normal。同時具有system和hidden屬性的檔案會在系統中徹底隱形,這也是病毒常用的伎倆。commpressed和encrypted不能共存。預設情況下檔案都有content_indexed屬性
這裡就能夠理解這裡輸出的
FileAttributes = 22
檢測目錄:D:\System Volume Information sys &{22 {1206047926 30310737} {1206983927 30310737} {1206983927 30310737} 0 0}
22代表的是也就是
1 0 0 0 0 16 目錄
1 0 0 4 系統
& 1 0 2 隱藏
-—————-
1 0 1 1 0 = 22
代表此檔案是隱藏檔案
檢測目錄: D:\SoftDown\Vice.2015.720p.WEB-DL.DD5.1.H.264-PLAYNOW sys &{16 {753370423 30421925} {753410426 30421925} {753420426 30421925} 0 0}
16 目錄
這樣就很明確了:也就是說如果二進位中倒數第二位為1,代表是隱藏目錄(檔案)
如此就能進行判斷了
package mainimport( "fmt" "io/ioutil" "os" "reflect" "strconv")var dirpath ="D:\\"func main(){ CheckDir(dirpath)}func CheckDir(dirpath string){ dirs, err := ioutil.ReadDir(dirpath) if err !=nil{ panic("目錄輸入有誤!") } for _, dir := range dirs { if dir.IsDir(){ if!CheckIsHidden(dir){ fmt.Println("檢測目錄:", dirpath+"\\"+dir.Name()) CheckDir(dirpath +"\\"+ dir.Name()) } }else{ fmt.Println("檔案:", dirpath+"\\"+dir.Name(),"大小:", dir.Size()) if dir.Size()==0{ fmt.Println("刪除檔案:", dirpath+"\\"+dir.Name(), dir.Size()) } } }}func CheckIsHidden(file os.FileInfo)bool{//"通過反射來擷取Win32FileAttributeData的FileAttributes fa := reflect.ValueOf(file.Sys()).Elem().FieldByName("FileAttributes").Uint() bytefa :=[]byte(strconv.FormatUint(fa,2)) if bytefa[len(bytefa)-2]=='1'{ fmt.Println("隱藏目錄:", file.Name()) return true } return false}