-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
134 additions
and
6 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,38 @@ | ||
package types | ||
|
||
import "regexp" | ||
|
||
// FileSystemDevicePath is a path to a filesystem device. | ||
type FileSystemDevicePath string | ||
|
||
// String returns the string representation of the file system device path. | ||
func (p FileSystemDevicePath) String() string { | ||
return string(p) | ||
} | ||
|
||
var ( | ||
nfsDevPathRX = regexp.MustCompile(`^([^:]+):(.+)$`) | ||
) | ||
|
||
// IsNFS returns information about a file system device path as if the path is | ||
// an NFS export. | ||
func (p FileSystemDevicePath) IsNFS() ( | ||
ok bool, | ||
remoteHost string, | ||
remoteDir string) { | ||
|
||
m := nfsDevPathRX.FindStringSubmatch(string(p)) | ||
if len(m) == 0 { | ||
return false, "", "" | ||
} | ||
|
||
return true, m[1], m[2] | ||
} | ||
|
||
// IsBind returns a flag indicating whether or not the path appears to be a | ||
// bind mount path. This is decided based on whether or not the device path is | ||
// in the /dev directory. | ||
func (p FileSystemDevicePath) IsBind() bool { | ||
nfs, _, _ := p.IsNFS() | ||
return !nfs && p.isBind() | ||
} |
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,15 @@ | ||
// +build !linux,!darwin | ||
|
||
package types | ||
|
||
import ( | ||
"fmt" | ||
"runtime" | ||
) | ||
|
||
// isBind returns a flag indicating whether or not the path appears to be a | ||
// bind mount path. | ||
func (p FileSystemDevicePath) isBind() bool { | ||
panic(fmt.Errorf( | ||
"FileSystemDevicePath.IsBind unsupported on %s", runtime.GOOS)) | ||
} |
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,56 @@ | ||
package types | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestFileSystemDevicePathIsNFS(t *testing.T) { | ||
|
||
nfs, remoteHost, remoteDir := FileSystemDevicePath( | ||
"/dev/xvda").IsNFS() | ||
assert.False(t, nfs) | ||
|
||
nfs, remoteHost, remoteDir = FileSystemDevicePath( | ||
"server1:/shares/mine").IsNFS() | ||
assert.True(t, nfs) | ||
assert.Equal(t, "server1", remoteHost) | ||
assert.Equal(t, "/shares/mine", remoteDir) | ||
|
||
nfs, remoteHost, remoteDir = FileSystemDevicePath( | ||
"/home/myhome/share").IsNFS() | ||
assert.False(t, nfs) | ||
} | ||
|
||
func TestFileSystemDevicePathIsBind(t *testing.T) { | ||
|
||
assert.False(t, FileSystemDevicePath("/dev/xvda").IsBind()) | ||
assert.False(t, FileSystemDevicePath("server1:/shares/mine").IsBind()) | ||
assert.True(t, FileSystemDevicePath("/home/myhome/share").IsBind()) | ||
} | ||
|
||
func TestFileSystemDevicePathMarshalJSON(t *testing.T) { | ||
|
||
buf, err := json.Marshal(FileSystemDevicePath(`/dev/xvda`)) | ||
if err != nil { | ||
assert.NoError(t, err) | ||
t.FailNow() | ||
} | ||
assert.EqualValues(t, []byte(`"/dev/xvda"`), buf) | ||
|
||
buf, err = json.Marshal(FileSystemDevicePath(`server1:/shares/mine`)) | ||
if err != nil { | ||
assert.NoError(t, err) | ||
t.FailNow() | ||
} | ||
assert.EqualValues(t, []byte(`"server1:/shares/mine"`), buf) | ||
|
||
buf, err = json.Marshal(FileSystemDevicePath(`/home/myhome/share`)) | ||
if err != nil { | ||
assert.NoError(t, err) | ||
t.FailNow() | ||
} | ||
assert.EqualValues(t, []byte(`"/home/myhome/share"`), buf) | ||
} |
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,18 @@ | ||
// +build linux darwin | ||
|
||
package types | ||
|
||
import ( | ||
"regexp" | ||
) | ||
|
||
var ( | ||
bindDevPathRX = regexp.MustCompile(`^/dev/.+$`) | ||
) | ||
|
||
// isBind returns a flag indicating whether or not the path appears to be a | ||
// bind mount path. This is decided based on whether or not the device path is | ||
// in the /dev directory. | ||
func (p FileSystemDevicePath) isBind() bool { | ||
return !bindDevPathRX.MatchString(string(p)) | ||
} |