Example #1
0
 async def check_file_exist(
     path: str,
     headers: Optional[Dict[str, str]] = None,
 ):
     try:
         if check_support(path):
             session = ClientSession()
             response: ClientResponse = await session.get(
                 path,
                 timeout=5,
                 headers=headers,
             )
             response.close()
             await session.close()
             if response.status == 200 or \
                     response.status == 403:
                 return
             else:
                 py_logger.info(
                     f'{path} returned with {response.status} code', )
     except ClientConnectorError:
         pass
     except TimeoutError:
         pass
     if S_ISFIFO(os.stat(path).st_mode):
         return
     if not os.path.isfile(path):
         raise FileNotFoundError()
Example #2
0
def get_stdin():
	#check if programm is being piped
	if S_ISFIFO(os.fstat(0).st_mode):
		for line in sys.stdin:
			line = line.replace('\n', '')
			line = line.replace('	', '')
			stdincontent.append(line)
Example #3
0
 def run(self):
     start = time.time()
     try:
         if os.path.exists(self.location):
             mode = os.stat(self.location)[ST_MODE]
             if S_ISFIFO(mode):
                 cmd = open(self.location, 'w', buffering=1)
                 if len(self.data) == 1:
                     cmd.write(self.data[0].encode('utf-8') + '\n')
                 else:
                     cmd.write(
                         ('\n'.join(self.data) + '\n').encode('utf-8'))
                 cmd.close()
                 end = time.time()
                 self.logger.info(
                     "Data succesfully submitted to %s in %s seconds. %s commands processed. Delivery queue left %s items. Size: %s bytes."
                     % (self.location, round(end - start, 3), self.size,
                        self.queue_size, self.queue_bytes))
                 self.status = True
             else:
                 self.logger.error("%s is not a named pipe" %
                                   (self.location))
                 self.status = False
         else:
             self.logger.error("%s does not exist" % (self.location))
             self.status = False
     except Exception as error:
         self.logger.error(
             "Error submitting data to %s. Reason: %s. Delivery queue left %s items. Size: %s bytes"
             % (self.location, error, self.queue_size, self.queue_bytes))
         self.status = False
Example #4
0
    def run(self, interval):
        """
        Main loop, listen to named pipe and emit calls on each message.
        Before starting, first ensures that the named pipe exists.

        Exit the main loop by raising the exception Quit.
        """
        if not exists(self.name):
            umask = os.umask(0o000)
            os.mkfifo(self.name, mode=0o666)
            os.umask(umask)
        elif not S_ISFIFO(os.stat(self.name).st_mode):
            raise NotFifoError(self.name)

        fifo = os.open(self.name, os.O_RDONLY | os.O_NONBLOCK)
        try:
            while True:
                try:
                    _msg = os.read(fifo, 9999).decode('utf-8').strip()
                except OSError as err:
                    if err.errno == EAGAIN or err.errno == EWOULDBLOCK:
                        _msg = ''
                    else:
                        raise
                for msg in _msg.split('\n'):
                    self.emit(msg.strip())
                sleep(interval)
        except Quit:
            pass
        os.close(fifo)
Example #5
0
 def main2():
     hash_md5 = hashlib.md5()
     hash_sha256 = hashlib.sha256()
     hash_sha1 = hashlib.sha1()
     logger.info("Now we are hashing files on the disk!")
     csvfile = open(argv.outputDir + "/" + "filehash.csv", 'a')
     csvwriter = csv.writer(csvfile, dialect=("excel"))
     csvwriter.writerow(
         ["File Fullpath", "MD5 Hash", "SHA1 Hash", "SHA256 Hash"])
     for volume in volumeInfo:
         for parent, dirnames, filenames in os.walk(argv.mountDir + "/" +
                                                    volume.split(" ")[-1],
                                                    followlinks=False):
             for filename in filenames:
                 file_path = os.path.join(parent, filename)
                 if S_ISFIFO(os.stat(file_path).st_mode):
                     continue
                 try:
                     with open(file_path, "rb") as inputFile:
                         for chunk in iter(lambda: inputFile.read(4096),
                                           b""):
                             hash_md5.update(chunk)
                             hash_sha256.update(chunk)
                             hash_sha1.update(chunk)
                 except FileNotFoundError:
                     logger.critical("Error getting file %s content." %
                                     file_path)
                     continue
                 csvwriter.writerow([
                     file_path,
                     hash_md5.hexdigest(),
                     hash_sha256.hexdigest(),
                     hash_sha1.hexdigest()
                 ])
