Skip to content

Commit

Permalink
Merge pull request #2342 from dosuken123/if-no-space-then-remove-old-…
Browse files Browse the repository at this point in the history
…cache

If no space left then remove old cache
  • Loading branch information
mshibuya committed Oct 16, 2018
2 parents f32db18 + 096f7b2 commit b8c7b41
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lib/carrierwave/storage/file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def retrieve!(identifier)
#
def cache!(new_file)
new_file.move_to(::File.expand_path(uploader.cache_path, uploader.root), uploader.permissions, uploader.directory_permissions, true)
rescue Errno::EMLINK => e
rescue Errno::EMLINK, Errno::ENOSPC => e
raise(e) if @cache_called
@cache_called = true

Expand Down
9 changes: 8 additions & 1 deletion spec/storage/file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,14 @@

describe '#cache!' do
context "when FileUtils.mkdir_p raises Errno::EMLINK" do
before { fake_failed_mkdir_p }
before { fake_failed_mkdir_p(Errno::EMLINK) }
after { storage.cache!(sanitized_temp_file) }

it { is_expected.to receive(:clean_cache!).with(600) }
end

context "when FileUtils.mkdir_p raises Errno::ENOSPC" do
before { fake_failed_mkdir_p(Errno::ENOSPC) }
after { storage.cache!(sanitized_temp_file) }

it { is_expected.to receive(:clean_cache!).with(600) }
Expand Down
6 changes: 3 additions & 3 deletions spec/support/file_utils_helper.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
module FileUtilsHelper
# NOTE: Make FileUtils.mkdir_p to raise `Errno::EMLINK` only once
def fake_failed_mkdir_p
# NOTE: Make FileUtils.mkdir_p to raise error only once
def fake_failed_mkdir_p(error)
original_mkdir_p = FileUtils.method(:mkdir_p)
mkdir_p_called = false
allow(FileUtils).to receive(:mkdir_p) do |args|
if mkdir_p_called
original_mkdir_p.call(*args)
else
mkdir_p_called = true
raise Errno::EMLINK
raise error
end
end
end
Expand Down

0 comments on commit b8c7b41

Please sign in to comment.