-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Since [1], Mount() and Unmount() calls have their own structures. Update our code accordingly. [1] docker/go-plugins-helpers@461a12724a063 Signed-off-by: Kir Kolyshkin <[email protected]>
- Loading branch information
Showing
2 changed files
with
66 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"os" | ||
"strings" | ||
"syscall" | ||
) | ||
|
||
// Given a file or directory, finds which filesystem it is on, | ||
// by parsing /proc/self/mountinfo and comparing dev_t | ||
// of the file to that in the mountinfo field. | ||
func GetFilesystemType(path string) (string, error) { | ||
var st syscall.Stat_t | ||
|
||
err := syscall.Stat(path, &st) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return getFSTypeByDev(st.Dev) | ||
} | ||
|
||
// convert minor:major string from /proc/self/mountinfo into dev_t | ||
func parseDev(s string) uint64 { | ||
var major uint32 | ||
var minor uint32 | ||
|
||
n, _ := fmt.Sscanf(s, "%d:%d", &major, &minor) | ||
|
||
if n != 2 { | ||
return 0 | ||
} | ||
|
||
return uint64(major<<8 + minor) | ||
} | ||
|
||
func getFSTypeByDev(dev uint64) (string, error) { | ||
mi, err := os.Open("/proc/self/mountinfo") | ||
if err != nil { | ||
return "", err | ||
} | ||
defer mi.Close() | ||
|
||
sc := bufio.NewScanner(mi) | ||
for sc.Scan() { | ||
line := strings.Split(sc.Text(), " ") | ||
if len(line) < 10 { | ||
return "", fmt.Errorf("Short line in /proc/self/mountinfo: %v\n", line) | ||
} | ||
dstr := line[2] // major:minor: value of st_dev for files on filesystem | ||
fs := line[8] // filesystem type: name of filesystem of the form "type[.subtype]" | ||
d := parseDev(dstr) | ||
if d == 0 { | ||
return "", fmt.Errorf("Can't parse device %s", dstr) | ||
} | ||
if d == dev { | ||
return fs, nil | ||
} | ||
} | ||
|
||
return "", nil | ||
} |