Example #6
0
def sys_main():
    import sys
    PY2 = sys.version_info[0] == 2
    if PY2:
        reload(sys); sys.setdefaultencoding('utf-8')
    import os
    from stat import S_ISFIFO
    err = None
    try:
        cols = get_cols()
    except Exception as ex:
        err = str(ex)
        cols = 80
    if S_ISFIFO(os.fstat(0).st_mode): # pipe mode
        md = sys.stdin.read()
    else:
        if not len(sys.argv) > 1 or '-h' in sys.argv:
            md = get_help(cols, PY2)
        else:
            md = sys.argv[1]
        if os.path.exists(md):
            with open(md) as fd:
                md = fd.read()
    if err:
        print(err)
        print md
    else:
        main(md, term_width=cols)
Example #7
0
def safe_make_fifo(path, mode=0o666):
    if os.path.exists(path):
        mode = os.stat(path)[ST_MODE]
        if not S_ISFIFO(mode):
            raise Exception("Path is not a FIFO: %s" % path)
    else:
        os.mkfifo(path, mode)
Example #8
0
    def inode_type(self, inode, symtab, addr_space):
        imode = inode.m('i_mode').v()

        type = "UNKN"

        if S_ISREG(imode):
            type = "REG"
        elif S_ISLNK(imode):
            type = "LNK"
        elif S_ISCHR(imode):
            type = "CHR"
        elif S_ISBLK(imode):
            type = "BLK"
        elif S_ISDIR(imode):
            type = "DIR"
        elif S_ISSOCK(imode):
            type = "SOCK"
        elif S_ISFIFO(imode):
            type = "FIFO"
            if symtab.lookup("rdwr_pipe_fops"):
                i_fop_offset = inode.get_member_offset('i_fop')
                if i_fop_offset > 0:

                    i_fop = inode.get_member('i_fop').v()

                    if i_fop == symtab.lookup("rdwr_pipe_fops"):
                        type = "PIPE"
        return type
Example #9
0
def get_fifo():
    candidates = glob('/tmp/uzbl_fifo_*')
    for file in candidates:
        if S_ISFIFO(os_stat(file).st_mode):
            return file
        else:
            return None
Example #10
0
 def __init__(self, config_file, uri, venv):
     self.config_file = config_file
     self.uri = uri
     self.venv = venv
     self.venvs = []
     self.process_name = venv if venv else 'main'
     self._input_detected = True if S_ISFIFO(
         os.fstat(0).st_mode) or S_ISREG(os.fstat(0).st_mode) else False
     self._stdin_mode = True if uri else False
     try:
         config.initialize_config(filename=self.config_file)
         logging.initialize_logging(self.process_name)
         if self._input_detected != self._stdin_mode:
             if self._stdin_mode:
                 raise RuntimeError(
                     'uri parameter found, but no input detected')
             else:
                 raise RuntimeError(
                     'stdin data detected, but no uri parameter found')
         self.session = session.initialize_komlog_session()
     except Exception as e:
         sys.stderr.write('Error initializing komlogd.\n')
         sys.stderr.write(str(e) + '\n')
         if logging.logger is not None and len(logging.logger.handlers) > 0:
             sys.stderr.write('Log info: ' +
                              logging.logger.handlers[0].baseFilename +
                              '\n')
             ex_info = traceback.format_exc().splitlines()
             for line in ex_info:
                 logging.logger.error(line)
         else:
             ex_info = traceback.format_exc().splitlines()
             for line in ex_info:
                 print(line)
         exit()
Example #11
0
def gen_obj(path,
            stat=None,
            chksum_handlers=None,
            real_location=None,
            stat_func=os.lstat,
            **overrides):
    """
    given a fs path, and an optional stat, create an appropriate fs obj.

    :param stat: stat object to reuse if available
    :param real_location: real path to the object if path is the desired
        location, rather then existent location.
    :raise KeyError: if no obj type matches the stat checks
    :return: :obj:`pkgcore.fs.fs.fsBase` derivative
    """

    if real_location is None:
        real_location = path
    if stat is None:
        try:
            stat = stat_func(real_location)
        except EnvironmentError as e:
            if stat_func == os.lstat or e.errno != errno.ENOENT:
                raise
            stat = os.lstat(real_location)

    mode = stat.st_mode
    d = {
        "mtime": stat.st_mtime,
        "mode": S_IMODE(mode),
        "uid": stat.st_uid,
        "gid": stat.st_gid
    }
    if S_ISREG(mode):
        d["size"] = stat.st_size
        d["data"] = local_source(real_location)
        d["dev"] = stat.st_dev
        d["inode"] = stat.st_ino
        if chksum_handlers is not None:
            d["chf_types"] = chksum_handlers
        d.update(overrides)
        return fsFile(path, **d)

    d.update(overrides)
    if S_ISDIR(mode):
        return fsDir(path, **d)
    elif S_ISLNK(mode):
        d["target"] = os.readlink(real_location)
        return fsSymlink(path, **d)
    elif S_ISFIFO(mode):
        return fsFifo(path, **d)
    else:
        major, minor = get_major_minor(stat)
        d["minor"] = minor
        d["major"] = major
        d["mode"] = mode
        return fsDev(path, **d)
