github.com/yasker/longhorn-engine@v0.0.0-20160621014712-6ed6cfca0729/backend/file/file.go (about)

     1  package file
     2  
     3  import (
     4  	"os"
     5  
     6  	"github.com/Sirupsen/logrus"
     7  	"github.com/rancher/longhorn/types"
     8  )
     9  
    10  func New() types.BackendFactory {
    11  	return &Factory{}
    12  }
    13  
    14  type Factory struct {
    15  }
    16  
    17  type Wrapper struct {
    18  	*os.File
    19  }
    20  
    21  func (f *Wrapper) Close() error {
    22  	logrus.Infof("Closing: %s", f.Name())
    23  	return f.File.Close()
    24  }
    25  
    26  func (f *Wrapper) Snapshot(name string) error {
    27  	return nil
    28  }
    29  
    30  func (f *Wrapper) Size() (int64, error) {
    31  	stat, err := f.Stat()
    32  	if err != nil {
    33  		return 0, err
    34  	}
    35  	return stat.Size(), nil
    36  }
    37  
    38  func (f *Wrapper) SectorSize() (int64, error) {
    39  	return 4096, nil
    40  }
    41  
    42  func (ff *Factory) Create(address string) (types.Backend, error) {
    43  	logrus.Infof("Creating file: %s", address)
    44  	file, err := os.OpenFile(address, os.O_RDWR|os.O_CREATE, 0600)
    45  	if err != nil {
    46  		logrus.Infof("Failed to create file %s: %v", address, err)
    47  		return nil, err
    48  	}
    49  
    50  	return &Wrapper{file}, nil
    51  }