go.mondoo.com/cnquery@v0.0.0-20231005093811-59568235f6ea/providers/os/fs/fs.go (about) 1 // Copyright (c) Mondoo, Inc. 2 // SPDX-License-Identifier: BUSL-1.1 3 4 package fs 5 6 import ( 7 "errors" 8 "os" 9 "path/filepath" 10 "regexp" 11 "time" 12 13 "github.com/spf13/afero" 14 ) 15 16 var notSupported = errors.New("not supported") 17 18 type MountedFs struct { 19 prefix string 20 } 21 22 func NewMountedFs(mountedDir string) afero.Fs { 23 return &MountedFs{ 24 prefix: mountedDir, 25 } 26 } 27 28 func (t *MountedFs) getPath(name string) string { 29 // NOTE: this uses local os filepaths, so mounting a linux system on windows will not work yet 30 return filepath.Join(t.prefix, name) 31 } 32 33 func (t *MountedFs) Name() string { return "Mounted Fs" } 34 35 func (t *MountedFs) Create(name string) (afero.File, error) { 36 mountedPath := t.getPath(name) 37 f, e := os.Create(mountedPath) 38 if f == nil { 39 // while this looks strange, we need to return a bare nil (of type nil) not 40 // a nil value of type *os.File or nil won't be nil 41 return nil, e 42 } 43 return NewMountedFile(name, f), e 44 } 45 46 func (t *MountedFs) Mkdir(name string, perm os.FileMode) error { 47 return notSupported 48 } 49 50 func (t *MountedFs) MkdirAll(path string, perm os.FileMode) error { 51 return notSupported 52 } 53 54 func (t *MountedFs) Open(name string) (afero.File, error) { 55 mountedPath := t.getPath(name) 56 f, e := os.Open(mountedPath) 57 if f == nil { 58 // while this looks strange, we need to return a bare nil (of type nil) not 59 // a nil value of type *os.File or nil won't be nil 60 return nil, e 61 } 62 return NewMountedFile(name, f), e 63 } 64 65 func (t *MountedFs) OpenFile(name string, flag int, perm os.FileMode) (afero.File, error) { 66 mountedPath := t.getPath(name) 67 f, e := os.OpenFile(mountedPath, flag, perm) 68 if f == nil { 69 // while this looks strange, we need to return a bare nil (of type nil) not 70 // a nil value of type *os.File or nil won't be nil 71 return nil, e 72 } 73 return NewMountedFile(name, f), e 74 } 75 76 func (t *MountedFs) Remove(name string) error { 77 return notSupported 78 } 79 80 func (t *MountedFs) RemoveAll(path string) error { 81 return notSupported 82 } 83 84 func (t *MountedFs) Rename(oldname, newname string) error { 85 return notSupported 86 } 87 88 func (t *MountedFs) Stat(name string) (os.FileInfo, error) { 89 mountedPath := t.getPath(name) 90 return os.Stat(mountedPath) 91 } 92 93 func (t *MountedFs) Chmod(name string, mode os.FileMode) error { 94 return notSupported 95 } 96 97 func (t *MountedFs) Chtimes(name string, atime time.Time, mtime time.Time) error { 98 return notSupported 99 } 100 101 func (t *MountedFs) LstatIfPossible(name string) (os.FileInfo, bool, error) { 102 mountedPath := t.getPath(name) 103 fi, err := os.Lstat(mountedPath) 104 return fi, true, err 105 } 106 107 func (t *MountedFs) ReadlinkIfPossible(name string) (string, error) { 108 mountedPath := t.getPath(name) 109 return os.Readlink(mountedPath) 110 } 111 112 func (t *MountedFs) Chown(name string, uid, gid int) error { 113 return notSupported 114 } 115 116 func (t *MountedFs) Find(from string, r *regexp.Regexp, typ string) ([]string, error) { 117 iofs := afero.NewIOFS(t) 118 return FindFiles(iofs, from, r, typ) 119 }