Use LockFile instead of Hbc::Utils::file_locked?.

This commit is contained in:
Markus Reiter 2017-05-22 04:15:11 +02:00
parent 060af0a26a
commit fd97e88b99
5 changed files with 21 additions and 29 deletions

View File

@ -75,14 +75,18 @@ module Hbc
paths.each do |item|
next unless item.exist?
processed_files += 1
if Utils.file_locked?(item)
begin
LockFile.new(item.basename).with_lock do
puts item
item_size = File.size?(item)
cleanup_size += item_size unless item_size.nil?
item.unlink
end
rescue OperationInProgressError
puts "skipping: #{item} is locked"
next
end
puts item
item_size = File.size?(item)
cleanup_size += item_size unless item_size.nil?
item.unlink
end
if processed_files.zero?

View File

@ -82,10 +82,16 @@ module Hbc
end
def clear_cache
[cached_location, temporary_path].each do |f|
next unless f.exist?
raise CurlDownloadStrategyError, "#{f} is in use by another process" if Utils.file_locked?(f)
f.unlink
[cached_location, temporary_path].each do |path|
next unless path.exist?
begin
LockFile.new(path.basename).with_lock do
path.unlink
end
rescue OperationInProgressError
raise CurlDownloadStrategyError, "#{path} is in use by another process"
end
end
end
@ -105,10 +111,8 @@ module Hbc
else
had_incomplete_download = temporary_path.exist?
begin
File.open(temporary_path, "a+") do |f|
f.flock(File::LOCK_EX)
LockFile.new(temporary_path.basename).with_lock do
_fetch
f.flock(File::LOCK_UN)
end
rescue ErrorDuringExecution
# 33 == range not supported

View File

@ -2,8 +2,6 @@ require "yaml"
require "open3"
require "stringio"
require "hbc/utils/file"
BUG_REPORTS_URL = "https://github.com/caskroom/homebrew-cask#reporting-bugs".freeze
# monkeypatch Object - not a great idea

View File

@ -1,14 +0,0 @@
module Hbc
module Utils
module_function
def file_locked?(file)
unlocked = File.open(file).flock(File::LOCK_EX | File::LOCK_NB)
# revert lock if file was unlocked before check
File.open(file).flock(File::LOCK_UN) if unlocked
!unlocked
rescue
true
end
end
end

View File

@ -2,7 +2,7 @@ require "fcntl"
class LockFile
def initialize(name)
@name = name
@name = name.to_s
@path = HOMEBREW_LOCK_DIR/"#{@name}.lock"
@lockfile = nil
end