github.com/IBM/fsgo@v0.0.0-20220920202152-e16fd2119d49/os.go (about)

     1  // Copyright 2022 IBM Inc. All rights reserved
     2  // Copyright © 2014 Steve Francia <spf@spf13.com>
     3  // Copyright 2013 tsuru authors. All rights reserved.
     4  //
     5  // SPDX-License-Identifier: Apache2.0
     6  package fsgo
     7  
     8  import (
     9  	"os"
    10  	"time"
    11  )
    12  
    13  var _ Lstater = (*OsFs)(nil)
    14  
    15  // OsFs is a Fs implementation that uses functions provided by the os package.
    16  //
    17  // For details in any method, check the documentation of the os package
    18  // (http://golang.org/pkg/os/).
    19  type OsFs struct{}
    20  
    21  func NewOsFs() Fs {
    22  	return &OsFs{}
    23  }
    24  
    25  func (OsFs) Name() string { return "OsFs" }
    26  
    27  func (OsFs) Create(name string) (File, error) {
    28  	f, e := os.Create(name)
    29  	if f == nil {
    30  		// while this looks strange, we need to return a bare nil (of type nil) not
    31  		// a nil value of type *os.File or nil won't be nil
    32  		return nil, e
    33  	}
    34  	return f, e
    35  }
    36  
    37  func (OsFs) Mkdir(name string, perm os.FileMode) error {
    38  	return os.Mkdir(name, perm)
    39  }
    40  
    41  func (OsFs) MkdirAll(path string, perm os.FileMode) error {
    42  	return os.MkdirAll(path, perm)
    43  }
    44  
    45  func (OsFs) Open(name string) (File, error) {
    46  	f, e := os.Open(name)
    47  	if f == nil {
    48  		// while this looks strange, we need to return a bare nil (of type nil) not
    49  		// a nil value of type *os.File or nil won't be nil
    50  		return nil, e
    51  	}
    52  	return f, e
    53  }
    54  
    55  func (OsFs) OpenFile(name string, flag int, perm os.FileMode) (File, error) {
    56  	f, e := os.OpenFile(name, flag, perm)
    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 f, e
    63  }
    64  
    65  func (OsFs) Remove(name string) error {
    66  	return os.Remove(name)
    67  }
    68  
    69  func (OsFs) RemoveAll(path string) error {
    70  	return os.RemoveAll(path)
    71  }
    72  
    73  func (OsFs) Rename(oldname, newname string) error {
    74  	return os.Rename(oldname, newname)
    75  }
    76  
    77  func (OsFs) Stat(name string) (os.FileInfo, error) {
    78  	return os.Stat(name)
    79  }
    80  
    81  func (OsFs) Chmod(name string, mode os.FileMode) error {
    82  	return os.Chmod(name, mode)
    83  }
    84  
    85  func (OsFs) Chown(name string, uid, gid int) error {
    86  	return os.Chown(name, uid, gid)
    87  }
    88  
    89  func (OsFs) Chtimes(name string, atime time.Time, mtime time.Time) error {
    90  	return os.Chtimes(name, atime, mtime)
    91  }
    92  
    93  func (OsFs) LstatIfPossible(name string) (os.FileInfo, bool, error) {
    94  	fi, err := os.Lstat(name)
    95  	return fi, true, err
    96  }
    97  
    98  func (OsFs) SymlinkIfPossible(oldname, newname string) error {
    99  	return os.Symlink(oldname, newname)
   100  }
   101  
   102  func (OsFs) ReadlinkIfPossible(name string) (string, error) {
   103  	return os.Readlink(name)
   104  }