| Class | Puppet::Network::Handler::FileBucket |
| In: |
lib/puppet/network/handler/filebucket.rb
|
| Parent: | Handler |
Accept files and store them by md5 sum, returning the md5 sum back to the client. Alternatively, accept an md5 sum and return the associated content.
| name | [R] | |
| path | [R] |
# File lib/puppet/network/handler/filebucket.rb, line 48
48: def initialize(hash)
49: if hash.include?(:ConflictCheck)
50: @conflictchk = hash[:ConflictCheck]
51: hash.delete(:ConflictCheck)
52: else
53: @conflictchk = false
54: end
55:
56: if hash.include?(:Path)
57: @path = hash[:Path]
58: hash.delete(:Path)
59: else
60: if defined? Puppet
61: @path = Puppet[:bucketdir]
62: else
63: @path = File.expand_path("~/.filebucket")
64: end
65: end
66:
67: Puppet.settings.use(:filebucket)
68:
69: @name = "Filebucket[#{@path}]"
70: end
this doesn‘t work for relative paths
# File lib/puppet/network/handler/filebucket.rb, line 32
32: def self.paths(base,md5)
33: dir = File.join(md5[0..7].split(""))
34: basedir = File.join(base, dir, md5)
35: return [
36: basedir,
37: File.join(basedir, "contents"),
38: File.join(basedir, "paths")
39: ]
40: end
Accept a file from a client and store it by md5 sum, returning the sum.
# File lib/puppet/network/handler/filebucket.rb, line 74
74: def addfile(contents, path, client = nil, clientip = nil)
75: if client
76: contents = Base64.decode64(contents)
77: end
78: md5 = Digest::MD5.hexdigest(contents)
79:
80: bpath, bfile, pathpath = FileBucket.paths(@path,md5)
81:
82: # If the file already exists, just return the md5 sum.
83: if FileTest.exists?(bfile)
84: # If verification is enabled, then make sure the text matches.
85: if conflict_check?
86: verify(contents, md5, bfile)
87: end
88: return md5
89: end
90:
91: # Make the directories if necessary.
92: unless FileTest.directory?(bpath)
93: Puppet::Util.withumask(0007) do
94: FileUtils.mkdir_p(bpath)
95: end
96: end
97:
98: # Write the file to disk.
99: msg = "Adding %s(%s)" % [path, md5]
100: msg += " from #{client}" if client
101: self.info msg
102:
103: # ...then just create the file
104: Puppet::Util.withumask(0007) do
105: File.open(bfile, File::WRONLY|File::CREAT, 0440) { |of|
106: of.print contents
107: }
108: end
109:
110: # Write the path to the paths file.
111: add_path(path, pathpath)
112:
113: return md5
114: end
Should we check each file as it comes in to make sure the md5 sums match? Defaults to false.
# File lib/puppet/network/handler/filebucket.rb, line 44
44: def conflict_check?
45: @confictchk
46: end
Return the contents associated with a given md5 sum.
# File lib/puppet/network/handler/filebucket.rb, line 117
117: def getfile(md5, client = nil, clientip = nil)
118: bpath, bfile, bpaths = FileBucket.paths(@path,md5)
119:
120: unless FileTest.exists?(bfile)
121: # Try the old flat style.
122: bpath, bfile, bpaths = FileBucket.oldpaths(@path,md5)
123: unless FileTest.exists?(bfile)
124: return false
125: end
126: end
127:
128: contents = nil
129: File.open(bfile) { |of|
130: contents = of.read
131: }
132:
133: if client
134: return Base64.encode64(contents)
135: else
136: return contents
137: end
138: end