-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathcat_file.go
More file actions
164 lines (134 loc) · 3.09 KB
/
cat_file.go
File metadata and controls
164 lines (134 loc) · 3.09 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
// Copyright (c) Mondoo, Inc.
// SPDX-License-Identifier: BUSL-1.1
package cat
import (
"bytes"
"encoding/base64"
"io"
"os"
"path/filepath"
"strings"
"github.com/cockroachdb/errors"
"github.com/kballard/go-shellquote"
)
func NewFile(catfs *Fs, path string, useBase64encoding bool) *File {
return &File{catfs: catfs, path: path, useBase64encoding: useBase64encoding}
}
type File struct {
catfs *Fs
buf *bytes.Buffer
path string
useBase64encoding bool
}
func (f *File) readContent() (*bytes.Buffer, error) {
// we need shellquote to escape filenames with spaces
catCmd := shellquote.Join("cat", f.path)
if f.useBase64encoding {
catCmd = catCmd + " | base64"
}
cmd, err := f.catfs.commandRunner.RunCommand(catCmd)
if err != nil {
return nil, err
}
data, err := io.ReadAll(cmd.Stdout)
if err != nil {
return nil, err
}
if f.useBase64encoding {
data, err = base64.StdEncoding.DecodeString(string(data))
if err != nil {
return nil, errors.Wrap(err, "could not decode base64 data stream")
}
}
return bytes.NewBuffer(data), 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 nil
}
func (f *File) Truncate(size int64) error {
return nil
}
func (f *File) Read(b []byte) (n int, err error) {
if f.buf == nil {
bufData, err := f.readContent()
if err != nil {
return 0, err
}
f.buf = bufData
}
return f.buf.Read(b)
}
func (f *File) ReadAt(b []byte, off int64) (n int, err error) {
return 0, errors.New("not implemented")
}
func (f *File) Readdir(count int) (res []os.FileInfo, err error) {
names, err := f.Readdirnames(count)
if err != nil {
return nil, err
}
res = make([]os.FileInfo, len(names))
for i, name := range names {
var statPath string
if filepath.IsAbs(name) {
statPath = name
} else {
statPath = filepath.Join(f.path, name)
}
res[i], err = f.catfs.Stat(statPath)
if err != nil {
return nil, err
}
}
return res, nil
}
func (f *File) Readdirnames(n int) (names []string, err error) {
// TODO: input n is ignored
cmd, err := f.catfs.commandRunner.RunCommand("ls -1 '" + f.path + "'")
if err != nil {
return nil, err
}
data, err := io.ReadAll(cmd.Stdout)
if err != nil {
return nil, err
}
list := strings.Split(strings.TrimSpace(string(data)), "\n")
if list != nil {
// filter . and ..
keep := func(x string) bool {
if x == "." || x == ".." || x == "" {
return false
}
return true
}
m := 0
for _, x := range list {
if keep(x) {
list[m] = x
m++
}
}
list = list[:m]
}
return list, 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")
}