// request.
//
type UnixVolume struct {
- root string // path to this volume
- queue chan *IORequest
+ root string // path to this volume
+ queue chan *IORequest
+ readonly bool
}
func (v *UnixVolume) IOHandler() {
}
}
-func MakeUnixVolume(root string, serialize bool) (v UnixVolume) {
+func MakeUnixVolume(root string, serialize bool, readonly bool) *UnixVolume {
+ v := &UnixVolume{
+ root: root,
+ queue: nil,
+ readonly: readonly,
+ }
if serialize {
- v = UnixVolume{root, make(chan *IORequest)}
+ v.queue =make(chan *IORequest)
go v.IOHandler()
- } else {
- v = UnixVolume{root, nil}
}
- return
+ return v
}
func (v *UnixVolume) Get(loc string) ([]byte, error) {
}
func (v *UnixVolume) Put(loc string, block []byte) error {
+ if v.readonly {
+ return MethodDisabledError
+ }
if v.queue == nil {
return v.Write(loc, block)
}
}
func (v *UnixVolume) Touch(loc string) error {
+ if v.readonly {
+ return MethodDisabledError
+ }
p := v.blockPath(loc)
f, err := os.OpenFile(p, os.O_RDWR|os.O_APPEND, 0644)
if err != nil {
return err
}
+ defer f.Close()
if e := lockfile(f); e != nil {
return e
}
}
func (v *UnixVolume) Delete(loc string) error {
+ // Touch() must be called before calling Write() on a block. Touch()
+ // also uses lockfile(). This avoids a race condition between Write()
+ // and Delete() because either (a) the file will be deleted and Touch()
+ // will signal to the caller that the file is not present (and needs to
+ // be re-written), or (b) Touch() will update the file's timestamp and
+ // Delete() will read the correct up-to-date timestamp and choose not to
+ // delete the file.
+
+ if v.readonly {
+ return MethodDisabledError
+ }
p := v.blockPath(loc)
f, err := os.OpenFile(p, os.O_RDWR|os.O_APPEND, 0644)
if err != nil {
return err
}
+ defer f.Close()
if e := lockfile(f); e != nil {
return e
}
return fmt.Sprintf("[UnixVolume %s]", v.root)
}
+func (v *UnixVolume) Writable() bool {
+ return !v.readonly
+}
+
// lockfile and unlockfile use flock(2) to manage kernel file locks.
func lockfile(f *os.File) error {
return syscall.Flock(int(f.Fd()), syscall.LOCK_EX)