-
Notifications
You must be signed in to change notification settings - Fork 0
/
action_file_num.go
332 lines (260 loc) · 6.05 KB
/
action_file_num.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
package main
import (
"fmt"
"os"
"sort"
"syscall"
ntfs "github.com/corebreaker/ntfstool/core"
)
func do_file_num(file int64, arg *tActionArg) error {
var record ntfs.FileRecord
if err := arg.disk.ReadFileRecord(file, &record); err != nil {
return err
}
fmt.Println()
fmt.Println("Record:")
ntfs.PrintStruct(record)
fmt.Println("Good record:", record.Type.IsGood())
if record.Type != ntfs.RECTYP_FILE {
return nil
}
_, ok := arg.GetExt("data")
if ok {
fmt.Println()
fmt.Println("Data:")
ntfs.PrintBytes(record.Data[:])
}
_, ok = arg.GetExt("raw")
if ok {
return nil
}
attributes, err := record.GetAttributes(false)
if err != nil {
return err
}
_, ok = arg.GetExt("name")
if ok {
for _, attr := range attributes {
if attr.AttributeType == ntfs.ATTR_FILE_NAME {
desc, err := record.MakeAttributeFromHeader(attr)
if err != nil {
return err
}
val, err := arg.disk.GetAttributeValue(desc, true)
if err != nil {
return err
}
fmt.Println()
fmt.Println("Filename:", val.GetFilename())
}
}
}
index, ok := arg.IntExt("index")
if ok {
attr, ok := attributes[int(index)]
if !ok {
fmt.Println("Attribute", index, "not found")
return nil
}
switch attr.AttributeType {
case ntfs.ATTR_INDEX_ROOT, ntfs.ATTR_INDEX_ALLOCATION:
default:
fmt.Println("Attribute", index, "is not an index")
return nil
}
desc, err := record.MakeAttributeFromHeader(attr)
if err != nil {
return err
}
entry_idx, ok := arg.IntExt("block")
if ok {
_, noread := arg.GetExt("noread")
val, err := arg.disk.GetAttributeValue(desc, !noread)
if err != nil {
return err
}
entry, err := val.GetFirstEntry()
if err != nil {
return err
}
for i := int64(0); i < entry_idx; i++ {
entry, err = val.GetNextEntry(entry)
if err != nil {
return err
}
}
fmt.Println()
fmt.Println("Entry:")
ntfs.PrintStruct(entry.DirectoryEntryExtendedHeader)
block, err := val.GetIndexBlockFromEntry(entry)
if err != nil {
return err
}
fmt.Println()
fmt.Println("Block:")
ntfs.PrintStruct(block)
return nil
}
fmt.Println()
fmt.Println("Attribute:")
ntfs.PrintStruct(desc.Desc)
if desc.Name != "" {
fmt.Println(" Name:", desc.Name)
}
val, err := arg.disk.GetAttributeValue(desc, true)
if err != nil {
return err
}
fmt.Println()
fmt.Println("Index Content:")
next_entry, err := val.GetFirstEntry()
if err != nil {
return err
}
for i := 0; next_entry != nil; i++ {
entry := next_entry
next_entry, err = val.GetNextEntry(entry)
if err != nil {
return err
}
if entry.FileReferenceNumber == 0 {
fmt.Println(fmt.Sprintf(" - %d : [%v] {%s}", i, entry.FileReferenceNumber, entry.Name))
continue
}
var file_rec ntfs.FileRecord
if err := arg.disk.ReadFileRecordFromRef(entry.FileReferenceNumber, &file_rec); err != nil {
if ntfs.IsEof(err) {
fmt.Println(fmt.Sprintf(" - %d : [%v] {%s}", i, entry.FileReferenceNumber, entry.Name))
continue
}
return err
}
name, err := arg.disk.GetFileRecordFilename(&file_rec)
if err != nil {
return err
}
fmt.Println(fmt.Sprintf(" - %d : %s [%v] {%s}", i, name, entry.FileReferenceNumber, entry.Name))
}
return nil
}
attr_num, ok := arg.IntExt("attribute")
if ok {
attr, ok := attributes[int(attr_num)]
if !ok {
fmt.Println("Attribute", attr_num, "not found")
return nil
}
desc, err := record.MakeAttributeFromHeader(attr)
if err != nil {
return err
}
fmt.Println()
fmt.Println("Attribute:")
ntfs.PrintStruct(desc.Desc)
if desc.Name != "" {
fmt.Println(" Name:", desc.Name)
}
_, noread := arg.GetExt("noread")
_, ask_runlist := arg.GetExt("runlist")
val, err := arg.disk.GetAttributeValue(desc, !(noread || ask_runlist))
if err != nil {
return err
}
fmt.Println()
fmt.Println(fmt.Sprintf("Value (size=%d):", val.Size))
ntfs.PrintStruct(val.Value)
fmt.Println(" First LCN:", val.FirstLCN)
if ask_runlist {
fmt.Println()
fmt.Println("Run list:")
for _, entry := range desc.GetRunList() {
fmt.Println(" -", entry)
}
return nil
}
if _, value := arg.GetExt("value"); value {
fmt.Println()
fmt.Println("Content:")
ntfs.PrintBytes(val.Data)
}
entry_idx, ok := arg.IntExt("block")
if ok {
entry, err := val.GetFirstEntry()
if err != nil {
return err
}
for i := int64(0); i < entry_idx; i++ {
entry, err = val.GetNextEntry(entry)
if err != nil {
return err
}
}
fmt.Println()
fmt.Println("Entry:")
ntfs.PrintStruct(entry.DirectoryEntryExtendedHeader)
block, err := val.GetIndexBlockFromEntry(entry)
if err != nil {
return err
}
fmt.Println()
fmt.Println("Block:")
ntfs.PrintStruct(block)
return nil
}
save, ok := arg.GetExt("save")
if ok {
_, err := os.Stat(save)
switch err {
case os.ErrExist, os.ErrNotExist, nil:
default:
_, ok := err.(*os.PathError)
if !ok {
return ntfs.WrapError(err)
}
}
var f *os.File
exists := err != os.ErrNotExist
if exists {
perr, ok := err.(*os.PathError)
if ok {
exists = perr.Err != syscall.ENOENT
}
}
if exists {
f, err = os.OpenFile(save, os.O_TRUNC|os.O_WRONLY, 0660)
} else {
f, err = os.OpenFile(save, os.O_CREATE|os.O_WRONLY, 0660)
}
if err != nil {
return ntfs.WrapError(err)
}
defer ntfs.DeferedCall(f.Close)
fmt.Println()
fmt.Println("Header size:", val.Size-len(val.Data))
_, err = f.Write(val.Content)
if err != nil {
return ntfs.WrapError(err)
}
}
return nil
}
attr_list := make([]int, 0)
for idx := range attributes {
attr_list = append(attr_list, idx)
}
sort.Ints(attr_list)
for _, idx := range attr_list {
attr := attributes[idx]
fmt.Println()
fmt.Println(fmt.Sprintf("Attribute %d :", idx))
ntfs.PrintStruct(attr)
desc, err := record.MakeAttributeFromHeader(attr)
if err != nil {
return err
}
if desc.Name != "" {
fmt.Println(" Name:", desc.Name)
}
}
return nil
}