Skip to content

Commit

Permalink
Fix FileStore cache incorrectly regenerating its key from a pathname …
Browse files Browse the repository at this point in the history
…when a regexp is used in expire_fragment

[#5850 state:committed]

Signed-off-by: Santiago Pastorino <santiago@wyeworks.com>
  • Loading branch information
odorcicd authored and spastorino committed Nov 7, 2010
1 parent f676beb commit ad2c0bd
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
7 changes: 3 additions & 4 deletions activesupport/lib/active_support/cache/file_store.rb
@@ -1,5 +1,6 @@
require 'active_support/core_ext/file/atomic'
require 'active_support/core_ext/string/conversions'
require 'rack/utils'

module ActiveSupport
module Cache
Expand All @@ -11,8 +12,6 @@ class FileStore < Store
attr_reader :cache_path

DIR_FORMATTER = "%03X"
ESCAPE_FILENAME_CHARS = /[^a-z0-9_.-]/i
UNESCAPE_FILENAME_CHARS = /%[0-9A-F]{2}/

def initialize(cache_path, options = nil)
super(options)
Expand Down Expand Up @@ -136,7 +135,7 @@ def lock_file(file_name, &block) # :nodoc:

# Translate a key into a file path.
def key_file_path(key)
fname = key.to_s.gsub(ESCAPE_FILENAME_CHARS){|match| "%#{match.ord.to_s(16).upcase}"}
fname = Rack::Utils.escape(key)
hash = Zlib.adler32(fname)
hash, dir_1 = hash.divmod(0x1000)
dir_2 = hash.modulo(0x1000)
Expand All @@ -156,7 +155,7 @@ def key_file_path(key)
# Translate a file path into a key.
def file_path_key(path)
fname = path[cache_path.size, path.size].split(File::SEPARATOR, 4).last
fname.gsub(UNESCAPE_FILENAME_CHARS){|match| $1.ord.to_s(16)}
Rack::Utils.unescape(fname)
end

# Delete empty directories in the cache.
Expand Down
4 changes: 4 additions & 0 deletions activesupport/test/caching_test.rb
Expand Up @@ -356,9 +356,13 @@ module CacheDeleteMatchedBehavior
def test_delete_matched
@cache.write("foo", "bar")
@cache.write("fu", "baz")
@cache.write("foo/bar", "baz")
@cache.write("fu/baz", "bar")
@cache.delete_matched(/oo/)
assert_equal false, @cache.exist?("foo")
assert_equal true, @cache.exist?("fu")
assert_equal false, @cache.exist?("foo/bar")
assert_equal true, @cache.exist?("fu/baz")
end
end

Expand Down

0 comments on commit ad2c0bd

Please sign in to comment.