def _read_block(self) -> bytes: self._aligned_buf.seek(0) os.lseek(self._fd, 0, os.SEEK_SET) os.readv(self._fd, [self._aligned_buf]) self._aligned_buf.seek(0) data = self._aligned_buf.read(BLOCK_SIZE) logger.debug("Read: " + _format_hex_for_log(data)) return data
def read(self) -> int: """Read from the inotify fd into buffers, unpack bytes to InotifyEvent instances, and call the event handler. Returns: number of bytes read. Raises: BufferError: bytes_read was equal to the total combined buffer size. """ if (bytes_read := os.readv(self.inotify_fd, self.read_buffers)) == self.max_read: raise BufferError("Inotify.read exceeded allocated buffers")
def get_directory(source): transport_scheme = get_transport_scheme(source) if transport_scheme: curl = subprocess.run(["curl", "-q", "-s", "-l", source], stdout=subprocess.PIPE, stderr=subprocess.PIPE) fullpath = [ urllib.parse.urljoin(source, filename) for filename in curl.stdout.decode('utf-8').splitlines() ] print(fullpath) print(curl.stderr) pipe = os.pipe() curl = subprocess.run(["curl", "-q", "-s", "-S", "-I"] + fullpath, stdout=pipe[1], stderr=pipe[1]) output = bytearray(65536) os.set_blocking(pipe[0], False) reading = True while reading: try: bytes_read = os.readv(pipe[0], [output]) print(bytes_read) except BlockingIOError: reading = False pass pass pass else: return [] pass
def __call__(self, log, *args, **kwargs): buf = mmap.mmap(-1, 4 * 1024 * 1024) for sdmmc in SDMMC.enumerate(): log.debug('%s: ' % sdmmc.path, end='', flush=True) fd = os.open(sdmmc.path, os.O_DIRECT | os.O_RDONLY) count = 0 start = time.perf_counter() while True: num = os.readv(fd, [buf]) end = time.perf_counter() delta = end - start count += num if delta > 3.0: break os.close(fd) count = count / (1024 * 1024) log.cont('%.2f MiB in %.2f seconds = %.2f MiB/s' % (count, delta, count / delta)) buf.close()
def read(self): buf = bytearray(self.mtu) protocol = ctypes.c_uint32() count = os.readv(self.fd.fileno(), [protocol, buf]) if count < 0: raise OSError(errno.value) protocol = libc.ntohl(protocol) return bytes(buf[:count-4])
def _data_available(self): """ Called when data is available on the TAP device. """ length = os.readv(self._fd, [self._buf]) self.delegate.handle_tap_data(self._buf, length)
def read(self): buf = bytearray(self.mtu) protocol = ctypes.c_uint32() count = os.readv(self.fd.fileno(), [protocol, buf]) if count < 0: raise OSError(errno.value) protocol = socket.ntohl(protocol.value) return bytes(buf[:count - 4])
def read_csi_data(fd, BUFFSIZE): """ Read CSI status and CSI data from file buffer to our own buffer :param fd: opened file buffer :param BUFFSIZE: size to read from file buffer :return: how many bytes were read from file buffer, our buffer (buffer contains bytes) """ info_array = bytearray(BUFFSIZE) cnt = os.readv(fd, [info_array]) return cnt, info_array
def select(self): if not self._fileno: return try: events = (InputEvent * 64)() bytes_read = os.readv(self._fileno, events) except OSError: self.close() return n_events = bytes_read // ctypes.sizeof(InputEvent) for event in events[:n_events]: try: control = self.control_map[(event.type, event.code)] control.value = event.value except KeyError: pass
def recv_into(self, buf): pipe_r = self.pipe_r if pipe_r is None: return 0 return os.readv(pipe_r, [buf])
def backupFile(sourceId, refSourceId, relativePath, path): # Insert the catalog entry st = path.stat() insertFileEntry(sourceId, relativePath, 'F', st) fileId = last_insert_rowid() # Get the file.id just inserted #XXX 途中のを再利用したい。 deleteBlockEntriesForFile(fileId) # Check if changed global linkedDiskBlocks while refSourceId: cur = referenceDatabase.execute( "select id, lastmod, size from file " "where source = :source and path = :path and type = 'F'", { 'source': refSourceId, 'path': str(relativePath) }) row = cur.fetchone() cur.close() if not row: # No entries found in the reference set. break if row['lastmod'] != encode64(st.st_mtime_ns): # mtime changed break if row['size'] != st.st_size: # size changed break cur = referenceDatabase.execute( 'select * from block where file = :file', {'file': row['id']}) for row in cur: hash = row['hash'] if not optDryRun: if not linkReferenceBlock(hash): print('block file disappeared for', relativePath, hash) #XXX linkedDiskBlocks += (row['size'] + 1023) // 1024 insertBlockEntry(fileId, row['offset'], row['size'], hash) if optShowFileProgress: print('-/-/U {}'.format(relativePath)) global unchangedFilesCounter unchangedFilesCounter += 1 return # This 'while' is not intended as a loop # Open the source file and split into block files global changedFilesCounter changedFilesCounter += 1 try: fd = os.open(str(path), os.O_RDONLY) # raise FileNotFoundError, O_BINARY? except PermissionError: #XXX Log return except OSError: #XXX Log other OS error return global createdDiskBlocks total = (st.st_size + BLOCK_SIZE - 1) // BLOCK_SIZE checked = 0 created = 0 offset = 0 while True: if optShowBlockProgress: print('{:d}/{:d}/{:d} {}'.format(created, checked, total, relativePath), end='\r') try: n = os.readv(fd, [fileReadBuffer]) except OSError: # File opened, but could not be read. # Mandatory Lock file etc. #XXX Log break if n == 0: # EOF break view = memoryview(fileReadBuffer) view = view[:n] hash = hashlib.sha1(view).hexdigest() if linkOrCreateBlock(hash, view): created += 1 createdDiskBlocks += (n + 1023) // 1024 else: linkedDiskBlocks += (n + 1023) // 1024 insertBlockEntry(fileId, offset, n, hash) offset += n checked += 1 os.close(fd) print('{:d}/{:d}/{:d} {}'.format(created, checked, total, relativePath))
def recv_into(self, buf): return os.readv(self.pipe_r, [buf])
#!/usr/bin/python #Filname is osreadvfun.py import os path = "./file.txt" fd = os.open(path, os.O_RDONLY) size = 20 buffer1 = bytearray(size) buffer2 = bytearray(size) buffer3 = bytearray(size) numBytes = os.readv(fd, [buffer1, buffer2, buffer3]) print("Data read in buffer 1:", buffer1.decode()) print("Data read in buffer 2:", buffer2.decode()) print("Data read in buffer 3:", buffer3.decode()) print("\nTotal Number of bytes actually read:", numBytes)