Skip to content

Commit a199b58

Browse files
committed
support for io/fs
1 parent d557f16 commit a199b58

File tree

386 files changed

+65650
-32472
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

386 files changed

+65650
-32472
lines changed

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,13 @@ for example a `cwdfs.WorkingDirectoryFileSystem` or a
7777
The package `osfs` supports creating a temporary os filesystem based
7878
virtual filesystem residing in a temporary operating system directory.
7979

80-
Additional the interface `VFS` includes the standard filesystem operations
80+
Additionally, the interface `VFS` includes the standard filesystem operations
8181
and some implementation independent utility functions based on a virtual
8282
filesystem known from the `os`, `ìoutil` and `filepath` packages.
8383
The function `vfs.New(fs)` can be used to create such a wrapper for
84-
any virtual filesystem.
84+
any virtual filesystem.
85+
86+
A virtual filesystem can be used as `io/fs.FS` or `io/fs.ReadDirFS`.
87+
Because of the Go typesystem and the stripped interface `io/fs.File`,
88+
this is not directly possible. But any virtual filesystem can be converted
89+
by a type converting wrapper function `vfs.AsIoFS(fs)`.

go.sum

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ github.com/onsi/gomega v1.10.1 h1:o0+MgICZLuZ7xjH7Vx6zS/zcu93/BEp1VwkIW1mEXCE=
2626
github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo=
2727
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
2828
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
29-
golang.org/x/net v0.0.0-20200520004742-59133d7f0dd7 h1:AeiKBIuRw3UomYXSbLy0Mc2dDLfdtbT/IVn4keq83P0=
3029
golang.org/x/net v0.0.0-20200520004742-59133d7f0dd7/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
3130
golang.org/x/net v0.11.0 h1:Gi2tvZIJyBtO9SDr1q9h5hEQCp/4L2RQ+ar0qjx2oNU=
3231
golang.org/x/net v0.11.0/go.mod h1:2L/ixqYpgIVXmeoSA/4Lu7BzTG4KIyPIryS4IsOd1oQ=
@@ -37,17 +36,14 @@ golang.org/x/sys v0.0.0-20190904154756-749cb33beabd/go.mod h1:h1NjWce9XRLGQEsW7w
3736
golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
3837
golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
3938
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
40-
golang.org/x/sys v0.0.0-20200519105757-fe76b779f299 h1:DYfZAGf2WMFjMxbgTjaC+2HC7NkNAQs+6Q8b9WEB/F4=
4139
golang.org/x/sys v0.0.0-20200519105757-fe76b779f299/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
4240
golang.org/x/sys v0.9.0 h1:KS/R3tvhPqvJvwcKfnBHJwwthS11LRhmM5D59eEXa0s=
4341
golang.org/x/sys v0.9.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
4442
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
45-
golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
4643
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
4744
golang.org/x/text v0.10.0 h1:UpjohKhiEgNc0CSauXmwYftY1+LlaC75SJwh0SgCX58=
4845
golang.org/x/text v0.10.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
4946
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
50-
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
5147
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
5248
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 h1:H2TDz8ibqkAF6YGhCdN3jS9O0/s90v0rJh3X/OLHEUk=
5349
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2/go.mod h1:K8+ghG5WaK9qNqU5K3HdILfMLy1f3aNYFI/wnl100a8=

pkg/utils/file.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"bytes"
2323
"errors"
2424
"io"
25+
"io/fs"
2526
"os"
2627
"sort"
2728
"time"
@@ -86,6 +87,19 @@ func (f *File) Sync() error {
8687
return nil
8788
}
8889

