| 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 |
package storage // import "modernc.org/fileutil/storage" |
| 8 |
|
| 9 |
import ( |
| 10 |
"errors" |
| 11 |
"fmt" |
| 12 |
"io/ioutil" |
| 13 |
"math" |
| 14 |
"os" |
| 15 |
) |
| 16 |
|
| 17 |
//TODO -> exported type w/ exported fields |
| 18 |
type memaccessor struct { |
| 19 |
f *os.File |
| 20 |
fi *FileInfo |
| 21 |
b []byte |
| 22 |
} |
| 23 |
|
| 24 |
// Implementation of Accessor. |
| 25 |
func (m *memaccessor) BeginUpdate() error { return nil } |
| 26 |
|
| 27 |
// Implementation of Accessor. |
| 28 |
func (f *memaccessor) EndUpdate() error { return nil } |
| 29 |
|
| 30 |
// NewMem returns a new Accessor backed by an os.File. The returned Accessor |
| 31 |
// keeps all of the store content in memory. The memory and file images are |
| 32 |
// synced only by Sync and Close. Recomended for small amounts of data only |
| 33 |
// and content which may be lost on process kill/crash. NewMem return the |
| 34 |
// Accessor or an error of any. |
| 35 |
// |
| 36 |
// NOTE: The returned Accessor implements BeginUpdate and EndUpdate as a no op. |
| 37 |
func NewMem(f *os.File) (store Accessor, err error) { |
| 38 |
a := &memaccessor{f: f} |
| 39 |
if err = f.Truncate(0); err != nil { |
| 40 |
return |
| 41 |
} |
| 42 |
|
| 43 |
var fi os.FileInfo |
| 44 |
if fi, err = a.f.Stat(); err != nil { |
| 45 |
return |
| 46 |
} |
| 47 |
|
| 48 |
a.fi = NewFileInfo(fi, a) |
| 49 |
store = a |
| 50 |
return |
| 51 |
} |
| 52 |
|
| 53 |
// OpenMem return a new Accessor backed by an os.File. The store content is |
| 54 |
// loaded from f. The returned Accessor keeps all of the store content in |
| 55 |
// memory. The memory and file images are synced only Sync and Close. |
| 56 |
// Recomended for small amounts of data only and content which may be lost on |
| 57 |
// process kill/crash. OpenMem return the Accessor or an error of any. |
| 58 |
// |
| 59 |
// NOTE: The returned Accessor implements BeginUpdate and EndUpdate as a no op. |
| 60 |
func OpenMem(f *os.File) (store Accessor, err error) { |
| 61 |
a := &memaccessor{f: f} |
| 62 |
if a.b, err = ioutil.ReadAll(a.f); err != nil { |
| 63 |
a.f.Close() |
| 64 |
return |
| 65 |
} |
| 66 |
|
| 67 |
var fi os.FileInfo |
| 68 |
if fi, err = a.f.Stat(); err != nil { |
| 69 |
a.f.Close() |
| 70 |
return |
| 71 |
} |
| 72 |
|
| 73 |
a.fi = NewFileInfo(fi, a) |
| 74 |
store = a |
| 75 |
return |
| 76 |
} |
| 77 |
|
| 78 |
// Close implements Accessor. Specifically it synchronizes the memory and file images. |
| 79 |
func (a *memaccessor) Close() (err error) { |
| 80 |
defer func() { |
| 81 |
a.b = nil |
| 82 |
if a.f != nil { |
| 83 |
if e := a.f.Close(); e != nil && err == nil { |
| 84 |
err = e |
| 85 |
} |
| 86 |
} |
| 87 |
a.f = nil |
| 88 |
}() |
| 89 |
|
| 90 |
return a.Sync() |
| 91 |
} |
| 92 |
|
| 93 |
func (a *memaccessor) Name() string { |
| 94 |
return a.f.Name() |
| 95 |
} |
| 96 |
|
| 97 |
func (a *memaccessor) ReadAt(b []byte, off int64) (n int, err error) { |
| 98 |
if off < 0 || off > math.MaxInt32 { |
| 99 |
return -1, fmt.Errorf("ReadAt: illegal offset %#x", off) |
| 100 |
} |
| 101 |
|
| 102 |
rq, fp := len(b), int(off) |
| 103 |
if fp+rq > len(a.b) { |
| 104 |
return -1, fmt.Errorf("ReadAt: illegal rq %#x @ offset %#x, len %#x", rq, fp, len(a.b)) |
| 105 |
} |
| 106 |
|
| 107 |
copy(b, a.b[fp:]) |
| 108 |
return |
| 109 |
} |
| 110 |
|
| 111 |
func (a *memaccessor) Stat() (fi os.FileInfo, err error) { |
| 112 |
i := a.fi |
| 113 |
i.FSize = int64(len(a.b)) |
| 114 |
fi = i |
| 115 |
return |
| 116 |
} |
| 117 |
|
| 118 |
// Sync implements Accessor. Specifically it synchronizes the memory and file images. |
| 119 |
func (a *memaccessor) Sync() (err error) { |
| 120 |
var n int |
| 121 |
if n, err = a.f.WriteAt(a.b, 0); n != len(a.b) { |
| 122 |
return |
| 123 |
} |
| 124 |
|
| 125 |
return a.f.Truncate(int64(len(a.b))) |
| 126 |
} |
| 127 |
|
| 128 |
func (a *memaccessor) Truncate(size int64) (err error) { |
| 129 |
defer func() { |
| 130 |
if e := recover(); e != nil { |
| 131 |
err = e.(error) |
| 132 |
} |
| 133 |
}() |
| 134 |
|
| 135 |
if size > math.MaxInt32 { |
| 136 |
panic(errors.New("truncate: illegal size")) |
| 137 |
} |
| 138 |
|
| 139 |
a.b = a.b[:int(size)] |
| 140 |
return |
| 141 |
} |
| 142 |
|
| 143 |
func (a *memaccessor) WriteAt(b []byte, off int64) (n int, err error) { |
| 144 |
if off < 0 || off > math.MaxInt32 { |
| 145 |
return -1, errors.New("WriteAt: illegal offset") |
| 146 |
} |
| 147 |
|
| 148 |
rq, fp, size := len(b), int(off), len(a.b) |
| 149 |
if need := rq + fp; need > size { |
| 150 |
if need <= cap(a.b) { |
| 151 |
a.b = a.b[:need] |
| 152 |
} else { |
| 153 |
nb := make([]byte, need, 2*need) |
| 154 |
copy(nb, a.b) |
| 155 |
a.b = nb |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
copy(a.b[int(off):], b) |
| 160 |
return |
| 161 |
} |