Skip to content

Commit 9f6b721

Browse files
Merge pull request rails#49694 from fatkodima/fix-file_store-key-splitting
Fix file cache store `delete_matched` to work on keys longer than allowed filename size
2 parents c48dc47 + 9e9fe7f commit 9f6b721

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

activesupport/lib/active_support/cache/file_store.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ def normalize_key(key, options)
176176

177177
# Translate a file path into a key.
178178
def file_path_key(path)
179-
fname = path[cache_path.to_s.size..-1].split(File::SEPARATOR, 4).last
179+
fname = path[cache_path.to_s.size..-1].split(File::SEPARATOR, 4).last.delete(File::SEPARATOR)
180180
URI.decode_www_form_component(fname, Encoding::UTF_8)
181181
end
182182

activesupport/test/cache/stores/file_store_test.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,18 @@ def test_key_transformation_max_filename_size
9191
assert_equal "B", File.basename(path)
9292
end
9393

94+
def test_delete_matched_when_key_exceeds_max_filename_size
95+
submaximal_key = "_" * (ActiveSupport::Cache::FileStore::FILENAME_MAX_SIZE - 1)
96+
97+
@cache.write(submaximal_key + "AB", "value")
98+
@cache.delete_matched(/AB/)
99+
assert_not @cache.exist?(submaximal_key + "AB")
100+
101+
@cache.write(submaximal_key + "/A", "value")
102+
@cache.delete_matched(/A/)
103+
assert_not @cache.exist?(submaximal_key + "/A")
104+
end
105+
94106
# If nothing has been stored in the cache, there is a chance the cache directory does not yet exist
95107
# Ensure delete_matched gracefully handles this case
96108
def test_delete_matched_when_cache_directory_does_not_exist

0 commit comments

Comments
 (0)