Skip to content

Commit

Permalink
FileSystemDevicePath
Browse files Browse the repository at this point in the history
  • Loading branch information
akutz committed Jun 2, 2016
1 parent 0cd2df6 commit cd300c8
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 6 deletions.
13 changes: 7 additions & 6 deletions api/types/types_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ type MountInfo struct {
// FSType indicates the type of filesystem, such as EXT3.
FSType string `json:"fsType"`

// Source indicates filesystem specific information or "none".
Source string `json:"source"`
// DevicePath is the path of the mounted path.
DevicePath FileSystemDevicePath `json:"devicePath"`

// VFSOpts represents per super block options.
VFSOpts string `json:"vfsOpts"`
Expand All @@ -105,7 +105,8 @@ type Snapshot struct {
// The snapshot's ID.
ID string `json:"id"`

// The time (epoch) at which the request to create the snapshot was submitted.
// The time (epoch) at which the request to create the snapshot was
// submitted.
StartTime int64 `json:"startTime,omitempty"`

// The status of the snapshot.
Expand Down Expand Up @@ -173,9 +174,9 @@ func (v *Volume) MountPoint() string {
// VolumeAttachment provides information about an object attached to a
// storage volume.
type VolumeAttachment struct {
// The name of the device on which the volume to which the object is
// attached is mounted.
DeviceName string `json:"deviceName"`
// DevicePath is the name of the device on which the volume to which the
// object is attached is mounted.
DevicePath FileSystemDevicePath `json:"devicePath"`

// MountPoint is the mount point for the volume. This field is set when a
// volume is retrieved via an integration driver.
Expand Down
38 changes: 38 additions & 0 deletions api/types/types_paths_fsdev.go
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()
}
15 changes: 15 additions & 0 deletions api/types/types_paths_fsdev_other.go
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))
}
56 changes: 56 additions & 0 deletions api/types/types_paths_fsdev_test.go
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)
}
18 changes: 18 additions & 0 deletions api/types/types_paths_fsdev_unix.go
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))
}

0 comments on commit cd300c8

Please sign in to comment.