vhd 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- MWQ2NzlhZDJhOThjZTgyMDZkOTU5ODIzYzczNmQ4N2FlMTJkYjBhMg==
4
+ NTBlMjI2MjFlMGFkNWZlODViODg5ZGJmYjIxYmMwN2JjYzg5ZTRlNQ==
5
5
  data.tar.gz: !binary |-
6
- MzRmMjJiODJhZTZkNzg5M2FjNWYzMDRiN2IwMWVhOTJiY2NhNjNjZg==
6
+ NWU1N2VmNGU0Mjc4MDc4NmMyYWNlZjliYWE1ODQ2ZmQ5ZjUyMGU1ZQ==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- ODhiZTdmYTg1MmM2NjQwMDU1YTRlYTUxZjBjY2U3MDIyMTU5NDFjNjkwMjM0
10
- NmVlNzg2YTgwYzhiNTMzMjE5NGZmYzVjMDVmM2Y1YjA5MzhiYTFmZmFmYzhl
11
- YTYxOWEyNmY3YWRjZmQ1NjQzOWNjMjJiMTA4ZDRhNzcxMjhmNmU=
9
+ YTMzOTdiNWZkMThjYmRhZGExODZhMmE0OTA1MTM4Yzg3YTFjMzk2OGRmYTc4
10
+ MWY5YTcxMDI1N2MzZGUyODYyNGU5YzUyNjg4NzhhNmZmNjgzMGU2YjhiNGYx
11
+ MzdkOGU4NzI1MjY3MmQ3ZTdkYzkzZjAwODkwY2I0YmE5ODA4MTY=
12
12
  data.tar.gz: !binary |-
13
- ZDhlZWNmMTc0MWU3OGFmYjZhY2UzNTc2OTZmMGMyMDdjYTM3NDg0ZTk0ZDQ4
14
- MWMwMDEwNjkwYjUxNzJhN2Q2YTViY2Q1MzZjMTVlMjRkMDQ5OWU4YjE4Y2Yw
15
- ZjIzYTZiODdlMWFhZDFiNDA2MWM1ZmIyNTE3MWY2ZDE2NzY0ZmQ=
13
+ YjY0YTRkMzAzNzMyYzkyMzM4NjBkNWFhZDNlNzU1OTk3ZTNmN2Q0MGJlNmMw
14
+ ZTUyMTZlMzIyYjFlN2IyNGEyZjYwZGQzNzU3MjQwMzkxODgwMDI0MmUwMjA2
15
+ MDkwN2U4N2U0MjY5MjUxMzkwOWQ5YjRjOThhMWI0NmZkMDVjY2M=
data/Gemfile CHANGED
@@ -1,3 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
1
3
  gemspec
2
4
 
3
5
  group :test do
data/Gemfile.lock CHANGED
@@ -1,32 +1,33 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- vhd-ruby (0.0.1)
5
- ffi
4
+ vhd (0.0.2)
5
+ bit-struct
6
6
 
7
7
  GEM
8
+ remote: https://rubygems.org/
8
9
  specs:
9
- awesome_print (1.1.0)
10
- coderay (1.0.9)
11
- diff-lcs (1.2.1)
12
- ffaker (1.15.0)
13
- ffi (1.9.3)
14
- method_source (0.8.1)
15
- pry (0.9.12)
16
- coderay (~> 1.0.5)
10
+ awesome_print (1.2.0)
11
+ bit-struct (0.15.0)
12
+ coderay (1.1.0)
13
+ diff-lcs (1.2.5)
14
+ ffaker (1.23.0)
15
+ method_source (0.8.2)
16
+ pry (0.9.12.4)
17
+ coderay (~> 1.0)
17
18
  method_source (~> 0.8)
18
19
  slop (~> 3.4)
19
20
  pry-nav (0.2.3)
20
21
  pry (~> 0.9.10)
21
- rspec (2.13.0)
22
- rspec-core (~> 2.13.0)
23
- rspec-expectations (~> 2.13.0)
24
- rspec-mocks (~> 2.13.0)
25
- rspec-core (2.13.1)
26
- rspec-expectations (2.13.0)
22
+ rspec (2.14.1)
23
+ rspec-core (~> 2.14.0)
24
+ rspec-expectations (~> 2.14.0)
25
+ rspec-mocks (~> 2.14.0)
26
+ rspec-core (2.14.7)
27
+ rspec-expectations (2.14.4)
27
28
  diff-lcs (>= 1.1.3, < 2.0)
28
- rspec-mocks (2.13.0)
29
- slop (3.4.4)
29
+ rspec-mocks (2.14.4)
30
+ slop (3.4.7)
30
31
 
31
32
  PLATFORMS
32
33
  ruby
@@ -37,4 +38,4 @@ DEPENDENCIES
37
38
  pry
38
39
  pry-nav
39
40
  rspec
