mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-08-07 22:02:56 +03:00
Bump go-git
This commit is contained in:
190
vendor/github.com/go-git/go-billy/v5/osfs/os.go
generated
vendored
190
vendor/github.com/go-git/go-billy/v5/osfs/os.go
generated
vendored
@@ -1,135 +1,123 @@
|
||||
//go:build !js
|
||||
// +build !js
|
||||
|
||||
// Package osfs provides a billy filesystem for the OS.
|
||||
package osfs // import "github.com/go-git/go-billy/v5/osfs"
|
||||
package osfs
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
|
||||
"github.com/go-git/go-billy/v5"
|
||||
"github.com/go-git/go-billy/v5/helper/chroot"
|
||||
)
|
||||
|
||||
const (
|
||||
defaultDirectoryMode = 0755
|
||||
defaultCreateMode = 0666
|
||||
defaultDirectoryMode = 0o755
|
||||
defaultCreateMode = 0o666
|
||||
)
|
||||
|
||||
// OS is a filesystem based on the os filesystem.
|
||||
type OS struct{}
|
||||
// Default Filesystem representing the root of the os filesystem.
|
||||
var Default = &ChrootOS{}
|
||||
|
||||
// New returns a new OS filesystem.
|
||||
func New(baseDir string) billy.Filesystem {
|
||||
return chroot.New(&OS{}, baseDir)
|
||||
// By default paths are deduplicated, but still enforced
|
||||
// under baseDir. For more info refer to WithDeduplicatePath.
|
||||
func New(baseDir string, opts ...Option) billy.Filesystem {
|
||||
o := &options{
|
||||
deduplicatePath: true,
|
||||
}
|
||||
for _, opt := range opts {
|
||||
opt(o)
|
||||
}
|
||||
|
||||
if o.Type == BoundOSFS {
|
||||
return newBoundOS(baseDir, o.deduplicatePath)
|
||||
}
|
||||
|
||||
return newChrootOS(baseDir)
|
||||
}
|
||||
|
||||
func (fs *OS) Create(filename string) (billy.File, error) {
|
||||
return fs.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, defaultCreateMode)
|
||||
// WithBoundOS returns the option of using a Bound filesystem OS.
|
||||
func WithBoundOS() Option {
|
||||
return func(o *options) {
|
||||
o.Type = BoundOSFS
|
||||
}
|
||||
}
|
||||
|
||||
func (fs *OS) OpenFile(filename string, flag int, perm os.FileMode) (billy.File, error) {
|
||||
if flag&os.O_CREATE != 0 {
|
||||
if err := fs.createDir(filename); err != nil {
|
||||
// WithChrootOS returns the option of using a Chroot filesystem OS.
|
||||
func WithChrootOS() Option {
|
||||
return func(o *options) {
|
||||
o.Type = ChrootOSFS
|
||||
}
|
||||
}
|
||||
|
||||
// WithDeduplicatePath toggles the deduplication of the base dir in the path.
|
||||
// This occurs when absolute links are being used.
|
||||
// Assuming base dir /base/dir and an absolute symlink /base/dir/target:
|
||||
//
|
||||
// With DeduplicatePath (default): /base/dir/target
|
||||
// Without DeduplicatePath: /base/dir/base/dir/target
|
||||
//
|
||||
// This option is only used by the BoundOS OS type.
|
||||
func WithDeduplicatePath(enabled bool) Option {
|
||||
return func(o *options) {
|
||||
o.deduplicatePath = enabled
|
||||
}
|
||||
}
|
||||
|
||||
type options struct {
|
||||
Type
|
||||
deduplicatePath bool
|
||||
}
|
||||
|
||||
type Type int
|
||||
|
||||
const (
|
||||
ChrootOSFS Type = iota
|
||||
BoundOSFS
|
||||
)
|
||||
|
||||
func readDir(dir string) ([]os.FileInfo, error) {
|
||||
entries, err := os.ReadDir(dir)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
infos := make([]fs.FileInfo, 0, len(entries))
|
||||
for _, entry := range entries {
|
||||
fi, err := entry.Info()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
infos = append(infos, fi)
|
||||
}
|
||||
|
||||
f, err := os.OpenFile(filename, flag, perm)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &file{File: f}, err
|
||||
return infos, nil
|
||||
}
|
||||
|
||||
func (fs *OS) createDir(fullpath string) error {
|
||||
dir := filepath.Dir(fullpath)
|
||||
if dir != "." {
|
||||
if err := os.MkdirAll(dir, defaultDirectoryMode); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (fs *OS) ReadDir(path string) ([]os.FileInfo, error) {
|
||||
l, err := ioutil.ReadDir(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var s = make([]os.FileInfo, len(l))
|
||||
for i, f := range l {
|
||||
s[i] = f
|
||||
}
|
||||
|
||||
return s, nil
|
||||
}
|
||||
|
||||
func (fs *OS) Rename(from, to string) error {
|
||||
if err := fs.createDir(to); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return rename(from, to)
|
||||
}
|
||||
|
||||
func (fs *OS) MkdirAll(path string, perm os.FileMode) error {
|
||||
return os.MkdirAll(path, defaultDirectoryMode)
|
||||
}
|
||||
|
||||
func (fs *OS) Open(filename string) (billy.File, error) {
|
||||
return fs.OpenFile(filename, os.O_RDONLY, 0)
|
||||
}
|
||||
|
||||
func (fs *OS) Stat(filename string) (os.FileInfo, error) {
|
||||
return os.Stat(filename)
|
||||
}
|
||||
|
||||
func (fs *OS) Remove(filename string) error {
|
||||
return os.Remove(filename)
|
||||
}
|
||||
|
||||
func (fs *OS) TempFile(dir, prefix string) (billy.File, error) {
|
||||
if err := fs.createDir(dir + string(os.PathSeparator)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
f, err := ioutil.TempFile(dir, prefix)
|
||||
func tempFile(dir, prefix string) (billy.File, error) {
|
||||
f, err := os.CreateTemp(dir, prefix)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &file{File: f}, nil
|
||||
}
|
||||
|
||||
func (fs *OS) Join(elem ...string) string {
|
||||
return filepath.Join(elem...)
|
||||
}
|
||||
|
||||
func (fs *OS) RemoveAll(path string) error {
|
||||
return os.RemoveAll(filepath.Clean(path))
|
||||
}
|
||||
|
||||
func (fs *OS) Lstat(filename string) (os.FileInfo, error) {
|
||||
return os.Lstat(filepath.Clean(filename))
|
||||
}
|
||||
|
||||
func (fs *OS) Symlink(target, link string) error {
|
||||
if err := fs.createDir(link); err != nil {
|
||||
return err
|
||||
func openFile(fn string, flag int, perm os.FileMode, createDir func(string) error) (billy.File, error) {
|
||||
if flag&os.O_CREATE != 0 {
|
||||
if createDir == nil {
|
||||
return nil, fmt.Errorf("createDir func cannot be nil if file needs to be opened in create mode")
|
||||
}
|
||||
if err := createDir(fn); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return os.Symlink(target, link)
|
||||
}
|
||||
|
||||
func (fs *OS) Readlink(link string) (string, error) {
|
||||
return os.Readlink(link)
|
||||
}
|
||||
|
||||
// Capabilities implements the Capable interface.
|
||||
func (fs *OS) Capabilities() billy.Capability {
|
||||
return billy.DefaultCapabilities
|
||||
f, err := os.OpenFile(fn, flag, perm)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &file{File: f}, err
|
||||
}
|
||||
|
||||
// file is a wrapper for an os.File which adds support for file locking.
|
||||
|
Reference in New Issue
Block a user