Пример #1
0
    def _write_block(self, data: bytes):
        self._aligned_buf.seek(0)
        self._aligned_buf.write(data)

        os.lseek(self._fd, 0, os.SEEK_SET)
        logger.debug("Write: " + _format_hex_for_log(data))
        os.writev(self._fd, [self._aligned_buf])
Пример #2
0
 def test_writev(self):
     fd = os.open(support.TESTFN, os.O_RDWR | os.O_CREAT)
     try:
         os.writev(fd, (b'test1', b'tt2', b't3'))
         os.lseek(fd, 0, os.SEEK_SET)
         self.assertEqual(b'test1tt2t3', posix.read(fd, 10))
     finally:
         os.close(fd)
Пример #3
0
 def test_writev(self):
     fd = os.open(support.TESTFN, os.O_RDWR | os.O_CREAT)
     try:
         os.writev(fd, (b'test1', b'tt2', b't3'))
         os.lseek(fd, 0, os.SEEK_SET)
         self.assertEqual(b'test1tt2t3', posix.read(fd, 10))
     finally:
         os.close(fd)
Пример #4
0
def logging_handler(fd_info, buf):
    """
    Custom log handler to always show stdout to console and stderr only in DEBUG mode
    """
    (_unused, fd_type) = fd_info
    if fd_type != STDOUT and not config.is_debug():
        return
    target = sys.stdout if fd_type == STDOUT else sys.stderr
    if sys.version_info > (3, 0):
        os.writev(target.fileno(), [buf])
    else:
        target.write(buf)
Пример #5
0
 def _write_ready(self, handler, fd):
     if len(handler.write_buf) > self.iov_limit:
         handler.write_buf[self.iov_limit - 1] = b''.join(
             handler.write_buf[self.iov_limit - 1:])
         del handler.write_buf[self.iov_limit:]
     sizes = tuple(map(len, handler.write_buf))
     try:
         written = os.writev(fd, handler.write_buf)
     except BlockingIOError:
         return
     if not written:
         raise EOFError('The output stream is closed')
     if written >= sum(sizes):
         handler.write_buf = []
     else:
         consumed = 0
         for i, buf in enumerate(handler.write_buf):
             if not written:
                 break
             if len(buf) <= written:
                 written -= len(buf)
                 consumed += 1
                 continue
             handler.write_buf[i] = buf[written:]
             break
         del handler.write_buf[:consumed]
Пример #6
0
 def _write_ready(self, handler: Handler, fd: int) -> None:
     if len(self.write_buf) > self.iov_limit:
         self.write_buf[self.iov_limit - 1] = b''.join(self.write_buf[self.iov_limit - 1:])
         del self.write_buf[self.iov_limit:]
     total_size = self.total_pending_bytes_to_write
     if total_size:
         try:
             written = os.writev(fd, self.write_buf)
         except BlockingIOError:
             return
         if not written:
             handler.terminal_io_ended = True
             self.quit(1)
             return
     else:
         written = 0
     if written >= total_size:
         self.write_buf: List[bytes] = []
         self.asyncio_loop.remove_writer(fd)
         self.waiting_for_writes = False
         handler.on_writing_finished()
     else:
         consumed = 0
         for i, buf in enumerate(self.write_buf):
             if not written:
                 break
             if len(buf) <= written:
                 written -= len(buf)
                 consumed += 1
                 continue
             self.write_buf[i] = buf[written:]
             break
         del self.write_buf[:consumed]
Пример #7
0
 def _write_ready(self, handler: Handler, fd: int) -> None:
     if len(self.write_buf) > self.iov_limit:
         self.write_buf[self.iov_limit - 1] = b''.join(
             self.write_buf[self.iov_limit - 1:])
         del self.write_buf[self.iov_limit:]
     sizes = tuple(map(len, self.write_buf))
     total_size = sum(sizes)
     if total_size:
         try:
             written = os.writev(fd, self.write_buf)
         except BlockingIOError:
             return
         if not written:
             raise EOFError('The output stream is closed')
     else:
         written = 0
     if written >= total_size:
         self.write_buf: List[bytes] = []
         self.asycio_loop.remove_writer(fd)
         self.waiting_for_writes = False
     else:
         consumed = 0
         for i, buf in enumerate(self.write_buf):
             if not written:
                 break
             if len(buf) <= written:
                 written -= len(buf)
                 consumed += 1
                 continue
             self.write_buf[i] = buf[written:]
             break
         del self.write_buf[:consumed]
Пример #8
0
def write_intf_packets(fd: io.RawIOBase, outq: MIOVQ, freeq: MIOVQ):
    logger.info("write_packets: from %s", outq.name)
    while True:
        m = outq.pop()
        mlen = m.len()

        n = os.writev(fd.fileno(), m.iov)
        if n != mlen:
            logger.error("write: bad write %d (mlen %d) on interface", n, mlen)
        if DEBUG:
            logger.debug("write: %d bytes on interface", n)
            # logger.debug("write: %d bytes (%s) on interface", n, binascii.hexlify(m.start[:8]))
        freeq.push(m)
