Class: E2Phokz::App
- Inherits:
-
Object
- Object
- E2Phokz::App
- Defined in:
- lib/e2phokz.rb
Constant Summary collapse
- BUFFER_SIZE =
megabytes
16
Instance Method Summary collapse
- #buffercopy(infile, outfile, cls, s, e) ⇒ Object
- #close_files ⇒ Object
- #connect_stomp ⇒ Object
- #eta_continuous(count) ⇒ Object
- #eta_initial ⇒ Object
-
#initialize(options) ⇒ App
constructor
A new instance of App.
- #parse_args ⇒ Object
- #parse_dumpe2fs ⇒ Object
- #progress_bar(progress) ⇒ Object
- #progress_stomp(progress) ⇒ Object
- #run! ⇒ Object
Constructor Details
#initialize(options) ⇒ App
Returns a new instance of App.
19 20 21 |
# File 'lib/e2phokz.rb', line 19 def initialize() = end |
Instance Method Details
#buffercopy(infile, outfile, cls, s, e) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/e2phokz.rb', line 23 def buffercopy(infile,outfile,cls,s,e) bs = @fsinfo['block_size'].to_i num = (e-s+1)*bs warn "#{cls} from block #{s} to #{e} number of bytes #{num}" unless @debug.nil? maxbuf = BUFFER_SIZE*1024*1024 #16M rest = num count = 0 infile.seek(s*bs,IO::SEEK_SET) while rest > 0 count = rest > maxbuf ? maxbuf : rest rest = rest - count a = Time.now.to_f outfile.write(infile.read(count)) b = Time.now.to_f warn "#{count} bytes read in #{b-a} sec, #{count/(b-a)} bytes/s" unless @debug.nil? eta_continuous count GC.start end end |
#close_files ⇒ Object
233 234 235 236 237 |
# File 'lib/e2phokz.rb', line 233 def close_files @file.close @gzip.close @devzero.close end |
#connect_stomp ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/e2phokz.rb', line 99 def connect_stomp begin config=YAML::load(File.open(CONFIG_FILENAME))['stomp'] rescue warn "Error: cannot load or parse stomp config #{CONFIG_FILENAME}" && exit end begin @stomp_client = Stomp::Client.new(config['user'], config['password'], config['server'], config['port']) rescue warn "Error: cannot connect to stomp server #{config['server']}" && exit end end |
#eta_continuous(count) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/e2phokz.rb', line 63 def eta_continuous count elapsed=Time.now.to_f-@time_start @written = @written + count to_be_written = @fsinfo['block_count'].to_i * @fsinfo['block_size'].to_i progress = (1000.0*@written/to_be_written)/10 # weighted average eta = elapsed/progress*100 - elapsed new_eta = (progress*eta + (100-progress)*@initial_eta)/100 #TODO support for non-gzipped ETA calculation progress_stomp [sprintf("%.2f",progress)+'%',new_eta] [progress.to_s+'%',new_eta] end |
#eta_initial ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/e2phokz.rb', line 76 def eta_initial #TODO support for non-gzipped ETA calculation #constants gzip_free=0.02 gzip_data=0.09 bs=@fsinfo['block_size'].to_i @megs_used=(@fsinfo['block_count'].to_i-@fsinfo['free_blocks'].to_i)*bs/1024/1024 @megs_free=(@fsinfo['free_blocks'].to_i*bs/1024/1024) eta = @megs_used*gzip_data+@megs_free*gzip_free @written = 0 @initial_eta = eta progress_stomp ['0%',eta] ['0%',eta] end |
#parse_args ⇒ Object
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 |
# File 'lib/e2phokz.rb', line 113 def parse_args @time_start=Time.now.to_f if .length < 2 or .length > 3 puts File.open(__FILE__).read.split("\n").collect{|l| l.gsub('# ','') unless l.match(/^# /).nil?}.compact.join("\n") exit end if .length == 3 @channel_name = .last connect_stomp end #test for presence of in file || device begin warn "Warning: input file #{@options.first} is not a block device" unless File::Stat.new(.first).blockdev? @file=File.open(.first,'rb') rescue warn "Error: infile #{@options.first} cannot be open." && exit end #test for presence of out file begin if File::Stat.new([1]).blockdev? warn "Error: I will not overwrite block device #{@options[1]}" else warn "Error: I will not overwrite output file #{@options[1]}" end exit rescue warn "OK" end if [1]=='-' # writing to stdout, no compression @gzip=IO.new(1,"w") else begin if [1].split('.').last == 'gz' #compression required @gzip=Zlib::GzipWriter.open([1]) else @gzip=File.open([1],'wb') end rescue warn "Error: cannot open output file #{@options[1]} for writing." end end @devzero=File.open("/dev/zero",'rb') end |
#parse_dumpe2fs ⇒ Object
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 |
# File 'lib/e2phokz.rb', line 165 def parse_dumpe2fs begin dump = File.popen("dumpe2fs #{@options.first}",'r').read.split("\n") rescue warn "Error: cannot execute dump2efs" && exit end header = 1 @fsinfo={} group_number=0 from=0 to=0 dump.each do |row| if row=='' # header ends with empty row header = 0 #we have processed heade, so we can count ETA now eta_initial next end if header == 1 #we are processing header to @fsinfo hash fields = row.split(':') varname = fields.shift.split(' ').join('_').downcase value = fields.join(':').strip @fsinfo.merge!({varname => value}) else #we are processing group info fields = row.split(' ') if fields.first=='Group' group_number = fields[1].to_i from = fields[3].split('-').first.to_i to = fields[3].split('-').last.to_i warn "Processing group #{group_number} #{from} #{to}" unless @debug.nil? else # we are only interested in free blocks info as of now fields = row.split(':') name = fields.first.strip if name == 'Free blocks' then ranges = fields.last.strip.split(', ') if ranges.empty? #copy whole group warn "Full copy of group #{group_number}" unless @debug.nil? buffercopy(@file,@gzip,'copy', from,to) else #copy only non-free blocks warn "Partial copy of group #{group_number}" unless @debug.nil? pointer = from ranges.each do |range| range_start=range.split('-').first.to_i range_end=range.split('-').last.to_i buffercopy(@file,@gzip,'copy',pointer,range_start-1) buffercopy(@devzero,@gzip,'zero',range_start,range_end) pointer=range_end+1 end if to > pointer warn "Remaining blocks from last free to end of group #{group_number}" unless @debug.nil? buffercopy(@file,@gzip,'copy',pointer,to) end end end end end end end |
#progress_bar(progress) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/e2phokz.rb', line 48 def progress t = progress.last i = progress.first.split('%').first.to_f h=(t/3600).to_i m=((t%3600)/60).to_i s=((t%60)*10).to_i/10.0 =(40*i/100).ceil =(1..).collect {'#'}.join spaces=40- bspaces=(1..spaces).collect {' '}.join sep = @debug.nil? ? "\r" : "\n" $stderr.write "ETA #{sprintf('%02d:%02d:%02.1f',h,m,s)} #{sprintf('%3d',i)}% [#{bbars}#{bspaces}] #{sep}" end |
#progress_stomp(progress) ⇒ Object
93 94 95 96 97 |
# File 'lib/e2phokz.rb', line 93 def progress_stomp progress unless @stomp_client.nil? @stomp_client.publish(@channel_name,progress.join(';')) end end |
#run! ⇒ Object
240 241 242 243 244 245 |
# File 'lib/e2phokz.rb', line 240 def run! parse_args parse_dumpe2fs close_files warn "\n" end |