Ejemplo n.º 1
0
    def file_existing(volume_name:str, path:str, file_name:str, ftype:str) -> bool:
        """_summary_
            Check specific file or directory is in pyfilesystem.MemoryFS
        Args:
            volume_name(str): volume_name
            path (str): absolute path
                e.g) volume_name -> c:
                    path -> /test/test/test.txt
            file_name (str): file name string which want to create
            ftype (str): directory or file
        """
        file_name = file_name.lower()
        if not EmuIOLayer._check_valid_voulme(volume_name):
            return False
        o = convert_winpath_to_emupath(emu_path_join(volume_name, path))
        abs_path = emu_path_join(o["vl"], o["ps"])
        full_path = emu_path_join(abs_path, file_name)

        if ftype == 'directory':
            return EmuIOLayer.emu_fs.isdir(full_path)
        elif ftype == 'file':
            return EmuIOLayer.emu_fs.isfile(full_path)
        else:
            Exception('file_existing() invalid file type')
        
        pass
Ejemplo n.º 2
0
    def create_file(volume_name:str, path:str, file_name:str, ftype:str, turncated=False, recursive:bool=False) -> any:
        """_summary_
            Create virtual file at pyfilesystem.MemoryFS
        Args:
            volume_name(str): volume_name
            path (str): absolute path
                e.g) volume_name -> c:
                    path -> /test/test/test.txt
            file_name (str): file name string which want to create
            ftype (str): directory or file
            recursive (bool, optional): 
                create directory with recursive option
                if this option is setted, all directory related with path is created recursivly
        """
        ret = {
            'success': False,
            'fp': ''
        }
        file_name = file_name.lower()
        if not EmuIOLayer._check_valid_voulme(volume_name):
            return ret
        o = convert_winpath_to_emupath(emu_path_join(volume_name, path))
        abs_path = emu_path_join(o["vl"], o["ps"])
        full_path = emu_path_join(abs_path, file_name)

        volume = EmuIOLayer._get_volume(o["vl"])
    
        if recursive:
            paths = o["ps"].split("/")
            cur_path = ""
            for p in paths:
                cur_path += p + "/"
                try:
                    volume.makedir(cur_path)
                except DirectoryExists:
                    continue
        
        if ftype == 'directory':
            try:
                EmuIOLayer.emu_fs.makedir(full_path)
            except DirectoryExists:
                pass
        elif ftype == 'file':
            EmuIOLayer.emu_fs.create(path=full_path, wipe=turncated)
        else:
            Exception('create_file() invalid file type')

        ret["success"] = True
        ret["fp"] = full_path

        return ret
Ejemplo n.º 3
0
    def im_delete_file(self, path: str, ftype: str = 'file'):
        ret = {"success": False}
        vl, p = convert_winpath_to_emupath(path)
        path = emu_path_join(vl, p)
        ref_cnt = self.get_references(path)
        if ref_cnt > 0:
            return ret
        volume_name, path, file_name = parse_file_fullpath(path)
        df = EmuIOLayer.delete_file(volume_name, path, file_name, ftype)
        if not df["success"]:
            return ret

        ret["success"] = True
        return ret
Ejemplo n.º 4
0
    def open_file(volume_name:str, path:str, file_name:str, ftype:str, mode:str):
        """_summary_
            Open file object which in pyfilesystem.MemoryFS
        Args:
            volume_name(str): volume_name
            path (str): absolute path
                e.g) volume_name -> c:
                    path -> /test/test/test.txt
            file_name (str): file name string which want to create
            ftype (str): directory or file
            mode (str): 
                python-like file open mode
                e.g) wb
        """
        ret = {
            "success": False,
            "fp": '',
            "ftype": '',
            "obj": None
        }
        file_name = file_name.lower()

        pinfo = convert_winpath_to_emupath(emu_path_join(volume_name, path, file_name))
        full_path = emu_path_join(pinfo["vl"], pinfo["ps"])

        ret["fp"] = full_path
        ret["mode"] = mode
        ret["ftype"] = ftype

        obj = None
        try:
            if ftype == 'directory':
                obj = EmuIOLayer.emu_fs.opendir(full_path)
            elif ftype == 'file':
                obj = EmuIOLayer.emu_fs.open(full_path, mode=mode)
            else:
                Exception('open_file() invalid file type')
        except ResourceNotFound:
            return ret

        ret["success"] = True
        ret["obj"] = obj

        return ret
Ejemplo n.º 5
0
    def delete_file(volume_name:str, path:str, file_name:str, ftype:str):
        ret = {
            "success": False,
            "fp": '',
        }

        o = convert_winpath_to_emupath(emu_path_join(volume_name, path))
        abs_path = emu_path_join(o["vl"], o["ps"])
        full_path = emu_path_join(abs_path, file_name)

        try:
            if ftype == 'directory':
                EmuIOLayer.emu_fs.removedir(full_path)
            elif ftype == 'file':
                EmuIOLayer.emu_fs.remove(full_path)
            else:
                Exception('delete_file() invalid file type')
        except ResourceNotFound:
            return ret
        
        ret["fp"] = full_path
        
        return ret

    # def create_file_mapping(self, file_handle, map_max, protect, name)->EmMMFile:
    #     if file_handle == 0xFFFFFFFF: # Invalid File Handle
    #         file_handle = self.create_file("C:/pagefile.sys", "wb+")
    #     mmf_handle = self.file_handle_manager.create_file_mapping_handle(file_handle, map_max, protect, name)

    #     return mmf_handle

    # def set_map_object(self, file_handle, offset, map_region)->EmMMFile:
    #     mmf_obj:EmMMFile = obj_manager.ObjectManager.get_obj_by_handle(file_handle)
    #     self.set_file_pointer(file_handle, offset)
    #     mmf_obj.set_view(map_region)

    #     return mmf_obj
Ejemplo n.º 6
0
import fs_emu_util
if __name__ == '__main__':
    path = "c:/1/2/.././3/wow.txt"
    p = fs_emu_util.convert_winpath_to_emupath(path)
    print(p["vl"], p["ps"])