abstract_store.rb 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. require 'pathname'
  2. module Docs
  3. class AbstractStore
  4. class InvalidPathError < StandardError; end
  5. class LockError < StandardError; end
  6. include Instrumentable
  7. def initialize(path)
  8. path = Pathname.new(path).cleanpath
  9. raise ArgumentError if path.relative?
  10. @root_path = @working_path = path.freeze
  11. end
  12. def root_path
  13. @root_path.to_s
  14. end
  15. def working_path
  16. @working_path.to_s
  17. end
  18. def expand_path(path)
  19. join_paths @working_path, path
  20. end
  21. def open(path, &block)
  22. if block_given?
  23. open_yield_close(path, &block)
  24. else
  25. set_working_path join_paths(@root_path, path)
  26. end
  27. end
  28. def close
  29. set_working_path @root_path
  30. end
  31. def read(path)
  32. path = expand_path(path)
  33. read_file(path) if file_exist?(path)
  34. end
  35. def write(path, value)
  36. path = expand_path(path)
  37. touch(path)
  38. if file_exist?(path)
  39. update(path, value)
  40. else
  41. create(path, value)
  42. end
  43. end
  44. def delete(path)
  45. path = expand_path(path)
  46. if file_exist?(path)
  47. destroy(path)
  48. true
  49. end
  50. end
  51. def exist?(path)
  52. file_exist? expand_path(path)
  53. end
  54. def mtime(path)
  55. path = expand_path(path)
  56. file_mtime(path) if file_exist?(path)
  57. end
  58. def size(path)
  59. path = expand_path(path)
  60. file_size(path) if file_exist?(path)
  61. end
  62. def each(&block)
  63. list_files(working_path, &block)
  64. end
  65. def replace(path = nil, &block)
  66. if path
  67. return open(path) { replace(&block) }
  68. else
  69. lock { track_touched { yield.tap { delete_untouched } } }
  70. end
  71. end
  72. private
  73. def read_file(path)
  74. raise NotImplementedError
  75. end
  76. def create_file(path, value)
  77. raise NotImplementedError
  78. end
  79. def update_file(path, value)
  80. raise NotImplementedError
  81. end
  82. def delete_file(path)
  83. raise NotImplementedError
  84. end
  85. def file_exist?(path)
  86. raise NotImplementedError
  87. end
  88. def file_mtime(path)
  89. raise NotImplementedError
  90. end
  91. def file_size(path)
  92. raise NotImplementedError
  93. end
  94. def list_files(path, &block)
  95. raise NotImplementedError
  96. end
  97. def set_working_path(path)
  98. @working_path = Pathname.new(path).freeze if assert_unlocked
  99. end
  100. def join_paths(base, path)
  101. base = Pathname.new(base).cleanpath
  102. path = Pathname.new(path).cleanpath
  103. path = base + path unless path.absolute?
  104. unless File.join(path, '').start_with? File.join(base, '')
  105. raise InvalidPathError, "Tried accessing #{path} outside #{base}"
  106. end
  107. path.to_s
  108. end
  109. def open_yield_close(path)
  110. working_path_was = working_path
  111. open(path)
  112. yield
  113. ensure
  114. set_working_path working_path_was
  115. end
  116. def create(path, value)
  117. instrument 'create.store', path: path do
  118. create_file(path, value)
  119. end
  120. end
  121. def update(path, value)
  122. instrument 'update.store', path: path do
  123. update_file(path, value)
  124. end
  125. end
  126. def destroy(path)
  127. instrument 'destroy.store', path: path do
  128. delete_file(path)
  129. end
  130. end
  131. def lock
  132. assert_unlocked
  133. @locked = true
  134. yield
  135. ensure
  136. @locked = false
  137. end
  138. def assert_unlocked
  139. raise LockError if @locked
  140. true
  141. end
  142. def track_touched
  143. @touched = []
  144. yield
  145. ensure
  146. @touched = nil
  147. end
  148. def touch(path)
  149. @touched << path if @touched
  150. end
  151. def touched?(path)
  152. dir = File.join(path, '')
  153. @touched.any? do |touched_path|
  154. touched_path == path || touched_path.start_with?(dir)
  155. end
  156. end
  157. def delete_untouched
  158. return if @touched.empty?
  159. each do |path|
  160. destroy(path) unless touched?(path)
  161. end
  162. end
  163. end
  164. end