2022-11-21 02:22:26 +08:00
|
|
|
package system
|
2014-10-31 04:41:24 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2022-05-01 22:12:39 +08:00
|
|
|
"path"
|
|
|
|
"regexp"
|
2014-10-31 04:41:24 +08:00
|
|
|
)
|
|
|
|
|
2022-11-05 14:42:28 +08:00
|
|
|
// nativeFilesystem represents the native file system implementation
|
|
|
|
type nativeFilesystem struct{}
|
2014-10-31 04:41:24 +08:00
|
|
|
|
2022-11-05 14:42:28 +08:00
|
|
|
// openFile calls os.OpenFile().
|
|
|
|
func (fs *nativeFilesystem) openFile(name string, flag int, perm os.FileMode) (file File, err error) {
|
2014-10-31 04:41:24 +08:00
|
|
|
return os.OpenFile(name, flag, perm)
|
|
|
|
}
|
|
|
|
|
2022-11-05 14:42:28 +08:00
|
|
|
// stat calls os.Stat()
|
|
|
|
func (fs *nativeFilesystem) stat(name string) (os.FileInfo, error) {
|
2017-03-14 07:11:06 +08:00
|
|
|
return os.Stat(name)
|
|
|
|
}
|
|
|
|
|
2022-11-05 14:42:28 +08:00
|
|
|
// find returns all items (files or folders) below the given directory matching the given pattern.
|
|
|
|
func (fs *nativeFilesystem) find(baseDir string, pattern string) ([]string, error) {
|
2022-05-01 22:12:39 +08:00
|
|
|
reg, err := regexp.Compile(pattern)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
items, err := os.ReadDir(baseDir)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var found []string
|
|
|
|
for _, item := range items {
|
|
|
|
if reg.MatchString(item.Name()) {
|
|
|
|
found = append(found, path.Join(baseDir, item.Name()))
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return found, nil
|
|
|
|
}
|
|
|
|
|
2022-11-05 14:42:28 +08:00
|
|
|
// readFile reads the named file and returns the contents. A successful call returns err == nil, not err == EOF.
|
|
|
|
// Because ReadFile reads the whole file, it does not treat an EOF from Read as an error to be reported.
|
|
|
|
func (fs *nativeFilesystem) readFile(name string) ([]byte, error) {
|
|
|
|
return os.ReadFile(name)
|
2022-05-01 22:12:39 +08:00
|
|
|
}
|