def is_block_device(path):
    '''
    Confirm device at path is a valid block device node.

    :returns: boolean: True if path is a block device, False if not.
    '''
    return S_ISBLK(stat(path).st_mode)
Exemple #2
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
Exemple #3
0
def is_block(fthing):
    """Take in a file object and checks to see if it's a block or fifo."""
    if fthing is sys.stdout or fthing is sys.stdin:
        return True
    else:
        mode = os.stat(fthing.name).st_mode
        return S_ISBLK(mode) or S_ISCHR(mode)
Exemple #4
0
def format_device(mode, major, minor):
    if S_ISCHR(mode):
        kind = 'character'
    elif S_ISBLK(mode):
        kind = 'block'
    else:
        kind = 'weird'
    return 'device:%s\nmajor: %d\nminor: %d\n' % (kind, major, minor)
Exemple #5
0
 def checkRaidPath(self):
     logging.info("# Checking for device " + self.getDevPath())
     try:
         mode = lstat(self.getDevPath()).st_mode
     except OSError:
         return False
     else:
         return S_ISBLK(mode)
 def is_block_device(self):
     """
     Whether this path is a block device.
     """
     try:
         return S_ISBLK(self.stat().st_mode)
     except OSError as e:
         if e.errno not in (ENOENT, ENOTDIR):
             raise
         return False
Exemple #7
0
    def __init__(self, device):
        """ Function doc """

        if not os.path.exists(device):
            raise RuntimeError('Cannot find device "%s"' % device)

        mode = os.stat(device).st_mode
        if not S_ISBLK(mode):
            raise RuntimeError('"%s" is not a valid block device' % device)

        self._device = device