Пример #9
0
def __write_raw(fd_info, a_buffer):
    """
    Raw write to fd compatible in Py2 and Py3 (3.3+)

    Buffer Output Discrimination is based on fd_type in fd_info.

    :param fd_info: Contains File descriptor type
    :type fd_info: tuple
    :param a_buffer: buffer interface
    :type a_buffer: bytes array
    """
    (unused_fd, fd_type) = fd_info
    if sys.version_info > (3, 0):
        if fd_type == STDOUT:
            fd = sys.stdout.fileno()
        else:
            # FIXME: Issue #488 - what to do in case fd_type != STDERR?
            fd = sys.stderr.fileno()
        os.writev(fd, [a_buffer])
    else:
        if fd_type == STDOUT:
            sys.stdout.write(a_buffer)
        else:
            sys.stderr.write(a_buffer)
Пример #10
0
 def test_writev(self):
     fd = os.open(support.TESTFN, os.O_RDWR | os.O_CREAT)
     try:
         n = os.writev(fd, (b'test1', b'tt2', b't3'))
         self.assertEqual(n, 10)
         os.lseek(fd, 0, os.SEEK_SET)
         self.assertEqual(b'test1tt2t3', posix.read(fd, 10))
         try:
             size = posix.writev(fd, [])
         except OSError:
             pass
         else:
             self.assertEqual(size, 0)
     finally:
         os.close(fd)
Пример #11
0
    def test_writev(self):
        fd = os.open(support.TESTFN, os.O_RDWR | os.O_CREAT)
        try:
            n = os.writev(fd, (b'test1', b'tt2', b't3'))
            self.assertEqual(n, 10)

            os.lseek(fd, 0, os.SEEK_SET)
            self.assertEqual(b'test1tt2t3', posix.read(fd, 10))

            # Issue #20113: empty list of buffers should not crash
            try:
                size = posix.writev(fd, [])
            except OSError:
                # writev(fd, []) raises OSError(22, "Invalid argument")
                # on OpenIndiana
                pass
            else:
                self.assertEqual(size, 0)
        finally:
            os.close(fd)
Пример #12
0
    def test_writev(self):
        fd = os.open(support.TESTFN, os.O_RDWR | os.O_CREAT)
        try:
            n = os.writev(fd, (b'test1', b'tt2', b't3'))
            self.assertEqual(n, 10)

            os.lseek(fd, 0, os.SEEK_SET)
            self.assertEqual(b'test1tt2t3', posix.read(fd, 10))

            # Issue #20113: empty list of buffers should not crash
            try:
                size = posix.writev(fd, [])
            except OSError:
                # writev(fd, []) raises OSError(22, "Invalid argument")
                # on OpenIndiana
                pass
            else:
                self.assertEqual(size, 0)
        finally:
            os.close(fd)
Пример #13
0
def linkOrCreateBlock(hash, view):
    global processedBlocksCounter
    processedBlocksCounter += 1

    # Make the block file name and make the parent directory
    blockFile = destinationSet / hash[:2]
    if not optDryRun and not blockFile.is_dir():  # raise
        blockFile.mkdir()  # raise
    blockFile /= hash[2:]

    # Skip if already exists in the same destination backup set
    if not optDryRun and blockFile.is_file() and blockFile.stat(
    ).st_size == len(view):
        global duplicateBlocksCounter
        duplicateBlocksCounter += 1
        return False

    # Link if already exists in any of other backup sets
    ref = first(destinationRoot.glob('*/{}/{}'.format(hash[:2], hash[2:])))
    if ref and ref.is_file() and ref.stat().st_size == len(view):
        if not optDryRun:
            ref.link_to(blockFile)
        global linkedBlocksCounter
        linkedBlocksCounter += 1
        return False

    # Dump the given memory into the file
    if not optDryRun:
        fd = os.open(str(blockFile),
                     os.O_WRONLY | os.O_CREAT | os.O_TRUNC)  # raise
        n = os.writev(fd, [view])  # raise OSError
        os.close(fd)
        if n != len(view):
            raise RuntimeError

    global createdBlocksCounter
    createdBlocksCounter += 1
    return True
Пример #14
0
#Filname is oswritevmethod.py
# import os module
import os

# File path
path = "/root/python/one/file.txt"

# Create a file and get the
# file descriptor associated
# with it using os.open() method
fd = os.open(path, os.O_CREAT | os.O_WRONLY)

# Bytes-like objects
# the data to be written in the file
buffer1 = bytearray(b"Working hard you will win the game of life ")
buffer2 = bytearray(b"I will never give up ")
buffer3 = bytearray(b"doing will till need of life")

# write the data contained in
# bytes-like objects
# to the file descriptor fd
# using os.writev() method
numBytes = os.writev(fd, [buffer1, buffer2, buffer3])

# print the content of file
with open(path) as f:
    print(f.read())

# Print the number of bytes actually written
print("Total Number of bytes actually written:", numBytes)
Пример #15
0
 def write(self, buf):
     v6 = (buf[0]>>4) == 6
     protocol = ctypes.c_uint32(libc.htonl((socket.AF_INET, socket.AF_INET6)[v6]))
     count = os.writev(self.fd.fileno(), [protocol, buf])
     return max(count-4, 0) if (count>0) else count
Пример #16
0
 def write(self, buf):
     v6 = (buf[0] >> 4) == 6
     protocol = ctypes.c_uint32(
         socket.htonl((socket.AF_INET, socket.AF_INET6)[v6]))
     count = os.writev(self.fd.fileno(), [protocol, buf])
     return max(count - 4, 0) if (count > 0) else count
Пример #17
0
 def write(self, prio, tag, message):
     return os.writev(
         self.fd, [struct.pack("I", prio), tag + b"\0", message + b"\0"]
     )