Class: DAV4Rack::FileResourceLock

Inherits:
Object
  • Object
show all
Defined in:
lib/dav4rack/file_resource_lock.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ FileResourceLock

Returns a new instance of FileResourceLock.



85
86
87
88
89
90
91
92
93
94
# File 'lib/dav4rack/file_resource_lock.rb', line 85

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

Instance Attribute Details

#created_atObject (readonly)

Returns the value of attribute created_at.



13
14
15
# File 'lib/dav4rack/file_resource_lock.rb', line 13

def created_at
  @created_at
end

#depthObject

Returns the value of attribute depth.



8
9
10
# File 'lib/dav4rack/file_resource_lock.rb', line 8

def depth
  @depth
end

#kindObject

Returns the value of attribute kind.



11
12
13
# File 'lib/dav4rack/file_resource_lock.rb', line 11

def kind
  @kind
end

#ownerObject

Returns the value of attribute owner.



12
13
14
# File 'lib/dav4rack/file_resource_lock.rb', line 12

def owner
  @owner
end

#pathObject

Returns the value of attribute path.



5
6
7
# File 'lib/dav4rack/file_resource_lock.rb', line 5

def path
  @path
end

#rootObject (readonly)

Returns the value of attribute root.



14
15
16
# File 'lib/dav4rack/file_resource_lock.rb', line 14

def root
  @root
end

#scopeObject

Returns the value of attribute scope.



10
11
12
# File 'lib/dav4rack/file_resource_lock.rb', line 10

def scope
  @scope
end

#timeoutObject

Returns the value of attribute timeout.



7
8
9
# File 'lib/dav4rack/file_resource_lock.rb', line 7

def timeout
  @timeout
end

#tokenObject

Returns the value of attribute token.



6
7
8
# File 'lib/dav4rack/file_resource_lock.rb', line 6

def token
  @token
end

#userObject

Returns the value of attribute user.



9
10
11
# File 'lib/dav4rack/file_resource_lock.rb', line 9

def user
  @user
end

Class Method Details

.explicit_locks(path, croot, args = {}) ⇒ Object



33
34
# File 'lib/dav4rack/file_resource_lock.rb', line 33

def explicit_locks(path, croot, args={})
end

.explicitly_locked?(path, croot = nil) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
20
21
22
# File 'lib/dav4rack/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) ⇒ Object



36
37
38
39
# File 'lib/dav4rack/file_resource_lock.rb', line 36

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) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/dav4rack/file_resource_lock.rb', line 41

def find_by_token(token, croot=nil)
  store = init_pstore(croot)
  struct = store.transaction(true){
    store[:tokens][token]
  }
  if(tok)
    self.class.new(:path => struct[:path], :root => croot)
  else
    nil
  end
end

.generate(path, user, token, croot) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/dav4rack/file_resource_lock.rb', line 53

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) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
27
28
29
30
31
# File 'lib/dav4rack/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

.init_pstore(croot) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/dav4rack/file_resource_lock.rb', line 70

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

.rootObject



66
67
68
# File 'lib/dav4rack/file_resource_lock.rb', line 66

def root
  @root || '/tmp/dav4rack'
end

.root=(path) ⇒ Object



62
63
64
# File 'lib/dav4rack/file_resource_lock.rb', line 62

def root=(path)
  @root = path
end

Instance Method Details

#destroyObject



128
129
130
131
132
133
134
135
# File 'lib/dav4rack/file_resource_lock.rb', line 128

def destroy
  @store.transaction do
    @store[:paths].delete(path)
    @store[:tokens].delete(token)
    @store.commit
  end
  nil
end

#owner?(user) ⇒ Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/dav4rack/file_resource_lock.rb', line 96

def owner?(user)
  user == owner
end

#reloadObject



100
101
102
103
# File 'lib/dav4rack/file_resource_lock.rb', line 100

def reload
  load_if_exists
  self
end

#remaining_timeoutObject



105
106
107
108
# File 'lib/dav4rack/file_resource_lock.rb', line 105

def remaining_timeout
  t = timeout.to_i - (Time.now.to_i - created_at.to_i)
  t < 0 ? 0 : t
end

#saveObject



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/dav4rack/file_resource_lock.rb', line 110

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