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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
|
#!/usr/bin/ruby -w
#--------------------
# accept and serve files
#
# $Id$
require 'webrick'
#require 'webrick/https'
require 'xmlrpc/server'
require 'xmlrpc/client'
#require 'webrick/httpstatus'
require 'facter'
require 'digest/md5'
require 'base64'
class BucketError < RuntimeError; end
module FileBucket
DEFAULTPORT = 8139
# this doesn't work for relative paths
def FileBucket.mkdir(dir)
if FileTest.exist?(dir)
return false
else
tmp = dir.sub(/^\//,'')
path = [File::SEPARATOR]
tmp.split(File::SEPARATOR).each { |dir|
path.push dir
unless FileTest.exist?(File.join(path))
Dir.mkdir(File.join(path))
end
}
return true
end
end
def FileBucket.paths(base,md5)
return [
File.join(base, md5),
File.join(base, md5, "contents"),
File.join(base, md5, "paths")
]
end
#---------------------------------------------------------------
class Bucket
def initialize(hash)
# build our AST
if hash.include?(:ConflictCheck)
@conflictchk = hash[:ConflictCheck]
hash.delete(:ConflictCheck)
else
@conflictchk = true
end
if hash.include?(:Path)
@bucket = hash[:Path]
hash.delete(:Path)
else
if defined? Puppet
@bucket = Puppet[:bucketdir]
else
@bucket = File.expand_path("~/.filebucket")
end
end
# XXX this should really be done using Puppet::Type instances...
FileBucket.mkdir(@bucket)
end
# accept a file from a client
def addfile(string,path)
#puts "entering addfile"
contents = Base64.decode64(string)
#puts "string is decoded"
md5 = Digest::MD5.hexdigest(contents)
#puts "md5 is made"
bpath, bfile, pathpath = FileBucket.paths(@bucket,md5)
# if it's a new directory...
if FileBucket.mkdir(bpath)
# ...then just create the file
#puts "creating file"
File.open(bfile, File::WRONLY|File::CREAT) { |of|
of.print contents
}
#puts "File is created"
else # if the dir already existed...
# ...we need to verify that the contents match the existing file
if @conflictchk
unless FileTest.exists?(bfile)
raise(BucketError,
"No file at %s for sum %s" % [bfile,md5], caller)
end
curfile = ""
File.open(bfile) { |of|
curfile = of.read
}
# if the contents don't match, then we've found a conflict
# unlikely, but quite bad
if curfile != contents
raise(BucketError,
"Got passed new contents for sum %s" % md5, caller)
end
end
#puts "Conflict check is done"
end
# in either case, add the passed path to the list of paths
paths = nil
addpath = false
if FileTest.exists?(pathpath)
File.open(pathpath) { |of|
paths = of.readlines
}
# unless our path is already there...
unless paths.include?(path)
addpath = true
end
else
addpath = true
end
#puts "Path is checked"
# if it's a new file, or if our path isn't in the file yet, add it
if addpath
File.open(pathpath, File::WRONLY|File::CREAT|File::APPEND) { |of|
of.puts path
}
#puts "Path is added"
end
return md5
end
def getfile(md5)
bpath, bfile, bpaths = FileBucket.paths(@bucket,md5)
unless FileTest.exists?(bfile)
return false
end
contents = nil
File.open(bfile) { |of|
contents = of.read
}
return Base64.encode64(contents)
end
private
def on_init
@default_namespace = 'urn:filebucket-server'
add_method(self, 'addfile', 'string', 'path')
add_method(self, 'getfile', 'md5')
end
def cert(filename)
OpenSSL::X509::Certificate.new(File.open(File.join(@dir, filename)) { |f|
f.read
})
end
def key(filename)
OpenSSL::PKey::RSA.new(File.open(File.join(@dir, filename)) { |f|
f.read
})
end
end
#---------------------------------------------------------------
class BucketWebserver < WEBrick::HTTPServer
def initialize(hash)
unless hash.include?(:Port)
hash[:Port] = FileBucket::DEFAULTPORT
end
servlet = XMLRPC::WEBrickServlet.new
@bucket = FileBucket::Bucket.new(hash)
#puts @bucket
servlet.add_handler("bucket",@bucket)
super
self.mount("/RPC2", servlet)
end
end
class BucketClient < XMLRPC::Client
@@methods = [ :addfile, :getfile ]
@@methods.each { |method|
self.send(:define_method,method) { |*args|
begin
call("bucket.%s" % method.to_s,*args)
rescue => detail
#puts detail
end
}
}
def initialize(hash)
hash[:Path] ||= "/RPC2"
hash[:Server] ||= "localhost"
hash[:Port] ||= FileBucket::DEFAULTPORT
super(hash[:Server],hash[:Path],hash[:Port])
end
end
class Dipper
def initialize(hash)
if hash.include?(:Server)
@bucket = FileBucket::BucketClient.new(
:Server => hash[:Server]
)
elsif hash.include?(:Bucket)
@bucket = hash[:Bucket]
elsif hash.include?(:Path)
@bucket = FileBucket::Bucket.new(
:Path => hash[:Path]
)
end
end
def backup(file)
unless FileTest.exists?(file)
raise(BucketError, "File %s does not exist" % file, caller)
end
contents = File.open(file) { |of| of.read }
string = Base64.encode64(contents)
#puts "string is created"
sum = @bucket.addfile(string,file)
#puts "file %s is added" % file
return sum
end
def restore(file,sum)
restore = true
if FileTest.exists?(file)
contents = File.open(file) { |of| of.read }
cursum = Digest::MD5.hexdigest(contents)
# if the checksum has changed...
# this might be extra effort
if cursum == sum
restore = false
end
end
if restore
#puts "Restoring %s" % file
newcontents = Base64.decode64(@bucket.getfile(sum))
newsum = Digest::MD5.hexdigest(newcontents)
File.open(file,File::WRONLY|File::TRUNC) { |of|
of.print(newcontents)
}
#puts "Done"
return newsum
else
return nil
end
end
end
#---------------------------------------------------------------
end
|