Class: RAPI
- Inherits:
-
Object
- Object
- RAPI
- Defined in:
- lib/rapi.rb
Defined Under Namespace
Modules: Native Classes: Enum, FileAttributes, FileInformation, ProcessInformation, RAPIException
Instance Attribute Summary collapse
-
#copy_buffer_size ⇒ Object
Returns the value of attribute copy_buffer_size.
Instance Method Summary collapse
- #connect(timeout_seconds = 1) ⇒ Object
- #connected? ⇒ Boolean
- #copy(existing_file_name, new_file_name, overwrite = false) ⇒ Object
- #delete(file_name) ⇒ Object
- #disconnect ⇒ Object
- #download(remote_file_name, local_file_name, overwrite = false) ⇒ Object
- #exec(file_name, *args) ⇒ Object
- #exist?(remote_file_name) ⇒ Boolean (also: #exists?)
- #get_attributes(file_name) ⇒ Object (also: #get_attrs)
-
#initialize ⇒ RAPI
constructor
A new instance of RAPI.
- #move(existing_file_name, new_file_name) ⇒ Object
- #search(file_name) ⇒ Object (also: #glob)
- #set_attributes(file_name, attributes) ⇒ Object (also: #set_attrs)
- #upload(local_file_name, remote_file_name, overwrite = false) ⇒ Object
Constructor Details
#initialize ⇒ RAPI
Returns a new instance of RAPI.
8 9 10 11 |
# File 'lib/rapi.rb', line 8 def initialize @connected = false @copy_buffer_size = 0x1000 end |
Instance Attribute Details
#copy_buffer_size ⇒ Object
Returns the value of attribute copy_buffer_size.
6 7 8 |
# File 'lib/rapi.rb', line 6 def copy_buffer_size @copy_buffer_size end |
Instance Method Details
#connect(timeout_seconds = 1) ⇒ Object
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 |
# File 'lib/rapi.rb', line 17 def connect(timeout_seconds = 1) self.disconnect if connected? init = Native::Rapi::RAPIINIT.new init[:cbSize] = Native::Rapi::RAPIINIT.size ret = Native::Rapi.CeRapiInitEx(init) handle_hresult! ret init_event = init[:heRapiInit] timeout = timeout_seconds * 4 infinite_timeout = timeout < 0 begin ret = Native::Kernel32.WaitForSingleObject(init_event, 250) if ret == Native::WAIT_FAILED || ret == Native::WAIT_ABANDONED Native::Kernel32.CloseHandle(init_event) Native::Rapi.CeRapiUninit raise RAPIException, "Failed to Initialize RAPI" end if !infinite_timeout if (timeout -= 1) < 0 Native::Kernel32.CloseHandle(init_event) Native::Rapi.CeRapiUninit raise RAPIException, "Timeout waiting for device connection" end end end while ret != Native::WAIT_OBJECT_0 @connected = true Native::Kernel32.CloseHandle(init_event) true end |
#connected? ⇒ Boolean
13 14 15 |
# File 'lib/rapi.rb', line 13 def connected? @connected end |
#copy(existing_file_name, new_file_name, overwrite = false) ⇒ Object
137 138 139 140 141 142 143 144 145 |
# File 'lib/rapi.rb', line 137 def copy(existing_file_name, new_file_name, overwrite = false) check_connection() if Native::Rapi.CeCopyFile(to_utf16(existing_file_name), to_utf16(new_file_name), overwrite ? 0 : 1) == 0 raise RAPIException, "Cannot copy file" end true end |
#delete(file_name) ⇒ Object
147 148 149 150 151 152 153 154 155 |
# File 'lib/rapi.rb', line 147 def delete(file_name) check_connection() if Native::Rapi.CeDeleteFile(to_utf16(file_name)) == 0 raise RAPIException, "Could not delete file" end true end |
#disconnect ⇒ Object
53 54 55 56 57 58 59 60 |
# File 'lib/rapi.rb', line 53 def disconnect if connected? Native::Rapi.CeRapiUninit @connected = false end true end |
#download(remote_file_name, local_file_name, overwrite = false) ⇒ Object
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 |
# File 'lib/rapi.rb', line 70 def download(remote_file_name, local_file_name, overwrite = false) check_connection() if !overwrite && File.exists?(local_file_name) raise RAPIException, "A local file with the given name already exists" end remote_file = Native::Rapi.CeCreateFile(to_utf16(remote_file_name), Native::GENERIC_READ, 0, 0, Native::OPEN_EXISTING, Native::FILE_ATTRIBUTE_NORMAL, 0) if remote_file == Native::INVALID_HANDLE raise RAPIException, "Could not open remote file" end File.open(local_file_name, "wb") do |f| buffer = FFI::MemoryPointer.new(1, @copy_buffer_size) bytes_read_ptr = FFI::MemoryPointer.new(FFI::Type::INT.size) while true ret = Native::Rapi.CeReadFile(remote_file, buffer, buffer.size, bytes_read_ptr, 0) bytes_read = bytes_read_ptr.get_int(0) if bytes_read != 0 && ret == 0 buffer.free bytes_read_ptr.free Native::Rapi.CeCloseHandle(remote_file) raise RAPIException, "Failed to read device data" elsif bytes_read == 0 break end f << buffer.get_bytes(0, bytes_read) end buffer.free bytes_read_ptr.free Native::Rapi.CeCloseHandle(remote_file) end true end |
#exec(file_name, *args) ⇒ Object
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/rapi.rb', line 215 def exec(file_name, *args) check_connection args = if args.empty? nil else args.join(' ') end pi = Native::Rapi::PROCESS_INFORMATION.new if Native::Rapi.CeCreateProcess(to_utf16(file_name), to_utf16(args), nil, nil, 0, 0, nil, nil, nil, pi) == 0 errnum = Native::Rapi.CeGetLastError handle_hresult! errnum end ProcessInformation.new(pi) end |
#exist?(remote_file_name) ⇒ Boolean Also known as: exists?
62 63 64 65 66 |
# File 'lib/rapi.rb', line 62 def exist?(remote_file_name) check_connection() Native::Rapi::CeGetFileAttributes(to_utf16(remote_file_name)) != 0xFFFFFFFF end |
#get_attributes(file_name) ⇒ Object Also known as: get_attrs
167 168 169 170 171 172 173 174 175 176 |
# File 'lib/rapi.rb', line 167 def get_attributes(file_name) check_connection() ret = Native::Rapi.CeGetFileAttributes(to_utf16(file_name)) if ret == 0xFFFFFFFF raise RAPIException, "Could not get file attributes" end FileAttributes.new(ret) end |
#move(existing_file_name, new_file_name) ⇒ Object
157 158 159 160 161 162 163 164 165 |
# File 'lib/rapi.rb', line 157 def move(existing_file_name, new_file_name) check_connection() if Native::Rapi.CeMoveFile(to_utf16(existing_file_name), to_utf16(new_file_name)) == 0 raise RAPIException, "Cannot move file" end true end |
#search(file_name) ⇒ Object Also known as: glob
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/rapi.rb', line 190 def search(file_name) check_connection() find_data = Native::Rapi::CE_FIND_DATA.new file_infos = [] handle = Native::Rapi.CeFindFirstFile(to_utf16(file_name), find_data) if handle != Native::INVALID_HANDLE file_infos << FileInformation.new(find_data) find_data.pointer.clear while Native::Rapi.CeFindNextFile(handle, find_data) != 0 file_infos << FileInformation.new(find_data) find_data.pointer.clear end Native::Rapi.CeFindClose(handle) end file_infos end |
#set_attributes(file_name, attributes) ⇒ Object Also known as: set_attrs
180 181 182 183 184 185 186 |
# File 'lib/rapi.rb', line 180 def set_attributes(file_name, attributes) check_connection() if Native::Rapi.CeSetFileAttributes(to_utf16(file_name), attributes.to_i) == 0 raise RAPIExcpetion, "Cannot set device file attributes" end end |
#upload(local_file_name, remote_file_name, overwrite = false) ⇒ Object
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 |
# File 'lib/rapi.rb', line 111 def upload(local_file_name, remote_file_name, overwrite = false) check_connection() create = overwrite ? Native::CREATE_ALWAYS : Native::CREATE_NEW remote_file = Native::Rapi.CeCreateFile(to_utf16(remote_file_name), Native::GENERIC_WRITE, 0, 0, create, Native::FILE_ATTRIBUTE_NORMAL, 0) if remote_file == Native::INVALID_HANDLE raise RAPIException, "Could not create remote file" end if File.size(local_file_name) != 0 File.open(local_file_name, "rb") do |f| while buffer = f.read(copy_buffer_size) if Native::Rapi.CeWriteFile(remote_file, buffer, buffer.size, nil, 0) == 0 Native::Rapi.CeCloseHandle(remote_file) raise RAPIException, "Could not write to remote file" end end end end Native::Rapi.CeCloseHandle(remote_file) true end |