You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
292 lines
7.1 KiB
Go
292 lines
7.1 KiB
Go
3 years ago
|
package mft
|
||
3 years ago
|
|
||
|
import (
|
||
|
"b612.me/wincmd/ntfs/binutil"
|
||
|
"b612.me/wincmd/ntfs/utf16"
|
||
|
"encoding/binary"
|
||
|
"errors"
|
||
|
"io"
|
||
|
"os"
|
||
|
"reflect"
|
||
|
"runtime"
|
||
3 years ago
|
"strings"
|
||
3 years ago
|
"time"
|
||
|
"unsafe"
|
||
|
)
|
||
|
|
||
|
type MFTFile struct {
|
||
|
Name string
|
||
|
Path string
|
||
|
ModTime time.Time
|
||
|
Size uint64
|
||
3 years ago
|
Aszie uint64
|
||
3 years ago
|
IsDir bool
|
||
|
Node uint64
|
||
|
}
|
||
3 years ago
|
type FileEntry struct {
|
||
|
Name string
|
||
|
Parent uint64
|
||
|
}
|
||
3 years ago
|
|
||
|
func GetFileListsByMftFn(driver string, fn func(string, bool) bool) ([]MFTFile, error) {
|
||
|
var result []MFTFile
|
||
3 years ago
|
extendMftRecord := make(map[uint64][]Attribute)
|
||
|
fileMap := make(map[uint64]FileEntry)
|
||
|
f, size, err := GetMFTFile(driver)
|
||
3 years ago
|
if err != nil {
|
||
|
return []MFTFile{}, err
|
||
|
}
|
||
|
recordSize := int64(1024)
|
||
|
alreadyGot := int64(0)
|
||
|
maxRecordSize := size / recordSize
|
||
|
if maxRecordSize > 1024 {
|
||
|
maxRecordSize = 1024
|
||
|
}
|
||
|
for {
|
||
|
for {
|
||
|
if (size - alreadyGot) < maxRecordSize*recordSize {
|
||
|
maxRecordSize--
|
||
|
} else {
|
||
|
break
|
||
|
}
|
||
|
}
|
||
|
if maxRecordSize < 10 {
|
||
|
maxRecordSize = 1
|
||
|
}
|
||
|
buf := make([]byte, maxRecordSize*recordSize)
|
||
|
got, err := io.ReadFull(f, buf)
|
||
|
if err != nil {
|
||
|
if errors.Is(err, io.EOF) {
|
||
|
break
|
||
|
}
|
||
|
return []MFTFile{}, err
|
||
|
}
|
||
|
alreadyGot += int64(got)
|
||
|
for j := int64(0); j < 1024*maxRecordSize; j += 1024 {
|
||
3 years ago
|
record, err := ParseRecord(buf[j : j+1024])
|
||
3 years ago
|
if err != nil {
|
||
|
continue
|
||
|
}
|
||
3 years ago
|
if record.BaseRecordReference.ToUint64() != 0 {
|
||
|
val := extendMftRecord[record.BaseRecordReference.ToUint64()]
|
||
|
for _, v := range record.Attributes {
|
||
|
if v.Type == AttributeTypeData && v.ActualSize != 0 {
|
||
|
val = append(val, v)
|
||
|
}
|
||
|
}
|
||
|
if len(val) != 0 {
|
||
|
extendMftRecord[record.BaseRecordReference.ToUint64()] = val
|
||
|
}
|
||
|
}
|
||
|
if record.Flags&RecordFlagInUse == 1 && record.Flags&RecordFlagIsIndex == 0 {
|
||
3 years ago
|
var file MFTFile
|
||
3 years ago
|
file.IsDir = record.Flags&RecordFlagIsDirectory != 0
|
||
3 years ago
|
file.Node = record.FileReference.ToUint64()
|
||
|
parent := uint64(0)
|
||
|
for _, v := range record.Attributes {
|
||
3 years ago
|
if v.Type == AttributeTypeData {
|
||
|
file.Size = v.ActualSize
|
||
|
file.Aszie = v.AllocatedSize
|
||
|
}
|
||
|
if v.Type == AttributeTypeStandardInformation {
|
||
|
if len(v.Data) >= 48 {
|
||
|
r := binutil.NewLittleEndianReader(v.Data)
|
||
|
file.ModTime = ConvertFileTime(r.Uint64(0x08))
|
||
|
}
|
||
|
}
|
||
|
if v.Type == AttributeTypeFileName {
|
||
3 years ago
|
name := utf16.DecodeString(v.Data[66:], binary.LittleEndian)
|
||
|
if len(file.Name) < len(name) && len(name) > 0 {
|
||
3 years ago
|
if len(file.Name) > 0 && !strings.Contains(file.Name, "~") {
|
||
3 years ago
|
continue
|
||
|
}
|
||
|
file.Name = name
|
||
3 years ago
|
}
|
||
|
if file.Name != "" {
|
||
|
parent = binutil.NewLittleEndianReader(v.Data[:8]).Uint64(0)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
3 years ago
|
|
||
3 years ago
|
if file.Name != "" {
|
||
|
canAdd := fn(file.Name, file.IsDir)
|
||
|
if canAdd {
|
||
|
result = append(result, file)
|
||
|
}
|
||
|
if canAdd || file.IsDir {
|
||
3 years ago
|
fileMap[uint64(file.Node)] = FileEntry{
|
||
3 years ago
|
Name: file.Name,
|
||
3 years ago
|
Parent: uint64(parent),
|
||
3 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
(*reflect.SliceHeader)(unsafe.Pointer(&result)).Cap = len(result)
|
||
|
for k, v := range result {
|
||
3 years ago
|
if attrs, ok := extendMftRecord[v.Node]; ok {
|
||
|
if v.Aszie == 0 {
|
||
|
for _, v := range attrs {
|
||
|
if v.Type == AttributeTypeData && v.ActualSize != 0 {
|
||
|
result[k].Size = v.ActualSize
|
||
|
result[k].Aszie = v.AllocatedSize
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
delete(extendMftRecord, v.Node)
|
||
|
}
|
||
|
result[k].Path = GetFullUsnPath(driver, fileMap, uint64(v.Node))
|
||
3 years ago
|
}
|
||
|
fileMap = nil
|
||
|
runtime.GC()
|
||
|
return result, nil
|
||
|
}
|
||
|
|
||
|
func GetFileListsByMft(driver string) ([]MFTFile, error) {
|
||
|
return GetFileListsByMftFn(driver, func(string, bool) bool { return true })
|
||
|
}
|
||
|
|
||
|
func GetFileListsFromMftFileFn(filepath string, fn func(string, bool) bool) ([]MFTFile, error) {
|
||
|
var result []MFTFile
|
||
3 years ago
|
extendMftRecord := make(map[uint64][]Attribute)
|
||
|
fileMap := make(map[uint64]FileEntry)
|
||
3 years ago
|
f, err := os.Open(filepath)
|
||
|
if err != nil {
|
||
|
return []MFTFile{}, err
|
||
|
}
|
||
|
stat, err := f.Stat()
|
||
|
if err != nil {
|
||
|
return []MFTFile{}, err
|
||
|
}
|
||
|
size := stat.Size()
|
||
|
recordSize := int64(1024)
|
||
|
alreadyGot := int64(0)
|
||
|
maxRecordSize := size / recordSize
|
||
|
if maxRecordSize > 1024 {
|
||
|
maxRecordSize = 1024
|
||
|
}
|
||
|
for {
|
||
|
for {
|
||
|
if (size - alreadyGot) < maxRecordSize*recordSize {
|
||
|
maxRecordSize--
|
||
|
} else {
|
||
|
break
|
||
|
}
|
||
|
}
|
||
|
if maxRecordSize < 10 {
|
||
|
maxRecordSize = 1
|
||
|
}
|
||
|
buf := make([]byte, maxRecordSize*recordSize)
|
||
|
got, err := io.ReadFull(f, buf)
|
||
|
if err != nil {
|
||
|
if errors.Is(err, io.EOF) {
|
||
|
break
|
||
|
}
|
||
|
return []MFTFile{}, err
|
||
|
}
|
||
|
alreadyGot += int64(got)
|
||
|
for j := int64(0); j < 1024*maxRecordSize; j += 1024 {
|
||
3 years ago
|
record, err := ParseRecord(buf[j : j+1024])
|
||
3 years ago
|
if err != nil {
|
||
|
continue
|
||
|
}
|
||
3 years ago
|
if record.BaseRecordReference.ToUint64() != 0 {
|
||
|
val := extendMftRecord[record.BaseRecordReference.ToUint64()]
|
||
|
for _, v := range record.Attributes {
|
||
|
if v.Type == AttributeTypeData && v.ActualSize != 0 {
|
||
|
val = append(val, v)
|
||
|
}
|
||
|
}
|
||
|
if len(val) != 0 {
|
||
|
extendMftRecord[record.BaseRecordReference.ToUint64()] = val
|
||
|
}
|
||
|
}
|
||
|
if record.Flags&RecordFlagInUse == 1 && record.Flags&RecordFlagIsIndex == 0 {
|
||
3 years ago
|
var file MFTFile
|
||
3 years ago
|
file.IsDir = record.Flags&RecordFlagIsDirectory != 0
|
||
3 years ago
|
file.Node = record.FileReference.ToUint64()
|
||
|
parent := uint64(0)
|
||
|
for _, v := range record.Attributes {
|
||
3 years ago
|
if v.Type == AttributeTypeData {
|
||
|
file.Size = v.ActualSize
|
||
|
file.Aszie = v.AllocatedSize
|
||
|
}
|
||
|
if v.Type == AttributeTypeStandardInformation {
|
||
|
if len(v.Data) >= 48 {
|
||
|
r := binutil.NewLittleEndianReader(v.Data)
|
||
|
file.ModTime = ConvertFileTime(r.Uint64(0x08))
|
||
|
}
|
||
|
}
|
||
|
if v.Type == AttributeTypeFileName {
|
||
3 years ago
|
name := utf16.DecodeString(v.Data[66:], binary.LittleEndian)
|
||
|
if len(file.Name) < len(name) && len(name) > 0 {
|
||
3 years ago
|
if len(file.Name) > 0 && !strings.Contains(file.Name, "~") {
|
||
3 years ago
|
continue
|
||
|
}
|
||
|
file.Name = name
|
||
3 years ago
|
}
|
||
|
if file.Name != "" {
|
||
|
parent = binutil.NewLittleEndianReader(v.Data[:8]).Uint64(0)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
if file.Name != "" {
|
||
|
canAdd := fn(file.Name, file.IsDir)
|
||
|
if canAdd {
|
||
|
result = append(result, file)
|
||
|
}
|
||
|
if canAdd || file.IsDir {
|
||
3 years ago
|
fileMap[uint64(file.Node)] = FileEntry{
|
||
3 years ago
|
Name: file.Name,
|
||
3 years ago
|
Parent: uint64(parent),
|
||
3 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
(*reflect.SliceHeader)(unsafe.Pointer(&result)).Cap = len(result)
|
||
|
for k, v := range result {
|
||
3 years ago
|
if attrs, ok := extendMftRecord[v.Node]; ok {
|
||
|
if v.Aszie == 0 {
|
||
|
for _, v := range attrs {
|
||
|
if v.Type == AttributeTypeData && v.ActualSize != 0 {
|
||
|
result[k].Size = v.ActualSize
|
||
|
result[k].Aszie = v.AllocatedSize
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
delete(extendMftRecord, v.Node)
|
||
|
}
|
||
|
result[k].Path = GetFullUsnPath(" ", fileMap, uint64(v.Node))
|
||
3 years ago
|
}
|
||
|
fileMap = nil
|
||
|
runtime.GC()
|
||
|
return result, nil
|
||
|
}
|
||
|
|
||
|
func GetFileListsFromMftFile(filepath string) ([]MFTFile, error) {
|
||
|
return GetFileListsFromMftFileFn(filepath, func(string, bool) bool { return true })
|
||
|
}
|
||
3 years ago
|
|
||
|
func GetFullUsnPath(diskName string, fileMap map[uint64]FileEntry, id uint64) (name string) {
|
||
|
for id != 0 {
|
||
|
fe := fileMap[id]
|
||
|
if id == fe.Parent {
|
||
|
name = "\\" + name
|
||
|
break
|
||
|
}
|
||
|
if name == "" {
|
||
|
name = fe.Name
|
||
|
} else {
|
||
|
name = fe.Name + "\\" + name
|
||
|
}
|
||
|
id = fe.Parent
|
||
|
}
|
||
|
name = diskName[:len(diskName)-1] + name
|
||
|
return
|
||
|
}
|