For IsNFS check all parent directories up to the root until one exist

Signed-off-by: Jan Dubois <jan.dubois@suse.com>
This commit is contained in:
Jan Dubois committed 2025-08-17 20:44:01 -07:00
1 parent ba5409441b
commit 58fc7a8b87
2 files changed
+18 -6

No files matched your search

-6
View File
@@ -131,12 +131,6 @@ func newApp() *cobra.Command {
if err != nil {
return err
}
// Make sure that directory is on a local filesystem, not on NFS
// if the directory does not yet exist, check the home directory
_, err = os.Stat(dir)
if errors.Is(err, os.ErrNotExist) {
dir = filepath.Dir(dir)
}
nfs, err := fsutil.IsNFS(dir)
if err != nil {
return err
+18
View File
@@ -4,10 +4,28 @@
package fsutil
import (
"errors"
"os"
"path/filepath"
"golang.org/x/sys/unix"
)
// IsNFS checks if the path is on NFS. If the path does not exist yet, it will walk
// up parent directories until one exists, or it hits '/' or '.'.
// Any other stat errors will cause IsNFS to fail.
func IsNFS(path string) (bool, error) {
for len(path) > 1 {
_, err := os.Stat(path)
if err == nil {
break
}
if !errors.Is(err, os.ErrNotExist) {
return false, err
}
path = filepath.Dir(path)
}
var sf unix.Statfs_t
if err := unix.Statfs(path, &sf); err != nil {
return false, err