2019-11-07 19:05:39 +00:00
|
|
|
package fscommon
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
2023-03-25 21:33:54 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/backup/backupnames"
|
2019-11-07 19:05:39 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
|
|
|
|
)
|
|
|
|
|
2023-09-18 14:10:16 +00:00
|
|
|
// AppendFiles appends paths to all the files from local dir to dst.
|
2019-11-07 19:05:39 +00:00
|
|
|
//
|
2023-09-18 14:10:16 +00:00
|
|
|
// All the appended filepaths will have dir prefix.
|
|
|
|
// The returned paths have local OS-specific directory separators.
|
2019-11-07 19:05:39 +00:00
|
|
|
func AppendFiles(dst []string, dir string) ([]string, error) {
|
|
|
|
d, err := os.Open(dir)
|
|
|
|
if err != nil {
|
2022-12-04 05:55:06 +00:00
|
|
|
return nil, fmt.Errorf("cannot open directory: %w", err)
|
2019-11-07 19:05:39 +00:00
|
|
|
}
|
|
|
|
dst, err = appendFilesInternal(dst, d)
|
|
|
|
if err1 := d.Close(); err1 != nil {
|
|
|
|
err = err1
|
|
|
|
}
|
|
|
|
return dst, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func appendFilesInternal(dst []string, d *os.File) ([]string, error) {
|
|
|
|
dir := d.Name()
|
|
|
|
dfi, err := d.Stat()
|
|
|
|
if err != nil {
|
2020-06-30 19:58:18 +00:00
|
|
|
return nil, fmt.Errorf("cannot stat %q: %w", dir, err)
|
2019-11-07 19:05:39 +00:00
|
|
|
}
|
|
|
|
if !dfi.IsDir() {
|
|
|
|
return nil, fmt.Errorf("%q isn't a directory", dir)
|
|
|
|
}
|
|
|
|
fis, err := d.Readdir(-1)
|
|
|
|
if err != nil {
|
2020-06-30 19:58:18 +00:00
|
|
|
return nil, fmt.Errorf("cannot read directory contents in %q: %w", dir, err)
|
2019-11-07 19:05:39 +00:00
|
|
|
}
|
|
|
|
for _, fi := range fis {
|
|
|
|
name := fi.Name()
|
|
|
|
if name == "." || name == ".." {
|
|
|
|
continue
|
|
|
|
}
|
2021-12-22 11:10:15 +00:00
|
|
|
if isSpecialFile(name) {
|
|
|
|
// Do not take into account special files.
|
2019-11-19 16:35:31 +00:00
|
|
|
continue
|
|
|
|
}
|
2023-05-03 08:48:53 +00:00
|
|
|
path := filepath.Join(dir, name)
|
2019-11-07 19:05:39 +00:00
|
|
|
if fi.IsDir() {
|
|
|
|
// Process directory
|
|
|
|
dst, err = AppendFiles(dst, path)
|
|
|
|
if err != nil {
|
2023-08-01 07:18:04 +00:00
|
|
|
return nil, fmt.Errorf("cannot append files %q: %w", path, err)
|
2019-11-07 19:05:39 +00:00
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if fi.Mode()&os.ModeSymlink != os.ModeSymlink {
|
|
|
|
// Process file
|
|
|
|
dst = append(dst, path)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
pathOrig := path
|
|
|
|
again:
|
|
|
|
// Process symlink
|
|
|
|
pathReal, err := filepath.EvalSymlinks(pathOrig)
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) || strings.Contains(err.Error(), "no such file or directory") {
|
|
|
|
// Skip symlink that points to nowhere.
|
|
|
|
continue
|
|
|
|
}
|
2020-06-30 19:58:18 +00:00
|
|
|
return nil, fmt.Errorf("cannot resolve symlink %q: %w", pathOrig, err)
|
2019-11-07 19:05:39 +00:00
|
|
|
}
|
|
|
|
sfi, err := os.Stat(pathReal)
|
|
|
|
if err != nil {
|
2020-06-30 19:58:18 +00:00
|
|
|
return nil, fmt.Errorf("cannot stat %q from symlink %q: %w", pathReal, path, err)
|
2019-11-07 19:05:39 +00:00
|
|
|
}
|
|
|
|
if sfi.IsDir() {
|
|
|
|
// Symlink points to directory
|
|
|
|
dstNew, err := AppendFiles(dst, pathReal)
|
|
|
|
if err != nil {
|
2020-06-30 19:58:18 +00:00
|
|
|
return nil, fmt.Errorf("cannot list files at %q from symlink %q: %w", pathReal, path, err)
|
2019-11-07 19:05:39 +00:00
|
|
|
}
|
2023-05-03 08:48:53 +00:00
|
|
|
pathReal += string(filepath.Separator)
|
2019-11-07 19:05:39 +00:00
|
|
|
for i := len(dst); i < len(dstNew); i++ {
|
|
|
|
x := dstNew[i]
|
|
|
|
if !strings.HasPrefix(x, pathReal) {
|
|
|
|
return nil, fmt.Errorf("unexpected prefix for path %q; want %q", x, pathReal)
|
|
|
|
}
|
2023-05-03 08:48:53 +00:00
|
|
|
dstNew[i] = filepath.Join(path, x[len(pathReal):])
|
2019-11-07 19:05:39 +00:00
|
|
|
}
|
|
|
|
dst = dstNew
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if sfi.Mode()&os.ModeSymlink != os.ModeSymlink {
|
|
|
|
// Symlink points to file
|
|
|
|
dst = append(dst, path)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Symlink points to symlink. Process it again.
|
|
|
|
pathOrig = pathReal
|
|
|
|
goto again
|
|
|
|
}
|
|
|
|
return dst, nil
|
|
|
|
}
|
|
|
|
|
2021-12-22 11:10:15 +00:00
|
|
|
func isSpecialFile(name string) bool {
|
2023-06-26 12:44:02 +00:00
|
|
|
return name == "flock.lock" || name == backupnames.RestoreInProgressFilename || name == backupnames.RestoreMarkFileName
|
2021-12-22 11:10:15 +00:00
|
|
|
}
|
|
|
|
|
2019-11-07 19:05:39 +00:00
|
|
|
// RemoveEmptyDirs recursively removes empty directories under the given dir.
|
|
|
|
func RemoveEmptyDirs(dir string) error {
|
|
|
|
_, err := removeEmptyDirs(dir)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func removeEmptyDirs(dir string) (bool, error) {
|
|
|
|
d, err := os.Open(dir)
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
ok, err := removeEmptyDirsInternal(d)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return ok, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func removeEmptyDirsInternal(d *os.File) (bool, error) {
|
|
|
|
dir := d.Name()
|
|
|
|
dfi, err := d.Stat()
|
|
|
|
if err != nil {
|
2020-06-30 19:58:18 +00:00
|
|
|
return false, fmt.Errorf("cannot stat %q: %w", dir, err)
|
2019-11-07 19:05:39 +00:00
|
|
|
}
|
|
|
|
if !dfi.IsDir() {
|
|
|
|
return false, fmt.Errorf("%q isn't a directory", dir)
|
|
|
|
}
|
|
|
|
fis, err := d.Readdir(-1)
|
|
|
|
if err != nil {
|
2020-06-30 19:58:18 +00:00
|
|
|
return false, fmt.Errorf("cannot read directory contents in %q: %w", dir, err)
|
2019-11-07 19:05:39 +00:00
|
|
|
}
|
|
|
|
dirEntries := 0
|
|
|
|
for _, fi := range fis {
|
|
|
|
name := fi.Name()
|
|
|
|
if name == "." || name == ".." {
|
|
|
|
continue
|
|
|
|
}
|
2023-05-03 08:48:53 +00:00
|
|
|
path := filepath.Join(dir, name)
|
2019-11-07 19:05:39 +00:00
|
|
|
if fi.IsDir() {
|
|
|
|
// Process directory
|
|
|
|
ok, err := removeEmptyDirs(path)
|
|
|
|
if err != nil {
|
2023-08-01 07:18:04 +00:00
|
|
|
return false, fmt.Errorf("cannot remove empty dirs %q: %w", path, err)
|
2019-11-07 19:05:39 +00:00
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
dirEntries++
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if fi.Mode()&os.ModeSymlink != os.ModeSymlink {
|
2023-06-26 12:44:02 +00:00
|
|
|
// isSpecialFile is not suitable for this function, because the root directory must be considered not empty
|
|
|
|
// i.e. function must consider the markers of the restore in progress as files that are not allowed to be removed by this function.
|
|
|
|
if name == "flock.lock" {
|
2019-11-28 11:36:02 +00:00
|
|
|
continue
|
|
|
|
}
|
2019-11-07 19:05:39 +00:00
|
|
|
dirEntries++
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
pathOrig := path
|
|
|
|
again:
|
|
|
|
// Process symlink
|
|
|
|
pathReal, err := filepath.EvalSymlinks(pathOrig)
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) || strings.Contains(err.Error(), "no such file or directory") {
|
|
|
|
// Remove symlink that points to nowere.
|
|
|
|
logger.Infof("removing broken symlink %q", pathOrig)
|
|
|
|
if err := os.Remove(pathOrig); err != nil {
|
2020-06-30 19:58:18 +00:00
|
|
|
return false, fmt.Errorf("cannot remove %q: %w", pathOrig, err)
|
2019-11-07 19:05:39 +00:00
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
2020-06-30 19:58:18 +00:00
|
|
|
return false, fmt.Errorf("cannot resolve symlink %q: %w", pathOrig, err)
|
2019-11-07 19:05:39 +00:00
|
|
|
}
|
|
|
|
sfi, err := os.Stat(pathReal)
|
|
|
|
if err != nil {
|
2020-06-30 19:58:18 +00:00
|
|
|
return false, fmt.Errorf("cannot stat %q from symlink %q: %w", pathReal, path, err)
|
2019-11-07 19:05:39 +00:00
|
|
|
}
|
|
|
|
if sfi.IsDir() {
|
|
|
|
// Symlink points to directory
|
|
|
|
ok, err := removeEmptyDirs(pathReal)
|
|
|
|
if err != nil {
|
2020-06-30 19:58:18 +00:00
|
|
|
return false, fmt.Errorf("cannot list files at %q from symlink %q: %w", pathReal, path, err)
|
2019-11-07 19:05:39 +00:00
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
dirEntries++
|
|
|
|
} else {
|
|
|
|
// Remove the symlink
|
|
|
|
logger.Infof("removing symlink that points to empty dir %q", pathOrig)
|
|
|
|
if err := os.Remove(pathOrig); err != nil {
|
2020-06-30 19:58:18 +00:00
|
|
|
return false, fmt.Errorf("cannot remove %q: %w", pathOrig, err)
|
2019-11-07 19:05:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if sfi.Mode()&os.ModeSymlink != os.ModeSymlink {
|
|
|
|
// Symlink points to file. Skip it.
|
|
|
|
dirEntries++
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Symlink points to symlink. Process it again.
|
|
|
|
pathOrig = pathReal
|
|
|
|
goto again
|
|
|
|
}
|
|
|
|
if dirEntries > 0 {
|
|
|
|
return false, nil
|
|
|
|
}
|
2023-08-01 07:18:04 +00:00
|
|
|
if err := d.Close(); err != nil {
|
|
|
|
return false, fmt.Errorf("cannot close %q: %w", dir, err)
|
|
|
|
}
|
2023-03-25 21:33:54 +00:00
|
|
|
// Use os.RemoveAll() instead of os.Remove(), since the dir may contain special files such as flock.lock and backupnames.RestoreInProgressFilename,
|
2023-02-13 12:27:13 +00:00
|
|
|
// which must be ignored.
|
2021-12-22 11:10:15 +00:00
|
|
|
if err := os.RemoveAll(dir); err != nil {
|
2020-06-30 19:58:18 +00:00
|
|
|
return false, fmt.Errorf("cannot remove %q: %w", dir, err)
|
2019-11-07 19:05:39 +00:00
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
2020-01-09 13:24:26 +00:00
|
|
|
|
|
|
|
// IgnorePath returns true if the given path must be ignored.
|
|
|
|
func IgnorePath(path string) bool {
|
|
|
|
return strings.HasSuffix(path, ".ignore")
|
|
|
|
}
|