public
Description: couchdb + ruby + fuse = couchdbfs
Homepage:
Clone URL: git://github.com/niky81/couchdbfs.git
couchdbfs / couchdbfs.rb.old
100644 255 lines (232 sloc) 5.824 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
require 'fusefs'
require 'rubygems'
require 'couchrest'
 
DEBUG = true
 
class FileCouch < CouchRest::Model
  use_database CouchRest.database!('http://localhost:5984/couchfs')
def FileCouch.usedb
   use_database CouchRest.database!('http://localhost:5984/couchfs')
end
view_by :name
view_by :path,
:map => " function(doc) {
            if (doc['couchrest-type'] == 'FileCouch' && doc['path']) {
if(doc['path'] == \"/\")
{
              emit(doc['path'], null);
}
else
{
              emit(doc['path'].replace(/\\/$/, \"\"), null);
            }
}
          }"
 
view_by :base_path,
:map => " function(doc) {
            if (doc['couchrest-type'] == 'FileCouch' && doc['base_path']) {
if(doc['base_path'] == \"/\")
{
              emit(doc['base_path'], null);
}
else
{
              emit(doc['base_path'].replace(/\\/$/, \"\"), null);
            }
}
          }"
view_by :type
end
 
 
class CouchdbDir
  def initialize()
@db = FileCouch.usedb()
@cache = {}
  end
 
  def contents(path)
p "call:contents?(#{path})" if DEBUG
@cache = {}
files = FileCouch.by_path :key => path
ret = []
files.each do |fil|
ret += fil["files"].map {|t| t.sub(/^[D|F]/, "") }
end
@cache[path] = files
return ret
  end
  def directory?(path)
p "call:directory?(#{path})" if DEBUG
name = File.basename(path)
path_only_b = path.sub(/#{name}$/, "")
path_only = path_only_b != "/" ? path_only_b.sub(/\/$/, "") : path_only_b
if @cache[path_only]
files = @cache[path_only]
else
files = FileCouch.by_path :key => path_only
end
trovato = false
    files.each do |fil|
if fil["files"].include?("D#{name}")
trovato = true
end
end
return trovato
  end
 
  def file?(path)
p "call:file?(#{path})" if DEBUG
name = File.basename(path)
path_only_b = path.sub(/#{name}$/, "")
path_only = path_only_b != "/" ? path_only_b.sub(/\/$/, "") : path_only_b
if @cache[path_only] != nil
files = @cache[path_only]
else
files = FileCouch.by_path :key => path_only
end
trovato = false
    files.each do |fil|
if fil["files"].include?("F#{name}")
trovato = true
end
end
return trovato
  end
 
  def touch(path)
    puts "#{path} has been pushed like a button!"
  end
  def read_file(path)
    ret = FileCouch.by_path :key => path
ret.each do |fil|
return @db.fetch_attachment(fil["_id"], fil["name"])
end
  end
  def size(path)
p "call:size(#{path})"
ret = FileCouch.by_path :key => path
ret.each do |fil|
return fil["size"] ? fil["size"] : 10
end
  end
 
  # File writing
  def can_write?(path)
    true
  end
  def write_to(path,body)
name = File.basename(path)
path_only = path.sub(/#{name}$/, "")
body ||= ""
ret = FileCouch.by_path :key => path
ret.each do |r|
r.destroy
end
    ret = FileCouch.new({"path" => path, "base_path" => path_only, "_attachments" => { name => {"data" => body}}, "name" => name, "size" => body.length})
ret.save
ret3 = {}
if path_only != "/"
ret2 = FileCouch.by_path :key => path_only.sub(/\/$/, "")
else
ret2 = FileCouch.by_path :key => path_only
end
ret3 = nil
if ret2.nitems == 0
name2 = File.basename(path_only)
path_only2 = path_only.sub(/\/#{name2}$/, "")
if path_only2 == ""
path_only2 = "/"
end
     ret3 = FileCouch.new({"path" => path_only, "base_path" => path_only2, "name" => name2, "files" => []})
else
ret2.each do |r|
if r["files"].nitems < 1000
ret3 = r
end
end
if ret3 == nil
#new
     ret3 = FileCouch.new({"path" => path_only, "base_path" => path_only2, "name" => name2, "files" => []})
end
end
    ret3["files"] ||= []
ret3["files"] << "F#{name}"
ret3.save
  end
 
  # Delete a file
  def can_delete?(path)
true
  end
  def delete(path)
name = File.basename(path)
path_only_b = path.sub(/#{name}$/, "")
path_only = path_only_b != "/" ? path_only_b.sub(/\/$/, "") : path_only_b
    
ret = FileCouch.by_path :key => path
ret.each do |fil|
fil.destroy
end
ret = FileCouch.by_path :key => path_only
ret.each do |dir|
if dir["files"].include?("D#{name}")
dir["files"].delete("D#{name}")
end
end
  end
 
 
  def can_mkdir?(path)
true
  end
  def mkdir(path)
name = File.basename(path)
path_only = path.sub(/#{name}$/, "")
    ret = FileCouch.new({"path" => path, "base_path" => path_only, "name" => name, "files" => []})
ret.save
ret3 = {}
if path_only != "/"
ret2 = FileCouch.by_path :key => path_only.sub(/\/$/, "")
else
ret2 = FileCouch.by_path :key => path_only
end
ret3 = nil
if ret2.nitems == 0
name2 = File.basename(path_only)
path_only2 = path_only.sub(/\/#{name2}$/, "")
if path_only2 == ""
path_only2 = "/"
end
     ret3 = FileCouch.new({"path" => path_only, "base_path" => path_only2, "name" => name2, "files" => []})
else
ret2.each do |r|
if r["files"].nitems < 100
ret3 = r
end
end
if ret3 == nil
#new
     ret3 = FileCouch.new({"path" => path_only, "base_path" => path_only2, "name" => name2, "files" => []})
end
end
    ret3["files"] ||= []
ret3["files"] << "D#{name}"
ret3.save
  end
 
  # rmdir
  def can_rmdir?(path)
true
  end
  def rmdir(path)
    ret = FileCouch.by_path :key => path
ret.each do |fil|
fil.destroy
end
  end
 
end
 
if (File.basename($0) == File.basename(__FILE__))
  if (ARGV.size < 1)
    puts "Usage: #{$0} <directory> <options>"
    exit
  end
 
  dirname, yamlfile = ARGV.shift, ARGV.shift
 
  unless File.directory?(dirname)
    puts "Usage: #{dirname} is not a directory."
    exit
  end
 
  root = CouchdbDir.new()
 
  # Set the root FuseFS
  FuseFS.set_root(root)
 
  FuseFS.mount_under(dirname, *ARGV)
 
  FuseFS.run # This doesn't return until we're unmounted.
end