2
0

cache.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import msgpack
  2. import os
  3. from . import NS_ARCHIVE_CHUNKS, NS_CHUNK
  4. class Cache(object):
  5. """Client Side cache
  6. """
  7. def __init__(self, store, keychain):
  8. self.store = store
  9. self.keychain = keychain
  10. self.path = os.path.join(Cache.cache_dir_path(),
  11. '%s.cache' % self.store.id.encode('hex'))
  12. self.tid = -1
  13. self.open()
  14. if self.tid != self.store.tid:
  15. self.init()
  16. @staticmethod
  17. def cache_dir_path():
  18. """Return path to directory used for storing users cache files"""
  19. return os.path.join(os.path.expanduser('~'), '.darc', 'cache')
  20. def open(self):
  21. if not os.path.exists(self.path):
  22. return
  23. with open(self.path, 'rb') as fd:
  24. data, hash = self.keychain.decrypt(fd.read())
  25. cache = msgpack.unpackb(data)
  26. assert cache['version'] == 1
  27. self.chunk_counts = cache['chunk_counts']
  28. self.file_chunks = cache['file_chunks']
  29. self.tid = cache['tid']
  30. def init(self):
  31. """Initializes cache by fetching and reading all archive indicies
  32. """
  33. print 'Initializing cache...'
  34. self.chunk_counts = {}
  35. self.file_chunks = {}
  36. self.tid = self.store.tid
  37. if self.store.tid == 0:
  38. return
  39. for id in list(self.store.list(NS_ARCHIVE_CHUNKS)):
  40. data, hash = self.keychain.decrypt(self.store.get(NS_ARCHIVE_CHUNKS, id))
  41. cindex = msgpack.unpackb(data)
  42. for id, size in cindex['chunks']:
  43. try:
  44. count, size = self.chunk_counts[id]
  45. self.chunk_counts[id] = count + 1, size
  46. except KeyError:
  47. self.chunk_counts[id] = 1, size
  48. self.save()
  49. def filter_file_chunks(self):
  50. for key, value in self.file_chunks.iteritems():
  51. if value[0] < 8:
  52. yield key, (value[0] + 1,) + value[1:]
  53. def save(self):
  54. cache = {'version': 1,
  55. 'tid': self.store.tid,
  56. 'chunk_counts': self.chunk_counts,
  57. 'file_chunks': dict(self.filter_file_chunks()),
  58. }
  59. data, hash = self.keychain.encrypt_create(msgpack.packb(cache))
  60. cachedir = os.path.dirname(self.path)
  61. if not os.path.exists(cachedir):
  62. os.makedirs(cachedir)
  63. with open(self.path, 'wb') as fd:
  64. fd.write(data)
  65. def add_chunk(self, id, data):
  66. if self.seen_chunk(id):
  67. return self.chunk_incref(id)
  68. data, hash = self.keychain.encrypt_read(data)
  69. csize = len(data)
  70. self.store.put(NS_CHUNK, id, data)
  71. self.chunk_counts[id] = (1, csize)
  72. return id, csize
  73. def seen_chunk(self, id):
  74. return self.chunk_counts.get(id, (0, 0))[0]
  75. def chunk_incref(self, id):
  76. count, size = self.chunk_counts[id]
  77. self.chunk_counts[id] = (count + 1, size)
  78. return id, size
  79. def chunk_decref(self, id):
  80. count, size = self.chunk_counts[id]
  81. if count == 1:
  82. del self.chunk_counts[id]
  83. self.store.delete(NS_CHUNK, id)
  84. else:
  85. self.chunk_counts[id] = (count - 1, size)
  86. def file_known_and_unchanged(self, path_hash, st):
  87. entry = self.file_chunks.get(path_hash)
  88. if (entry and entry[3] == st.st_mtime
  89. and entry[2] == st.st_size and entry[1] == st.st_ino):
  90. # reset entry age
  91. self.file_chunks[path_hash] = (0,) + entry[1:]
  92. return entry[4], entry[2]
  93. else:
  94. return None, 0
  95. def memorize_file_chunks(self, path_hash, st, ids):
  96. # Entry: Age, inode, size, mtime, chunk ids
  97. self.file_chunks[path_hash] = 0, st.st_ino, st.st_size, st.st_mtime, ids