Example #12
0
def check_if_there_is_data_being_piped_to_stdin():
    import sys
    import os
    from stat import S_ISFIFO

    if S_ISFIFO(os.fstat(0).st_mode):
        return True
    else:
        return False
Example #13
0
 def is_fifo(self):
     """
     Whether this path is a FIFO.
     """
     try:
         return S_ISFIFO(self.stat().st_mode)
     except OSError as e:
         if e.errno not in (ENOENT, ENOTDIR):
             raise
         return False
Example #14
0
def filetype_str(mode):
    if S_ISCHR(mode):
        msg = 'character special device file'
    elif S_ISBLK(mode):
        msg = 'block special device file'
    elif S_ISFIFO(mode):
        msg = 'FIFO (named pipe)'
    elif S_ISSOCK(mode):
        msg = 'socket'
    else:
        msg = 'unknown file type'
    return msg
Example #15
0
def fs_modes(stat: int) -> Iterator[Mode]:
    if S_ISDIR(stat):
        yield Mode.folder
    if S_ISREG(stat):
        yield Mode.file
    if S_ISFIFO(stat):
        yield Mode.pipe
    if S_ISSOCK(stat):
        yield Mode.socket
    for bit, mode in FILE_MODES.items():
        if stat & bit == bit:
            yield mode
Example #16
0
 def is_fifo(self):
     """
     Whether this path is a FIFO.
     """
     try:
         return S_ISFIFO(self.stat().st_mode)
     except OSError as e:
         if not _ignore_error(e):
             raise
         # Path doesn't exist or is a broken symlink
         # (see https://bitbucket.org/pitrou/pathlib/issue/12/)
         return False
Example #17
0
 def is_fifo(self):
     """
     Whether this path is a FIFO.
     """
     try:
         return S_ISFIFO(self.stat().st_mode)
     except OSError as e:
         #            if e.errno not in (ENOENT, ENOTDIR):
         if e_errno_not_in_ENOENT_ENOTDIR(e):  ###
             raise
         # Path doesn't exist or is a broken symlink
         # (see https://bitbucket.org/pitrou/pathlib/issue/12/)
         return False
Example #18
0
def main():
    MODE = fstat(stdin.fileno()).st_mode
    if S_ISREG(MODE) or S_ISFIFO(MODE):
        PROMPT = ""
    else:
        PROMPT = "roll> "

    try:
        while True:
            for line in execute(input(PROMPT)):
                print(line)

    except (KeyboardInterrupt, EOFError):
        exit()
Example #19
0
 def get_fifo(self):
     """
     Look for UZBL's FIFO-file in /tmp.
     Don't give up until it has been found.
     """
     candidates = glob('/tmp/uzbl_fifo_*')
     for file in candidates:
         if S_ISFIFO(os_stat(file).st_mode):
             self.mon.log(self, 'Found UZBL fifo  in %s.' % file)
             self.fifo=file
             self.start_play_signal=True
             return
     # print 'not found trying again'
     self.widget.after(500,self.get_fifo)
Example #20
0
 def is_fifo(self):
     """
     Whether this path is a FIFO.
     """
     try:
         return S_ISFIFO(self.stat().st_mode)
     except OSError as e:
         if not _ignore_error(e):
             raise
         # Path doesn't exist or is a broken symlink
         # (see http://web.archive.org/web/20200623061726/https://bitbucket.org/pitrou/pathlib/issues/12/ )
         return False
     except ValueError:
         # Non-encodable path
         return False
Example #21
0
 def is_fifo(self):
     """
     Whether this path is a FIFO.
     """
     try:
         return S_ISFIFO(self.stat().st_mode)
     except OSError as e:
         if e.errno not in _IGNORED_ERROS:
             raise
         # Path doesn't exist or is a broken symlink
         # (see https://bitbucket.org/pitrou/pathlib/issue/12/)
         return False
     except ValueError:
         # Non-encodable path
         return False
