Skip to content
Browse files

Merge pull request #9329 from chuckbjones/fix-cache-delete-rc

Fix deletion of empty directories
  • Loading branch information...
2 parents 2fa9c88 + b883706 commit 95883ca4b1178a423c68ab5cc4bcac50f06551ec @steveklabnik steveklabnik committed
View
2 activesupport/CHANGELOG.md
@@ -3,6 +3,8 @@
## Rails 4.0.0.beta1 (February 25, 2013) ##
+* Fix deletion of empty directories in ActiveSupport::Cache::FileStore. *Charles Jones*
+
* Prevent `DateTime#change` from truncating the second fraction, when seconds
do not need to be changed.
View
4 activesupport/lib/active_support/cache/file_store.rb
@@ -150,9 +150,9 @@ def file_path_key(path)
# Delete empty directories in the cache.
def delete_empty_directories(dir)
- return if dir == cache_path
+ return if File.realpath(dir) == File.realpath(cache_path)
if Dir.entries(dir).reject {|f| EXCLUDED_DIRS.include?(f)}.empty?
- File.delete(dir) rescue nil
+ Dir.delete(dir) rescue nil
delete_empty_directories(File.dirname(dir))
end
end
View
12 activesupport/test/caching_test.rb
@@ -672,6 +672,18 @@ def test_delete_matched_when_cache_directory_does_not_exist
end
end
+ def test_delete_does_not_delete_empty_parent_dir
+ sub_cache_dir = File.join(cache_dir, 'subdir/')
+ sub_cache_store = ActiveSupport::Cache::FileStore.new(sub_cache_dir)
+ assert_nothing_raised(Exception) do
+ assert sub_cache_store.write('foo', 'bar')
+ assert sub_cache_store.delete('foo')
+ end
+ assert File.exist?(cache_dir), "Parent of top level cache dir was deleted!"
+ assert File.exist?(sub_cache_dir), "Top level cache dir was deleted!"
+ assert Dir.entries(sub_cache_dir).reject {|f| ActiveSupport::Cache::FileStore::EXCLUDED_DIRS.include?(f)}.empty?
+ end
+
def test_log_exception_when_cache_read_fails
File.expects(:exist?).raises(StandardError, "failed")
@cache.send(:read_entry, "winston", {})

0 comments on commit 95883ca

Please sign in to comment.
Something went wrong with that request. Please try again.