Skip to content

The only downloader you need. 下载器的集大成者。

feat(sysio): 添加 Windows 平台稀疏文件盒文件截断功能支持

XiaoYouChR926a65befff4
diff --git a/app/supports/sysio.py b/app/supports/sysio.py
index 45bbcaa6..8d870873 100644
--- a/app/supports/sysio.py
+++ b/app/supports/sysio.py
@@ -3,7 +3,8 @@ from sys import platform
if platform == "win32":
import msvcrt
- from ctypes import wintypes, Structure, Union, c_void_p, POINTER, windll, WinError, cast, c_char_p, byref
+ from ctypes import wintypes, Structure, Union, c_void_p, POINTER, windll, WinError, cast, c_char_p, byref, \
+ c_longlong, get_last_error
INVALID_HANDLE_VALUE = -1
@@ -71,6 +72,71 @@ if platform == "win32":
)
return written.value
+ FSCTL_SET_SPARSE = 0x000900C4
+ FILE_BEGIN = 0
+
+ DeviceIoControl = windll.kernel32.DeviceIoControl
+ DeviceIoControl.argtypes = [
+ wintypes.HANDLE,
+ wintypes.DWORD,
+ wintypes.LPVOID,
+ wintypes.DWORD,
+ wintypes.LPVOID,
+ wintypes.DWORD,
+ POINTER(wintypes.DWORD),
+ wintypes.LPVOID,
+ ]
+ DeviceIoControl.restype = wintypes.BOOL
+
+ SetFilePointerEx = windll.kernel32.SetFilePointerEx
+ SetFilePointerEx.argtypes = [
+ wintypes.HANDLE,
+ c_longlong,
+ POINTER(c_longlong),
+ wintypes.DWORD,
+ ]
+ SetFilePointerEx.restype = wintypes.BOOL
+
+ SetEndOfFile = windll.kernel32.SetEndOfFile
+ SetEndOfFile.argtypes = [wintypes.HANDLE]
+ SetEndOfFile.restype = wintypes.BOOL
+
+
+ def _raise_winerror(msg: str):
+ err = get_last_error()
+ raise OSError(err, f"{msg}, WinError={err}")
+
+ def ftruncate(fd: typing.Union[int, wintypes.HANDLE], size: int) -> None:
+ if isinstance(fd, int):
+ handle = wintypes.HANDLE(
+ msvcrt.get_osfhandle(fd)
+ )
+ else:
+ handle = fd
+
+ returned = wintypes.DWORD(0)
+ ok = DeviceIoControl(
+ handle,
+ FSCTL_SET_SPARSE,
+ None,
+ 0,
+ None,
+ 0,
+ byref(returned),
+ None,
+ )
+ if not ok:
+ _raise_winerror("FSCTL_SET_SPARSE failed")
+
+ newPos = c_longlong()
+ ok = SetFilePointerEx(handle, size, byref(newPos), FILE_BEGIN)
+ if not ok:
+ _raise_winerror("SetFilePointerEx failed")
+
+ ok = SetEndOfFile(handle)
+ if not ok:
+ _raise_winerror("SetEndOfFile failed")
+
else:
# noinspection PyUnresolvedReferences
- from os import pwrite
+ from os import pwrite, ftruncate
diff --git a/features/http_pack/task.py b/features/http_pack/task.py
index debbee4d..82ef040d 100644
--- a/features/http_pack/task.py
+++ b/features/http_pack/task.py
@@ -11,7 +11,7 @@ from loguru import logger
from app.bases.interfaces import Worker
from app.bases.models import Task, TaskStage, TaskStatus
from app.supports.config import DEFAULT_HEADERS, cfg
-from app.supports.sysio import pwrite
+from app.supports.sysio import ftruncate, pwrite
from app.supports.utils import getProxies
from features.http_pack.config import httpConfig
from features.http_pack.const import SpecialFileSize
@@ -248,7 +248,7 @@ class HttpWorker(Worker):
if not restored:
try:
- os.ftruncate(self.fileHandle, self.stage.fileSize)
+ ftruncate(self.fileHandle, self.stage.fileSize)
except Exception as e:
print(repr(e))