用户空间虚拟文件系统(usvfs)库的python绑定
py-usvfs的Python项目详细描述
py usvfs
用户空间虚拟文件系统(usvfs)库的python绑定
什么是USVFS?
引用开发人员的话:
< Buff行情>usvfs(用户空间虚拟文件系统的简称)旨在允许windows应用程序创建仅对选定的一组进程可见的文件或目录链接。它是通过使用api钩住来欺骗文件访问函数来发现/打开实际上在其他地方的文件来实现的
usvfs最初是由tannione编写的,它是modorganizer的一部分,modorganizer是一个用于各种pc游戏(特别是bethesda游戏)的mod manager应用程序。modorganizer2团队继续开发modorganizer和usvfs。 usvfs是modorganizer的核心组件。它允许modorganizer轻松地安装和卸载mods,而无需不断地移动磁盘上的千兆字节数据,并且它可以控制当多个mod覆盖同一个文件时会发生什么。
然而,usvfs被设计成也可以作为一个独立的库使用。这很好,因为这种功能有各种有趣的应用程序。
什么是py-usvfs?
UVFS是用C++编写的。这是必须的,因为它需要执行低级任务,比如挂接windows api的i/o函数调用和注入进程。我在usvfs中看到了一些个人项目的使用,但我通常更喜欢使用更高级的语言——首先是python。PY USVFS旨在实现这一目标。
py usvfs是两件事: a.用于usvfs dll功能的python绑定,以pybind11 cpython扩展的形式 b.一个python包装器模块,它为低级功能提供了方便、希望更多的python抽象
基本上,它的目的是允许在python(a)中使用usvfs,并使使用它不那么痛苦(b)。
我可以在生产环境中使用它吗?
你能吗?我不是你妈妈。你应该吗?可能不是。usvfs很挑剔,不总是可靠的,而且文档记录也很差。py-usvfs也一样。当心清空器。py-usvfs未正确测试。没有任何保证,因为任何东西都会起作用。
要求
因为usvfs是通过挂接windows api的i/o函数调用来工作的,所以它只在windows机器上运行。因此,py usvfs也只能在windows上运行。 由于py usvfs通过cpython扩展公开了usvfs[x86 x64].dll中的功能,因此它只能在python的正式cpython实现上运行。
因此,要求如下:
- Windows操作系统(win32或win-amd64)
- cpython 3.7.x(32位或64位)
安装
使用pip安装预构建的软件包
预构建的python包可以从pypi获得。使用PIP进行安装。
pip install py-usvfs
一旦安装,名为usvfs
的python模块将可用。
自己建造
或者,可以从源代码构建所需的组件。没有给出详细说明,但以下是基本步骤:
- 从https://github.com/modorganizer2/usvfs/" rel="nofollow">https://github.com/modorganizer2/usvfs/ 获取usvfs源代码
- 使用msvc在release/win32和release/x64配置中构建usvfs-dll
- 将.dll文件(
usvfs_x86.dll
,usvfs_x64.dll
)放入python extension/usvfs/bin
,将.lib文件(usvfs_x86.lib
,usvfs_x64.lib
)放入python extension/usvfs/lib
,并将usvfs proxy exe(usvfs_x64.lib_python扩展名/usvfs/bi中的x64.exe
,usvfs_proxy_x86.exe
)n(请注意,即使您只为一个平台构建,也始终需要32位和64位usvfs_proxy exe) - 使用msvc在release/win32和release/x64配置中构建python扩展
- 运行
/build wheels.bat
为64位和32位cpython构建python wheel包 - 现在您可以使用
pip install[wheelname].whl在
dist/
中安装.whl文件
文档
目前,没有广泛的文件。请参考源代码和python docstrings。请参见下面的基本用法示例。
基本用法
假设我们在当前工作目录中有以下文件夹结构,我们希望诱使程序认为src/
的内容位于dest/
[current working directory]/
|
+-- src/
| |-- source1.txt
| |-- source2.txt
| |
| +-- subfolder/
| |-- source3.txt
|
+-- dest/
importusvfs# Define a virtual directory link rulevdir=usvfs.VirtualDirectory('src/','dest/')# If a process runnning # in the vfs tries to access# dest/*, it will be redirected to src/*# By default, py-usvfs links directories recursively, i.e. all underlying files # and directories of the source directory are linked automatically# If this behaviour is undesired, you can unset the recursive link flag:# vdir.link_recursively = False# All link flags defined by usvfs can be set/unset in this way, but only # if they are relevant to the context (e.g. link_recursively is # only available for VirtualDirectory links, not VirtualFile links)# Define our vfs layoutvfs_map=usvfs.Mapping()# Create a vfs mapping. This is a collection # of virtual link rules that can be applied to the vfsvfs_map.link(vdir)# Add any VirtualDirectory and/or VirtualFile rules like this# Set up the vfsvfs=usvfs.UserspaceVFS()# Create a usvfs controller with default instance name and configurationvfs.initialize()# Initialize itvfs.set_mapping(vfs_map)# Apply mapping# Now we can start a process in the vfs using vfs.run_process(...)# This function takes three arguments:# 1. string containing executable path + any command line arguments # 2. (optional) string specifying a working directory# the default is the current working directory of your Python program# 3. (optional) bool specifying if this call should block execution# until the child process finished (default is True)vfs.run_process('notepad.exe')# Usvfs will launch a usvfs_proxy process with the appropriate bitness, then # start and inject the specified process# If you browse to dest/ in notepad, you should see the contents of src/# If you then browse to dest/ in explorer, you will see that there are actually no files there!# When you're done, be sure to clean upvfs.close()
python包装类将一些更丑陋的usvfs隐藏在一个干净、相对容易使用的api中。如果需要更多的低级访问,可以通过访问usvfs.dll
模块中定义的函数直接调用usvfs dll(半-)。
importusvfs# Note that you cannot import usvfs.dll directly# Access dll functions, properties, structs like this:usvfs.dll.LINKFLAG_MONITORCHANGESusvfs.dll.LogLevelusvfs.dll.USVFSParameters()usvfs.dll.CreateVFS(...)# etc...
这将公开由usvfs[x86 x64].dll导出的大多数函数、结构和属性(但不公开调试内容)。Python中的函数签名类似于UVFS C++头文件中的函数签名,但是您可以将基本的Python类型传递为参数,而不必处理缓冲区<代码> WCHAR[]/COD>。参见
这里的异常是usvfs.dll.createprocesshooked()
。在C/C++直接调用UVFS DLL时,您必须将一堆Windows API的无意义传递给函数并处理Windows句柄。py usvfs-cpython扩展可以帮您解决这个问题,因为(a)它很方便,(b)向python公开所有必需的windows api内容将是一件痛苦的事情。缺点是您无法通过windows句柄轻松访问新生成的进程状态和输入/输出。
py usvfs中的createprocesshooked()有两个参数:astr
包含可执行路径和要传递给它的任何命令行参数,astr
包含新进程的预期工作目录路径。
许可证
(c)2019 PWSSNK——GPL V3许可下提供的代码