def FileEntryExistsByPathSpec(self, path_spec): """Determines if a file entry for a path specification exists. Args: path_spec: a path specification (instance of path.PathSpec). Returns: Boolean indicating if the file entry exists. """ location = getattr(path_spec, 'location', None) if location is None: return False is_device = False if platform.system() == 'Windows': # Windows does not support running os.path.exists on device files # so we use libsmdev to do the check. try: is_device = pysmdev.check_device(location) except IOError as exception: # Since pysmdev will raise IOError when it has no access to the device # we check if the exception message contains ' access denied ' and # return true. if u' access denied ' in exception.message: is_device = True if not is_device and not os.path.exists(location): return False return True
def FileEntryExistsByPathSpec(self, path_spec): """Determines if a file entry for a path specification exists. Args: path_spec (PathSpec): a path specification. Returns: bool: True if the file entry exists, false otherwise. """ location = getattr(path_spec, 'location', None) if location is None: return False is_device = False if platform.system() == 'Windows': # Note that os.path.exists() returns False for Windows device files so # instead use libsmdev to do the check. try: is_device = pysmdev.check_device(location) except IOError as exception: # Since pysmdev will raise IOError when it has no access to the device # we check if the exception message contains ' access denied ' and # set is_device to True. is_device = bool(' access denied ' in str(exception)) # Note that os.path.exists() returns False for broken symbolic links hence # an additional check using os.path.islink() is necessary. return is_device or os.path.exists(location) or os.path.islink( location)
def _Open(self, path_spec=None, mode='rb'): """Opens the file-like object defined by path specification. Args: path_spec: optional path specification (instance of path.PathSpec). The default is None. mode: optional file access mode. The default is 'rb' read-only binary. Raises: AccessError: if the access to open the file was denied. IOError: if the file-like object could not be opened. PathSpecError: if the path specification is incorrect. ValueError: if the path specification is invalid. """ if not path_spec: raise ValueError(u'Missing path specfication.') if path_spec.HasParent(): raise errors.PathSpecError(u'Unsupported path specification with parent.') location = getattr(path_spec, 'location', None) if location is None: raise errors.PathSpecError(u'Path specification missing location.') # Windows does not support running os.stat on device files so we use # libsmdev to do an initial check. try: is_device = pysmdev.check_device(location) except IOError as exception: # Since os.stat() will not recognize Windows device file names and # will return '[Error 87] The parameter is incorrect' we check here # if pysmdev exception message contains ' access denied ' and raise # AccessError instead. if u' access denied ' in exception.message: raise errors.AccessError( u'Access denied to file: {0:s} with error: {1:s}'.format( location, exception)) is_device = False if not is_device: try: stat_info = os.stat(location) except OSError as exception: raise IOError(u'Unable to open file with error: {0:s}.'.format( exception)) # In case the libsmdev check is not able to detect the device also use # the stat information. if stat.S_ISCHR(stat_info.st_mode) or stat.S_ISBLK(stat_info.st_mode): is_device = True if is_device: self._file_object = pysmdev.handle() self._file_object.open(location, mode=mode) self._size = self._file_object.media_size else: self._file_object = open(location, mode=mode) self._size = stat_info.st_size
def _Open(self, mode='rb'): """Opens the file-like object defined by path specification. Args: mode (Optional[str]): file access mode. Raises: AccessError: if the access to open the file was denied. IOError: if the file-like object could not be opened. OSError: if the file-like object could not be opened. PathSpecError: if the path specification is incorrect. """ if self._path_spec.HasParent(): raise errors.PathSpecError('Unsupported path specification with parent.') location = getattr(self._path_spec, 'location', None) if location is None: raise errors.PathSpecError('Path specification missing location.') # Windows does not support running os.stat on device files so we use # libsmdev to do an initial check. try: is_device = pysmdev.check_device(location) except IOError as exception: # Since os.stat() will not recognize Windows device file names and # will return '[Error 87] The parameter is incorrect' we check here # if pysmdev exception message contains ' access denied ' and raise # AccessError instead. if ' access denied ' in str(exception): raise errors.AccessError( 'Access denied to file: {0:s} with error: {1!s}'.format( location, exception)) is_device = False if not is_device: try: stat_info = os.stat(location) except OSError as exception: raise IOError('Unable to open file with error: {0!s}.'.format( exception)) # In case the libsmdev check is not able to detect the device also use # the stat information. if stat.S_ISCHR(stat_info.st_mode) or stat.S_ISBLK(stat_info.st_mode): is_device = True if is_device: smdev_handle = pysmdev.handle() smdev_handle.open(location, mode=mode) self._file_object = smdev_handle self._size = smdev_handle.media_size else: self._file_object = open(location, mode=mode) self._size = stat_info.st_size
def PathExists(file_path): """Determine whether given file path exists as a file, directory or a device. Args: file_path: A string denoting the file path that needs checking. Returns: A tuple, a boolean indicating whether or not the path exists and a string that contains the reason, if any, why this was not determined to be a file. """ if os.path.exists(file_path): return True, u'' try: if pysmdev.check_device(file_path): return True, u'' except IOError as exception: return False, u'Unable to determine, with error: {0:s}'.format(exception) return False, u'Not an existing file.'
def PathExists(file_path): """Determine whether given file path exists as a file, directory or a device. Args: file_path: A string denoting the file path that needs checking. Returns: A tuple, a boolean indicating whether or not the path exists and a string that contains the reason, if any, why this was not determined to be a file. """ if os.path.exists(file_path): return True, u'' try: if pysmdev.check_device(file_path): return True, u'' except IOError as exception: return False, u'Unable to determine, with error: {0:s}'.format(exception) return False, u'Not an existing file.'
def FileEntryExistsByPathSpec(self, path_spec): """Determines if a file entry for a path specification exists. Args: path_spec: a path specification (instance of path.PathSpec). Returns: Boolean indicating if the file entry exists. """ location = getattr(path_spec, u'location', None) if location is None: return False is_device = False if platform.system() == u'Windows': # Windows does not support running os.path.exists on device files # so we use libsmdev to do the check. try: is_device = pysmdev.check_device(location) except IOError as exception: # Since pysmdev will raise IOError when it has no access to the device # we check if the exception message contains ' access denied ' and # return true. # Note that exception.message no longer works in Python 3. exception_string = str(exception) if not isinstance(exception_string, py2to3.UNICODE_TYPE): exception_string = py2to3.UNICODE_TYPE(exception_string, errors=u'replace') if u' access denied ' in exception_string: is_device = True if not is_device and not os.path.exists(location): return False return True
def FileEntryExistsByPathSpec(self, path_spec): """Determines if a file entry for a path specification exists. Args: path_spec: a path specification (instance of PathSpec). Returns: Boolean indicating if the file entry exists. """ location = getattr(path_spec, u'location', None) if location is None: return False is_device = False if platform.system() == u'Windows': # Windows does not support running os.path.exists on device files # so we use libsmdev to do the check. try: is_device = pysmdev.check_device(location) except IOError as exception: # Since pysmdev will raise IOError when it has no access to the device # we check if the exception message contains ' access denied ' and # return true. # Note that exception.message no longer works in Python 3. exception_string = str(exception) if not isinstance(exception_string, py2to3.UNICODE_TYPE): exception_string = py2to3.UNICODE_TYPE( exception_string, errors=u'replace') if u' access denied ' in exception_string: is_device = True if not is_device and not os.path.exists(location): return False return True
def FileEntryExistsByPathSpec(self, path_spec): """Determines if a file entry for a path specification exists. Args: path_spec (PathSpec): a path specification. Returns: bool: True if the file entry exists, false otherwise. """ location = getattr(path_spec, 'location', None) if location is None: return False is_device = False if platform.system() == 'Windows': # Note that os.path.exists() returns False for Windows device files so # instead use libsmdev to do the check. try: is_device = pysmdev.check_device(location) except IOError as exception: # Since pysmdev will raise IOError when it has no access to the device # we check if the exception message contains ' access denied ' and # return true. # Note that exception.message no longer works in Python 3. exception_string = str(exception) if not isinstance(exception_string, py2to3.UNICODE_TYPE): exception_string = py2to3.UNICODE_TYPE( exception_string, errors='replace') if ' access denied ' in exception_string: is_device = True # Note that os.path.exists() returns False for broken symbolic links hence # an additional check using os.path.islink() is necessary. return is_device or os.path.exists(location) or os.path.islink(location)
def __init__(self, resolver_context, file_system, path_spec, is_root=False): """Initializes a file entry. Args: resolver_context (Context): resolver context. file_system (FileSystem): file system. path_spec (PathSpec): path specification. is_root (Optional[bool]): True if the file entry is the root file entry of the corresponding file system. """ location = getattr(path_spec, 'location', None) # Windows does not support running os.stat on device files so we use # libsmdev to do an initial check. is_windows_device = False if self._OS_IS_WINDOWS and location: try: # pylint: disable=no-member is_windows_device = pysmdev.check_device(location) except IOError: pass stat_info = None if not is_windows_device and location: # We are only catching OSError. However on the Windows platform # a WindowsError can be raised as well. We are not catching that since # that error does not exist on non-Windows platforms. try: stat_info = os.lstat(location) except (IOError, OSError): stat_info = None super(OSFileEntry, self).__init__(resolver_context, file_system, path_spec, is_root=is_root, is_virtual=False) self._is_windows_device = is_windows_device self._name = None self._stat_info = stat_info if is_windows_device: self.entry_type = definitions.FILE_ENTRY_TYPE_DEVICE elif stat_info: # If location contains a trailing segment separator and points to # a symbolic link to a directory stat info will not indicate # the file entry as a symbolic link. The following check ensures # that the LINK type is correctly detected. is_link = os.path.islink(location) # The stat info member st_mode can have multiple types e.g. # LINK and DIRECTORY in case of a symbolic link to a directory # dfVFS currently only supports one type so we need to check # for LINK first. if stat.S_ISLNK(stat_info.st_mode) or is_link: self.entry_type = definitions.FILE_ENTRY_TYPE_LINK elif stat.S_ISREG(stat_info.st_mode): self.entry_type = definitions.FILE_ENTRY_TYPE_FILE elif stat.S_ISDIR(stat_info.st_mode): self.entry_type = definitions.FILE_ENTRY_TYPE_DIRECTORY elif (stat.S_ISCHR(stat_info.st_mode) or stat.S_ISBLK(stat_info.st_mode)): self.entry_type = definitions.FILE_ENTRY_TYPE_DEVICE elif stat.S_ISFIFO(stat_info.st_mode): self.entry_type = definitions.FILE_ENTRY_TYPE_PIPE elif stat.S_ISSOCK(stat_info.st_mode): self.entry_type = definitions.FILE_ENTRY_TYPE_SOCKET
def _GetStat(self): """Retrieves the stat object (instance of vfs.VFSStat). Raises: BackEndError: If an OSError comes up it is caught and an BackEndError error is raised instead. Returns: Stat object (instance of VFSStat) or None if no location is set. """ location = getattr(self.path_spec, 'location', None) if location is None: return stat_object = vfs_stat.VFSStat() is_windows_device = False stat_info = None # Windows does not support running os.stat on device files so we use # libsmdev to do an initial check. if platform.system() == 'Windows': try: is_windows_device = pysmdev.check_device(location) except IOError: pass if is_windows_device: stat_object.type = stat_object.TYPE_DEVICE else: # We are only catching OSError. However on the Windows platform # a WindowsError can be raised as well. We are not catching that since # that error does not exist on non-Windows platforms. try: stat_info = os.stat(location) except OSError as exception: raise errors.BackEndError( u'Unable to retrieve stat object with error: {0:s}'.format( exception)) # File data stat information. stat_object.size = stat_info.st_size # Date and time stat information. stat_object.atime = stat_info.st_atime stat_object.ctime = stat_info.st_ctime stat_object.mtime = stat_info.st_mtime # Ownership and permissions stat information. stat_object.mode = stat.S_IMODE(stat_info.st_mode) stat_object.uid = stat_info.st_uid stat_object.gid = stat_info.st_gid # If location contains a trailing segment separator and points to # a symbolic link to a directory stat info will not indicate # the file entry as a symbolic link. The following check ensures # that the LINK type is correctly detected. is_link = os.path.islink(location) # File entry type stat information. # The stat info member st_mode can have multiple types e.g. # LINK and DIRECTORY in case of a symbolic link to a directory # dfVFS currently only supports one type so we need to check # for LINK first. if stat.S_ISLNK(stat_info.st_mode) or is_link: stat_object.type = stat_object.TYPE_LINK elif stat.S_ISREG(stat_info.st_mode): stat_object.type = stat_object.TYPE_FILE elif stat.S_ISDIR(stat_info.st_mode): stat_object.type = stat_object.TYPE_DIRECTORY elif (stat.S_ISCHR(stat_info.st_mode) or stat.S_ISBLK(stat_info.st_mode)): stat_object.type = stat_object.TYPE_DEVICE elif stat.S_ISFIFO(stat_info.st_mode): stat_object.type = stat_object.TYPE_PIPE elif stat.S_ISSOCK(stat_info.st_mode): stat_object.type = stat_object.TYPE_SOCKET # Other stat information. stat_object.ino = stat_info.st_ino # stat_info.st_dev # stat_info.st_nlink return stat_object
def _GetStat(self): """Retrieves the stat object (instance of vfs.VFSStat). Raises: BackEndError: If an OSError comes up it is caught and an BackEndError error is raised instead. Returns: Stat object (instance of VFSStat) or None if no location is set. """ location = getattr(self.path_spec, u'location', None) if location is None: return stat_object = vfs_stat.VFSStat() is_windows_device = False stat_info = None # Windows does not support running os.stat on device files so we use # libsmdev to do an initial check. if platform.system() == u'Windows': try: is_windows_device = pysmdev.check_device(location) except IOError: pass if is_windows_device: stat_object.type = stat_object.TYPE_DEVICE else: # We are only catching OSError. However on the Windows platform # a WindowsError can be raised as well. We are not catching that since # that error does not exist on non-Windows platforms. try: stat_info = os.stat(location) except OSError as exception: raise errors.BackEndError( u'Unable to retrieve stat object with error: {0:s}'.format( exception)) # File data stat information. stat_object.size = stat_info.st_size # Date and time stat information. stat_object.atime = stat_info.st_atime stat_object.ctime = stat_info.st_ctime stat_object.mtime = stat_info.st_mtime # Ownership and permissions stat information. stat_object.mode = stat.S_IMODE(stat_info.st_mode) stat_object.uid = stat_info.st_uid stat_object.gid = stat_info.st_gid # If location contains a trailing segment separator and points to # a symbolic link to a directory stat info will not indicate # the file entry as a symbolic link. The following check ensures # that the LINK type is correctly detected. is_link = os.path.islink(location) # File entry type stat information. # The stat info member st_mode can have multiple types e.g. # LINK and DIRECTORY in case of a symbolic link to a directory # dfVFS currently only supports one type so we need to check # for LINK first. if stat.S_ISLNK(stat_info.st_mode) or is_link: stat_object.type = stat_object.TYPE_LINK elif stat.S_ISREG(stat_info.st_mode): stat_object.type = stat_object.TYPE_FILE elif stat.S_ISDIR(stat_info.st_mode): stat_object.type = stat_object.TYPE_DIRECTORY elif (stat.S_ISCHR(stat_info.st_mode) or stat.S_ISBLK(stat_info.st_mode)): stat_object.type = stat_object.TYPE_DEVICE elif stat.S_ISFIFO(stat_info.st_mode): stat_object.type = stat_object.TYPE_PIPE elif stat.S_ISSOCK(stat_info.st_mode): stat_object.type = stat_object.TYPE_SOCKET # Other stat information. stat_object.ino = stat_info.st_ino # stat_info.st_dev # stat_info.st_nlink return stat_object
def _Open(self, path_spec=None, mode='rb'): """Opens the file-like object defined by path specification. Args: path_spec: optional path specification (instance of path.PathSpec). The default is None. mode: optional file access mode. The default is 'rb' read-only binary. Raises: AccessError: if the access to open the file was denied. IOError: if the file-like object could not be opened. PathSpecError: if the path specification is incorrect. ValueError: if the path specification is invalid. """ if not path_spec: raise ValueError(u'Missing path specfication.') if path_spec.HasParent(): raise errors.PathSpecError( u'Unsupported path specification with parent.') location = getattr(path_spec, u'location', None) if location is None: raise errors.PathSpecError(u'Path specification missing location.') # Windows does not support running os.stat on device files so we use # libsmdev to do an initial check. try: is_device = pysmdev.check_device(location) except IOError as exception: # Since os.stat() will not recognize Windows device file names and # will return '[Error 87] The parameter is incorrect' we check here # if pysmdev exception message contains ' access denied ' and raise # AccessError instead. # Note that exception.message no longer works in Python 3. exception_string = str(exception) if not isinstance(exception_string, py2to3.UNICODE_TYPE): exception_string = py2to3.UNICODE_TYPE(exception_string, errors=u'replace') if u' access denied ' in exception_string: raise errors.AccessError( u'Access denied to file: {0:s} with error: {1:s}'.format( location, exception_string)) is_device = False if not is_device: try: stat_info = os.stat(location) except OSError as exception: raise IOError(u'Unable to open file with error: {0:s}.'.format( exception)) # In case the libsmdev check is not able to detect the device also use # the stat information. if stat.S_ISCHR(stat_info.st_mode) or stat.S_ISBLK( stat_info.st_mode): is_device = True if is_device: self._file_object = pysmdev.handle() self._file_object.open(location, mode=mode) self._size = self._file_object.media_size else: self._file_object = open(location, mode=mode) self._size = stat_info.st_size
def __init__(self, resolver_context, file_system, path_spec, is_root=False): """Initializes a file entry. Args: resolver_context (Context): resolver context. file_system (FileSystem): file system. path_spec (PathSpec): path specification. is_root (Optional[bool]): True if the file entry is the root file entry of the corresponding file system. Raises: BackEndError: If an OSError comes up it is caught and an BackEndError error is raised instead. """ location = getattr(path_spec, 'location', None) # Windows does not support running os.stat on device files so we use # libsmdev to do an initial check. is_windows_device = False if platform.system() == 'Windows' and location: try: # pylint: disable=no-member is_windows_device = pysmdev.check_device(location) except IOError: pass stat_info = None if not is_windows_device and location: # We are only catching OSError. However on the Windows platform # a WindowsError can be raised as well. We are not catching that since # that error does not exist on non-Windows platforms. try: stat_info = os.lstat(location) except OSError as exception: raise errors.BackEndError( 'Unable to retrieve stat object with error: {0!s}'.format( exception)) super(OSFileEntry, self).__init__( resolver_context, file_system, path_spec, is_root=is_root, is_virtual=False) self._is_windows_device = is_windows_device self._name = None self._stat_info = stat_info if is_windows_device: self.entry_type = definitions.FILE_ENTRY_TYPE_DEVICE elif stat_info: # If location contains a trailing segment separator and points to # a symbolic link to a directory stat info will not indicate # the file entry as a symbolic link. The following check ensures # that the LINK type is correctly detected. is_link = os.path.islink(location) # The stat info member st_mode can have multiple types e.g. # LINK and DIRECTORY in case of a symbolic link to a directory # dfVFS currently only supports one type so we need to check # for LINK first. if stat.S_ISLNK(stat_info.st_mode) or is_link: self.entry_type = definitions.FILE_ENTRY_TYPE_LINK elif stat.S_ISREG(stat_info.st_mode): self.entry_type = definitions.FILE_ENTRY_TYPE_FILE elif stat.S_ISDIR(stat_info.st_mode): self.entry_type = definitions.FILE_ENTRY_TYPE_DIRECTORY elif (stat.S_ISCHR(stat_info.st_mode) or stat.S_ISBLK(stat_info.st_mode)): self.entry_type = definitions.FILE_ENTRY_TYPE_DEVICE elif stat.S_ISFIFO(stat_info.st_mode): self.entry_type = definitions.FILE_ENTRY_TYPE_PIPE elif stat.S_ISSOCK(stat_info.st_mode): self.entry_type = definitions.FILE_ENTRY_TYPE_SOCKET
def _Open(self, path_spec=None, mode='rb'): """Opens the file-like object defined by path specification. Args: path_spec (PathSpec): path specification. mode (Optional[str]): file access mode. Raises: AccessError: if the access to open the file was denied. IOError: if the file-like object could not be opened. OSError: if the file-like object could not be opened. PathSpecError: if the path specification is incorrect. ValueError: if the path specification is invalid. """ if not path_spec: raise ValueError('Missing path specification.') if path_spec.HasParent(): raise errors.PathSpecError('Unsupported path specification with parent.') location = getattr(path_spec, 'location', None) if location is None: raise errors.PathSpecError('Path specification missing location.') # Windows does not support running os.stat on device files so we use # libsmdev to do an initial check. try: is_device = pysmdev.check_device(location) except IOError as exception: # Since os.stat() will not recognize Windows device file names and # will return '[Error 87] The parameter is incorrect' we check here # if pysmdev exception message contains ' access denied ' and raise # AccessError instead. # Note that exception.message no longer works in Python 3. exception_string = str(exception) if not isinstance(exception_string, py2to3.UNICODE_TYPE): exception_string = py2to3.UNICODE_TYPE( exception_string, errors='replace') if ' access denied ' in exception_string: raise errors.AccessError( 'Access denied to file: {0:s} with error: {1!s}'.format( location, exception_string)) is_device = False if not is_device: try: stat_info = os.stat(location) except OSError as exception: raise IOError('Unable to open file with error: {0!s}.'.format( exception)) # In case the libsmdev check is not able to detect the device also use # the stat information. if stat.S_ISCHR(stat_info.st_mode) or stat.S_ISBLK(stat_info.st_mode): is_device = True if is_device: self._file_object = pysmdev.handle() self._file_object.open(location, mode=mode) self._size = self._file_object.media_size else: self._file_object = open(location, mode=mode) self._size = stat_info.st_size