mirror of
https://github.com/restic/restic.git
synced 2025-08-12 11:47:43 +00:00
Moves files
This commit is contained in:
30
internal/fs/deviceid_unix.go
Normal file
30
internal/fs/deviceid_unix.go
Normal file
@@ -0,0 +1,30 @@
|
||||
// +build !windows
|
||||
|
||||
package fs
|
||||
|
||||
import (
|
||||
"os"
|
||||
"syscall"
|
||||
|
||||
"restic/errors"
|
||||
)
|
||||
|
||||
// DeviceID extracts the device ID from an os.FileInfo object by casting it
|
||||
// to syscall.Stat_t
|
||||
func DeviceID(fi os.FileInfo) (deviceID uint64, err error) {
|
||||
if fi == nil {
|
||||
return 0, errors.New("unable to determine device: fi is nil")
|
||||
}
|
||||
|
||||
if fi.Sys() == nil {
|
||||
return 0, errors.New("unable to determine device: fi.Sys() is nil")
|
||||
}
|
||||
|
||||
if st, ok := fi.Sys().(*syscall.Stat_t); ok {
|
||||
// st.Dev is uint32 on Darwin and uint64 on Linux. Just cast
|
||||
// everything to uint64.
|
||||
return uint64(st.Dev), nil
|
||||
}
|
||||
|
||||
return 0, errors.New("Could not cast to syscall.Stat_t")
|
||||
}
|
15
internal/fs/deviceid_windows.go
Normal file
15
internal/fs/deviceid_windows.go
Normal file
@@ -0,0 +1,15 @@
|
||||
// +build windows
|
||||
|
||||
package fs
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"restic/errors"
|
||||
)
|
||||
|
||||
// DeviceID extracts the device ID from an os.FileInfo object by casting it
|
||||
// to syscall.Stat_t
|
||||
func DeviceID(fi os.FileInfo) (deviceID uint64, err error) {
|
||||
return 0, errors.New("Device IDs are not supported on Windows")
|
||||
}
|
3
internal/fs/doc.go
Normal file
3
internal/fs/doc.go
Normal file
@@ -0,0 +1,3 @@
|
||||
// Package fs implements an OS independent abstraction of a file system
|
||||
// suitable for backup purposes.
|
||||
package fs
|
122
internal/fs/file.go
Normal file
122
internal/fs/file.go
Normal file
@@ -0,0 +1,122 @@
|
||||
package fs
|
||||
|
||||
import (
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
// File is an open file on a file system.
|
||||
type File interface {
|
||||
io.Reader
|
||||
io.Writer
|
||||
io.Closer
|
||||
|
||||
Fd() uintptr
|
||||
Readdirnames(n int) ([]string, error)
|
||||
Readdir(int) ([]os.FileInfo, error)
|
||||
Seek(int64, int) (int64, error)
|
||||
Stat() (os.FileInfo, error)
|
||||
}
|
||||
|
||||
// Mkdir creates a new directory with the specified name and permission bits.
|
||||
// If there is an error, it will be of type *PathError.
|
||||
func Mkdir(name string, perm os.FileMode) error {
|
||||
return os.Mkdir(fixpath(name), perm)
|
||||
}
|
||||
|
||||
// Readlink returns the destination of the named symbolic link.
|
||||
// If there is an error, it will be of type *PathError.
|
||||
func Readlink(name string) (string, error) {
|
||||
return os.Readlink(fixpath(name))
|
||||
}
|
||||
|
||||
// Remove removes the named file or directory.
|
||||
// If there is an error, it will be of type *PathError.
|
||||
func Remove(name string) error {
|
||||
return os.Remove(fixpath(name))
|
||||
}
|
||||
|
||||
// RemoveAll removes path and any children it contains.
|
||||
// It removes everything it can but returns the first error
|
||||
// it encounters. If the path does not exist, RemoveAll
|
||||
// returns nil (no error).
|
||||
func RemoveAll(path string) error {
|
||||
return os.RemoveAll(fixpath(path))
|
||||
}
|
||||
|
||||
// Rename renames (moves) oldpath to newpath.
|
||||
// If newpath already exists, Rename replaces it.
|
||||
// OS-specific restrictions may apply when oldpath and newpath are in different directories.
|
||||
// If there is an error, it will be of type *LinkError.
|
||||
func Rename(oldpath, newpath string) error {
|
||||
return os.Rename(fixpath(oldpath), fixpath(newpath))
|
||||
}
|
||||
|
||||
// Symlink creates newname as a symbolic link to oldname.
|
||||
// If there is an error, it will be of type *LinkError.
|
||||
func Symlink(oldname, newname string) error {
|
||||
return os.Symlink(fixpath(oldname), fixpath(newname))
|
||||
}
|
||||
|
||||
// Link creates newname as a hard link to oldname.
|
||||
// If there is an error, it will be of type *LinkError.
|
||||
func Link(oldname, newname string) error {
|
||||
return os.Link(fixpath(oldname), fixpath(newname))
|
||||
}
|
||||
|
||||
// Stat returns a FileInfo structure describing the named file.
|
||||
// If there is an error, it will be of type *PathError.
|
||||
func Stat(name string) (os.FileInfo, error) {
|
||||
return os.Stat(fixpath(name))
|
||||
}
|
||||
|
||||
// Lstat returns the FileInfo structure describing the named file.
|
||||
// If the file is a symbolic link, the returned FileInfo
|
||||
// describes the symbolic link. Lstat makes no attempt to follow the link.
|
||||
// If there is an error, it will be of type *PathError.
|
||||
func Lstat(name string) (os.FileInfo, error) {
|
||||
return os.Lstat(fixpath(name))
|
||||
}
|
||||
|
||||
// Create creates the named file with mode 0666 (before umask), truncating
|
||||
// it if it already exists. If successful, methods on the returned
|
||||
// File can be used for I/O; the associated file descriptor has mode
|
||||
// O_RDWR.
|
||||
// If there is an error, it will be of type *PathError.
|
||||
func Create(name string) (*os.File, error) {
|
||||
return os.Create(fixpath(name))
|
||||
}
|
||||
|
||||
// Open opens a file for reading.
|
||||
func Open(name string) (File, error) {
|
||||
return os.Open(fixpath(name))
|
||||
}
|
||||
|
||||
// OpenFile is the generalized open call; most users will use Open
|
||||
// or Create instead. It opens the named file with specified flag
|
||||
// (O_RDONLY etc.) and perm, (0666 etc.) if applicable. If successful,
|
||||
// methods on the returned File can be used for I/O.
|
||||
// If there is an error, it will be of type *PathError.
|
||||
func OpenFile(name string, flag int, perm os.FileMode) (*os.File, error) {
|
||||
return os.OpenFile(fixpath(name), flag, perm)
|
||||
}
|
||||
|
||||
// Walk walks the file tree rooted at root, calling walkFn for each file or
|
||||
// directory in the tree, including root. All errors that arise visiting files
|
||||
// and directories are filtered by walkFn. The files are walked in lexical
|
||||
// order, which makes the output deterministic but means that for very
|
||||
// large directories Walk can be inefficient.
|
||||
// Walk does not follow symbolic links.
|
||||
func Walk(root string, walkFn filepath.WalkFunc) error {
|
||||
return filepath.Walk(fixpath(root), walkFn)
|
||||
}
|
||||
|
||||
// RemoveIfExists removes a file, returning no error if it does not exist.
|
||||
func RemoveIfExists(filename string) error {
|
||||
err := os.Remove(filename)
|
||||
if err != nil && os.IsNotExist(err) {
|
||||
err = nil
|
||||
}
|
||||
return err
|
||||
}
|
58
internal/fs/file_unix.go
Normal file
58
internal/fs/file_unix.go
Normal file
@@ -0,0 +1,58 @@
|
||||
// +build !windows
|
||||
|
||||
package fs
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
// fixpath returns an absolute path on windows, so restic can open long file
|
||||
// names.
|
||||
func fixpath(name string) string {
|
||||
return name
|
||||
}
|
||||
|
||||
// MkdirAll creates a directory named path, along with any necessary parents,
|
||||
// and returns nil, or else returns an error. The permission bits perm are used
|
||||
// for all directories that MkdirAll creates. If path is already a directory,
|
||||
// MkdirAll does nothing and returns nil.
|
||||
func MkdirAll(path string, perm os.FileMode) error {
|
||||
return os.MkdirAll(fixpath(path), perm)
|
||||
}
|
||||
|
||||
// TempFile creates a temporary file which has already been deleted (on
|
||||
// supported platforms)
|
||||
func TempFile(dir, prefix string) (f *os.File, err error) {
|
||||
f, err = ioutil.TempFile(dir, prefix)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err = os.Remove(f.Name()); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return f, nil
|
||||
}
|
||||
|
||||
// isNotSuported returns true if the error is caused by an unsupported file system feature.
|
||||
func isNotSupported(err error) bool {
|
||||
if perr, ok := err.(*os.PathError); ok && perr.Err == syscall.ENOTSUP {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// Chmod changes the mode of the named file to mode.
|
||||
func Chmod(name string, mode os.FileMode) error {
|
||||
err := os.Chmod(fixpath(name), mode)
|
||||
|
||||
// ignore the error if the FS does not support setting this mode (e.g. CIFS with gvfs on Linux)
|
||||
if err != nil && isNotSupported(err) {
|
||||
return nil
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
98
internal/fs/file_windows.go
Normal file
98
internal/fs/file_windows.go
Normal file
@@ -0,0 +1,98 @@
|
||||
package fs
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
// fixpath returns an absolute path on windows, so restic can open long file
|
||||
// names.
|
||||
func fixpath(name string) string {
|
||||
abspath, err := filepath.Abs(name)
|
||||
if err == nil {
|
||||
// Check if \\?\UNC\ already exist
|
||||
if strings.HasPrefix(abspath, `\\?\UNC\`) {
|
||||
return abspath
|
||||
}
|
||||
// Check if \\?\ already exist
|
||||
if strings.HasPrefix(abspath, `\\?\`) {
|
||||
return abspath
|
||||
}
|
||||
// Check if path starts with \\
|
||||
if strings.HasPrefix(abspath, `\\`) {
|
||||
return strings.Replace(abspath, `\\`, `\\?\UNC\`, 1)
|
||||
}
|
||||
// Normal path
|
||||
return `\\?\` + abspath
|
||||
}
|
||||
return name
|
||||
}
|
||||
|
||||
// MkdirAll creates a directory named path, along with any necessary parents,
|
||||
// and returns nil, or else returns an error. The permission bits perm are used
|
||||
// for all directories that MkdirAll creates. If path is already a directory,
|
||||
// MkdirAll does nothing and returns nil.
|
||||
//
|
||||
// Adapted from the stdlib MkdirAll, added test for volume name.
|
||||
func MkdirAll(path string, perm os.FileMode) error {
|
||||
// Fast path: if we can tell whether path is a directory or file, stop with success or error.
|
||||
dir, err := os.Stat(path)
|
||||
if err == nil {
|
||||
if dir.IsDir() {
|
||||
return nil
|
||||
}
|
||||
return &os.PathError{
|
||||
Op: "mkdir",
|
||||
Path: path,
|
||||
Err: syscall.ENOTDIR,
|
||||
}
|
||||
}
|
||||
|
||||
// Slow path: make sure parent exists and then call Mkdir for path.
|
||||
i := len(path)
|
||||
for i > 0 && os.IsPathSeparator(path[i-1]) { // Skip trailing path separator.
|
||||
i--
|
||||
}
|
||||
|
||||
j := i
|
||||
for j > 0 && !os.IsPathSeparator(path[j-1]) { // Scan backward over element.
|
||||
j--
|
||||
}
|
||||
|
||||
if j > 1 {
|
||||
// Create parent
|
||||
parent := path[0 : j-1]
|
||||
if parent != filepath.VolumeName(parent) {
|
||||
err = MkdirAll(parent, perm)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Parent now exists; invoke Mkdir and use its result.
|
||||
err = os.Mkdir(path, perm)
|
||||
if err != nil {
|
||||
// Handle arguments like "foo/." by
|
||||
// double-checking that directory doesn't exist.
|
||||
dir, err1 := os.Lstat(path)
|
||||
if err1 == nil && dir.IsDir() {
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// TempFile creates a temporary file.
|
||||
func TempFile(dir, prefix string) (f *os.File, err error) {
|
||||
return ioutil.TempFile(dir, prefix)
|
||||
}
|
||||
|
||||
// Chmod changes the mode of the named file to mode.
|
||||
func Chmod(name string, mode os.FileMode) error {
|
||||
return os.Chmod(fixpath(name), mode)
|
||||
}
|
Reference in New Issue
Block a user