Exemple #8
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
Exemple #9
0
 def is_block_device(self):
     """
     Whether this path is a block device.
     """
     try:
         return S_ISBLK(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
Exemple #10
0
 def is_block_device(self):
     """
     Whether this path is a block device.
     """
     try:
         return S_ISBLK(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
Exemple #11
0
    def isBlockDevice(self):
        """
        Returns whether the underlying path is a block device.

        :return: C{True} if it is a block device, C{False} otherwise
        :rtype: L{bool}
        """
        st = self.statinfo
        if not st:
            self.restat(False)
            st = self.statinfo
            if not st:
                return False
        return S_ISBLK(st.st_mode)
Exemple #12
0
 def is_block_device(self):
     """
     Whether this path is a block device.
     """
     try:
         return S_ISBLK(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
Exemple #13
0
 def is_block_device(self):
     """
     Whether this path is a block device.
     """
     try:
         return S_ISBLK(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
Exemple #14
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
Exemple #15
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)
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
Exemple #17
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 "?"
Exemple #18
0
def check_valid_file_exists(in_files):
    """Warn if input files are empty or missing.

    In a scenario where we expect multiple input files and
    are OK with some of them being empty or non-existent,
    this check warns to stderr if any input file is empty
    or non-existent.
    """
    for in_file in in_files:
        if os.path.exists(in_file):
            mode = os.stat(in_file).st_mode
            if os.stat(in_file).st_size > 0 or S_ISBLK(mode) or S_ISFIFO(mode):
                return
            else:
                print('WARNING: Input file %s is empty' % in_file,
                      file=sys.stderr)
        else:
            print('WARNING: Input file %s not found' % in_file,
                  file=sys.stderr)
Exemple #19
0
  async def is_block_device(self) -> bool:
    """
    Whether this path is a block device.
    """
    try:
      stat = await self.stat()
      return S_ISBLK(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
Exemple #20
0
def get_ftype_and_perm(mode):
    """ Returns a tuple of whether the file is: file(regular file)/dir/slink
    /char. spec/block spec/pipe/socket and the permission bits of the file.
    If it does not match any of the cases below (it will return "unknown" twice)"""
    if S_ISREG(mode):
        return "file", S_IMODE(mode)
    if S_ISDIR(mode):
        return "dir", S_IMODE(mode)
    if S_ISLNK(mode):
        return "slink", S_IMODE(mode)
    if S_ISCHR(mode):
        return "character special", S_IMODE(mode)
    if S_ISBLK(mode):
        return "block special", S_IMODE(mode)
    if S_ISFIFO(mode):
        return "pipe", S_IMODE(mode)
    if S_ISSOCK(mode):
        return "socket", S_IMODE(mode)
    return "unknown", "unknown"
Exemple #21
0
def check_file_status(file_path):
    """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 = os.stat(file_path).st_mode
    # block devices will be nonzero
    if S_ISBLK(mode) or S_ISFIFO(mode):
        return

    if not os.path.exists(file_path):
        print >>sys.stderr, "ERROR: Input file %s does not exist; exiting" % \
                            file_path
        sys.exit(1)
    else:
        if os.stat(file_path).st_size == 0:
            print >>sys.stderr, "ERROR: Input file %s is empty; exiting." % \
                                file_path
            sys.exit(1)
Exemple #22
0
def validate_blockdev(imgsize):
    blockdev = cfg.opts.primary_blockdev
    try:
        mode = os.stat(blockdev).st_mode
    except OSError as e:
        print(c.RED(e))
        print(
            "It looks like you passed the wrong thing to the --primary-blockdev option"
        )
        exit(1)
    if not S_ISBLK(mode):
        print(
            c.RED(
                "File passed as an arg to the --primary-blockdev option is not a block device"
            ))
        print(
            "This option is meant to be used to create a VM whose primary storage is a block device like /dev/sdb2"
        )
        exit(1)
    cmd = ['lsblk', '-no', 'size', blockdev]
    c.debug("Executing: {}".format(" ".join(cmd)))
    try:
        blksize = subprocess.check_output(cmd).strip()
    except:
        print(
            c.RED(
                "Unexpected error using lsblk to check size of --primary-blockdev device"
            ))
        print(
            "This shouldn't happen ... unless you somehow don't have util-linux and the lsblk command"
        )
        exit(1)
    blksize = convert_size_string_to_bytes(blksize)
    if imgsize > blksize:
        print(
            c.YELLOW(
                "Aborting because block device '{}' is smaller than projected image size"
                .format(blockdev)))
        print("Block device size: {} MiB".format(int(blksize / 1024 / 1024)))
        print("Image file size:   {} MiB".format(int(imgsize / 1024 / 1024)))
        exit(1)
Exemple #23
0
def get_ctl(path, mode):
    """get the string to control device access for cgroup
    :param path: the device file path
    :param mode: either "r" or "rw"
    :return: the string to control device access
    """
    try:
        st = os.stat(path)
    except OSError as e:
        log.error("Failed to get stat of {}: {}".format(path, str(e)))
        raise

    t = ""
    if S_ISBLK(st.st_mode):
        t = "b"
    elif S_ISCHR(st.st_mode):
        t = "c"
    if t and mode in ("r", "rw"):
        return "{} {}:{} {}".format(t, os.major(st.st_rdev),
                                    os.minor(st.st_rdev), mode)
    raise RuntimeError("Failed to get control string of {}".format(path))
Exemple #24
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 is '-':
        return
    try:
        mode = os.stat(file_path).st_mode
    except OSError:
        print >>sys.stderr, "ERROR: Input file %s does not exist" % \
                            file_path

        if not force:
            print >> sys.stderr, "Exiting"
            sys.exit(1)
        else:
            return

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

    if not os.path.exists(file_path):
        print >>sys.stderr, "ERROR: Input file %s does not exist; exiting" % \
                            file_path
        if not force:
            sys.exit(1)
    else:
        if os.stat(file_path).st_size == 0:
            print >>sys.stderr, "ERROR: Input file %s is empty; exiting." % \
                                file_path
            if not force:
                sys.exit(1)
Exemple #25
0
    def realdev(self):
        if self.fs_type in ("none", "tmpfs", "bind"):
            return
        if self.device is None:
            return
        if self.device.startswith("LABEL=") or self.device.startswith("UUID="):
            try:
                _dev = utilities.devices.linux.label_to_dev(
                    self.device, self.svc.node.devtree)
            except ex.Error as exc:
                self.status_log(str(exc))
                _dev = None
            if _dev:
                return _dev
            return self.device
        try:
            mode = os.stat(self.device)[ST_MODE]
        except:
            self.log.debug("can not stat %s", self.device)
            return

        if os.path.exists(self.device) and S_ISBLK(mode):
            dev = self.device
        else:
            mnt = getmount(self.device)
            if self.mounts is None:
                self.mounts = Mounts()
            mount = self.mounts.has_param("mnt", mnt)
            if mount is None:
                self.log.debug("can't find dev %s mounted in %s in mnttab",
                               mnt, self.device)
                return None
            dev = mount.dev
        if dev in ("tmpfs", "shm", "shmfs", "none"):
            # bind mounts for ex.
            return
        return dev
Exemple #26
0
def stat_is_special(attr):
    return S_ISCHR(attr.st_mode) or S_ISBLK(attr.st_mode)
Exemple #27
0
    def is_up(self):
        if self.device is None:
            self.status_log("dev is not defined", "info")
            return False
        if self.mount_point is None:
            self.status_log("mnt is not defined", "info")
            return False
        self.mounts = Mounts()
        for dev in [
                self.device
        ] + utilities.devices.linux.udevadm_query_symlink(self.device):
            ret = self.mounts.has_mount(dev, self.mount_point)
            if ret:
                return True

        # might be defined as a symlink. Linux display realpaths in /proc/mounts
        ret = self.mounts.has_mount(self.device,
                                    os.path.realpath(self.mount_point))
        if ret:
            return True

        # might be defined as a symlink. Linux display realpaths in /proc/mounts
        ret = self.mounts.has_mount(os.path.realpath(self.device),
                                    os.path.realpath(self.mount_point))
        if ret:
            return True

        # might be a loop device seen in mounts as its backing file
        if self.device.startswith("/dev/loop"):
            backfile = utilities.devices.linux.loop_to_file(self.device)
            if backfile and self.mounts.has_mount(
                    backfile, os.path.realpath(self.mount_point)):
                return True

        # might be a mount by label or uuid
        for dev in self.sub_devs():
            ret = self.mounts.has_mount(dev, self.mount_point)
            if ret:
                return True
            ret = self.mounts.has_mount(dev,
                                        os.path.realpath(self.mount_point))
            if ret:
                return True
            if dev.startswith("/dev/loop"):
                backfile = utilities.devices.linux.loop_to_file(dev)
                if backfile and self.mounts.has_mount(
                        backfile, os.path.realpath(self.mount_point)):
                    return True

        # might be mount using a /dev/mapper/ name too
        elements = self.device.split('/')
        if len(elements) == 4 and elements[2] != "mapper":
            dev = "/dev/mapper/%s-%s" % (elements[2].replace(
                '-', '--'), elements[3].replace('-', '--'))
            ret = self.mounts.has_mount(dev, self.mount_point)
            if ret:
                return True
            ret = self.mounts.has_mount(dev,
                                        os.path.realpath(self.mount_point))
            if ret:
                return True

        if self.device.startswith(os.sep) and os.path.exists(self.device):
            try:
                fstat = os.stat(self.device)
                mode = fstat[ST_MODE]
            except:
                self.log.debug("can not stat %s", self.device)
                return False

            if S_ISREG(mode):
                # might be a loopback mount
                devs = utilities.devices.linux.file_to_loop(self.device)
                for dev in devs:
                    ret = self.mounts.has_mount(dev, self.mount_point)
                    if ret:
                        return True
                    ret = self.mounts.has_mount(
                        dev, os.path.realpath(self.mount_point))
                    if ret:
                        return True
            elif S_ISBLK(mode):
                # might be a mount using a /dev/dm-<minor> name too
                if os.major(fstat.st_rdev) == self.dm_major:
                    dev = '/dev/dm-' + str(os.minor(fstat.st_rdev))
                    ret = self.mounts.has_mount(dev, self.mount_point)
                    if ret:
                        return True
                    ret = self.mounts.has_mount(
                        dev, os.path.realpath(self.mount_point))
                    if ret:
                        return True
            elif S_ISDIR(mode):
                try:
                    mnt_fstat = os.stat(self.mount_point)
                    mnt_ino = mnt_fstat[ST_INO]
                except:
                    self.log.debug("can not stat %s", self.mount_point)
                    return False
                dev_ino = fstat[ST_INO]
                if dev_ino == mnt_ino:
                    return True

        return False
Exemple #28
0
 def is_device(self):
     mode = os.lstat(self._name).st_mode
     return S_ISCHR(mode) or S_ISBLK(mode)
Exemple #29
0
 def isdev(self):
     from stat import S_ISBLK, S_ISCHR
     mode = self.__st_mode()
     return S_ISBLK(mode) or S_ISCHR(mode)
Exemple #30
0
 def isblockdev(self):
     from stat import S_ISBLK
     return S_ISBLK(self.__st_mode())