github.com/whamcloud/lemur@v0.0.0-20190827193804-4655df8a52af/cmd/lhsmd/agent/fileid/fileid.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 fileid
     6  
     7  import (
     8  	"fmt"
     9  
    10  	"github.com/intel-hpdd/go-lustre"
    11  	"github.com/intel-hpdd/go-lustre/fs"
    12  	"github.com/intel-hpdd/go-lustre/pkg/xattr"
    13  	"github.com/intel-hpdd/logging/debug"
    14  	"github.com/pkg/errors"
    15  )
    16  
    17  const xattrUUID = "trusted.lhsm_uuid"
    18  const xattrHash = "trusted.lhsm_hash"
    19  const xattrURL = "trusted.lhsm_url"
    20  
    21  type (
    22  	manager interface {
    23  		update(string, []byte) error
    24  		set(string, []byte) error
    25  		get(string) ([]byte, error)
    26  	}
    27  	attrManager struct {
    28  		attr string
    29  	}
    30  	// Attribute is an interface for managing exctended attributes.
    31  	Attribute struct {
    32  		mgr manager
    33  	}
    34  )
    35  
    36  var UUID, Hash, URL Attribute
    37  
    38  func init() {
    39  	defaultAttrs()
    40  }
    41  func defaultAttrs() {
    42  	UUID = Attribute{newManager(xattrUUID)}
    43  	Hash = Attribute{newManager(xattrHash)}
    44  	URL = Attribute{newManager(xattrURL)}
    45  }
    46  
    47  // Manager returns a new attrManager
    48  func newManager(attr string) *attrManager {
    49  	return &attrManager{attr: attr}
    50  }
    51  
    52  func (m *attrManager) String() string {
    53  	return m.attr
    54  }
    55  
    56  func (m *attrManager) update(p string, fileID []byte) error {
    57  	return m.set(p, fileID)
    58  }
    59  
    60  func (m *attrManager) set(p string, fileID []byte) error {
    61  	return xattr.Lsetxattr(p, m.attr, fileID, 0)
    62  }
    63  
    64  func (m *attrManager) get(p string) ([]byte, error) {
    65  	buf := make([]byte, 256)
    66  
    67  	sz, err := xattr.Lgetxattr(p, m.attr, buf)
    68  	if err != nil {
    69  		return nil, err
    70  	}
    71  	return buf[0:sz], nil
    72  }
    73  
    74  func (a Attribute) String() string {
    75  	return fmt.Sprintf("%s", a.mgr)
    76  }
    77  
    78  // Update updates an existing fileid attribute with a new value
    79  func (a Attribute) Update(p string, fileID []byte) error {
    80  	return a.mgr.update(p, fileID)
    81  }
    82  
    83  // UpdateByFid updates an existing fileid attribute with a new value
    84  func (a Attribute) UpdateByFid(mnt fs.RootDir, fid *lustre.Fid, fileID []byte) error {
    85  	p := fs.FidPath(mnt, fid)
    86  	return a.Update(p, fileID)
    87  }
    88  
    89  // Set sets a fileid attribute on a file
    90  func (a Attribute) Set(p string, fileID []byte) error {
    91  	debug.Printf("setting %s=%s on %s", xattrUUID, fileID, p)
    92  	return a.mgr.set(p, fileID)
    93  }
    94  
    95  // Get gets the fileid attribute for a file
    96  func (a Attribute) Get(path string) ([]byte, error) {
    97  	val, err := a.mgr.get(path)
    98  	if err != nil {
    99  		debug.Printf("Error reading attribute: %v (%s) will retry", err, a.mgr)
   100  		// WTF, let's try again
   101  		//time.Sleep(1 * time.Second)
   102  		val, err = a.mgr.get(path)
   103  		if err != nil {
   104  			return nil, errors.Wrap(err, a.String())
   105  		}
   106  	}
   107  	return val, nil
   108  }
   109  
   110  // GetByFid fetches attribute by root and FID.
   111  func (a Attribute) GetByFid(mnt fs.RootDir, fid *lustre.Fid) ([]byte, error) {
   112  	p := fs.FidPath(mnt, fid)
   113  	return a.Get(p)
   114  }