-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdrive.rb
More file actions
executable file
·228 lines (185 loc) · 4 KB
/
drive.rb
File metadata and controls
executable file
·228 lines (185 loc) · 4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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
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
164
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
#! /usr/bin/ruby
# TODO: check against drive UUID's not just identifiers.
@threads = []
@drives = []
@working = []
@readyed = false
# This will do damaging things.
@armed = true;
@settings = {
label: 'RESOURCE',
data: '/Users/michael/Desktop/CONTENT/*'
}
# These are essentially parameters to the `grep` command.
@drive_identifiers = [
'DOS_FAT_32'
]
def gui_clear
puts "\e[H\e[2J"
end
def gui_draw_table
gui_clear
puts @drives.count.to_s + ' drive(s)'
@drives.each do |drive|
puts drive + ":\t" + status(drive)
end
end
def main_thread()
while (true) do
gui_draw_table
look_for_new_drives()
sleep(0)
if !@readyed && @working.empty?
#puts 'READY / DONE.'
@readyed = true
end
end
end
def get_drives
disk_ids = []
drive_string = @drive_identifiers.join(' | grep ')
disks = `diskutil list | grep #{drive_string}`.split("\n")
disks.each do |disk|
disk_ids.push /.*GB\s+(.*)/.match(disk)[1]
end
disk_ids
end
def get_drive_uuid(identifier)
end
def new_a(old_list, new_list)
new_a = []
new_list.each do |n|
new_a.push n if !old_list.include?(n)
end
new_a
end
def old_a(old_list, new_list)
old_a = []
old_list.each do |n|
old_a.push n if !new_list.include?(n)
end
old_a
end
def status(drive, status = nil)
file_name = drive + '.lock'
if status == nil
if File.exists?(file_name)
`cat #{drive}.lock`
else
""
end
else
`echo '#{status}' > #{file_name}`
end
end
def lock_drive(drive)
file_name = drive + '.lock'
if File.exists?(file_name)
return false
end
status(drive, 'locked')
end
def unlock_drive(drive)
file_name = drive + '.lock'
File.delete file_name
end
def format_drive(drive)
#puts "Erasing #{drive}"
status(drive, 'formatting')
`diskutil reformat #{drive}`
if !$?.success?
sleep 1
#puts "Retrying erasing #{drive}"
`diskutil reformat #{drive}`
if !$?.success?
sleep 2
#puts "Retrying erase #{drive}"
`diskutil reformat #{drive}`
if !$?.success?
return status(drive, 'format failed')
end
end
end
status(drive, 'formatted')
end
def eject_drive(drive)
#puts "Ejecting #{drive}"
status(drive, 'ejecting')
`diskutil unmountDisk #{drive}`
if !$?.success?
sleep 1
# puts "retrying unmount #{drive}"
`diskutil unmountDisk #{drive}`
if !$?.success?
sleep 2
#puts "retrying unmount #{drive}"
`diskutil unmountDisk #{drive}`
if !$?.success?
return status(drive, 'eject failed')
else
end
end
end
status(drive, 'ejected')
end
def new_drive(drive)
#status(drive, 'found')
#puts "Found " + drive
if lock_drive(drive)
@working.push drive
sleep 1
# Erase Drive
if @armed
format_drive drive
sleep 1
end
# Label Drive
if @armed
status(drive, 'labeling')
#puts "Labeling #{drive}"
`diskutil renameVolume #{drive} "#{@settings[:label]}"`
status(drive, 'labeled')
sleep 1
end
# Copy Content
if @armed
mount_point = /^(?:\S+\s){2}(.*) \(/.match(`mount | grep #{drive}`)[1]
# TODO Wait for mount point.
#puts "Copying #{drive}"
status(drive, 'copying')
`rsync -av #{@settings[:data]} "#{mount_point}/"`
status(drive, 'copied')
sleep 1
end
# Eject
eject_drive drive
sleep 1
@working.delete drive
@readyed = false
unlock_drive(drive)
else
status(drive, 'lock failed')
end
end
def old_drive(drive)
#puts "Lost " + drive
end
def look_for_new_drives
current_drives = get_drives()
new_drives = new_a(@drives, current_drives)
old_drives = old_a(@drives, current_drives)
@drives = current_drives
new_drives.each do |drive|
@threads << Thread.new(drive) do |drive|
new_drive(drive)
end
end
old_drives.each do |drive|
@threads << Thread.new(drive) do |drive|
old_drive(drive)
end
end
end
`rm *.lock > /dev/null 2> /dev/null`
main_thread()
@threads.each { |aThread| aThread.join }