Class: SMPTool::VirtualVolume::VolumeData
- Inherits:
-
SimpleDelegator
- Object
- SimpleDelegator
- SMPTool::VirtualVolume::VolumeData
- Defined in:
- lib/smp_tool/virtual_volume/volume_data.rb
Overview
Provides set of methods to work with volume’s data.
Instance Method Summary collapse
-
#calc_n_free_clusters ⇒ Integer
Calculate total number of free clusters.
-
#f_delete(file_id) ⇒ String
Delete file from the virtual volume.
-
#f_push(file) ⇒ String
Append a file.
-
#f_rename(old_id, new_id) ⇒ Array<String>
Rename file on the virtual volume.
-
#initialize(obj, extra_word) ⇒ VolumeData
constructor
A new instance of VolumeData.
- #push_empty_entry(n_free_clusters) ⇒ Object
-
#resize(n_clusters) ⇒ Integer
<Description>.
- #snapshot ⇒ Object
-
#squeeze ⇒ Integer
Consolidate all free space at the end of the volume.
Constructor Details
#initialize(obj, extra_word) ⇒ VolumeData
Returns a new instance of VolumeData.
9 10 11 12 13 14 |
# File 'lib/smp_tool/virtual_volume/volume_data.rb', line 9 def initialize(obj, extra_word) @obj = obj @extra_word = extra_word super(obj) end |
Instance Method Details
#calc_n_free_clusters ⇒ Integer
Calculate total number of free clusters.
90 91 92 93 |
# File 'lib/smp_tool/virtual_volume/volume_data.rb', line 90 def calc_n_free_clusters self.select(&:empty_entry?) .sum(&:n_clusters) end |
#f_delete(file_id) ⇒ String
Delete file from the virtual volume.
57 58 59 60 61 62 63 64 65 |
# File 'lib/smp_tool/virtual_volume/volume_data.rb', line 57 def f_delete(file_id) idx = _find_idx(file_id.radix50) _raise_file_not_found(file_id.print_ascii) unless idx self[idx].clean file_id.print_ascii end |
#f_push(file) ⇒ String
Append a file.
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/smp_tool/virtual_volume/volume_data.rb', line 103 def f_push(file) _raise_already_exists(file.print_ascii_filename) if _already_exists?(file.filename) # We're starting from the end of the array, since free space tend to locate # at the end of the volume (esp. after the 'squeeze' command). idx = index(reverse_each.detect { |e| e.n_clusters >= file.n_clusters && e.empty_entry? }) unless idx raise ArgumentError, "no free space found to fit the file (try to squeeze, delete files or allocate more clusters)" end _f_push(file, idx) file.print_ascii_filename end |
#f_rename(old_id, new_id) ⇒ Array<String>
Rename file on the virtual volume.
34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/smp_tool/virtual_volume/volume_data.rb', line 34 def f_rename(old_id, new_id) _raise_already_exists(new_id.print_ascii) if _already_exists?(new_id.radix50) idx = _find_idx(old_id.radix50) _raise_file_not_found(old_id.print_ascii) unless idx self[idx].rename(new_id.radix50) [old_id.print_ascii, new_id.print_ascii] end |
#push_empty_entry(n_free_clusters) ⇒ Object
120 121 122 123 124 |
# File 'lib/smp_tool/virtual_volume/volume_data.rb', line 120 def push_empty_entry(n_free_clusters) push(_new_empty_entry(n_free_clusters)) self end |
#resize(n_clusters) ⇒ Integer
<Description>
135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/smp_tool/virtual_volume/volume_data.rb', line 135 def resize(n_clusters) n_free_clusters = calc_n_free_clusters diff = n_free_clusters + n_clusters if reject(&:empty_entry?).empty? && diff < 1 raise ArgumentError, "Can't trim: volume should keep at least one empty/file entry" end reject!(&:empty_entry?) push_empty_entry(diff) unless diff.zero? n_clusters end |
#snapshot ⇒ Object
16 17 18 |
# File 'lib/smp_tool/virtual_volume/volume_data.rb', line 16 def snapshot map(&:snapshot) end |
#squeeze ⇒ Integer
Consolidate all free space at the end of the volume.
73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/smp_tool/virtual_volume/volume_data.rb', line 73 def squeeze n_free_clusters = calc_n_free_clusters return n_free_clusters if n_free_clusters.zero? reject!(&:empty_entry?) push_empty_entry(n_free_clusters) n_free_clusters end |