2021-04-18 15:41:13 -04:00
|
|
|
// Copyright (c) 2015-2021 MinIO, Inc.
|
|
|
|
//
|
|
|
|
// This file is part of MinIO Object Storage stack
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2017-01-16 20:05:00 -05:00
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2018-04-05 18:04:40 -04:00
|
|
|
"context"
|
2017-01-16 20:05:00 -05:00
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
pathutil "path"
|
2017-04-14 15:06:24 -04:00
|
|
|
"runtime"
|
2021-02-26 12:52:27 -05:00
|
|
|
"strings"
|
2017-11-25 14:58:29 -05:00
|
|
|
|
2018-04-05 18:04:40 -04:00
|
|
|
"github.com/minio/minio/cmd/logger"
|
2018-01-31 16:17:24 -05:00
|
|
|
"github.com/minio/minio/pkg/lock"
|
2017-01-16 20:05:00 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// Removes only the file at given path does not remove
|
|
|
|
// any parent directories, handles long paths for
|
|
|
|
// windows automatically.
|
2018-04-05 18:04:40 -04:00
|
|
|
func fsRemoveFile(ctx context.Context, filePath string) (err error) {
|
2017-01-16 20:05:00 -05:00
|
|
|
if filePath == "" {
|
2018-04-05 18:04:40 -04:00
|
|
|
logger.LogIf(ctx, errInvalidArgument)
|
|
|
|
return errInvalidArgument
|
2017-01-16 20:05:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if err = checkPathLength(filePath); err != nil {
|
2018-04-05 18:04:40 -04:00
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
return err
|
2017-01-16 20:05:00 -05:00
|
|
|
}
|
|
|
|
|
2020-08-31 15:35:40 -04:00
|
|
|
if err = os.Remove(filePath); err != nil {
|
|
|
|
if err = osErrToFileErr(err); err != errFileNotFound {
|
2018-04-05 18:04:40 -04:00
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
}
|
2017-01-16 20:05:00 -05:00
|
|
|
}
|
|
|
|
|
2018-05-02 22:59:38 -04:00
|
|
|
return err
|
2017-01-16 20:05:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Removes all files and folders at a given path, handles
|
|
|
|
// long paths for windows automatically.
|
2018-04-05 18:04:40 -04:00
|
|
|
func fsRemoveAll(ctx context.Context, dirPath string) (err error) {
|
2017-01-16 20:05:00 -05:00
|
|
|
if dirPath == "" {
|
2018-04-05 18:04:40 -04:00
|
|
|
logger.LogIf(ctx, errInvalidArgument)
|
|
|
|
return errInvalidArgument
|
2017-01-16 20:05:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if err = checkPathLength(dirPath); err != nil {
|
2018-04-05 18:04:40 -04:00
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
return err
|
2017-01-16 20:05:00 -05:00
|
|
|
}
|
|
|
|
|
2018-08-06 00:15:28 -04:00
|
|
|
if err = removeAll(dirPath); err != nil {
|
2020-11-23 11:36:49 -05:00
|
|
|
if osIsPermission(err) {
|
2018-04-05 18:04:40 -04:00
|
|
|
logger.LogIf(ctx, errVolumeAccessDenied)
|
|
|
|
return errVolumeAccessDenied
|
2017-08-12 22:24:20 -04:00
|
|
|
} else if isSysErrNotEmpty(err) {
|
2018-04-05 18:04:40 -04:00
|
|
|
logger.LogIf(ctx, errVolumeNotEmpty)
|
|
|
|
return errVolumeNotEmpty
|
2017-01-16 20:05:00 -05:00
|
|
|
}
|
2018-04-05 18:04:40 -04:00
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
return err
|
2017-01-16 20:05:00 -05:00
|
|
|
}
|
|
|
|
|
2017-01-26 18:40:10 -05:00
|
|
|
return nil
|
2017-01-16 20:05:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Removes a directory only if its empty, handles long
|
|
|
|
// paths for windows automatically.
|
2018-04-05 18:04:40 -04:00
|
|
|
func fsRemoveDir(ctx context.Context, dirPath string) (err error) {
|
2017-01-16 20:05:00 -05:00
|
|
|
if dirPath == "" {
|
2018-04-05 18:04:40 -04:00
|
|
|
logger.LogIf(ctx, errInvalidArgument)
|
|
|
|
return errInvalidArgument
|
2017-01-16 20:05:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if err = checkPathLength(dirPath); err != nil {
|
2018-04-05 18:04:40 -04:00
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
return err
|
2017-01-16 20:05:00 -05:00
|
|
|
}
|
|
|
|
|
2017-08-12 22:25:43 -04:00
|
|
|
if err = os.Remove((dirPath)); err != nil {
|
2020-11-23 11:36:49 -05:00
|
|
|
if osIsNotExist(err) {
|
2018-04-05 18:04:40 -04:00
|
|
|
return errVolumeNotFound
|
2017-01-16 20:05:00 -05:00
|
|
|
} else if isSysErrNotEmpty(err) {
|
2018-04-05 18:04:40 -04:00
|
|
|
return errVolumeNotEmpty
|
2017-01-16 20:05:00 -05:00
|
|
|
}
|
2018-04-05 18:04:40 -04:00
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
return err
|
2017-01-16 20:05:00 -05:00
|
|
|
}
|
|
|
|
|
2017-01-26 18:40:10 -05:00
|
|
|
return nil
|
2017-01-16 20:05:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Creates a new directory, parent dir should exist
|
|
|
|
// otherwise returns an error. If directory already
|
|
|
|
// exists returns an error. Windows long paths
|
|
|
|
// are handled automatically.
|
2018-04-05 18:04:40 -04:00
|
|
|
func fsMkdir(ctx context.Context, dirPath string) (err error) {
|
2017-01-16 20:05:00 -05:00
|
|
|
if dirPath == "" {
|
2018-04-05 18:04:40 -04:00
|
|
|
logger.LogIf(ctx, errInvalidArgument)
|
|
|
|
return errInvalidArgument
|
2017-01-16 20:05:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if err = checkPathLength(dirPath); err != nil {
|
2018-04-05 18:04:40 -04:00
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
return err
|
2017-01-16 20:05:00 -05:00
|
|
|
}
|
|
|
|
|
2017-08-12 22:25:43 -04:00
|
|
|
if err = os.Mkdir((dirPath), 0777); err != nil {
|
2018-08-06 13:26:40 -04:00
|
|
|
switch {
|
2020-11-23 11:36:49 -05:00
|
|
|
case osIsExist(err):
|
2018-04-05 18:04:40 -04:00
|
|
|
return errVolumeExists
|
2020-11-23 11:36:49 -05:00
|
|
|
case osIsPermission(err):
|
2018-04-05 18:04:40 -04:00
|
|
|
logger.LogIf(ctx, errDiskAccessDenied)
|
|
|
|
return errDiskAccessDenied
|
2018-08-06 13:26:40 -04:00
|
|
|
case isSysErrNotDir(err):
|
2017-01-16 20:05:00 -05:00
|
|
|
// File path cannot be verified since
|
|
|
|
// one of the parents is a file.
|
2018-04-05 18:04:40 -04:00
|
|
|
logger.LogIf(ctx, errDiskAccessDenied)
|
|
|
|
return errDiskAccessDenied
|
2018-08-06 13:26:40 -04:00
|
|
|
case isSysErrPathNotFound(err):
|
2017-01-16 20:05:00 -05:00
|
|
|
// Add specific case for windows.
|
2018-04-05 18:04:40 -04:00
|
|
|
logger.LogIf(ctx, errDiskAccessDenied)
|
|
|
|
return errDiskAccessDenied
|
2018-08-06 13:26:40 -04:00
|
|
|
default:
|
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
return err
|
2017-01-16 20:05:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-10-16 20:20:54 -04:00
|
|
|
// fsStat is a low level call which validates input arguments
|
|
|
|
// and checks input length upto supported maximum. Does
|
|
|
|
// not perform any higher layer interpretation of files v/s
|
|
|
|
// directories. For higher level interpretation look at
|
|
|
|
// fsStatFileDir, fsStatFile, fsStatDir.
|
2018-04-05 18:04:40 -04:00
|
|
|
func fsStat(ctx context.Context, statLoc string) (os.FileInfo, error) {
|
2017-06-06 15:15:35 -04:00
|
|
|
if statLoc == "" {
|
2018-04-05 18:04:40 -04:00
|
|
|
logger.LogIf(ctx, errInvalidArgument)
|
|
|
|
return nil, errInvalidArgument
|
2017-01-16 20:05:00 -05:00
|
|
|
}
|
2017-06-06 15:15:35 -04:00
|
|
|
if err := checkPathLength(statLoc); err != nil {
|
2018-04-05 18:04:40 -04:00
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
return nil, err
|
2017-01-16 20:05:00 -05:00
|
|
|
}
|
2018-05-02 22:59:38 -04:00
|
|
|
fi, err := os.Stat(statLoc)
|
2017-06-06 15:15:35 -04:00
|
|
|
if err != nil {
|
2018-04-05 18:04:40 -04:00
|
|
|
return nil, err
|
2017-06-06 15:15:35 -04:00
|
|
|
}
|
2017-06-12 20:40:28 -04:00
|
|
|
|
2017-06-06 15:15:35 -04:00
|
|
|
return fi, nil
|
|
|
|
}
|
2017-01-16 20:05:00 -05:00
|
|
|
|
2017-10-16 20:20:54 -04:00
|
|
|
// Lookup if volume exists, returns volume attributes upon success.
|
2018-04-05 18:04:40 -04:00
|
|
|
func fsStatVolume(ctx context.Context, volume string) (os.FileInfo, error) {
|
|
|
|
fi, err := fsStat(ctx, volume)
|
2017-01-16 20:05:00 -05:00
|
|
|
if err != nil {
|
2020-11-23 11:36:49 -05:00
|
|
|
if osIsNotExist(err) {
|
2018-04-05 18:04:40 -04:00
|
|
|
return nil, errVolumeNotFound
|
2020-11-23 11:36:49 -05:00
|
|
|
} else if osIsPermission(err) {
|
2018-04-05 18:04:40 -04:00
|
|
|
return nil, errVolumeAccessDenied
|
2017-01-16 20:05:00 -05:00
|
|
|
}
|
2018-04-05 18:04:40 -04:00
|
|
|
return nil, err
|
2017-01-16 20:05:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if !fi.IsDir() {
|
2018-04-05 18:04:40 -04:00
|
|
|
return nil, errVolumeAccessDenied
|
2017-01-16 20:05:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return fi, nil
|
|
|
|
}
|
|
|
|
|
2017-10-16 20:20:54 -04:00
|
|
|
// Lookup if directory exists, returns directory attributes upon success.
|
2018-04-05 18:04:40 -04:00
|
|
|
func fsStatDir(ctx context.Context, statDir string) (os.FileInfo, error) {
|
|
|
|
fi, err := fsStat(ctx, statDir)
|
2017-10-16 20:20:54 -04:00
|
|
|
if err != nil {
|
2020-06-12 23:04:01 -04:00
|
|
|
err = osErrToFileErr(err)
|
2018-05-02 22:59:38 -04:00
|
|
|
if err != errFileNotFound {
|
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
}
|
|
|
|
return nil, err
|
2017-10-16 20:20:54 -04:00
|
|
|
}
|
|
|
|
if !fi.IsDir() {
|
2018-06-25 21:24:00 -04:00
|
|
|
return nil, errFileNotFound
|
2017-10-16 20:20:54 -04:00
|
|
|
}
|
|
|
|
return fi, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lookup if file exists, returns file attributes upon success.
|
2018-04-05 18:04:40 -04:00
|
|
|
func fsStatFile(ctx context.Context, statFile string) (os.FileInfo, error) {
|
|
|
|
fi, err := fsStat(ctx, statFile)
|
2017-01-16 20:05:00 -05:00
|
|
|
if err != nil {
|
2020-06-12 23:04:01 -04:00
|
|
|
err = osErrToFileErr(err)
|
2018-05-02 22:59:38 -04:00
|
|
|
if err != errFileNotFound {
|
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
}
|
|
|
|
return nil, err
|
2017-01-16 20:05:00 -05:00
|
|
|
}
|
|
|
|
if fi.IsDir() {
|
2018-06-25 21:24:00 -04:00
|
|
|
return nil, errFileNotFound
|
2017-01-16 20:05:00 -05:00
|
|
|
}
|
|
|
|
return fi, nil
|
|
|
|
}
|
|
|
|
|
2018-05-08 22:09:15 -04:00
|
|
|
// Returns if the filePath is a regular file.
|
|
|
|
func fsIsFile(ctx context.Context, filePath string) bool {
|
|
|
|
fi, err := fsStat(ctx, filePath)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return fi.Mode().IsRegular()
|
|
|
|
}
|
|
|
|
|
2017-01-16 20:05:00 -05:00
|
|
|
// Opens the file at given path, optionally from an offset. Upon success returns
|
|
|
|
// a readable stream and the size of the readable stream.
|
2018-04-05 18:04:40 -04:00
|
|
|
func fsOpenFile(ctx context.Context, readPath string, offset int64) (io.ReadCloser, int64, error) {
|
2017-01-16 20:05:00 -05:00
|
|
|
if readPath == "" || offset < 0 {
|
2018-04-05 18:04:40 -04:00
|
|
|
logger.LogIf(ctx, errInvalidArgument)
|
|
|
|
return nil, 0, errInvalidArgument
|
2017-01-16 20:05:00 -05:00
|
|
|
}
|
|
|
|
if err := checkPathLength(readPath); err != nil {
|
2018-04-05 18:04:40 -04:00
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
return nil, 0, err
|
2017-01-16 20:05:00 -05:00
|
|
|
}
|
|
|
|
|
2018-05-02 22:59:38 -04:00
|
|
|
fr, err := os.Open(readPath)
|
2017-01-16 20:05:00 -05:00
|
|
|
if err != nil {
|
2020-06-12 23:04:01 -04:00
|
|
|
return nil, 0, osErrToFileErr(err)
|
2017-01-16 20:05:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Stat to get the size of the file at path.
|
2019-07-23 01:36:15 -04:00
|
|
|
st, err := fr.Stat()
|
2017-01-16 20:05:00 -05:00
|
|
|
if err != nil {
|
2020-06-12 23:04:01 -04:00
|
|
|
err = osErrToFileErr(err)
|
2018-05-02 22:59:38 -04:00
|
|
|
if err != errFileNotFound {
|
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
}
|
2018-04-05 18:04:40 -04:00
|
|
|
return nil, 0, err
|
2017-01-16 20:05:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Verify if its not a regular file, since subsequent Seek is undefined.
|
|
|
|
if !st.Mode().IsRegular() {
|
2018-04-05 18:04:40 -04:00
|
|
|
return nil, 0, errIsNotRegular
|
2017-01-16 20:05:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Seek to the requested offset.
|
|
|
|
if offset > 0 {
|
2019-02-13 07:59:36 -05:00
|
|
|
_, err = fr.Seek(offset, io.SeekStart)
|
2017-01-16 20:05:00 -05:00
|
|
|
if err != nil {
|
2018-04-05 18:04:40 -04:00
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
return nil, 0, err
|
2017-01-16 20:05:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Success.
|
|
|
|
return fr, st.Size(), nil
|
|
|
|
}
|
|
|
|
|
fix: use buffers only when necessary for io.Copy() (#11229)
Use separate sync.Pool for writes/reads
Avoid passing buffers for io.CopyBuffer()
if the writer or reader implement io.WriteTo or io.ReadFrom
respectively then its useless for sync.Pool to allocate
buffers on its own since that will be completely ignored
by the io.CopyBuffer Go implementation.
Improve this wherever we see this to be optimal.
This allows us to be more efficient on memory usage.
```
385 // copyBuffer is the actual implementation of Copy and CopyBuffer.
386 // if buf is nil, one is allocated.
387 func copyBuffer(dst Writer, src Reader, buf []byte) (written int64, err error) {
388 // If the reader has a WriteTo method, use it to do the copy.
389 // Avoids an allocation and a copy.
390 if wt, ok := src.(WriterTo); ok {
391 return wt.WriteTo(dst)
392 }
393 // Similarly, if the writer has a ReadFrom method, use it to do the copy.
394 if rt, ok := dst.(ReaderFrom); ok {
395 return rt.ReadFrom(src)
396 }
```
From readahead package
```
// WriteTo writes data to w until there's no more data to write or when an error occurs.
// The return value n is the number of bytes written.
// Any error encountered during the write is also returned.
func (a *reader) WriteTo(w io.Writer) (n int64, err error) {
if a.err != nil {
return 0, a.err
}
n = 0
for {
err = a.fill()
if err != nil {
return n, err
}
n2, err := w.Write(a.cur.buffer())
a.cur.inc(n2)
n += int64(n2)
if err != nil {
return n, err
}
```
2021-01-06 12:36:55 -05:00
|
|
|
// Creates a file and copies data from incoming reader.
|
|
|
|
func fsCreateFile(ctx context.Context, filePath string, reader io.Reader, fallocSize int64) (int64, error) {
|
2017-05-05 11:49:09 -04:00
|
|
|
if filePath == "" || reader == nil {
|
2018-04-05 18:04:40 -04:00
|
|
|
logger.LogIf(ctx, errInvalidArgument)
|
|
|
|
return 0, errInvalidArgument
|
2017-01-16 20:05:00 -05:00
|
|
|
}
|
|
|
|
|
2017-03-07 15:25:40 -05:00
|
|
|
if err := checkPathLength(filePath); err != nil {
|
2018-04-05 18:04:40 -04:00
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
return 0, err
|
2017-01-16 20:05:00 -05:00
|
|
|
}
|
|
|
|
|
2018-01-13 12:13:02 -05:00
|
|
|
if err := mkdirAll(pathutil.Dir(filePath), 0777); err != nil {
|
2020-08-04 15:09:41 -04:00
|
|
|
switch {
|
2020-11-23 11:36:49 -05:00
|
|
|
case osIsPermission(err):
|
2020-08-04 15:09:41 -04:00
|
|
|
return 0, errFileAccessDenied
|
2020-11-23 11:36:49 -05:00
|
|
|
case osIsExist(err):
|
2020-08-04 15:09:41 -04:00
|
|
|
return 0, errFileAccessDenied
|
|
|
|
case isSysErrIO(err):
|
|
|
|
return 0, errFaultyDisk
|
|
|
|
case isSysErrInvalidArg(err):
|
|
|
|
return 0, errUnsupportedDisk
|
|
|
|
case isSysErrNoSpace(err):
|
|
|
|
return 0, errDiskFull
|
|
|
|
}
|
2018-04-05 18:04:40 -04:00
|
|
|
return 0, err
|
2017-03-07 15:25:40 -05:00
|
|
|
}
|
|
|
|
|
2020-05-12 22:24:59 -04:00
|
|
|
flags := os.O_CREATE | os.O_WRONLY
|
|
|
|
if globalFSOSync {
|
|
|
|
flags = flags | os.O_SYNC
|
|
|
|
}
|
|
|
|
writer, err := lock.Open(filePath, flags, 0666)
|
2017-01-16 20:05:00 -05:00
|
|
|
if err != nil {
|
2020-06-12 23:04:01 -04:00
|
|
|
return 0, osErrToFileErr(err)
|
2017-01-16 20:05:00 -05:00
|
|
|
}
|
|
|
|
defer writer.Close()
|
|
|
|
|
fix: use buffers only when necessary for io.Copy() (#11229)
Use separate sync.Pool for writes/reads
Avoid passing buffers for io.CopyBuffer()
if the writer or reader implement io.WriteTo or io.ReadFrom
respectively then its useless for sync.Pool to allocate
buffers on its own since that will be completely ignored
by the io.CopyBuffer Go implementation.
Improve this wherever we see this to be optimal.
This allows us to be more efficient on memory usage.
```
385 // copyBuffer is the actual implementation of Copy and CopyBuffer.
386 // if buf is nil, one is allocated.
387 func copyBuffer(dst Writer, src Reader, buf []byte) (written int64, err error) {
388 // If the reader has a WriteTo method, use it to do the copy.
389 // Avoids an allocation and a copy.
390 if wt, ok := src.(WriterTo); ok {
391 return wt.WriteTo(dst)
392 }
393 // Similarly, if the writer has a ReadFrom method, use it to do the copy.
394 if rt, ok := dst.(ReaderFrom); ok {
395 return rt.ReadFrom(src)
396 }
```
From readahead package
```
// WriteTo writes data to w until there's no more data to write or when an error occurs.
// The return value n is the number of bytes written.
// Any error encountered during the write is also returned.
func (a *reader) WriteTo(w io.Writer) (n int64, err error) {
if a.err != nil {
return 0, a.err
}
n = 0
for {
err = a.fill()
if err != nil {
return n, err
}
n2, err := w.Write(a.cur.buffer())
a.cur.inc(n2)
n += int64(n2)
if err != nil {
return n, err
}
```
2021-01-06 12:36:55 -05:00
|
|
|
bytesWritten, err := io.Copy(writer, reader)
|
|
|
|
if err != nil {
|
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
return 0, err
|
2017-01-16 20:05:00 -05:00
|
|
|
}
|
2018-01-31 16:17:24 -05:00
|
|
|
|
2017-01-16 20:05:00 -05:00
|
|
|
return bytesWritten, nil
|
|
|
|
}
|
|
|
|
|
2019-07-23 01:36:15 -04:00
|
|
|
// Renames source path to destination path, fails if the destination path
|
|
|
|
// parents are not already created.
|
|
|
|
func fsSimpleRenameFile(ctx context.Context, sourcePath, destPath string) error {
|
|
|
|
if err := checkPathLength(sourcePath); err != nil {
|
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := checkPathLength(destPath); err != nil {
|
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.Rename(sourcePath, destPath); err != nil {
|
|
|
|
logger.LogIf(ctx, err)
|
2020-06-12 23:04:01 -04:00
|
|
|
return osErrToFileErr(err)
|
2019-07-23 01:36:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-26 12:52:27 -05:00
|
|
|
func deleteFile(basePath, deletePath string, recursive bool) error {
|
|
|
|
if basePath == "" || deletePath == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
isObjectDir := HasSuffix(deletePath, SlashSeparator)
|
|
|
|
basePath = pathutil.Clean(basePath)
|
|
|
|
deletePath = pathutil.Clean(deletePath)
|
|
|
|
if !strings.HasPrefix(deletePath, basePath) || deletePath == basePath {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
if recursive {
|
|
|
|
os.RemoveAll(deletePath)
|
|
|
|
} else {
|
|
|
|
err = os.Remove(deletePath)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
switch {
|
|
|
|
case isSysErrNotEmpty(err):
|
|
|
|
// if object is a directory, but if its not empty
|
|
|
|
// return FileNotFound to indicate its an empty prefix.
|
|
|
|
if isObjectDir {
|
|
|
|
return errFileNotFound
|
|
|
|
}
|
|
|
|
// Ignore errors if the directory is not empty. The server relies on
|
|
|
|
// this functionality, and sometimes uses recursion that should not
|
|
|
|
// error on parent directories.
|
|
|
|
return nil
|
|
|
|
case osIsNotExist(err):
|
|
|
|
return errFileNotFound
|
|
|
|
case osIsPermission(err):
|
|
|
|
return errFileAccessDenied
|
|
|
|
case isSysErrIO(err):
|
|
|
|
return errFaultyDisk
|
|
|
|
default:
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
deletePath = pathutil.Dir(deletePath)
|
|
|
|
|
|
|
|
// Delete parent directory obviously not recursively. Errors for
|
|
|
|
// parent directories shouldn't trickle down.
|
|
|
|
deleteFile(basePath, deletePath, false)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-08-03 23:04:28 -04:00
|
|
|
// fsDeleteFile is a wrapper for deleteFile(), after checking the path length.
|
2018-04-05 18:04:40 -04:00
|
|
|
func fsDeleteFile(ctx context.Context, basePath, deletePath string) error {
|
2017-01-16 20:05:00 -05:00
|
|
|
if err := checkPathLength(basePath); err != nil {
|
2018-04-05 18:04:40 -04:00
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
return err
|
2017-01-16 20:05:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := checkPathLength(deletePath); err != nil {
|
2018-04-05 18:04:40 -04:00
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
return err
|
2017-01-16 20:05:00 -05:00
|
|
|
}
|
|
|
|
|
2020-03-11 11:56:36 -04:00
|
|
|
if err := deleteFile(basePath, deletePath, false); err != nil {
|
2018-05-08 22:09:15 -04:00
|
|
|
if err != errFileNotFound {
|
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
}
|
2018-04-05 18:04:40 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2017-01-16 20:05:00 -05:00
|
|
|
}
|
2017-04-14 15:06:24 -04:00
|
|
|
|
|
|
|
// fsRemoveMeta safely removes a locked file and takes care of Windows special case
|
2018-04-05 18:04:40 -04:00
|
|
|
func fsRemoveMeta(ctx context.Context, basePath, deletePath, tmpDir string) error {
|
2017-04-14 15:06:24 -04:00
|
|
|
// Special case for windows please read through.
|
|
|
|
if runtime.GOOS == globalWindowsOSName {
|
|
|
|
// Ordinarily windows does not permit deletion or renaming of files still
|
|
|
|
// in use, but if all open handles to that file were opened with FILE_SHARE_DELETE
|
|
|
|
// then it can permit renames and deletions of open files.
|
|
|
|
//
|
|
|
|
// There are however some gotchas with this, and it is worth listing them here.
|
|
|
|
// Firstly, Windows never allows you to really delete an open file, rather it is
|
|
|
|
// flagged as delete pending and its entry in its directory remains visible
|
|
|
|
// (though no new file handles may be opened to it) and when the very last
|
|
|
|
// open handle to the file in the system is closed, only then is it truly
|
|
|
|
// deleted. Well, actually only sort of truly deleted, because Windows only
|
|
|
|
// appears to remove the file entry from the directory, but in fact that
|
|
|
|
// entry is merely hidden and actually still exists and attempting to create
|
|
|
|
// a file with the same name will return an access denied error. How long it
|
|
|
|
// silently exists for depends on a range of factors, but put it this way:
|
|
|
|
// if your code loops creating and deleting the same file name as you might
|
|
|
|
// when operating a lock file, you're going to see lots of random spurious
|
|
|
|
// access denied errors and truly dismal lock file performance compared to POSIX.
|
|
|
|
//
|
|
|
|
// We work-around these un-POSIX file semantics by taking a dual step to
|
|
|
|
// deleting files. Firstly, it renames the file to tmp location into multipartTmpBucket
|
|
|
|
// We always open files with FILE_SHARE_DELETE permission enabled, with that
|
|
|
|
// flag Windows permits renaming and deletion, and because the name was changed
|
|
|
|
// to a very random name somewhere not in its origin directory before deletion,
|
|
|
|
// you don't see those unexpected random errors when creating files with the
|
|
|
|
// same name as a recently deleted file as you do anywhere else on Windows.
|
|
|
|
// Because the file is probably not in its original containing directory any more,
|
|
|
|
// deletions of that directory will not fail with "directory not empty" as they
|
|
|
|
// otherwise normally would either.
|
|
|
|
|
|
|
|
tmpPath := pathJoin(tmpDir, mustGetUUID())
|
|
|
|
|
2018-04-05 18:04:40 -04:00
|
|
|
fsRenameFile(ctx, deletePath, tmpPath)
|
2017-04-14 15:06:24 -04:00
|
|
|
|
|
|
|
// Proceed to deleting the directory if empty
|
2018-04-05 18:04:40 -04:00
|
|
|
fsDeleteFile(ctx, basePath, pathutil.Dir(deletePath))
|
2017-04-14 15:06:24 -04:00
|
|
|
|
|
|
|
// Finally delete the renamed file.
|
2018-04-05 18:04:40 -04:00
|
|
|
return fsDeleteFile(ctx, tmpDir, tmpPath)
|
2017-04-14 15:06:24 -04:00
|
|
|
}
|
2018-04-05 18:04:40 -04:00
|
|
|
return fsDeleteFile(ctx, basePath, deletePath)
|
2017-04-14 15:06:24 -04:00
|
|
|
}
|