-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathfile.go
More file actions
184 lines (153 loc) · 3.88 KB
/
file.go
File metadata and controls
184 lines (153 loc) · 3.88 KB
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
// Copyright (c) Mondoo, Inc.
// SPDX-License-Identifier: BUSL-1.1
package docker
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/client"
"github.com/spf13/afero"
"go.mondoo.com/cnquery/v12/providers/os/connection/ssh/cat"
"go.mondoo.com/cnquery/v12/providers/os/fsutil"
)
func FileOpen(dockerClient *client.Client, path string, container string, conn *ContainerConnection, catFs *cat.Fs) (afero.File, error) {
f := &File{
path: path,
dockerClient: dockerClient,
container: container,
connection: conn,
catFs: catFs,
}
err := f.Open()
return f, err
}
type File struct {
path string
container string
dockerClient *client.Client
connection *ContainerConnection
reader *bytes.Reader
catFs *cat.Fs
}
func (f *File) Open() error {
r, _, err := f.getFileDockerReader(f.path)
if err != nil {
return os.ErrNotExist
}
defer r.Close()
data, err := fsutil.ReadFileFromTarStream(r)
if err != nil {
return err
}
f.reader = bytes.NewReader(data)
return nil
}
func (f *File) Close() error {
return nil
}
func (f *File) Name() string {
return f.path
}
func (f *File) Stat() (os.FileInfo, error) {
return f.catFs.Stat(f.path)
}
func (f *File) Sync() error {
return errors.New("not implemented")
}
func (f *File) Truncate(size int64) error {
return errors.New("not implemented")
}
func (f *File) Read(b []byte) (n int, err error) {
return f.reader.Read(b)
}
func (f *File) ReadAt(b []byte, off int64) (n int, err error) {
return f.reader.ReadAt(b, off)
}
func (f *File) Readdir(count int) (res []os.FileInfo, err error) {
return f.catFs.ReadDir(f.path)
}
func (f *File) Readdirnames(n int) ([]string, error) {
c, err := f.connection.RunCommand(fmt.Sprintf("find %s -maxdepth 1 -type d", f.path))
if err != nil {
return []string{}, err
}
content, err := io.ReadAll(c.Stdout)
if err != nil {
return []string{}, err
}
directories := strings.Split(string(content), "\n")
// first result is always self
if len(directories) > 0 {
directories = directories[1:]
}
// extract names
basenames := []string{}
for _, dir := range directories {
if dir == "" {
continue
}
basenames = append(basenames, filepath.Base(dir))
}
return basenames, nil
}
func (f *File) Seek(offset int64, whence int) (int64, error) {
return 0, errors.New("not implemented")
}
func (f *File) Write(b []byte) (n int, err error) {
return 0, errors.New("not implemented")
}
func (f *File) WriteAt(b []byte, off int64) (n int, err error) {
return 0, errors.New("not implemented")
}
func (f *File) WriteString(s string) (ret int, err error) {
return 0, errors.New("not implemented")
}
func (f *File) getFileDockerReader(path string) (io.ReadCloser, container.PathStat, error) {
r, stat, err := f.dockerClient.CopyFromContainer(context.Background(), f.container, path)
// follow symlink if stat.LinkTarget is set
if len(stat.LinkTarget) > 0 {
return f.getFileDockerReader(stat.LinkTarget)
}
return r, stat, err
}
// returns a TarReader stream the caller is responsible for closing the stream
func (f *File) Tar() (io.ReadCloser, error) {
r, _, err := f.getFileDockerReader(f.path)
return r, err
}
// func (f *File) Exists() bool {
// if strings.HasPrefix(f.path, "/proc") {
// entries := f.procls()
// for i := range entries {
// if entries[i] == f.path {
// return true
// }
// }
// return false
// }
// r, _, err := f.getFileReader(f.path)
// if err != nil {
// return false
// }
// r.Close()
// return true
// }
// returns all directories and files under /proc
func (f *File) procls() []string {
c, err := f.connection.RunCommand("find /proc")
if err != nil {
return []string{}
}
content, err := io.ReadAll(c.Stdout)
if err != nil {
return []string{}
}
// all files
return strings.Split(string(content), "\n")
}