| 1 |
// Copyright (c) 2011 CZ.NIC z.s.p.o. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
// blame: jnml, labs.nic.cz |
| 6 |
|
| 7 |
// WIP: Package storage defines and implements storage providers and store accessors. |
| 8 |
package storage // import "modernc.org/fileutil/storage" |
| 9 |
|
| 10 |
import ( |
| 11 |
"os" |
| 12 |
"sync" |
| 13 |
"time" |
| 14 |
) |
| 15 |
|
| 16 |
// FileInfo is a type implementing os.FileInfo which has setable fields, like |
| 17 |
// the older os.FileInfo used to have. It is used wehere e.g. the Size is |
| 18 |
// needed to be faked (encapsulated/memory only file, file cache, etc.). |
| 19 |
type FileInfo struct { |
| 20 |
FName string // base name of the file |
| 21 |
FSize int64 // length in bytes |
| 22 |
FMode os.FileMode // file mode bits |
| 23 |
FModTime time.Time // modification time |
| 24 |
FIsDir bool // abbreviation for Mode().IsDir() |
| 25 |
sys interface{} // underlying data source (can be nil) |
| 26 |
} |
| 27 |
|
| 28 |
// NewFileInfo creates FileInfo from os.FileInfo fi. |
| 29 |
func NewFileInfo(fi os.FileInfo, sys interface{}) *FileInfo { |
| 30 |
return &FileInfo{fi.Name(), fi.Size(), fi.Mode(), fi.ModTime(), fi.IsDir(), sys} |
| 31 |
} |
| 32 |
|
| 33 |
// Implementation of os.FileInfo |
| 34 |
func (fi *FileInfo) Name() string { |
| 35 |
return fi.FName |
| 36 |
} |
| 37 |
|
| 38 |
// Implementation of os.FileInfo |
| 39 |
func (fi *FileInfo) Size() int64 { |
| 40 |
return fi.FSize |
| 41 |
} |
| 42 |
|
| 43 |
// Implementation of os.FileInfo |
| 44 |
func (fi *FileInfo) Mode() os.FileMode { |
| 45 |
return fi.FMode |
| 46 |
} |
| 47 |
|
| 48 |
// Implementation of os.FileInfo |
| 49 |
func (fi *FileInfo) ModTime() time.Time { |
| 50 |
return fi.FModTime |
| 51 |
} |
| 52 |
|
| 53 |
// Implementation of os.FileInfo |
| 54 |
func (fi *FileInfo) IsDir() bool { |
| 55 |
return fi.FIsDir |
| 56 |
} |
| 57 |
|
| 58 |
func (fi *FileInfo) Sys() interface{} { |
| 59 |
return fi.sys |
| 60 |
} |
| 61 |
|
| 62 |
// Accessor provides I/O methods to access a store. |
| 63 |
type Accessor interface { |
| 64 |
|
| 65 |
// Close closes the store, rendering it unusable for I/O. It returns an |
| 66 |
// error, if any. |
| 67 |
Close() error |
| 68 |
|
| 69 |
// Name returns the name of the file as presented to Open. |
| 70 |
Name() string |
| 71 |
|
| 72 |
// ReadAt reads len(b) bytes from the store starting at byte offset off. |
| 73 |
// It returns the number of bytes read and the error, if any. |
| 74 |
// EOF is signaled by a zero count with err set to os.EOF. |
| 75 |
// ReadAt always returns a non-nil Error when n != len(b). |
| 76 |
ReadAt(b []byte, off int64) (n int, err error) |
| 77 |
|
| 78 |
// Stat returns the FileInfo structure describing the store. It returns |
| 79 |
// the os.FileInfo and an error, if any. |
| 80 |
Stat() (fi os.FileInfo, err error) |
| 81 |
|
| 82 |
// Sync commits the current contents of the store to stable storage. |
| 83 |
// Typically, this means flushing the file system's in-memory copy of |
| 84 |
// recently written data to disk. |
| 85 |
Sync() (err error) |
| 86 |
|
| 87 |
// Truncate changes the size of the store. It does not change the I/O |
| 88 |
// offset. |
| 89 |
Truncate(size int64) error |
| 90 |
|
| 91 |
// WriteAt writes len(b) bytes to the store starting at byte offset off. |
| 92 |
// It returns the number of bytes written and an error, if any. |
| 93 |
// WriteAt returns a non-nil Error when n != len(b). |
| 94 |
WriteAt(b []byte, off int64) (n int, err error) |
| 95 |
|
| 96 |
// Before every [structural] change of a store the BeginUpdate is to be |
| 97 |
// called and paired with EndUpdate after the change makes the store's |
| 98 |
// state consistent again. Invocations of BeginUpdate may nest. On |
| 99 |
// invoking the last non nested EndUpdate an implicit "commit" should |
| 100 |
// be performed by the store/provider. The concrete mechanism is |
| 101 |
// unspecified. It could be for example a write-ahead log. Stores may |
| 102 |
// implement BeginUpdate and EndUpdate as a (documented) no op. |
| 103 |
BeginUpdate() error |
| 104 |
EndUpdate() error |
| 105 |
} |
| 106 |
|
| 107 |
// Mutate is a helper/wrapper for executing f in between a.BeginUpdate and |
| 108 |
// a.EndUpdate. Any parameters and/or return values except an error should be |
| 109 |
// captured by a function literal passed as f. The returned err is either nil |
| 110 |
// or the first non nil error returned from the sequence of execution: |
| 111 |
// BeginUpdate, [f,] EndUpdate. The pair BeginUpdate/EndUpdate *is* invoked |
| 112 |
// always regardles of any possible errors produced. Mutate doesn't handle |
| 113 |
// panic, it should be used only with a function [literal] which doesn't panic. |
| 114 |
// Otherwise the pairing of BeginUpdate/EndUpdate is not guaranteed. |
| 115 |
// |
| 116 |
// NOTE: If BeginUpdate, which is invoked before f, returns a non-nil error, |
| 117 |
// then f is not invoked at all (but EndUpdate still is). |
| 118 |
func Mutate(a Accessor, f func() error) (err error) { |
| 119 |
defer func() { |
| 120 |
if e := a.EndUpdate(); e != nil && err == nil { |
| 121 |
err = e |
| 122 |
} |
| 123 |
}() |
| 124 |
|
| 125 |
if err = a.BeginUpdate(); err != nil { |
| 126 |
return |
| 127 |
} |
| 128 |
|
| 129 |
return f() |
| 130 |
} |
| 131 |
|
| 132 |
// LockedMutate wraps Mutate in yet another layer consisting of a |
| 133 |
// l.Lock/l.Unlock pair. All other limitations apply as in Mutate, e.g. no |
| 134 |
// panics are allowed to happen - otherwise no guarantees can be made about |
| 135 |
// Unlock matching the Lock. |
| 136 |
func LockedMutate(a Accessor, l sync.Locker, f func() error) (err error) { |
| 137 |
l.Lock() |
| 138 |
defer l.Unlock() |
| 139 |
|
| 140 |
return Mutate(a, f) |
| 141 |
} |