Skip to content

Commit

Permalink
filesystem: make FindMount() fall back to search by path
Browse files Browse the repository at this point in the history
This is needed to make FindMount() work on btrfs filesystems.

Update #339
  • Loading branch information
ebiggers committed Jan 27, 2022
1 parent 65a445d commit 51c421d
Showing 1 changed file with 25 additions and 5 deletions.
30 changes: 25 additions & 5 deletions filesystem/mountpoint.go
Original file line number Diff line number Diff line change
@@ -364,18 +364,38 @@ func FindMount(path string) (*Mount, error) {
if err := loadMountInfo(); err != nil {
return nil, err
}
// First try to find the mount by the number of the containing device.
deviceNumber, err := getNumberOfContainingDevice(path)
if err != nil {
return nil, err
}
mnt, ok := mountsByDevice[deviceNumber]
if !ok {
return nil, errors.Errorf("couldn't find mountpoint containing %q", path)
if ok {
if mnt == nil {
return nil, filesystemLacksMainMountError(deviceNumber)
}
return mnt, nil
}
if mnt == nil {
return nil, filesystemLacksMainMountError(deviceNumber)
// The mount couldn't be found by the number of the containing device.
// Fall back to walking up the directory hierarchy and checking for a
// mount at each directory path. This is necessary for btrfs, where
// files report a different st_dev from the /proc/self/mountinfo entry.
curPath, err := canonicalizePath(path)
if err != nil {
return nil, err
}
for {
mnt := mountsByPath[curPath]
if mnt != nil {
return mnt, nil
}
// Move to the parent directory unless we have reached the root.
parent := filepath.Dir(curPath)
if parent == curPath {
return nil, errors.Errorf("couldn't find mountpoint containing %q", path)
}
curPath = parent
}
return mnt, nil
}

// GetMount is like FindMount, except GetMount also returns an error if the path

0 comments on commit 51c421d

Please sign in to comment.