Skip to content

Commit

Permalink
Merge pull request #22278 from poporul/master
Browse files Browse the repository at this point in the history
Allow to store .keep file in cache directory
  • Loading branch information
chancancode committed Jan 15, 2016
2 parents 0d5c39c + 4f3e868 commit 2b90048
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
12 changes: 9 additions & 3 deletions activesupport/lib/active_support/cache/file_store.rb
Expand Up @@ -17,17 +17,18 @@ class FileStore < Store
FILENAME_MAX_SIZE = 228 # max filename size on file system is 255, minus room for timestamp and random characters appended by Tempfile (used by atomic write)
FILEPATH_MAX_SIZE = 900 # max is 1024, plus some room
EXCLUDED_DIRS = ['.', '..'].freeze
GITKEEP_FILES = ['.gitkeep', '.keep'].freeze

def initialize(cache_path, options = nil)
super(options)
@cache_path = cache_path.to_s
end

# Deletes all items from the cache. In this case it deletes all the entries in the specified
# file store directory except for .gitkeep. Be careful which directory is specified in your
# file store directory except for .keep or .gitkeep. Be careful which directory is specified in your
# config file when using +FileStore+ because everything in that directory will be deleted.
def clear(options = nil)
root_dirs = Dir.entries(cache_path).reject {|f| (EXCLUDED_DIRS + [".gitkeep"]).include?(f)}
root_dirs = exclude_from(cache_path, EXCLUDED_DIRS + GITKEEP_FILES)
FileUtils.rm_r(root_dirs.collect{|f| File.join(cache_path, f)})
rescue Errno::ENOENT
end
Expand Down Expand Up @@ -154,7 +155,7 @@ def file_path_key(path)
# Delete empty directories in the cache.
def delete_empty_directories(dir)
return if File.realpath(dir) == File.realpath(cache_path)
if Dir.entries(dir).reject {|f| EXCLUDED_DIRS.include?(f)}.empty?
if exclude_from(dir, EXCLUDED_DIRS).empty?
Dir.delete(dir) rescue nil
delete_empty_directories(File.dirname(dir))
end
Expand Down Expand Up @@ -193,6 +194,11 @@ def modify_value(name, amount, options)
end
end
end

# Exclude entries from source directory
def exclude_from(source, excludes)
Dir.entries(source).reject { |f| excludes.include?(f) }
end
end
end
end
8 changes: 5 additions & 3 deletions activesupport/test/caching_test.rb
Expand Up @@ -780,10 +780,12 @@ def cache_dir
include AutoloadingCacheBehavior

def test_clear
filepath = File.join(cache_dir, ".gitkeep")
FileUtils.touch(filepath)
gitkeep = File.join(cache_dir, ".gitkeep")
keep = File.join(cache_dir, ".keep")
FileUtils.touch([gitkeep, keep])
@cache.clear
assert File.exist?(filepath)
assert File.exist?(gitkeep)
assert File.exist?(keep)
end

def test_clear_without_cache_dir
Expand Down

0 comments on commit 2b90048

Please sign in to comment.