mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-12-14 21:50:17 +00:00
792b4dba2c
* update github.com/blevesearch/bleve v2.0.2 -> v2.0.3 * github.com/denisenkom/go-mssqldb v0.9.0 -> v0.10.0 * github.com/editorconfig/editorconfig-core-go v2.4.1 -> v2.4.2 * github.com/go-chi/cors v1.1.1 -> v1.2.0 * github.com/go-git/go-billy v5.0.0 -> v5.1.0 * github.com/go-git/go-git v5.2.0 -> v5.3.0 * github.com/go-ldap/ldap v3.2.4 -> v3.3.0 * github.com/go-redis/redis v8.6.0 -> v8.8.2 * github.com/go-sql-driver/mysql v1.5.0 -> v1.6.0 * github.com/go-swagger/go-swagger v0.26.1 -> v0.27.0 * github.com/lib/pq v1.9.0 -> v1.10.1 * github.com/mattn/go-sqlite3 v1.14.6 -> v1.14.7 * github.com/go-testfixtures/testfixtures v3.5.0 -> v3.6.0 * github.com/issue9/identicon v1.0.1 -> v1.2.0 * github.com/klauspost/compress v1.11.8 -> v1.12.1 * github.com/mgechev/revive v1.0.3 -> v1.0.6 * github.com/microcosm-cc/bluemonday v1.0.7 -> v1.0.8 * github.com/niklasfasching/go-org v1.4.0 -> v1.5.0 * github.com/olivere/elastic v7.0.22 -> v7.0.24 * github.com/pelletier/go-toml v1.8.1 -> v1.9.0 * github.com/prometheus/client_golang v1.9.0 -> v1.10.0 * github.com/xanzy/go-gitlab v0.44.0 -> v0.48.0 * github.com/yuin/goldmark v1.3.3 -> v1.3.5 * github.com/6543/go-version v1.2.4 -> v1.3.1 * do github.com/lib/pq v1.10.0 -> v1.10.1 again ...
289 lines
6.2 KiB
Go
Vendored
289 lines
6.2 KiB
Go
Vendored
// +build go1.16
|
|
|
|
package afero
|
|
|
|
import (
|
|
"io"
|
|
"io/fs"
|
|
"os"
|
|
"path"
|
|
"time"
|
|
)
|
|
|
|
// IOFS adopts afero.Fs to stdlib io/fs.FS
|
|
type IOFS struct {
|
|
Fs
|
|
}
|
|
|
|
func NewIOFS(fs Fs) IOFS {
|
|
return IOFS{Fs: fs}
|
|
}
|
|
|
|
var (
|
|
_ fs.FS = IOFS{}
|
|
_ fs.GlobFS = IOFS{}
|
|
_ fs.ReadDirFS = IOFS{}
|
|
_ fs.ReadFileFS = IOFS{}
|
|
_ fs.StatFS = IOFS{}
|
|
_ fs.SubFS = IOFS{}
|
|
)
|
|
|
|
func (iofs IOFS) Open(name string) (fs.File, error) {
|
|
const op = "open"
|
|
|
|
// by convention for fs.FS implementations we should perform this check
|
|
if !fs.ValidPath(name) {
|
|
return nil, iofs.wrapError(op, name, fs.ErrInvalid)
|
|
}
|
|
|
|
file, err := iofs.Fs.Open(name)
|
|
if err != nil {
|
|
return nil, iofs.wrapError(op, name, err)
|
|
}
|
|
|
|
// file should implement fs.ReadDirFile
|
|
if _, ok := file.(fs.ReadDirFile); !ok {
|
|
file = readDirFile{file}
|
|
}
|
|
|
|
return file, nil
|
|
}
|
|
|
|
func (iofs IOFS) Glob(pattern string) ([]string, error) {
|
|
const op = "glob"
|
|
|
|
// afero.Glob does not perform this check but it's required for implementations
|
|
if _, err := path.Match(pattern, ""); err != nil {
|
|
return nil, iofs.wrapError(op, pattern, err)
|
|
}
|
|
|
|
items, err := Glob(iofs.Fs, pattern)
|
|
if err != nil {
|
|
return nil, iofs.wrapError(op, pattern, err)
|
|
}
|
|
|
|
return items, nil
|
|
}
|
|
|
|
func (iofs IOFS) ReadDir(name string) ([]fs.DirEntry, error) {
|
|
items, err := ReadDir(iofs.Fs, name)
|
|
if err != nil {
|
|
return nil, iofs.wrapError("readdir", name, err)
|
|
}
|
|
|
|
ret := make([]fs.DirEntry, len(items))
|
|
for i := range items {
|
|
ret[i] = dirEntry{items[i]}
|
|
}
|
|
|
|
return ret, nil
|
|
}
|
|
|
|
func (iofs IOFS) ReadFile(name string) ([]byte, error) {
|
|
const op = "readfile"
|
|
|
|
if !fs.ValidPath(name) {
|
|
return nil, iofs.wrapError(op, name, fs.ErrInvalid)
|
|
}
|
|
|
|
bytes, err := ReadFile(iofs.Fs, name)
|
|
if err != nil {
|
|
return nil, iofs.wrapError(op, name, err)
|
|
}
|
|
|
|
return bytes, nil
|
|
}
|
|
|
|
func (iofs IOFS) Sub(dir string) (fs.FS, error) { return IOFS{NewBasePathFs(iofs.Fs, dir)}, nil }
|
|
|
|
func (IOFS) wrapError(op, path string, err error) error {
|
|
if _, ok := err.(*fs.PathError); ok {
|
|
return err // don't need to wrap again
|
|
}
|
|
|
|
return &fs.PathError{
|
|
Op: op,
|
|
Path: path,
|
|
Err: err,
|
|
}
|
|
}
|
|
|
|
// dirEntry provides adapter from os.FileInfo to fs.DirEntry
|
|
type dirEntry struct {
|
|
fs.FileInfo
|
|
}
|
|
|
|
var _ fs.DirEntry = dirEntry{}
|
|
|
|
func (d dirEntry) Type() fs.FileMode { return d.FileInfo.Mode().Type() }
|
|
|
|
func (d dirEntry) Info() (fs.FileInfo, error) { return d.FileInfo, nil }
|
|
|
|
// readDirFile provides adapter from afero.File to fs.ReadDirFile needed for correct Open
|
|
type readDirFile struct {
|
|
File
|
|
}
|
|
|
|
var _ fs.ReadDirFile = readDirFile{}
|
|
|
|
func (r readDirFile) ReadDir(n int) ([]fs.DirEntry, error) {
|
|
items, err := r.File.Readdir(n)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
ret := make([]fs.DirEntry, len(items))
|
|
for i := range items {
|
|
ret[i] = dirEntry{items[i]}
|
|
}
|
|
|
|
return ret, nil
|
|
}
|
|
|
|
// FromIOFS adopts io/fs.FS to use it as afero.Fs
|
|
// Note that io/fs.FS is read-only so all mutating methods will return fs.PathError with fs.ErrPermission
|
|
// To store modifications you may use afero.CopyOnWriteFs
|
|
type FromIOFS struct {
|
|
fs.FS
|
|
}
|
|
|
|
var _ Fs = FromIOFS{}
|
|
|
|
func (f FromIOFS) Create(name string) (File, error) { return nil, notImplemented("create", name) }
|
|
|
|
func (f FromIOFS) Mkdir(name string, perm os.FileMode) error { return notImplemented("mkdir", name) }
|
|
|
|
func (f FromIOFS) MkdirAll(path string, perm os.FileMode) error {
|
|
return notImplemented("mkdirall", path)
|
|
}
|
|
|
|
func (f FromIOFS) Open(name string) (File, error) {
|
|
file, err := f.FS.Open(name)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return fromIOFSFile{File: file, name: name}, nil
|
|
}
|
|
|
|
func (f FromIOFS) OpenFile(name string, flag int, perm os.FileMode) (File, error) {
|
|
return f.Open(name)
|
|
}
|
|
|
|
func (f FromIOFS) Remove(name string) error {
|
|
return notImplemented("remove", name)
|
|
}
|
|
|
|
func (f FromIOFS) RemoveAll(path string) error {
|
|
return notImplemented("removeall", path)
|
|
}
|
|
|
|
func (f FromIOFS) Rename(oldname, newname string) error {
|
|
return notImplemented("rename", oldname)
|
|
}
|
|
|
|
func (f FromIOFS) Stat(name string) (os.FileInfo, error) { return fs.Stat(f.FS, name) }
|
|
|
|
func (f FromIOFS) Name() string { return "fromiofs" }
|
|
|
|
func (f FromIOFS) Chmod(name string, mode os.FileMode) error {
|
|
return notImplemented("chmod", name)
|
|
}
|
|
|
|
func (f FromIOFS) Chown(name string, uid, gid int) error {
|
|
return notImplemented("chown", name)
|
|
}
|
|
|
|
func (f FromIOFS) Chtimes(name string, atime time.Time, mtime time.Time) error {
|
|
return notImplemented("chtimes", name)
|
|
}
|
|
|
|
type fromIOFSFile struct {
|
|
fs.File
|
|
name string
|
|
}
|
|
|
|
func (f fromIOFSFile) ReadAt(p []byte, off int64) (n int, err error) {
|
|
readerAt, ok := f.File.(io.ReaderAt)
|
|
if !ok {
|
|
return -1, notImplemented("readat", f.name)
|
|
}
|
|
|
|
return readerAt.ReadAt(p, off)
|
|
}
|
|
|
|
func (f fromIOFSFile) Seek(offset int64, whence int) (int64, error) {
|
|
seeker, ok := f.File.(io.Seeker)
|
|
if !ok {
|
|
return -1, notImplemented("seek", f.name)
|
|
}
|
|
|
|
return seeker.Seek(offset, whence)
|
|
}
|
|
|
|
func (f fromIOFSFile) Write(p []byte) (n int, err error) {
|
|
return -1, notImplemented("write", f.name)
|
|
}
|
|
|
|
func (f fromIOFSFile) WriteAt(p []byte, off int64) (n int, err error) {
|
|
return -1, notImplemented("writeat", f.name)
|
|
}
|
|
|
|
func (f fromIOFSFile) Name() string { return f.name }
|
|
|
|
func (f fromIOFSFile) Readdir(count int) ([]os.FileInfo, error) {
|
|
rdfile, ok := f.File.(fs.ReadDirFile)
|
|
if !ok {
|
|
return nil, notImplemented("readdir", f.name)
|
|
}
|
|
|
|
entries, err := rdfile.ReadDir(count)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
ret := make([]os.FileInfo, len(entries))
|
|
for i := range entries {
|
|
ret[i], err = entries[i].Info()
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
return ret, nil
|
|
}
|
|
|
|
func (f fromIOFSFile) Readdirnames(n int) ([]string, error) {
|
|
rdfile, ok := f.File.(fs.ReadDirFile)
|
|
if !ok {
|
|
return nil, notImplemented("readdir", f.name)
|
|
}
|
|
|
|
entries, err := rdfile.ReadDir(n)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
ret := make([]string, len(entries))
|
|
for i := range entries {
|
|
ret[i] = entries[i].Name()
|
|
}
|
|
|
|
return ret, nil
|
|
}
|
|
|
|
func (f fromIOFSFile) Sync() error { return nil }
|
|
|
|
func (f fromIOFSFile) Truncate(size int64) error {
|
|
return notImplemented("truncate", f.name)
|
|
}
|
|
|
|
func (f fromIOFSFile) WriteString(s string) (ret int, err error) {
|
|
return -1, notImplemented("writestring", f.name)
|
|
}
|
|
|
|
func notImplemented(op, path string) error {
|
|
return &fs.PathError{Op: op, Path: path, Err: fs.ErrPermission}
|
|
}
|