class RackWebDAV::FileResourceLock

Attributes

created_at[R]
depth[RW]
kind[RW]
owner[RW]
path[RW]
root[R]
scope[RW]
timeout[RW]
token[RW]
user[RW]

Public Class Methods

explicit_locks(path, croot, args={}) click to toggle source
# File lib/rack-webdav/file_resource_lock.rb, line 33
def explicit_locks(path, croot, args={})
end
explicitly_locked?(path, croot=nil) click to toggle source
# File lib/rack-webdav/file_resource_lock.rb, line 17
def explicitly_locked?(path, croot=nil)
  store = init_pstore(croot)
  !!store.transaction(true){
    store[:paths][path]
  }
end
find_by_path(path, croot=nil) click to toggle source
# File lib/rack-webdav/file_resource_lock.rb, line 39
def find_by_path(path, croot=nil)
  lock = self.class.new(:path => path, :root => croot)
  lock.token.nil? ? nil : lock
end
find_by_token(token, croot=nil) click to toggle source
# File lib/rack-webdav/file_resource_lock.rb, line 44
def find_by_token(token, croot=nil)
  store = init_pstore(croot)
  struct = store.transaction(true){
    store[:tokens][token]
  }
  if !struct
    struct = store.transaction(true) {
      store[:tokens].keys.each { |k| token = k if k.include?(token) }
      store[:tokens][token]
    }
  end

  if struct
    self.new(:path => struct[:path], :root => croot)
  else
    nil
  end
end
generate(path, user, token, croot) click to toggle source
# File lib/rack-webdav/file_resource_lock.rb, line 63
def generate(path, user, token, croot)
  lock = self.new(:root => croot)
  lock.user = user
  lock.path = path
  lock.token = token
  lock.save
  lock
end
implicitly_locked?(path, croot=nil) click to toggle source
# File lib/rack-webdav/file_resource_lock.rb, line 24
def implicitly_locked?(path, croot=nil)
  store = init_pstore(croot)
  !!store.transaction(true){
    store[:paths].keys.detect do |check|
      check.start_with?(path)
    end
  }
end
implict_locks(path) click to toggle source
# File lib/rack-webdav/file_resource_lock.rb, line 36
def implict_locks(path)
end
init_pstore(croot) click to toggle source
# File lib/rack-webdav/file_resource_lock.rb, line 80
def init_pstore(croot)
  path = File.join(croot, '.attribs', 'locks.pstore')
  FileUtils.mkdir_p(File.dirname(path)) unless File.directory?(File.dirname(path))
  store = IS_18 ? PStore.new(path) : PStore.new(path, true)
  store.transaction do
    unless(store[:paths])
      store[:paths] = {}
      store[:tokens] = {}
      store.commit
    end
  end
  store
end
new(args={}) click to toggle source
# File lib/rack-webdav/file_resource_lock.rb, line 95
def initialize(args={})
  @path = args[:path]
  @root = args[:root]
  @owner = args[:owner]
  @store = init_pstore(@root)
  @max_timeout = args[:max_timeout] || 86400
  @default_timeout = args[:max_timeout] || 60
  load_if_exists!
  @new_record = true if token.nil?
end
root() click to toggle source
# File lib/rack-webdav/file_resource_lock.rb, line 76
def root
  @root || '/tmp/rack-webdav'
end
root=(path) click to toggle source
# File lib/rack-webdav/file_resource_lock.rb, line 72
def root=(path)
  @root = path
end

Public Instance Methods

destroy() click to toggle source
# File lib/rack-webdav/file_resource_lock.rb, line 138
def destroy
  @store.transaction do
    @store[:paths].delete(path)
    @store[:tokens].delete(token)
    @store.commit
  end
  nil
end
owner?(user) click to toggle source
# File lib/rack-webdav/file_resource_lock.rb, line 106
def owner?(user)
  user == owner
end
reload() click to toggle source
# File lib/rack-webdav/file_resource_lock.rb, line 110
def reload
  load_if_exists
  self
end
remaining_timeout() click to toggle source
# File lib/rack-webdav/file_resource_lock.rb, line 115
def remaining_timeout
  t = timeout.to_i - (Time.now.to_i - created_at.to_i)
  t < 0 ? 0 : t
end
save() click to toggle source
# File lib/rack-webdav/file_resource_lock.rb, line 120
def save
  struct = {
    :path => path,
    :token => token,
    :timeout => timeout,
    :depth => depth,
    :created_at => Time.now,
    :owner => owner
  }
  @store.transaction do
    @store[:paths][path] = struct
    @store[:tokens][token] = struct
    @store.commit
  end
  @new_record = false
  self
end

Private Instance Methods

init_pstore(croot=nil) click to toggle source
# File lib/rack-webdav/file_resource_lock.rb, line 164
def init_pstore(croot=nil)
  self.class.init_pstore(croot || @root)
end
load_if_exists!() click to toggle source
# File lib/rack-webdav/file_resource_lock.rb, line 149
def load_if_exists!
  struct = @store.transaction do
    @store[:paths][path]
  end
  if(struct)
    @path = struct[:path]
    @token = struct[:token]
    @timeout = struct[:timeout]
    @depth = struct[:depth]
    @created_at = struct[:created_at]
    @owner = struct[:owner]
  end
  self
end