40
- vhd-ruby!
41
+ vhd!
data/fixed_footer.bin ADDED
Binary file
data/lib/vhd.rb CHANGED
@@ -1,5 +1,8 @@
1
- require 'ffi'
1
+ require 'time'
2
+ require 'bit-struct'
3
+ require 'securerandom'
2
4
 
3
5
  module Vhd
4
6
  autoload :Library, 'vhd/library'
7
+ autoload :Footer, 'vhd/footer'
5
8
  end
data/lib/vhd/footer.rb ADDED
@@ -0,0 +1,31 @@
1
+ class Vhd::Footer < BitStruct
2
+ unsigned :cookie , 8 , "Cookie"
3
+ unsigned :features , 4 , "Features"
4
+ unsigned :ff , 4 , "File Format"
5
+ unsigned :data_offset , 8 , "Data Offset"
6
+ unsigned :timestamp , 4 , "Timestamp"
7
+ unsigned :creator_app , 4 , "Creator Application"
8
+ unsigned :creator_ver , 4 , "Creator Version"
9
+ unsigned :creator_host , 4 , "Creator Host OS"
10
+ unsigned :original_size , 8 , "Original Size"
11
+ unsigned :current_size , 8 , "Current Size"
12
+ unsigned :geometry , 4 , "Disk Geometry"
13
+ unsigned :type , 4 , "Disk Type"
14
+ unsigned :checksum , 4 , "Footer Checksum"
15
+ unsigned :uuid , 16 , "Unique Id"
16
+ unsigned :saved_state , 1 , "Saved State"
17
+ unsigned :reserved , 427 , "Reserved Space"
18
+
19
+ initial_value.cookie = "conectix"
20
+ initial_value.features = 2
21
+ initial_value.ff = 65536
22
+ initial_value.offset = 18446744073709551615
23
+ initial_value.timestamp = (Time.now - Time.parse("Jan 1, 2000 12:00:00 AM GMT")).to_i
24
+ initial_value.creator_app = "rvhd"
25
+ initial_value.creator_ver = 0
26
+ initial_value.creator_host = "Wi2k"
27
+ initial_value.type = 2
28
+ initial_value.uuid = SecureRandom.uuid.delete('-')
29
+ initial_value.saved_state = 0
30
+ initial_value.reserved = 0
31
+ end
data/lib/vhd/library.rb CHANGED
@@ -1,24 +1,88 @@
1
- module Vhd::Library
2
- extend FFI::Library
3
- ffi_lib "libvhd.so.0.1.1"
4
- attach_function :vhd_create, [ :string, :uint64, :int, :uint64, :uint ], :int
1
+ class Vhd::Library
2
+ VALID_TYPES = [:fixed]
5
3
 
6
- HD_TYPE_FIXED = 2
7
- HD_TYPE_DYNAMIC = 3
4
+ attr_reader :name, :footer, :size
8
5
 
9
- def self.create_dynamic_disk(name, size_in_gb)
10
- size_in_bytes = self.size_in_bytes(size_in_gb)
6
+ def initialize(options={})
7
+ raise "Invalid vhd type" unless VALID_TYPES.include?(options[:type])
8
+ raise "Name is required" unless options[:name]
9
+ raise "Size is required" unless options[:size]
10
+ @name = options[:name]
11
+ @footer = {}
12
+ @size = options[:size]
11
13
 
12
- vhd_create(name, size_in_bytes, HD_TYPE_DYNAMIC, 0, 0)
14
+ self.generate_footer
13
15
  end
14
16
 
15
- def self.create_fixed_disk(name, size_in_gb)
16
- size_in_bytes = self.size_in_bytes(size_in_gb)
17
+ def create
18
+ File.open(@name, "wb") { |f| f.print @footer.values.join }
19
+ end
20
+
21
+ def generate_footer(options={})
22
+ @footer[:cookie] = "conectix".force_encoding("BINARY")
23
+ @footer[:features] = ["00000002"].pack("H*")
24
+ @footer[:ff] = ["00010000"].pack("H*")
25
+ @footer[:offset] = ["FFFFFFFFFFFFFFFF"].pack("H*")
26
+ @footer[:time] = [(Time.now - Time.parse("Jan 1, 2000 12:00:00 AM GMT")).to_i.to_s(16)].pack("H*")
27
+ @footer[:creator_app] = "rvhd".force_encoding("UTF-8")
28
+ @footer[:creator_ver] = ["00060002"].pack("H*")
29
+ @footer[:creator_host] = "Wi2k".force_encoding("UTF-8")
30
+ @footer[:orig_size] = size_in_hex
31
+ @footer[:curr_size] = size_in_hex
32
+ @footer[:geometry] = nil
33
+ @footer[:disk_type] = ["00000002"].pack("H*")
34
+ @footer[:checksum] = nil
35
+ @footer[:uuid] = SecureRandom.hex.scan(/../).map { |c| c.hex.chr.force_encoding("BINARY") }.join
36
+ @footer[:state] = ["0"].pack("H*")
37
+ @footer[:reserved] = Array("00"*427).pack("H*")
38
+
39
+ self.geometry
40
+ self.checksum
41
+ end
17
42
 
