Class: DriveInfo

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_gui_creator/drive_info.rb

Constant Summary collapse

@@drive_cache =
nil
@@drive_cache_mutex =
Mutex.new
@@drive_changed_notifies =
[]
@@updating_mutex =

theoretically 2 threads could call this at once…so synchronize it…

Mutex.new

Class Method Summary collapse

Class Method Details

.add_notify_on_changed_disks(&block) ⇒ Object



86
87
88
89
90
91
92
93
# File 'lib/simple_gui_creator/drive_info.rb', line 86

def self.add_notify_on_changed_disks &block
 raise unless block
 @@drive_changed_notifies << block
 should_call = false
 @@drive_cache_mutex.synchronize { should_call = true if @@drive_cache }
 block.call if should_call # should be called at least once, right, on init?
 true
end

.create_looping_drive_cacherObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/simple_gui_creator/drive_info.rb', line 41

def self.create_looping_drive_cacher
  # has to be in its own thread or wmi will choke...
  looped_at_least_once = false
  if @caching_thread
    looped_at_least_once = true
  else
    @caching_thread = Thread.new {
      # use a dir glob to avoid having to use wmi too frequently (or accessing disks too often, which we might still be doing accidentally anyway)
      if OS.doze?
  		  old_drive_glob = '{' + DriveInfo.get_dvd_drives_even_if_no_disc_present.map{|dr| dr.MountPoint[0..0]}.join(',') + '}:/.' # must be in the thread for wmi
      else
        old_drive_glob = '/Volumes/*'
      end
      previously_known_about_discs = nil
      loop {
	      should_update = false
        @@drive_cache_mutex.synchronize { # in case the first update takes too long, basically, so they miss it LODO still a tiny race condition in here...might be useless...
          looped_at_least_once = true # let the spawning waiting thread exit quickly
          cur_disks = Dir[old_drive_glob]
  		    if cur_disks != previously_known_about_discs
	          p 'updating disk lists...'
			      should_update = true
            @@drive_cache = get_all_drives_as_ostructs_internal
            previously_known_about_discs = cur_disks
		      end
        }
        notify_all_drive_blocks_that_change_has_occured if should_update
        sleep 0.5
      }
    }
  end
  # maintain some thread startup sanity :P
  while(!looped_at_least_once)
    sleep 0.01
  end
  true
end

.get_all_drives_as_ostructsObject

gets all drives not just DVD drives…



107
108
109
110
111
112
113
114
115
# File 'lib/simple_gui_creator/drive_info.rb', line 107

def self.get_all_drives_as_ostructs # gets all drives not just DVD drives...
 @@drive_cache_mutex.synchronize {
   if @caching_thread
     @@drive_cache
   else
     get_all_drives_as_ostructs_internal # first time through for the startup thread goes here too
   end
 }
end

.get_drive_with_most_space_with_slashObject



101
102
103
104
105
# File 'lib/simple_gui_creator/drive_info.rb', line 101

def self.get_drive_with_most_space_with_slash
 disks = get_all_drives_as_ostructs
 most_space = disks.sort_by{|d| d.FreeSpace}[-1]
 most_space.MountPoint + "/"
end

.get_dvd_drives_as_openstructObject



95
96
97
98
# File 'lib/simple_gui_creator/drive_info.rb', line 95

def self.get_dvd_drives_as_openstruct
  disks = get_all_drives_as_ostructs
  disks.select{|d| d.Description =~ /CD-ROM/ && File.exist?(d.Name + "/VIDEO_TS")}
end

.md5sum_disk(dir) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/simple_gui_creator/drive_info.rb', line 25

def self.md5sum_disk(dir)
 if OS.mac?
   exe = "#{__DIR__}/../../vendor/mac_dvdid/bin/dvdid"
 else
   exe = "#{__DIR__}/../../vendor/dvdid.exe"
 end
 raise exe + '--exe doesnt exist?' unless File.exist? exe
 command = "#{exe} \"#{dir}\""
 output = `#{command}` # can take like 2.2s to spin up the disk...
 raise 'dvdid command failed?' + command unless $?.exitstatus == 0
 output.strip
end

.notify_all_drive_blocks_that_change_has_occuredObject



80
81
82
83
84
# File 'lib/simple_gui_creator/drive_info.rb', line 80

def self.notify_all_drive_blocks_that_change_has_occured
  @@updating_mutex.synchronize {
    @@drive_changed_notifies.each{|block| block.call}
  }
end