Module: RAPI
- Defined in:
- lib/rapi.rb,
lib/rapi/registry.rb,
lib/rapi/remote_file.rb
Defined Under Namespace
Modules: Native, Util Classes: Enum, FileAttributes, FileInformation, OSVersionInfo, ProcessInformation, RAPIError, Registry, RemoteFile
Class Attribute Summary (collapse)
-
+ (Object) copy_buffer_size
Returns the value of attribute copy_buffer_size.
Class Method Summary (collapse)
- + (Object) connect(timeout_seconds = 1)
- + (Boolean) connected?
- + (Object) copy(existing_file_name, new_file_name, overwrite = false)
- + (Object) delete(file_name) (also: rm)
- + (Object) disconnect
- + (Object) download(remote_file_name, local_file_name, overwrite = false)
- + (Object) exec(file_name, *args)
- + (Boolean) exist?(remote_file_name) (also: exists?)
- + (Object) get_attributes(file_name) (also: get_attrs)
- + (Object) mkdir(path)
- + (Object) move(existing_file_name, new_file_name)
- + (Object) open(path, *rest)
- + (Object) os
- + (Object) rm_rf(path)
- + (Object) search(search_term) (also: glob)
- + (Object) set_attributes(file_name, attributes) (also: set_attrs)
- + (Object) tmp
- + (Object) upload(local_file_name, remote_file_name, overwrite = false)
Class Attribute Details
+ (Object) copy_buffer_size
Returns the value of attribute copy_buffer_size
11 12 13 |
# File 'lib/rapi.rb', line 11 def copy_buffer_size @copy_buffer_size end |
Class Method Details
+ (Object) connect(timeout_seconds = 1)
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 |
# File 'lib/rapi.rb', line 19 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) Util.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::Rapi.CeRapiUninit raise RAPIError, "Failed to Initialize RAPI." end if !infinite_timeout if (timeout -= 1) < 0 Native::Rapi.CeRapiUninit raise RAPIError, "Timeout waiting for device connection." end end end while ret != Native::WAIT_OBJECT_0 @connected = true true ensure Native::Kernel32.CloseHandle(init_event) if init_event init.to_ptr.free end |
+ (Boolean) connected?
13 14 15 16 17 |
# File 'lib/rapi.rb', line 13 def connected? # Try calling an arbitrary function. Native::Rapi.CeGetFileAttributes(Util.utf16le("")) Util.error != Native::CONNECTION_LOST end |
+ (Object) copy(existing_file_name, new_file_name, overwrite = false)
160 161 162 163 164 165 166 167 168 |
# File 'lib/rapi.rb', line 160 def copy(existing_file_name, new_file_name, overwrite = false) success = Native::Rapi.CeCopyFile(Util.utf16le(existing_file_name), Util.utf16le(new_file_name), overwrite ? 0 : 1) != 0 unless success Util.handle_hresult! Util.error, "Could not copy file." end true end |
+ (Object) delete(file_name) Also known as: rm
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/rapi.rb', line 170 def delete(file_name) attrs = get_attrs(file_name) rescue nil unless attrs Util.handle_hresult! Native::FILE_NOT_FOUND, "Could not delete file." end if attrs.directory? success = Native::Rapi.CeRemoveDirectory(Util.utf16le(file_name)) != 0 else success = Native::Rapi.CeDeleteFile(Util.utf16le(file_name)) != 0 end unless success Util.handle_hresult! Util.error, "Could not delete file." end true end |
+ (Object) disconnect
57 58 59 60 61 |
# File 'lib/rapi.rb', line 57 def disconnect Native::Rapi.CeRapiUninit true end |
+ (Object) download(remote_file_name, local_file_name, overwrite = false)
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 |
# File 'lib/rapi.rb', line 90 def download(remote_file_name, local_file_name, overwrite = false) if !overwrite && File.exists?(local_file_name) raise RAPIError, "A local file with the given name already exists." end handle = Native::Rapi.CeCreateFile(Util.utf16le(remote_file_name), Native::GENERIC_READ, 0, 0, Native::OPEN_EXISTING, Native::FILE_ATTRIBUTE_NORMAL, 0) unless handle.valid? Util.handle_hresult! Util.error, "Could not open remote file." end mode = overwrite ? "wb" : "r+b" buffer = FFI::MemoryPointer.new(:char, @copy_buffer_size) bytes_read_ptr = FFI::MemoryPointer.new(:uint) File.open(local_file_name, "wb") do |f| while true success = Native::Rapi.CeReadFile(handle, buffer, buffer.size, bytes_read_ptr, 0) != 0 unless success Util.handle_hresult! Util.error, "Failed to read device data." end bytes_read = bytes_read_ptr.get_int(0) if bytes_read != 0 && !success err = Util.error Native::Rapi.CeCloseHandle(handle) Util.handle_hresult! err, "Failed to read device data." elsif bytes_read == 0 break end f << buffer.get_bytes(0, bytes_read) end f.truncate(f.pos) end true ensure buffer.free if buffer bytes_read_ptr.free if bytes_read_ptr handle.close if handle end |
+ (Object) exec(file_name, *args)
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 |
# File 'lib/rapi.rb', line 269 def exec(file_name, *args) args = if args.empty? nil else args.join(' ') end pi = Native::Rapi::PROCESS_INFORMATION.new if Native::Rapi.CeCreateProcess(Util.utf16le(file_name), Util.utf16le(args), nil, nil, 0, 0, nil, nil, nil, pi) == 0 errnum = Native::Rapi.CeGetLastError Util.handle_hresult! errnum end ProcessInformation.new(pi) end |
+ (Boolean) exist?(remote_file_name) Also known as: exists?
63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/rapi.rb', line 63 def exist?(remote_file_name) attrs = Native::Rapi.CeGetFileAttributes(Util.utf16le(remote_file_name)) if attrs != 0xFFFFFFFF true else err = Util.error if err == Native::FILE_NOT_FOUND || err == Native::PATH_NOT_FOUND false else Util.handle_hresult! err end end end |
+ (Object) get_attributes(file_name) Also known as: get_attrs
209 210 211 212 213 214 215 216 217 218 |
# File 'lib/rapi.rb', line 209 def get_attributes(file_name) ret = Native::Rapi.CeGetFileAttributes(Util.utf16le(file_name)) success = ret != 0xFFFFFFFF unless success Util.handle_hresult! Util.error, "Could not get file attributes." end FileAttributes.new(ret) end |
+ (Object) mkdir(path)
80 81 82 83 84 85 86 87 88 |
# File 'lib/rapi.rb', line 80 def mkdir(path) success = Native::Rapi.CeCreateDirectory(Util.utf16le(path), nil) != 0 unless success Util.handle_hresult! Util.error, "Could not create directory." end true end |
+ (Object) move(existing_file_name, new_file_name)
199 200 201 202 203 204 205 206 207 |
# File 'lib/rapi.rb', line 199 def move(existing_file_name, new_file_name) success = Native::Rapi.CeMoveFile(Util.utf16le(existing_file_name), Util.utf16le(new_file_name)) != 0 unless success Util.handle_hresult! Util.error, "Could not move file." end true end |
+ (Object) open(path, *rest)
299 300 301 302 303 304 305 306 307 308 309 310 |
# File 'lib/rapi.rb', line 299 def open(path, *rest) file = RemoteFile.new(path, *rest) if block_given? begin yield file ensure file.close end else file end end |
+ (Object) os
312 313 314 315 316 317 318 319 320 321 |
# File 'lib/rapi.rb', line 312 def os os = Native::Rapi::CEOSVERSIONINFO.new os[:dwOSVersionInfoSize] = Native::Rapi::CEOSVERSIONINFO.size success = Native::Rapi.CeGetVersionEx(os) != 0 unless success raise RAPIError, "Error retrieving version information." end OSVersionInfo.new(os) end |
+ (Object) rm_rf(path)
192 193 194 195 196 197 |
# File 'lib/rapi.rb', line 192 def rm_rf(path) search(path).each do |file| rm_rf(File.join(file.path, "*")) if file.directory? delete(file.path) end end |
+ (Object) search(search_term) Also known as: glob
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 |
# File 'lib/rapi.rb', line 234 def search(search_term) file_infos = [] ppFindDataArray = FFI::MemoryPointer.new(:pointer) count_ptr = FFI::MemoryPointer.new(:uint) success = Native::Rapi.CeFindAllFiles(Util.utf16le(search_term), 255, count_ptr, ppFindDataArray) != 0 if !success && Util.error == Native::CONNECTION_LOST Util.handle_hresult! Native::CONNECTION_LOST end begin count = count_ptr.get_uint(0) if count > 0 array_ptr = FFI::Pointer.new(Native::Rapi::CE_FIND_DATA, ppFindDataArray.get_pointer(0)) directory = Util.sanitize_path(search_term) (0...count).each do |n| info = FileInformation.new(directory, Native::Rapi::CE_FIND_DATA.new(array_ptr[n])) file_infos << info end end end file_infos ensure ppFindDataArray.free if ppFindDataArray array_ptr.free if array_ptr count_ptr.free if count_ptr Native::Rapi.CeRapiFreeBuffer(array_ptr) if array_ptr end |
+ (Object) set_attributes(file_name, attributes) Also known as: set_attrs
222 223 224 225 226 227 228 229 230 |
# File 'lib/rapi.rb', line 222 def set_attributes(file_name, attributes) success = Native::Rapi.CeSetFileAttributes(Util.utf16le(file_name), attributes.to_i) != 0 unless success Util.handle_hresult! Util.error, "Could not set device file attributes." end true end |
+ (Object) tmp
286 287 288 289 290 291 292 293 294 295 296 297 |
# File 'lib/rapi.rb', line 286 def tmp buffer = FFI::MemoryPointer.new(:uint16, Native::MAX_PATH + 1) temp_path = nil if Native::Rapi.CeGetTempPath(Native::MAX_PATH, buffer) != 0 temp_path = Util.utf8(buffer.get_bytes(0, Native::MAX_PATH * 2)) end temp_path ensure buffer.free if buffer end |
+ (Object) upload(local_file_name, remote_file_name, overwrite = false)
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/rapi.rb', line 135 def upload(local_file_name, remote_file_name, overwrite = false) create = overwrite ? Native::CREATE_ALWAYS : Native::CREATE_NEW handle = Native::Rapi.CeCreateFile(Util.utf16le(remote_file_name), Native::GENERIC_WRITE, 0, 0, create, Native::FILE_ATTRIBUTE_NORMAL, 0) unless handle.valid? raise RAPIError, "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(handle, buffer, buffer.size, nil, 0) == 0 Native::Rapi.CeCloseHandle(handle) raise RAPIError, "Could not write to remote file." end end end end true ensure handle.close end |