18
- vhd_create(name, size_in_bytes, HD_TYPE_FIXED, 0, 2)
43
+ def size_in_bytes
44
+ (((@size * 1024) * 1024) * 1024).to_i
19
45
  end
20
46
 
21
- def self.size_in_bytes(size_in_gb)
22
- size_in_gb * 1024 * 1024 * 1024
47
+ def size_in_hex
48
+ hex_size = size_in_bytes.to_s(16)
49
+ hex_size = "0" + hex_size until hex_size.length == 16
50
+ [hex_size].pack("H*")
51
+ end
52
+
53
+ def geometry
54
+ max_size = 65535 * 16 * 255 * 512
55
+ capacity = size_in_bytes > max_size ? max_size : size_in_bytes
56
+ total_sectors = capacity / 512
57
+
58
+ if total_sectors > (65535 * 16 * 63)
59
+ sectors_per_track = 255
60
+ heads_per_cylinder = 16
61
+ else
62
+ sectors_per_track = 17
63
+ cylinders_times_heads = total_sectors / sectors_per_track
64
+ heads_per_cylinder = (cylinders_times_heads + 1023) / 1024
65
+ heads_per_cylinder = 4 if heads_per_cylinder < 4
66
+
67
+ cylinders_times_heads >= ((heads_per_cylinder * 1024) or (heads_per_cylinder > 16))
68
+ sectors_per_track = 63
69
+ heads_per_cylinder = 16
70
+ end
71
+
72
+ cylinders = (total_sectors / sectors_per_track) / heads_per_cylinder
73
+
74
+ @footer[:geometry] = [cylinders, heads_per_cylinder, sectors_per_track].pack("SCC")
75
+ end
76
+
77
+ def checksum
78
+ checksum = 0
79
+
80
+ @footer.each do |k,v|
81
+ next if k == :checksum
82
+
83
+ checksum += v.codepoints.inject(0) { |r,c| r += c }
84
+ end
85
+
86
+ @footer[:checksum] = ["%08x" % ((~checksum).abs ^ 0xFFFFFFFF)].pack("H*")
23
87
  end
24
88
  end
data/lib/vhd/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Vhd
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
data/spec/vhd_tests.rb CHANGED
@@ -2,22 +2,17 @@ require 'spec_helper'
2
2
 
3
3
  describe "vhds" do
4
4
  before(:each) do
5
- @file_name = "/tmp/#{Faker::Name.first_name}"
5
+ @file = Tempfile.new(Faker::Name.first_name.downcase)
6
6
  end
7
7
 
8
- let(:file_name) { @file_name }
9
-
10
- it "should create a dynamic vhd" do
11
- Vhd::Library.create_dynamic_disk(file_name, 0.1)
12
- File.size(file_name).should == 3584
13
- end
8
+ let(:file_name) { @file.path }
14
9
 
15
10
  it "should create a fixed vhd" do
16
- Vhd::Library.create_fixed_disk(file_name, 0.1)
17
- File.size(file_name).should == 109052416
11
+ disk = Vhd::Library.new(type: :fixed, size: 1, name: file_name)
12
+ disk.create
18
13
  end
19
14
 
20
15
  after(:each) do
21
- FileUtils.rm_f(@file_name)
16
+ @file.close
22
17
  end
23
18
  end
@@ -1,6 +1,5 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
  require File.expand_path('../lib/vhd/version', __FILE__)
3
- require File.expand_path('../lib/vhd.rb', __FILE__)
4
3
 
5
4
  Gem::Specification.new do |gem|
6
5
  gem.authors = ["Eugene Howe"]
@@ -16,5 +15,5 @@ Gem::Specification.new do |gem|
16
15
  gem.require_paths = ["lib"]
17
16
  gem.version = Vhd::VERSION
18
17
 
19
- gem.add_dependency "ffi"
18
+ gem.add_dependency "bit-struct"
20
19
  end
metadata CHANGED
@@ -1,17 +1,17 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vhd
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eugene Howe
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-01-20 00:00:00.000000000 Z
11
+ date: 2014-01-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: ffi
14
+ name: bit-struct
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - ! '>='
@@ -34,13 +34,14 @@ files:
34
34
  - Gemfile
35
35
  - Gemfile.lock
36
36
  - README.md
37
- - lib/libvhd.so.0.1.1
37
+ - fixed_footer.bin
38
38
  - lib/vhd.rb
39
+ - lib/vhd/footer.rb
39
40
  - lib/vhd/library.rb
40
41
  - lib/vhd/version.rb
41
42
  - spec/spec_helper.rb
42
43
  - spec/vhd_tests.rb
43
- - vhd-ruby.gemspec
44
+ - vhd.gemspec
44
45
  homepage: ''
45
46
  licenses: []
46
47
  metadata: {}
data/lib/libvhd.so.0.1.1 DELETED
Binary file