Class: BC3::Folder
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
-
#dirname ⇒ Object
(also: #basename)
readonly
Name of the folder.
-
#ex_link_path ⇒ Object
Part of the Extended Header Subtypes, see BC3::SnapshotParser#parse_extended_header_subtypes.
-
#ex_utf ⇒ Object
Part of the Extended Header Subtypes, see BC3::SnapshotParser#parse_extended_header_subtypes.
-
#snapshotpath ⇒ Object
readonly
Path (location) in snapshots.
-
#timestamp ⇒ Object
readonly
timestamp.
Class Method Summary collapse
-
.new_by_dirname(dirname) ⇒ Object
Create a folder from file system.
-
.newh(data) ⇒ Object
Create a folder from a hash.
Instance Method Summary collapse
-
#<<(content) ⇒ Object
Add content to folder.
-
#add_with_path(path, element) ⇒ Object
Add an element with a given path.
-
#bcss ⇒ Object
Prepare for snapshot (bcss-file).
-
#each(*options) ⇒ Object
Loop on content of the folder and return path + object.
-
#initialize(dirname, timestamp = Time.now, attributes = nil) ⇒ Folder
constructor
A new instance of Folder.
- #inspect ⇒ Object
-
#mixin(folder) ⇒ Object
Merge two folders.
-
#to_hash ⇒ Object
Return the content in a hash.
Methods included from Helper
crc32, #fixnum2int16, #fixnum2int32, #fixnum2int64, #shortstring2bcss
Constructor Details
#initialize(dirname, timestamp = Time.now, attributes = nil) ⇒ Folder
Returns a new instance of Folder.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/bc3/folder.rb', line 10 def initialize( dirname, = Time.now, attributes = nil ) @dirname = dirname $log.debug("Create folder #{@dirname}") raise ArgumentError, "Dirname no string #{@dirname.inspect}" unless @dirname.is_a?(String) @timestamp = || Time.now raise ArgumentError, "No Timestamp for Folder" unless @timestamp.is_a?(Time) @attributes = attributes || Attrib::Directory @content = {} @snapshotpath = {} end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
92 93 94 |
# File 'lib/bc3/folder.rb', line 92 def attributes @attributes end |
#dirname ⇒ Object (readonly) Also known as: basename
Name of the folder
85 86 87 |
# File 'lib/bc3/folder.rb', line 85 def dirname @dirname end |
#ex_link_path ⇒ Object
Part of the Extended Header Subtypes, see BC3::SnapshotParser#parse_extended_header_subtypes
96 97 98 |
# File 'lib/bc3/folder.rb', line 96 def ex_link_path @ex_link_path end |
#ex_utf ⇒ Object
Part of the Extended Header Subtypes, see BC3::SnapshotParser#parse_extended_header_subtypes
94 95 96 |
# File 'lib/bc3/folder.rb', line 94 def ex_utf @ex_utf end |
#snapshotpath ⇒ Object (readonly)
Path (location) in snapshots
89 90 91 |
# File 'lib/bc3/folder.rb', line 89 def snapshotpath @snapshotpath end |
#timestamp ⇒ Object (readonly)
timestamp
91 92 93 |
# File 'lib/bc3/folder.rb', line 91 def @timestamp end |
Class Method Details
.new_by_dirname(dirname) ⇒ Object
Create a folder from file system.
The argument must contain:
-
dirname
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/bc3/folder.rb', line 65 def self.new_by_dirname( dirname ) $log.debug("Build folder #{dirname} from file system") #The timestamp must be 'local time' -> ignore local time zone for generation folder = new( dirname, ::File.mtime(dirname) + Time.now.gmt_offset ) #fixme: attrib Dir.chdir(dirname){ #feature idea: Define Pattern as method parameter?? Dir['*'].each{|f| if ::File.directory?(f) folder << Folder.new_by_dirname(f) elsif ::File.exist?(f) folder << File.new_by_filename(f) else raise ArgumentError, "#{f} not found in #{dirname}" end } } folder end |
.newh(data) ⇒ Object
Create a folder from a hash.
The argument must contain:
-
dirname
It may contain:
-
timestamp
-
content (array of folders and files)
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/bc3/folder.rb', line 34 def self.newh( data ) raise ArgumentError, "No hash given" unless data.is_a?(Hash) raise ArgumentError, "dirname missing" unless data.has_key?(:dirname) if data.has_key?(:content) raise ArgumentError, "Content is no array" unless data[:content].is_a?(Array) end $log.debug("Build folder #{data[:dirname]} from hash") (data.keys - [:dirname, :content, :timestamp, :attributes]).each{|key| $log.warn("Undefined key #{key} for #{self.inspect}" ) } folder = new( data[:dirname], data[:timestamp], data[:attributes] ) data[:content].each{| element | if element.has_key?(:dirname) folder << Folder.newh(element) elsif element.has_key?(:filename) folder << File.new(element) else raise ArgumentError, "element without dir/filename" end } if data[:content] folder end |
Instance Method Details
#<<(content) ⇒ Object
Add content to folder. The ‘content’ must support the bcss-method.
Content may be:
-
another folder (if pathes are similar to existing folder: see #mixin)
-
a file
If another object with similar name exists, it will be overwritten.
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 |
# File 'lib/bc3/folder.rb', line 108 def << (content) #Check for BC3::File or BC3::Folder if ! ( content.respond_to?( :bcss ) and content.respond_to?( :basename ) ) $log.error("Add unknown datatype as content #{content.inspect}") raise ArgumentError, "Add unknown datatype as content #{content.inspect}" end #reset index in relatd snapshot. self.snapshotpath.each{|snapshot, path| snapshot.reset_index } if @content[content.basename] #content already available #Target and new element are both folders? if @content[content.basename].respond_to? :dirname and content.respond_to? :dirname $log.debug("Mix into folder #{content.basename} in folder #{self.inspect}") #return the folder, where it is mixed in content = @content[content.basename].mixin(content) else $log.info("Overwrite #{content.basename} in folder #{self.inspect}") @content[content.basename] = content end else #add new content @content[content.basename] = content $log.debug("Add to folder #{self.inspect}: #{content.inspect}") if $log.debug? end content #return the folder, where it is mixed in end |
#add_with_path(path, element) ⇒ Object
Add an element with a given path.
Folders will be created for the complete path if necessary.
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/bc3/folder.rb', line 149 def add_with_path(path, element) path = path.split(%r{[\\\/]}) unless path.is_a?(Array) if path.empty? self << element else #subpath #Search target folder target = nil @content.each{|name, folder| if name == path.first target = folder break #target found end } self << target = Folder.new(path.first) unless target path.shift target.add_with_path( path, element ) end element end |
#bcss ⇒ Object
Prepare for snapshot (bcss-file).
Each record starts with a single UByte ID value and then the data defined below.
ID_DIRECTORY (0x01)
Represents a directory on the system, or an expanded archive file.
Name : ShortString
Last Modified : FileTime
DOS Attributes : UInt32
ID_DIRECTORY_END (0xFF)
Represents the end of a directory listing. No data.
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 278 |
# File 'lib/bc3/folder.rb', line 242 def bcss() bcss = "".force_encoding('BINARY') bcss << 1 bcss << shortstring2bcss(@dirname) bcss << fixnum2int64(@timestamp.time2ad) #DOS Attributes bcss << @attributes bcss << 0 bcss << 0 bcss << 0 @content.each{|key, data| bcss << data.bcss } bcss << 255 if ex_utf $log.fatal("Untested ex_utf") bcss << 'xxxx' bcss << 4 #Extended Header Subtypes =begin EX_UTF8 (0x01) UTF-8 encoded filename for the ID_DIRECTORY that immediately preceeded this header. The length is given in the ID_EXTENDED header and the data is a char[]. If the .bcss header flags indicate that the data is not UTF-8 and the source path is included this can be included as the first record in the file in order to give a UTF-8 version of the source path. =end bcss << 1 bcss << ex_utf.bytesize #~ p ex_utf.bytesize #~ p ex_utf bcss << shortstring2bcss(ex_utf.dup.force_encoding('binary')) end bcss end |
#each(*options) ⇒ Object
Loop on content of the folder and return path + object.
Options may be
-
:files: return Files
-
:folder: return Folders
-
:recursive: return also subdirectories. The result is not nested, pathes are complete
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 |
# File 'lib/bc3/folder.rb', line 177 def each(*) = [:folders, :files ] if .empty? #take all, but not recursive extract = {} #~ @content.each{|key, content| #~ @content.sort_by{|key, x| x.is_a?(Folder) ? 'a' : 'b' + x.basename }.each{|key, content| @content.sort_by{|key, x| ( x.is_a?(Folder) ? 'a%s/' : 'b%s' ) % x.basename }.each{|key, content| case content when File extract[key] = content if .include?(:files) when Folder extract[key+'/'] = content if .include?(:folders) else raise "Internal error" end if .include?(:recursive) and content.is_a?(Folder) content.each(*).each{|subkey, subcontent| extract[[key, subkey].join('/')] = subcontent } end } if block_given? extract.each{|key, content| yield key, content } else extract end end |
#inspect ⇒ Object
209 210 211 |
# File 'lib/bc3/folder.rb', line 209 def inspect() "<BC3::Folder #{@dirname}>" end |
#mixin(folder) ⇒ Object
Merge two folders. The content of a folder is added to the actual content.
138 139 140 141 142 143 |
# File 'lib/bc3/folder.rb', line 138 def mixin( folder ) $log.debug("Mix in content to folder #{@dirname} in folder #{self.inspect}") raise ArgumentError, "Add unknown datatype as content #{content.inspect}" unless folder.is_a?(Folder) folder.each{|key, content| self << content } self #return the folder, where it is mixed in end |
#to_hash ⇒ Object
Return the content in a hash.
215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/bc3/folder.rb', line 215 def to_hash() #feature idea: Optional array with needed keys? { dirname: @dirname, timestamp: @timestamp, attributes: @attributes, #use each to get a sorted output #~ content: @content.values.map{|x| x.to_hash } content: self.each.values.map{|x| x.to_hash } } end |