Module: FilePool
- Defined in:
- lib/file_pool.rb,
lib/file_pool/version.rb
Defined Under Namespace
Classes: InvalidFileId
Constant Summary collapse
- VERSION =
Version 0.3.2
"0.3.2"
Class Method Summary collapse
-
.add(path) ⇒ Object
Add a file to the file pool.
-
.add!(path) ⇒ Object
Add a file to the file pool.
-
.path(fid, options = {}) ⇒ Object
Return the file’s path corresponding to the passed file ID, no matter if it exists or not.
-
.remove(fid) ⇒ Object
Remove a previously added file by its ID.
-
.remove!(fid) ⇒ Object
Remove a previously added file by its ID.
-
.setup(root, options = {}) ⇒ Object
Setup the root directory of the file pool and configure encryption.
-
.stat ⇒ Object
Returns some statistics about the current pool.
Class Method Details
.add(path) ⇒ Object
Add a file to the file pool.
Same as FilePool.add!, but doesn’t throw exceptions.
Parameters:
- source (String)
-
path of the file to add.
Return Value:
-
String containing a new unique ID for the file added.
-
falsewhen the file could not be stored.
90 91 92 93 94 95 |
# File 'lib/file_pool.rb', line 90 def self.add path self.add!(path) rescue Exception => ex return false end |
.add!(path) ⇒ Object
Add a file to the file pool.
Creates hard-links (ln) when file at path is on same file system as pool, otherwise copies it. When dealing with large files the latter should be avoided, because it takes more time and space.
Throws standard file exceptions when unable to store the file. See also FilePool.add to avoid it.
Parameters:
- path (String)
-
path of the file to add.
Return Value:
-
String containing a new unique ID for the file added.
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/file_pool.rb', line 57 def self.add! path newid = uuid target = path newid if @@crypted_mode FileUtils.mkpath(id2dir_secured newid) path = crypt(path) else FileUtils.mkpath(id2dir newid) end FileUtils.link(path, target) return newid rescue Errno::EXDEV FileUtils.copy(path, target) return newid end |
.path(fid, options = {}) ⇒ Object
Return the file’s path corresponding to the passed file ID, no matter if it exists or not. In encrypting mode the file is first decrypted and the returned path will point to a temporary location of the decrypted file.
To get the path of the encrypted file pass :decrypt => false, as an option.
Parameters:
- fid (String)
-
File ID which was generated by a previous #add operation.
- options (Hash)
-
:decrypt (true,false) In encryption mode don’t decrypt, but return the encrypted file’s path. Defaults to
true.
Return Value:
-
String, absolute path of the file in the pool or to temporary location if it was decrypted.
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 |
# File 'lib/file_pool.rb', line 115 def self.path fid, ={} [:decrypt] = true unless [:decrypt] == false raise InvalidFileId unless valid?(fid) # file present in pool? if File.file?(id2dir_secured(fid) + "/#{fid}") # present in secured tree if @@crypted_mode if [:decrypt] # return path of decrypted file (tmp path) decrypt id2dir_secured(fid) + "/#{fid}" else id2dir_secured(fid) + "/#{fid}" end else id2dir_secured(fid) + "/#{fid}" end elsif File.file?(id2dir(fid) + "/#{fid}") # present in plain tree id2dir(fid) + "/#{fid}" else # not present if @@crypted_mode id2dir_secured(fid) + "/#{fid}" else id2dir(fid) + "/#{fid}" end end end |
.remove(fid) ⇒ Object
Remove a previously added file by its ID. Same as FilePool.remove!, but doesn’t throw exceptions.
Parameters:
- fid (String)
-
File ID which was generated by a previous #add operation.
Return Value:
-
Boolean,
trueif file was removed successfully,falseelse
170 171 172 173 174 |
# File 'lib/file_pool.rb', line 170 def self.remove fid self.remove! fid rescue Exception => ex return false end |
.remove!(fid) ⇒ Object
Remove a previously added file by its ID. Same as FilePool.remove, but throws exceptions on failure.
Parameters:
- fid (String)
-
File ID which was generated by a previous #add operation.
154 155 156 |
# File 'lib/file_pool.rb', line 154 def self.remove! fid FileUtils.rm path(fid, :decrypt => false) end |
.setup(root, options = {}) ⇒ Object
Setup the root directory of the file pool and configure encryption
Parameters:
- root (String)
-
absolute path of the file pool’s root directory under which all files will be stored.
- config_file_path (String)
-
path to the config file of the filepool.
- options (Hash)
-
:secrets_file (String) path to file containing key and IV for encryption (if omitted FilePool does not encrypt/decrypt). If file is not present, the file is initialized with a new random key and IV.
-
:encryption_block_size (Integer) sets the block size for encryption/decryption in bytes. Larger blocks need more memory and less time (less IO). Defaults to 1’048’576 (1 MiB).
-
29 30 31 32 33 34 35 36 37 |
# File 'lib/file_pool.rb', line 29 def self.setup root, ={} unless(unknown = .keys - [:encryption_block_size, :secrets_file]).empty? puts "FilePool Warning: unknown option(s) passed to #setup: #{unknown.inspect}" end @@root = root @@crypted_mode = false @@block_size = [:encryption_block_size] || (1024*1024) configure [:secrets_file] end |
.stat ⇒ Object
Returns some statistics about the current pool. (It may be slow if the pool contains very many files as it computes them from scratch.)
Return Value
-
Hash with keys
- :total_number (Integer)
-
Number of files in pool
- :total_size (Integer)
-
Total number of bytes of all files
- :median_size (Float)
-
Median of file sizes (most frequent size)
- :last_add (Time)
-
Time and Date of last add operation
192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/file_pool.rb', line 192 def self.stat all_files = Dir.glob("#{root}_secured/*/*/*/*") all_files << Dir.glob("#{root}/*/*/*/*") all_stats = all_files.map{|f| File.stat(f) } { :total_size => all_stats.inject(0){|sum,stat| sum+=stat.size}, :median_size => median(all_stats.map{|stat| stat.size}), :file_number => all_files.length, :last_add => all_stats.map{|stat| stat.ctime}.max } end |