public
Description: couchdb + ruby + fuse = couchdbfs
Homepage:
Clone URL: git://github.com/niky81/couchdbfs.git
couchdbfs / couchdbfs_lib.rb
100644 181 lines (167 sloc) 3.799 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
require 'fusefs'
require 'rubygems'
require 'couchrest'
 
DEBUG = false #true
 
class CouchdbDir
  def initialize()
   @db = CouchRest.database!("http://127.0.0.1:5984/couchfs")
create_view()
create_root()
  end
 
def create_view
      @view = {}
@view['path'] = {'map' => "function(doc) {
if (doc['path']) {
if(doc['path'] == \"/\")
{
emit(doc['path'], null);
}
else
{
emit(doc['path'].replace(/\\/$/, \"\"), null);
}
}
}"
}
 
@view['base_path'] = {'map' => "function(doc) {
if (doc['base_path']) {
if(doc['base_path'] == \"/\")
{
emit(doc['base_path'], null);
}
else
{
emit(doc['base_path'].replace(/\\/$/, \"\"), null);
}
}
}"
}
      @db.save({
        "_id" => "_design/doc",
        :views => @view
      })
end
 
#create the root dir
def create_root
if @db.view("doc/path", :key => "/")['rows'].nitems == 0
ret = {"path" => "/", "name" => "/", "files" => []}
@db.save(ret)
end
end
 
  def contents(path)
p "call:contents?(#{path})" if DEBUG
files = @db.view("doc/base_path", :key => path)
p files if DEBUG
ret = []
files['rows'].each do |fil|
ret << @db.get(fil['id'])['name']
end
return ret
  end
  def directory?(path)
dir = @db.view("doc/path", :key => path)['rows']
if dir.nitems > 0
return !@db.get(dir[0]['id']).key?("size")
else
return false
end
  end
 
  def file?(path)
dir = @db.view("doc/path", :key => path)['rows']
if dir.nitems > 0
return @db.get(dir[0]['id']).key?("size")
else
return false
end
  end
 
  def touch(path)
    write_to(path, "")
  end
  def read_file(path)
    ret = @db.view("doc/path", :key => path)
if ret['rows'].nitems > 0
file = @db.get(ret['rows'][0]['id'])
if file.key?("size")
return @db.fetch_attachment(file["_id"], file["name"])
end
end
return false
  end
  def size(path)
ret = @db.view("doc/path", :key => path)
if ret['rows'].nitems > 0
file = @db.get(ret['rows'][0]['id'])
if file.key?("size")
return file["size"]
else
#directory
return 0
end
end
return 0
  end
 
  # File writing
  def can_write?(path)
return @db.view("doc/path", :key => path)['rows'].nitems == 0
  end
def write_to(path, body)
name = File.basename(path)
path_only = path.sub(/#{name}$/, "")
    file = {"path" => path, "base_path" => path_only, "_attachments" => { name => {"data" => body}}, "name" => name, "size" => body.length, "type" => "f"}
@db.save(file)
end
 
  # Delete a file
  def can_delete?(path)
rows = @db.view("doc/path", :key => path)['rows']
if rows.nitems > 0
file = @db.get(rows[0]['id'])
if file.key?("size")
return true
end
end
false
  end
  def delete(path)
row = @db.view("doc/path", :key => path)['rows']
if row.nitems > 0
file = @db.get(row[0]['id'])
@db.delete(file)
end
  end
 
  def can_mkdir?(path)
return @db.view("doc/path", :key => path)['rows'].nitems == 0
  end
  def mkdir(path)
name = File.basename(path)
path_only = path.sub(/#{name}$/, "")
    dir = {"path" => path, "base_path" => path_only, "name" => name}
@db.save(dir)
end
 
  # rmdir
  def can_rmdir?(path)
if path != "/"
if @db.view("doc/base_path", :key => path)['rows'].nitems == 0
row = @db.view("doc/path", :key => path)['rows']
if row.nitems > 0
file = @db.get(row[0]['id'])
if !file.key?('size')
return true
end
end
end
end
return false
  end
  def rmdir(path)
    rows = @db.view("doc/path", :key => path)['rows']
if rows.nitems > 0
dir = @db.get(rows[0]['id'])
if !dir.key?("size")
@db.delete(dir)
end
end
  end
 
def delete_db
@db.delete!
end
end