Example #22
0
def check(binary):
    if not access(binary, X_OK):
        print_silent("[-] Binary can't be executed")
        print_silent("[-] Exiting")
        exit(1)
    else:
        print_silent("[+] Binary is executable")
    try:
        if not S_ISFIFO(stat(FIFO).st_mode):
            print_silent("[-] Unable to create named pipe")
            print_silent("[-] Exiting")
            exit(1)
    except OSError as oe:
        mkfifo(FIFO)
    pass
Example #23
0
 def file_hl_group(self, file, stat_res=None, stat_error=None):
     """Return the highlight group that `file` should be colored in."""
     if stat_error is not None:
         return 'Error'
     if stat_res is None:
         return self.file_hl_group(file, *stat_path(file))
     mode = stat_res.st_mode
     if not S_ISREG(mode):  # Not a regular file
         if S_ISLNK(mode):
             if self._colors_special.get('ln') == 'target':
                 # TODO
                 # resolved = file.resolve()
                 # if resolved == file:
                 #     # Don't try to resolve another time
                 #     # TODO
                 #     raise Exception('recursion! %s' % resolved)
                 return self.file_hl_group(file,
                                           *stat_path(file, lstat=False))
             else:
                 ansi_color = self._colors_special.get('ln')
         elif S_ISCHR(mode):
             ansi_color = self._colors_special.get('cd')
         elif S_ISDIR(mode):
             ansi_color = self._colors_special.get('di')
         elif S_ISFIFO(mode):
             ansi_color = self._colors_special.get('pi')
         elif S_ISBLK(mode):
             ansi_color = self._colors_special.get('bd')
         elif S_ISSOCK(mode):
             ansi_color = self._colors_special.get('so')
         else:
             # TODO Does this happen?
             return 'Error'
     elif mode & S_IXUSR:  # Executable
         ansi_color = self._colors_special.get('ex')
     else: # Regular file
         needle = file.name.lower()
         for pattern, colorcode in self._colors.items():
             if needle.endswith(pattern):
                 ansi_color = colorcode
                 break
         else:
             # TODO Could not find a target color
             return None
     if ansi_color is None:
         return None
     hl_group = 'color' + ansi_color.replace(';', '_')
     return hl_group
Example #24
0
    async def __new__(cls, loop, pipe, protocol, extra=None):
        fileno = pipe.fileno()
        mode = os.fstat(fileno).st_mode
        is_char = S_ISCHR(mode)
        is_fifo = S_ISFIFO(mode)
        is_socket = S_ISSOCK(mode)
        if not (is_char or is_fifo or is_socket):
            raise ValueError(
                'Pipe transport is only for pipes, sockets and character devices.'
            )

        if extra is None:
            extra = {}

        extra['pipe'] = pipe

        self = object.__new__(cls)
        self._extra = extra
        self.loop = loop
        self.protocol_paused = False
        self.pipe = pipe
        self.fileno = fileno
        self.protocol = protocol
        self._buffer = bytearray()
        self._connection_lost = 0
        self.closing = False  # Set when close() or write_eof() called.

        self._high_water = 65536
        self._low_water = 16384

        try:
            os.set_blocking(fileno, False)
            # skip 1 callback loop
            future = Future(loop)
            loop.call_soon(Future.set_result_if_pending, future, None)
            await future

            protocol.connection_made(self)

            # On AIX, the reader trick (to be notified when the read end of the  socket is closed) only works for
            # sockets. On other platforms it works for pipes and sockets.
            if is_socket or (is_fifo and not IS_AIX):
                loop.add_reader(fileno, self._read_ready)
        except:
            self.close()
            raise

        return self
Example #25
0
 async def is_fifo(self) -> bool:
   """
   Whether this path is a FIFO.
   """
   try:
     stat = await self.stat()
     return S_ISFIFO(stat.st_mode)
   except OSError as e:
     if not _ignore_error(e):
       raise
     # Path doesn't exist or is a broken symlink
     # (see https://bitbucket.org/pitrou/pathlib/issue/12/)
     return False
   except ValueError:
     # Non-encodable path
     return False
Example #26
0
    def __init__(self):
        self.dlv = dlv()
        self.actions = []
        self.shot = None
        self.grabbed = None
        self.world = {}
        self.position = Location(1, 1)
        self.orientation = Orientation.RIGHT
        self.killed = False
        self.bumped = None
        self.previousAction = None
        self.size = 2
        self.paint = exists('agent') and S_ISFIFO(stat('agent').st_mode)

        _, self.prog = mkstemp()
        unlite(join(dirname(__file__), 'agent.md'), self.prog)
