github.com/whamcloud/lemur@v0.0.0-20190827193804-4655df8a52af/pkg/fsroot/client.go (about)

     1  // Copyright (c) 2018 DDN. All rights reserved.
     2  // Use of this source code is governed by a MIT-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package fsroot
     6  
     7  import (
     8  	"github.com/intel-hpdd/go-lustre/fs"
     9  	"github.com/intel-hpdd/go-lustre/pkg/mntent"
    10  )
    11  
    12  type (
    13  	// FsID is a Lustre filesystem ID
    14  	FsID struct {
    15  		val [2]int32
    16  	}
    17  
    18  	// Client defines an interface for Lustre filesystem clients
    19  	Client interface {
    20  		FsName() string
    21  		Path() string
    22  		Root() fs.RootDir
    23  	}
    24  
    25  	// FsClient is an implementation of the Client interface
    26  	fsClient struct {
    27  		root   fs.RootDir
    28  		fsName string
    29  		fsID   *FsID
    30  	}
    31  )
    32  
    33  func getFsName(mountPath string) (string, error) {
    34  	entry, err := mntent.GetEntryByDir(mountPath)
    35  	if err != nil {
    36  		return "", err
    37  	}
    38  	return entry.Fsname, nil
    39  }
    40  
    41  // New returns a new Client
    42  func New(path string) (Client, error) {
    43  	root, err := fs.MountRoot(path)
    44  	if err != nil {
    45  		return nil, err
    46  	}
    47  	name, err := getFsName(root.Path())
    48  	if err != nil {
    49  		return nil, err
    50  	}
    51  	id, err := getFsID(path)
    52  	if err != nil {
    53  		return nil, err
    54  	}
    55  	return &fsClient{root: root,
    56  		fsName: name,
    57  		fsID:   id,
    58  	}, nil
    59  }
    60  
    61  // FsName returns the filesystem name
    62  func (c *fsClient) FsName() string {
    63  	return c.fsName
    64  }
    65  
    66  // Path returns the filesystem root path
    67  func (c *fsClient) Path() string {
    68  	return c.root.Path()
    69  }
    70  
    71  // Root returns the underlying fs.RootDir item
    72  func (c *fsClient) Root() fs.RootDir {
    73  	return c.root
    74  }