90+
func (f *File) ReadDir(count int) (files []fs.DirEntry, err error) {
91+
o, err := f.Readdir(count)
92+
93+
if err != nil {
94+
return nil, err
95+
}
96+
r := make([]fs.DirEntry, len(o), len(o))
97+
for i, v := range o {
98+
r[i] = fs.FileInfoToDirEntry(v)
99+
}
100+
return r, nil
101+
}
102+
89103
func (f *File) Readdir(count int) (files []os.FileInfo, err error) {
90104
if !f.fileData.IsDir() {
91105
return nil, &os.PathError{Op: "readdir", Path: f.name, Err: ErrNotDir}

pkg/vfs/interface.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ package vfs
2020

2121
import (
2222
"io"
23+
"io/fs"
2324
"os"
2425
"time"
2526
)
@@ -29,6 +30,7 @@ const PathSeparatorString = "/"
2930

3031
type FileMode = os.FileMode
3132
type FileInfo = os.FileInfo
33+
type DirEntry = fs.DirEntry
3234

3335
const ModePerm = os.ModePerm
3436

@@ -77,17 +79,17 @@ type FileSystem interface {
7779

7880
// Mkdir creates a directory in the filesystem, return an error if any
7981
// happens.
80-
Mkdir(name string, perm os.FileMode) error
82+
Mkdir(name string, perm FileMode) error
8183

8284
// MkdirAll creates a directory path and all parents that does not exist
8385
// yet.
84-
MkdirAll(path string, perm os.FileMode) error
86+
MkdirAll(path string, perm FileMode) error
8587

8688
// Open opens a file, returning it or an error, if any happens.
8789
Open(name string) (File, error)
8890

8991
// OpenFile opens a file using the given flags and the given mode.
90-
OpenFile(name string, flags int, perm os.FileMode) (File, error)
92+
OpenFile(name string, flags int, perm FileMode) (File, error)
9193

9294
// Remove removes a file identified by name, returning an error, if any
9395
// happens.
@@ -102,13 +104,13 @@ type FileSystem interface {
102104

103105
// Stat returns a FileInfo describing the named file, or an error, if any
104106
// happens.
105-
Stat(name string) (os.FileInfo, error)
107+
Stat(name string) (FileInfo, error)
106108

107109
// Lstat returns a FileInfo describing the named file, or an error, if any
108110
// happens.
109111
// If the file is a symbolic link, the returned FileInfo
110112
// describes the symbolic link. Lstat makes no attempt to follow the link.
111-
Lstat(name string) (os.FileInfo, error)
113+
Lstat(name string) (FileInfo, error)
112114

113115
// Create a symlink if supported
114116
Symlink(oldname, newname string) error
@@ -120,7 +122,7 @@ type FileSystem interface {
120122
Name() string
121123

122124
// Chmod changes the mode of the named file to mode.
123-
Chmod(name string, mode os.FileMode) error
125+
Chmod(name string, mode FileMode) error
124126

125127
// Chtimes changes the access and modification times of the named file
126128
Chtimes(name string, atime time.Time, mtime time.Time) error
@@ -152,9 +154,10 @@ type File interface {
152154
io.WriterAt
153155

154156
Name() string
155-
Readdir(count int) ([]os.FileInfo, error)
157+
ReadDir(count int) ([]DirEntry, error)
158+
Readdir(count int) ([]FileInfo, error)
156159
Readdirnames(n int) ([]string, error)
157-
Stat() (os.FileInfo, error)
160+
Stat() (FileInfo, error)
158161
Sync() error
159162
Truncate(size int64) error
160163
WriteString(s string) (ret int, err error)
@@ -189,9 +192,9 @@ type VFS interface {
189192
IsDir(path string) (bool, error)
190193
IsFile(path string) (bool, error)
191194

192-
ReadDir(path string) ([]os.FileInfo, error)
195+
ReadDir(path string) ([]FileInfo, error)
193196
ReadFile(path string) ([]byte, error)
194-
WriteFile(path string, data []byte, mode os.FileMode) error
197+
WriteFile(path string, data []byte, mode FileMode) error
195198
TempFile(dir, prefix string) (File, error)
196199
TempDir(dir, prefix string) (string, error)
197200
}

pkg/vfs/iofs.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2023 Mandelsoft. All rights reserved.
3+
* This file is licensed under the Apache Software License, v. 2 except as noted
4+
* otherwise in the LICENSE file
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
package vfs
20+
21+
import (
22+
"io/fs"
23+
"sort"
24+
)
25+
26+
type iofs struct {
27+
FileSystem
28+
}
29+
30+
var (
31+
_ fs.File = File(nil)
32+
_ fs.ReadDirFile = File(nil)
33+
)
34+
35+
func (i *iofs) Open(name string) (fs.File, error) {
36+
return i.FileSystem.Open(name)
37+
}
38+
39+
func (i *iofs) ReadDir(name string) ([]fs.DirEntry, error) {
40+
f, err := i.FileSystem.Open(name)
41+
if err != nil {
42+
return nil, err
43+
}
44+
defer f.Close()
45+
dirs, err := f.ReadDir(-1)
46+
sort.Slice(dirs, func(i, j int) bool { return dirs[i].Name() < dirs[j].Name() })
47+
return dirs, err
48+
}
49+
50+
// AsIoFS maps a virtual filesystem
51+
func AsIoFS(fs FileSystem) fs.ReadDirFS {
52+
return &iofs{fs}
53+
}

vendor/golang.org/x/net/AUTHORS

Lines changed: 0 additions & 3 deletions
This file was deleted.

vendor/golang.org/x/net/CONTRIBUTORS

Lines changed: 0 additions & 3 deletions
This file was deleted.

vendor/golang.org/x/net/html/const.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/golang.org/x/net/html/doc.go

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/golang.org/x/net/html/escape.go

Lines changed: 81 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)