github.com/icexin/eggos@v0.4.2-0.20220216025428-78b167e4f349/fs/chdir/chdir.go (about)

     1  package chdir
     2  
     3  import (
     4  	"errors"
     5  	"os"
     6  	"path/filepath"
     7  	"time"
     8  
     9  	"github.com/spf13/afero"
    10  )
    11  
    12  // assert that chdir.Chdirfs implements afero.Fs.
    13  var _ afero.Fs = (*Chdirfs)(nil)
    14  
    15  type Chdirfs struct {
    16  	dir     string
    17  	backend afero.Fs
    18  }
    19  
    20  func New(backend afero.Fs) *Chdirfs {
    21  	return &Chdirfs{
    22  		dir:     "/",
    23  		backend: backend,
    24  	}
    25  }
    26  
    27  func (c *Chdirfs) Chdir(dir string) error {
    28  	name := c.name(dir)
    29  	fi, err := c.backend.Stat(name)
    30  	if err != nil {
    31  		return err
    32  	}
    33  	if !fi.IsDir() {
    34  		return errors.New("not a dir")
    35  	}
    36  	c.dir = name
    37  	return nil
    38  }
    39  
    40  func (c *Chdirfs) name(name string) string {
    41  	if filepath.IsAbs(name) {
    42  		return name
    43  	}
    44  	return filepath.Join(c.dir, name)
    45  }
    46  
    47  // Create creates a file in the filesystem, returning the file and an
    48  // error, if any happens.
    49  func (c *Chdirfs) Create(name string) (afero.File, error) {
    50  	return c.backend.Create(c.name(name))
    51  }
    52  
    53  // Mkdir creates a directory in the filesystem, return an error if any
    54  // happens.
    55  func (c *Chdirfs) Mkdir(name string, perm os.FileMode) error {
    56  	return c.backend.Mkdir(c.name(name), perm)
    57  }
    58  
    59  // MkdirAll creates a directory path and all parents that does not exist
    60  // yet.
    61  func (c *Chdirfs) MkdirAll(path string, perm os.FileMode) error {
    62  	return c.backend.MkdirAll(c.name(path), perm)
    63  }
    64  
    65  // Open opens a file, returning it or an error, if any happens.
    66  func (c *Chdirfs) Open(name string) (afero.File, error) {
    67  	return c.backend.Open(c.name(name))
    68  }
    69  
    70  // OpenFile opens a file using the given flags and the given mode.
    71  func (c *Chdirfs) OpenFile(name string, flag int, perm os.FileMode) (afero.File, error) {
    72  	return c.backend.OpenFile(c.name(name), flag, perm)
    73  }
    74  
    75  // Remove removes a file identified by name, returning an error, if any
    76  // happens.
    77  func (c *Chdirfs) Remove(name string) error {
    78  	return c.backend.Remove(c.name(name))
    79  }
    80  
    81  // RemoveAll removes a directory path and any children it contains. It
    82  // does not fail if the path does not exist (return nil).
    83  func (c *Chdirfs) RemoveAll(path string) error {
    84  	return c.backend.RemoveAll(c.name(path))
    85  }
    86  
    87  // Rename renames a file.
    88  func (c *Chdirfs) Rename(oldname string, newname string) error {
    89  	return c.backend.Rename(c.name(oldname), c.name(newname))
    90  }
    91  
    92  // Stat returns a FileInfo describing the named file, or an error, if any
    93  // happens.
    94  func (c *Chdirfs) Stat(name string) (os.FileInfo, error) {
    95  	return c.backend.Stat(c.name(name))
    96  }
    97  
    98  // The name of this FileSystem
    99  func (c *Chdirfs) Name() string {
   100  	return "chdirfs"
   101  }
   102  
   103  //Chmod changes the mode of the named file to mode.
   104  func (c *Chdirfs) Chmod(name string, mode os.FileMode) error {
   105  	return c.backend.Chmod(c.name(name), mode)
   106  }
   107  
   108  // Chown changes the uid and gid of the named file.
   109  func (c *Chdirfs) Chown(name string, uid, gid int) error {
   110  	return c.backend.Chown(name, uid, gid)
   111  }
   112  
   113  //Chtimes changes the access and modification times of the named file
   114  func (c *Chdirfs) Chtimes(name string, atime time.Time, mtime time.Time) error {
   115  	return c.backend.Chtimes(c.name(name), atime, mtime)
   116  }