Example #27
0
def check_input_files(file_path, force):
    """Check the status of the file.

    If the file is empty or doesn't exist AND if the file is NOT a
    fifo/block/named pipe then a warning is printed and sys.exit(1) is
    called
    """
    mode = None

    if file_path == '-':
        return

    try:
        mode = os.stat(file_path).st_mode
    except OSError:
        print("ERROR: Input file %s does not exist" % file_path,
              file=sys.stderr)

        if not force:
            print("NOTE: This can be overridden using the --force argument",
                  file=sys.stderr)
            print("Exiting", file=sys.stderr)
            sys.exit(1)
        else:
            return

    # block devices/stdin will be nonzero
    if S_ISBLK(mode) or S_ISFIFO(mode) or S_ISCHR(mode):
        return

    if not os.path.exists(file_path):
        print("ERROR: Input file %s does not exist; exiting" % file_path,
              file=sys.stderr)
        if not force:
            print("NOTE: This can be overridden using the --force argument",
                  file=sys.stderr)
            sys.exit(1)
    else:
        if os.stat(file_path).st_size == 0:
            print("ERROR: Input file %s is empty; exiting." % file_path,
                  file=sys.stderr)
            if not force:
                print(
                    "NOTE: This can be overridden using the --force"
                    " argument",
                    file=sys.stderr)
                sys.exit(1)
Example #28
0
def connect(device=DEFAULT_DEVICE):
    device = os.getenv("BLEMU_DEVICE", device)
    if not os.path.exists(device):
        raise ValueError("Device `%s` does not exist. Cannot connect" % device)

    mode = os.stat(device).st_mode
    ser = None
    if S_ISCHR(mode):
        ser = serial.Serial(port=device, baudrate=BAUD_RATE)
    elif S_ISFIFO(mode) or S_ISREG(mode):
        ser = open(device, "w")

    # So! Apparently when you connect to the arduino serial port, the
    # bootloader kicks in, resets the arduino and waits a second for a new
    # program to be loaded before running the actual already stored code
    time.sleep(2)
    return ser
Example #29
0
def analyze(src, length=io.DEFAULT_BUFFER_SIZE):
    md5 = hashlib.md5()
    src = os.path.abspath(src)
    try:
        mode = os.stat(src).st_mode

        if S_ISREG(mode):
            upsert_file_metadata(src,
                                 stat_types['REGULAR'],
                                 size=os.path.getsize(src),
                                 extension=os.path.splitext(src)[1])
        elif S_ISDIR(mode):
            upsert_file_metadata(src, stat_types['DIRECTORY'])
        elif S_ISCHR(mode):
            upsert_file_metadata(src, stat_types['CHAR'])
        elif S_ISBLK(mode):
            upsert_file_metadata(src, stat_types['BLOCK'])
        elif S_ISFIFO(mode):
            upsert_file_metadata(src, stat_types['FIFO'])
        elif S_ISLNK(mode):
            upsert_file_metadata(src, stat_types['SYMLINK'])
        elif S_ISSOCK(mode):
            upsert_file_metadata(src, stat_types['SOCKET'])
        else:
            upsert_file_metadata(src, stat_types['UNKNOWN'])
    except FileNotFoundError:
        mode = os.stat(src, follow_symlinks=False).st_mode
        if S_ISLNK(mode):
            upsert_file_metadata(src, stat_types['BROKEN_SYMLINK'])

    # Just return the MD5 hash of an empty string for non-regular files
    if not S_ISREG(mode):
        return md5

    try:
        upsert_file_metadata(src,
                             mime_type=(magic.from_file(src, mime=True)),
                             mime_detail=magic.from_file(src))
        with io.open(src, mode="rb") as fd:
            for chunk in iter(lambda: fd.read(length), b''):
                md5.update(chunk)
    except OSError:
        upsert_file_metadata(src, stat_types['ERROR'])
        pass
    return md5
Example #30
0
 def get_file_type(self, root, filename):
     filepath = fpath.join(root, filename)
     filemode = stat(filepath).st_mode
     if S_ISREG(filemode):
         return "f"
     elif S_ISSOCK(filemode):
         return "s"
     elif S_ISFIFO(filemode):
         return "p"
     elif S_ISLNK(filemode):
         return "l"
     elif S_ISDIR(filemode):
         return "d"
     elif S_ISCHR(filemode):
         return "c"
     elif S_ISBLK(filemode):
         return "b"
     return "?"