dataStore_files.go 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. // +build FILES_DB
  2. /*
  3. * Copyright (c) 2018, Psiphon Inc.
  4. * All rights reserved.
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. package psiphon
  21. import (
  22. "bytes"
  23. "encoding/hex"
  24. "errors"
  25. "io/ioutil"
  26. "os"
  27. "path/filepath"
  28. "strings"
  29. "sync"
  30. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common"
  31. )
  32. // datastoreDB is a simple filesystem-backed key/value store that implements
  33. // the datastore interface.
  34. //
  35. // The current implementation is intended only for experimentation.
  36. //
  37. // Buckets are subdirectories, keys are file names (hex-encoded), and values
  38. // are file contents. Unlike other datastores, update transactions are neither
  39. // atomic not isolcated; only each put is individually atomic.
  40. //
  41. // A buffer pool is used to reduce memory allocation/GC churn from loading
  42. // file values into memory. Transactions and cursors track and release shared
  43. // buffers.
  44. //
  45. // As with the original datastore interface, value slices are only valid
  46. // within a transaction; for cursors, there's a further limitation that the
  47. // value slices are only valid until the next iteration.
  48. type datastoreDB struct {
  49. dataDirectory string
  50. bufferPool sync.Pool
  51. lock sync.RWMutex
  52. closed bool
  53. }
  54. type datastoreTx struct {
  55. db *datastoreDB
  56. canUpdate bool
  57. buffers []*bytes.Buffer
  58. }
  59. type datastoreBucket struct {
  60. bucketDirectory string
  61. tx *datastoreTx
  62. }
  63. type datastoreCursor struct {
  64. bucket *datastoreBucket
  65. fileInfos []os.FileInfo
  66. index int
  67. lastBuffer *bytes.Buffer
  68. }
  69. func datastoreOpenDB(rootDataDirectory string) (*datastoreDB, error) {
  70. dataDirectory := filepath.Join(rootDataDirectory, "psiphon.filesdb")
  71. err := os.MkdirAll(dataDirectory, 0700)
  72. if err != nil {
  73. return nil, common.ContextError(err)
  74. }
  75. return &datastoreDB{
  76. dataDirectory: dataDirectory,
  77. bufferPool: sync.Pool{
  78. New: func() interface{} {
  79. return new(bytes.Buffer)
  80. },
  81. },
  82. }, nil
  83. }
  84. func (db *datastoreDB) getBuffer() *bytes.Buffer {
  85. return db.bufferPool.Get().(*bytes.Buffer)
  86. }
  87. func (db *datastoreDB) putBuffer(buffer *bytes.Buffer) {
  88. buffer.Truncate(0)
  89. db.bufferPool.Put(buffer)
  90. }
  91. func (db *datastoreDB) readBuffer(filename string) (*bytes.Buffer, error) {
  92. // Complete any partial put commit.
  93. err := datastoreApplyCommit(filename)
  94. if err != nil {
  95. return nil, common.ContextError(err)
  96. }
  97. file, err := os.Open(filename)
  98. if err != nil {
  99. if os.IsNotExist(err) {
  100. return nil, nil
  101. }
  102. return nil, common.ContextError(err)
  103. }
  104. defer file.Close()
  105. buffer := db.getBuffer()
  106. _, err = buffer.ReadFrom(file)
  107. if err != nil {
  108. return nil, common.ContextError(err)
  109. }
  110. return buffer, nil
  111. }
  112. func (db *datastoreDB) close() error {
  113. // close will await any active view and update transactions via this lock.
  114. db.lock.Lock()
  115. defer db.lock.Unlock()
  116. db.closed = true
  117. return nil
  118. }
  119. func (db *datastoreDB) view(fn func(tx *datastoreTx) error) error {
  120. db.lock.RLock()
  121. defer db.lock.RUnlock()
  122. if db.closed {
  123. return common.ContextError(errors.New("closed"))
  124. }
  125. tx := &datastoreTx{db: db}
  126. defer tx.releaseBuffers()
  127. err := fn(tx)
  128. if err != nil {
  129. return common.ContextError(err)
  130. }
  131. return nil
  132. }
  133. func (db *datastoreDB) update(fn func(tx *datastoreTx) error) error {
  134. db.lock.Lock()
  135. defer db.lock.Unlock()
  136. if db.closed {
  137. return common.ContextError(errors.New("closed"))
  138. }
  139. tx := &datastoreTx{db: db, canUpdate: true}
  140. defer tx.releaseBuffers()
  141. err := fn(tx)
  142. if err != nil {
  143. return common.ContextError(err)
  144. }
  145. return nil
  146. }
  147. func (tx *datastoreTx) bucket(name []byte) *datastoreBucket {
  148. bucketDirectory := filepath.Join(tx.db.dataDirectory, hex.EncodeToString(name))
  149. err := os.MkdirAll(bucketDirectory, 0700)
  150. if err != nil {
  151. // The original datastore interface does not return an error from Bucket,
  152. // so emit notice, and return zero-value bucket for which all
  153. // operations will fail.
  154. NoticeAlert("bucket failed: %s", common.ContextError(err))
  155. return &datastoreBucket{}
  156. }
  157. return &datastoreBucket{
  158. bucketDirectory: bucketDirectory,
  159. tx: tx,
  160. }
  161. }
  162. func (tx *datastoreTx) clearBucket(name []byte) error {
  163. bucketDirectory := filepath.Join(tx.db.dataDirectory, hex.EncodeToString(name))
  164. err := os.RemoveAll(bucketDirectory)
  165. if err != nil {
  166. return common.ContextError(err)
  167. }
  168. return nil
  169. }
  170. func (tx *datastoreTx) releaseBuffers() {
  171. for _, buffer := range tx.buffers {
  172. tx.db.putBuffer(buffer)
  173. }
  174. tx.buffers = nil
  175. }
  176. func (b *datastoreBucket) get(key []byte) []byte {
  177. if b.tx == nil {
  178. return nil
  179. }
  180. filename := filepath.Join(b.bucketDirectory, hex.EncodeToString(key))
  181. valueBuffer, err := b.tx.db.readBuffer(filename)
  182. if err != nil {
  183. // The original datastore interface does not return an error from Get,
  184. // so emit notice.
  185. NoticeAlert("get failed: %s", common.ContextError(err))
  186. return nil
  187. }
  188. if valueBuffer == nil {
  189. return nil
  190. }
  191. b.tx.buffers = append(b.tx.buffers, valueBuffer)
  192. return valueBuffer.Bytes()
  193. }
  194. func (b *datastoreBucket) put(key, value []byte) error {
  195. if b.tx == nil {
  196. return common.ContextError(errors.New("bucket not found"))
  197. }
  198. if !b.tx.canUpdate {
  199. return common.ContextError(errors.New("non-update transaction"))
  200. }
  201. filename := filepath.Join(b.bucketDirectory, hex.EncodeToString(key))
  202. // Complete any partial put commit.
  203. err := datastoreApplyCommit(filename)
  204. if err != nil {
  205. return common.ContextError(err)
  206. }
  207. putFilename := filename + ".put"
  208. err = ioutil.WriteFile(putFilename, value, 0600)
  209. if err != nil {
  210. return common.ContextError(err)
  211. }
  212. commitFilename := filename + ".commit"
  213. err = os.Rename(putFilename, commitFilename)
  214. if err != nil {
  215. return common.ContextError(err)
  216. }
  217. err = datastoreApplyCommit(filename)
  218. if err != nil {
  219. return common.ContextError(err)
  220. }
  221. return nil
  222. }
  223. func datastoreApplyCommit(filename string) error {
  224. commitFilename := filename + ".commit"
  225. if _, err := os.Stat(commitFilename); err != nil && os.IsNotExist(err) {
  226. return nil
  227. }
  228. // TODO: may not be sufficient atomic
  229. err := os.Rename(commitFilename, filename)
  230. if err != nil {
  231. return common.ContextError(err)
  232. }
  233. return nil
  234. }
  235. func (b *datastoreBucket) delete(key []byte) error {
  236. if b.tx == nil {
  237. return common.ContextError(errors.New("bucket not found"))
  238. }
  239. filename := filepath.Join(b.bucketDirectory, hex.EncodeToString(key))
  240. filenames := []string{filename + ".put", filename + ".commit", filename}
  241. for _, filename := range filenames {
  242. err := os.Remove(filename)
  243. if err != nil && !os.IsNotExist(err) {
  244. return common.ContextError(err)
  245. }
  246. }
  247. return nil
  248. }
  249. func (b *datastoreBucket) cursor() *datastoreCursor {
  250. if b.tx == nil {
  251. // The original datastore interface does not return an error from
  252. // Cursor, so emit notice, and return zero-value cursor for which all
  253. // operations will fail.
  254. return &datastoreCursor{}
  255. }
  256. fileInfos, err := ioutil.ReadDir(b.bucketDirectory)
  257. if err != nil {
  258. NoticeAlert("cursor failed: %s", common.ContextError(err))
  259. return &datastoreCursor{}
  260. }
  261. return &datastoreCursor{
  262. bucket: b,
  263. fileInfos: fileInfos,
  264. }
  265. }
  266. func (c *datastoreCursor) advance() {
  267. if c.bucket == nil {
  268. return
  269. }
  270. for {
  271. c.index += 1
  272. if c.index <= len(c.fileInfos) {
  273. break
  274. }
  275. // Skip any .put or .commit files
  276. if strings.Contains(c.fileInfos[c.index].Name(), ".") {
  277. continue
  278. }
  279. }
  280. }
  281. func (c *datastoreCursor) firstKey() []byte {
  282. if c.bucket == nil {
  283. return nil
  284. }
  285. c.index = 0
  286. return c.currentKey()
  287. }
  288. func (c *datastoreCursor) currentKey() []byte {
  289. if c.bucket == nil {
  290. return nil
  291. }
  292. if c.index >= len(c.fileInfos) {
  293. return nil
  294. }
  295. info := c.fileInfos[c.index]
  296. if info.IsDir() {
  297. NoticeAlert("cursor failed: unexpected dir")
  298. return nil
  299. }
  300. key, err := hex.DecodeString(info.Name())
  301. if err != nil {
  302. NoticeAlert("cursor failed: %s", common.ContextError(err))
  303. return nil
  304. }
  305. return key
  306. }
  307. func (c *datastoreCursor) nextKey() []byte {
  308. if c.bucket == nil {
  309. return nil
  310. }
  311. c.advance()
  312. return c.currentKey()
  313. }
  314. func (c *datastoreCursor) first() ([]byte, []byte) {
  315. if c.bucket == nil {
  316. return nil, nil
  317. }
  318. c.index = 0
  319. return c.current()
  320. }
  321. func (c *datastoreCursor) current() ([]byte, []byte) {
  322. key := c.currentKey()
  323. if key == nil {
  324. return nil, nil
  325. }
  326. if c.lastBuffer != nil {
  327. c.bucket.tx.db.putBuffer(c.lastBuffer)
  328. }
  329. c.lastBuffer = nil
  330. filename := filepath.Join(c.bucket.bucketDirectory, hex.EncodeToString(key))
  331. valueBuffer, err := c.bucket.tx.db.readBuffer(filename)
  332. if valueBuffer == nil {
  333. err = errors.New("unexpected nil value")
  334. }
  335. if err != nil {
  336. NoticeAlert("cursor failed: %s", common.ContextError(err))
  337. return nil, nil
  338. }
  339. c.lastBuffer = valueBuffer
  340. return key, valueBuffer.Bytes()
  341. }
  342. func (c *datastoreCursor) next() ([]byte, []byte) {
  343. if c.bucket == nil {
  344. return nil, nil
  345. }
  346. c.advance()
  347. return c.current()
  348. }
  349. func (c *datastoreCursor) close() {
  350. if c.lastBuffer != nil {
  351. c.bucket.tx.db.putBuffer(c.lastBuffer)
  352. c.lastBuffer = nil
  353. }
  354. }