Merge pull request #2663 from reitermarkus/lockfile
Convert `FormulaLock` to more generic `LockFile`.
This commit is contained in:
commit
3165fd2519
@ -75,14 +75,18 @@ module Hbc
|
|||||||
paths.each do |item|
|
paths.each do |item|
|
||||||
next unless item.exist?
|
next unless item.exist?
|
||||||
processed_files += 1
|
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"
|
puts "skipping: #{item} is locked"
|
||||||
next
|
next
|
||||||
end
|
end
|
||||||
puts item
|
|
||||||
item_size = File.size?(item)
|
|
||||||
cleanup_size += item_size unless item_size.nil?
|
|
||||||
item.unlink
|
|
||||||
end
|
end
|
||||||
|
|
||||||
if processed_files.zero?
|
if processed_files.zero?
|
||||||
|
|||||||
@ -82,10 +82,16 @@ module Hbc
|
|||||||
end
|
end
|
||||||
|
|
||||||
def clear_cache
|
def clear_cache
|
||||||
[cached_location, temporary_path].each do |f|
|
[cached_location, temporary_path].each do |path|
|
||||||
next unless f.exist?
|
next unless path.exist?
|
||||||
raise CurlDownloadStrategyError, "#{f} is in use by another process" if Utils.file_locked?(f)
|
|
||||||
f.unlink
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -105,10 +111,8 @@ module Hbc
|
|||||||
else
|
else
|
||||||
had_incomplete_download = temporary_path.exist?
|
had_incomplete_download = temporary_path.exist?
|
||||||
begin
|
begin
|
||||||
File.open(temporary_path, "a+") do |f|
|
LockFile.new(temporary_path.basename).with_lock do
|
||||||
f.flock(File::LOCK_EX)
|
|
||||||
_fetch
|
_fetch
|
||||||
f.flock(File::LOCK_UN)
|
|
||||||
end
|
end
|
||||||
rescue ErrorDuringExecution
|
rescue ErrorDuringExecution
|
||||||
# 33 == range not supported
|
# 33 == range not supported
|
||||||
|
|||||||
@ -2,8 +2,6 @@ require "yaml"
|
|||||||
require "open3"
|
require "open3"
|
||||||
require "stringio"
|
require "stringio"
|
||||||
|
|
||||||
require "hbc/utils/file"
|
|
||||||
|
|
||||||
BUG_REPORTS_URL = "https://github.com/caskroom/homebrew-cask#reporting-bugs".freeze
|
BUG_REPORTS_URL = "https://github.com/caskroom/homebrew-cask#reporting-bugs".freeze
|
||||||
|
|
||||||
# monkeypatch Object - not a great idea
|
# monkeypatch Object - not a great idea
|
||||||
|
|||||||
@ -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
|
|
||||||
@ -1,5 +1,5 @@
|
|||||||
require "formula_support"
|
require "formula_support"
|
||||||
require "formula_lock"
|
require "lock_file"
|
||||||
require "formula_pin"
|
require "formula_pin"
|
||||||
require "hardware"
|
require "hardware"
|
||||||
require "utils/bottles"
|
require "utils/bottles"
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
require "extend/pathname"
|
require "extend/pathname"
|
||||||
require "keg_relocate"
|
require "keg_relocate"
|
||||||
require "formula_lock"
|
require "lock_file"
|
||||||
require "ostruct"
|
require "ostruct"
|
||||||
|
|
||||||
class Keg
|
class Keg
|
||||||
|
|||||||
@ -1,9 +1,9 @@
|
|||||||
require "fcntl"
|
require "fcntl"
|
||||||
|
|
||||||
class FormulaLock
|
class LockFile
|
||||||
def initialize(name)
|
def initialize(name)
|
||||||
@name = name
|
@name = name.to_s
|
||||||
@path = HOMEBREW_LOCK_DIR/"#{@name}.brewing"
|
@path = HOMEBREW_LOCK_DIR/"#{@name}.lock"
|
||||||
@lockfile = nil
|
@lockfile = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -35,3 +35,15 @@ class FormulaLock
|
|||||||
@lockfile.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC)
|
@lockfile.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class FormulaLock < LockFile
|
||||||
|
def initialize(name)
|
||||||
|
super("#{name}.formula")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class CaskLock < LockFile
|
||||||
|
def initialize(name)
|
||||||
|
super("#{name}.cask")
|
||||||
|
end
|
||||||
|
end
|
||||||
@ -1,5 +1,5 @@
|
|||||||
require "formula"
|
require "formula"
|
||||||
require "formula_lock"
|
require "lock_file"
|
||||||
require "keg"
|
require "keg"
|
||||||
require "tab"
|
require "tab"
|
||||||
|
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
require "formula_lock"
|
require "lock_file"
|
||||||
|
|
||||||
describe FormulaLock do
|
describe LockFile do
|
||||||
subject { described_class.new("foo") }
|
subject { described_class.new("foo") }
|
||||||
|
|
||||||
describe "#lock" do
|
describe "#lock" do
|
||||||
@ -24,7 +24,7 @@ describe FormulaLock do
|
|||||||
expect { subject.unlock }.not_to raise_error
|
expect { subject.unlock }.not_to raise_error
|
||||||
end
|
end
|
||||||
|
|
||||||
it "unlocks a locked Formula" do
|
it "unlocks when locked" do
|
||||||
subject.lock
|
subject.lock
|
||||||
subject.unlock
|
subject.unlock
|
||||||
|
|
||||||
Loading…
x
Reference in New Issue
Block a user