/
recursive.go
129 lines (116 loc) 路 3.45 KB
/
recursive.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
// Copyright (c) 2019-2020 Siemens AG
// Copyright (c) 2019-2021 Jonas Plum
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
// Author(s): Jonas Plum
package recursivefs
import (
"archive/zip"
"errors"
"io"
"io/fs"
"os"
"path"
"strings"
"github.com/nlepage/go-tarfs"
"github.com/forensicanalysis/filetype"
"github.com/forensicanalysis/fslib/bufferfs"
"github.com/forensicanalysis/fslib/fat16"
"github.com/forensicanalysis/fslib/fsio"
"github.com/forensicanalysis/fslib/gpt"
"github.com/forensicanalysis/fslib/mbr"
"github.com/forensicanalysis/fslib/ntfs"
"github.com/forensicanalysis/goaff4"
)
func parseRealPath(fsys fs.FS, sample string) (rpath []element, err error) {
parts := strings.Split(sample, "/")
if len(parts) == 0 {
return []element{{fsys, "."}}, nil
}
key := "."
for len(parts) > 0 {
key = path.Join(key, parts[0])
parts = parts[1:]
info, err := fs.Stat(fsys, key)
if err != nil {
return nil, err
}
if !info.IsDir() {
rpath = append(rpath, element{fsys, key})
f, err := fsys.Open(key)
if err != nil {
return nil, err
}
cfsys, err := childFS(f, key)
if err != nil || cfsys == nil {
continue
}
fsys = cfsys
key = "."
} else if len(parts) == 0 {
rpath = append(rpath, element{fsys, key})
}
}
return rpath, nil
}
// func childFS(fsys fs.FS, name string) (fs.FS, error) {
func childFS(r io.Reader, name string) (fs.FS, error) { // nolint: gocyclo
t, err := filetype.DetectReaderByExtension(r, path.Ext(name))
if err != nil && err != io.EOF {
return nil, err
}
readSeekerAt, ok := r.(fsio.ReadSeekerAt)
if !ok {
return nil, errors.New("files must be ReadSeekerAt")
}
_, _ = readSeekerAt.Seek(0, os.SEEK_SET)
var fsys fs.FS
switch t {
case filetype.Zip, filetype.Xlsx, filetype.Pptx, filetype.Docx:
var size int64
size, err = fsio.GetSize(readSeekerAt)
if err != nil {
return nil, err
}
fsys, err = zip.NewReader(readSeekerAt, size)
case filetype.Tar:
fsys, err = tarfs.New(readSeekerAt)
case filetype.FAT16:
return fat16.New(readSeekerAt)
case filetype.MBR:
fsys, err = mbr.New(readSeekerAt)
case filetype.GPT:
return gpt.New(readSeekerAt)
case filetype.NTFS:
fsys, err = ntfs.New(readSeekerAt)
case filetype.AFF4:
var size int64
size, err = fsio.GetSize(readSeekerAt)
if err != nil {
return nil, err
}
fsys, err = goaff4.New(readSeekerAt, size)
default:
return nil, nil
}
if err != nil {
return nil, err
}
return bufferfs.New(fsys), nil
}