github.com/mckael/restic@v0.8.3/internal/fs/file_unix.go (about)

     1  // +build !windows
     2  
     3  package fs
     4  
     5  import (
     6  	"io/ioutil"
     7  	"os"
     8  	"syscall"
     9  )
    10  
    11  // fixpath returns an absolute path on windows, so restic can open long file
    12  // names.
    13  func fixpath(name string) string {
    14  	return name
    15  }
    16  
    17  // MkdirAll creates a directory named path, along with any necessary parents,
    18  // and returns nil, or else returns an error. The permission bits perm are used
    19  // for all directories that MkdirAll creates. If path is already a directory,
    20  // MkdirAll does nothing and returns nil.
    21  func MkdirAll(path string, perm os.FileMode) error {
    22  	return os.MkdirAll(fixpath(path), perm)
    23  }
    24  
    25  // TempFile creates a temporary file which has already been deleted (on
    26  // supported platforms)
    27  func TempFile(dir, prefix string) (f *os.File, err error) {
    28  	f, err = ioutil.TempFile(dir, prefix)
    29  	if err != nil {
    30  		return nil, err
    31  	}
    32  
    33  	if err = os.Remove(f.Name()); err != nil {
    34  		return nil, err
    35  	}
    36  
    37  	return f, nil
    38  }
    39  
    40  // isNotSuported returns true if the error is caused by an unsupported file system feature.
    41  func isNotSupported(err error) bool {
    42  	if perr, ok := err.(*os.PathError); ok && perr.Err == syscall.ENOTSUP {
    43  		return true
    44  	}
    45  	return false
    46  }
    47  
    48  // Chmod changes the mode of the named file to mode.
    49  func Chmod(name string, mode os.FileMode) error {
    50  	err := os.Chmod(fixpath(name), mode)
    51  
    52  	// ignore the error if the FS does not support setting this mode (e.g. CIFS with gvfs on Linux)
    53  	if err != nil && isNotSupported(err) {
    54  		return nil
    55  	}
    56  
    57  	return err
    58  }