Class: RackWebDAV::FileResourceLock

Inherits:
Object
  • Object
show all
Defined in:
lib/rack-webdav/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.



95
96
97
98
99
100
101
102
103
104
# 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

Instance Attribute Details

#created_atObject (readonly)

Returns the value of attribute created_at.



13
14
15
# File 'lib/rack-webdav/file_resource_lock.rb', line 13

def created_at
  @created_at
end

#depthObject

Returns the value of attribute depth.



8
9
10
# File 'lib/rack-webdav/file_resource_lock.rb', line 8

def depth
  @depth
end

#kindObject

Returns the value of attribute kind.



11
12
13
# File 'lib/rack-webdav/file_resource_lock.rb', line 11

def kind
  @kind
end

#ownerObject

Returns the value of attribute owner.



12
13
14
# File 'lib/rack-webdav/file_resource_lock.rb', line 12

def owner
  @owner
end

#pathObject

Returns the value of attribute path.



5
6
7
# File 'lib/rack-webdav/file_resource_lock.rb', line 5

def path
  @path
end

#rootObject (readonly)

Returns the value of attribute root.



14
15
16
# File 'lib/rack-webdav/file_resource_lock.rb', line 14

def root
  @root
end

#scopeObject

Returns the value of attribute scope.



10
11
12
# File 'lib/rack-webdav/file_resource_lock.rb', line 10

def scope
  @scope
end

#timeoutObject

Returns the value of attribute timeout.



7
8
9
# File 'lib/rack-webdav/file_resource_lock.rb', line 7

def timeout
  @timeout
end

#tokenObject

Returns the value of attribute token.



6
7
8
# File 'lib/rack-webdav/file_resource_lock.rb', line 6

def token
  @token
end

#userObject

Returns the value of attribute user.



9
10
11
# File 'lib/rack-webdav/file_resource_lock.rb', line 9

def user
  @user
end

Class Method Details

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



33
34
# File 'lib/rack-webdav/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/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) ⇒ Object



39
40
41
42
# 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) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# 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) ⇒ Object



63
64
65
66
67
68
69
70
# 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) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
27
28
29
30
31
# 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) ⇒ Object



36
37
# File 'lib/rack-webdav/file_resource_lock.rb', line 36

def implict_locks(path)
end

.init_pstore(croot) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
# 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

.rootObject



76
77
78
# File 'lib/rack-webdav/file_resource_lock.rb', line 76

def root
  @root || '/tmp/rack-webdav'
end

.root=(path) ⇒ Object



72
73
74
# File 'lib/rack-webdav/file_resource_lock.rb', line 72

def root=(path)
  @root = path
end

Instance Method Details

#destroyObject



138
139
140
141
142
143
144
145
# 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) ⇒ Boolean

Returns:

  • (Boolean)


106
107
108
# File 'lib/rack-webdav/file_resource_lock.rb', line 106

def owner?(user)
  user == owner
end

#reloadObject



110
111
112
113
# File 'lib/rack-webdav/file_resource_lock.rb', line 110

def reload
  load_if_exists
  self
end

#remaining_timeoutObject



115
116
117
118
# 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

#saveObject



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# 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