-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction_extract.go
291 lines (226 loc) · 5.27 KB
/
action_extract.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
package main
import (
"fmt"
"os"
"strings"
ntfs "github.com/corebreaker/ntfstool/core"
"github.com/corebreaker/ntfstool/core/data"
"github.com/corebreaker/ntfstool/extract"
"github.com/siddontang/go/ioutil2"
)
func do_show_id(id string, arg *tActionArg) error {
var src *os.File
var err error
new_name, rename := arg.GetExt("rename")
if rename {
src, err = arg.GetFile()
if err != nil {
return err
}
} else {
src, err = arg.GetInput()
if err != nil {
return err
}
}
fmt.Println("Reading")
files, err := extract.MakeFileModifier(src)
if err != nil {
return err
}
defer ntfs.DeferedCall(files.Close)
rec0, err := files.GetRecordAt(0)
if err != nil {
return err
}
index, ok := rec0.(*extract.Index)
if !ok {
return ntfs.WrapError(fmt.Errorf("Bad file format"))
}
i, found := index.IdMap[id]
if !found {
fmt.Println()
fmt.Println("Id", id, "has not found.")
}
file, err := files.GetRecordAt(int(i))
if err != nil {
return err
}
if rename && (len(new_name) > 0) {
file.SetName(new_name)
if err := files.SetRecordAt(int(i), file); err != nil {
return err
}
}
fmt.Println()
fmt.Println("Index= ", file.GetIndex())
fmt.Println("Is Dir?= ", file.IsDir())
fmt.Println("Is File?=", file.IsFile())
fmt.Println("Is Root?=", file.IsRoot())
fmt.Println()
fmt.Println("Record:")
file.Print()
return nil
}
func do_show_parent(id string, arg *tActionArg) error {
src, err := arg.GetInput()
if err != nil {
return err
}
fmt.Println("Reading")
files, err := extract.MakeFileReader(src)
if err != nil {
return err
}
defer ntfs.DeferedCall(files.Close)
stream, err := files.MakeStream()
if err != nil {
return err
}
defer ntfs.DeferedCall(stream.Close)
fmt.Println()
fmt.Println("Results:")
for entry := range stream {
record := entry.Record()
file := record.GetFile()
if file.Parent != id {
continue
}
fmt.Println(" -", file.Id, "(", file.Name, ")")
}
fmt.Println()
return nil
}
func do_show_parent_ref(ref int64, arg *tActionArg) error {
mft, has_mft := arg.GetExt("with-mft")
if !has_mft {
return ntfs.WrapError(fmt.Errorf("MFT Id is missing"))
}
src, err := arg.GetInput()
if err != nil {
return err
}
fmt.Println("Reading")
files, err := extract.MakeFileReader(src)
if err != nil {
return err
}
defer ntfs.DeferedCall(files.Close)
stream, err := files.MakeStream()
if err != nil {
return err
}
defer ntfs.DeferedCall(stream.Close)
file_idx := data.FileIndex(ref)
fmt.Println()
fmt.Println("Results:")
for entry := range stream {
record := entry.Record()
file := record.GetFile()
if (file.Mft != mft) && (file.ParentRef.GetFileIndex() == file_idx) {
continue
}
fmt.Println(" -", file.Id, "(", file.Name, ")")
}
fmt.Println()
return nil
}
func do_list_files(node_pattern string, arg *tActionArg) error {
src, err := arg.GetInput()
if err != nil {
return err
}
fmt.Println("Reading")
tree, err := extract.ReadTreeFromFile(src)
if err != nil {
return err
}
node_list := tree.Roots
msg := "Results:"
if node_pattern != "" {
matcher, err := parseNodePattern(node_pattern, tree)
if err != nil {
return err
}
var from_node *extract.Node
from := arg.GetFromParam()
if len(from) > 0 {
node, ok := tree.Nodes[from]
if !ok {
return ntfs.WrapError(fmt.Errorf("From ID `%s` not found", from))
}
if !node.IsDir() {
return ntfs.WrapError(fmt.Errorf("From ID `%s` is not a directory", from))
}
from_node = node
}
node_list = matcher.GetNodes(from_node)
if len(node_list) == 1 {
for _, n := range node_list {
if n.IsDir() {
path := tree.GetNodePath(n)
root := tree.GetRootID(n.File.Mft)
msg = fmt.Sprintf("Results for %s (DirID=%s, RootID=%s):", path, n.File.Id, root)
node_list = n.Children
}
}
}
}
_, nometa := arg.GetExt("nometa")
_, noempty := arg.GetExt("noempty")
fmt.Println()
fmt.Println(msg)
for _, n := range node_list {
if (nometa && extract.IsMetaFile(n.File)) || (noempty && n.IsEmpty(noempty)) {
continue
}
infos := n.File.Name
if n.IsDir() {
infos += fmt.Sprintf(", Dir {children=%d}", n.ChildCount(nometa))
}
if n.IsFile() {
infos += ", File"
}
fmt.Println(fmt.Sprintf(" - %s (%s)", n.File.Id, infos))
}
return nil
}
func do_save_file(file string, arg *tActionArg) error {
src, dest, err := arg.GetTransferFiles(".")
if err != nil {
return err
}
fmt.Println("Reading")
tree, err := extract.ReadTreeFromFile(src)
if err != nil {
return err
}
node, ok := tree.Nodes[file]
if !ok {
return ntfs.WrapError(fmt.Errorf("Bad ID: %d", file))
}
destname := strings.TrimRight(dest, string([]rune{os.PathSeparator}))
if len(destname) == 0 {
destname = "."
}
fmt.Println("Writing to:", destname)
if ioutil2.FileExists(destname) {
infos, err := os.Stat(destname)
if err != nil {
return ntfs.WrapError(err)
}
if !infos.IsDir() {
return ntfs.WrapError(fmt.Errorf("Path `%s` is a file, not a directory.", destname))
}
} else {
if err := os.MkdirAll(destname, 0770); err != nil {
return ntfs.WrapError(err)
}
}
disk := arg.disk.GetDisk()
defer ntfs.DeferedCall(disk.Close)
_, noempty := arg.GetExt("noempty")
_, nometa := arg.GetExt("nometa")
_, err = extract.SaveNode(disk, node, destname, noempty, nometa)
return err
}