Initial IFileOperation for Windows

- Try using IFileOperation instead of SHFileOperation
  - Use pywin32 to accomplish this
  - Implement fallback when pywin32 not available
- Handles paths like `C:\` just fine bu the `\\?\` paths in the test
  cause issue
- Add batching for IFileOperation version (performance)
- Minor formatting applied by editor
This commit is contained in:
Andrew Senetar 2020-05-21 22:13:59 -05:00
parent 66afce7252
commit 7abc048836
Signed by: arsenetar
GPG Key ID: C63300DCE48AB2F1
1 changed files with 139 additions and 74 deletions

View File

@ -5,88 +5,153 @@
# http://www.hardcoded.net/licenses/bsd_license # http://www.hardcoded.net/licenses/bsd_license
from __future__ import unicode_literals from __future__ import unicode_literals
from ctypes import (windll, Structure, byref, c_uint,
create_unicode_buffer, addressof,
GetLastError, FormatError)
from ctypes.wintypes import HWND, UINT, LPCWSTR, BOOL
import os.path as op import os.path as op
from .compat import text_type from .compat import text_type
kernel32 = windll.kernel32 try:
GetShortPathNameW = kernel32.GetShortPathNameW # Attempt to use pywin32 to use IFileOperation
import pythoncom
import pywintypes
from win32com.shell import shell, shellcon
from platform import version
shell32 = windll.shell32 def send2trash(path):
SHFileOperationW = shell32.SHFileOperationW if not isinstance(path, list):
path = [path]
# convert data type
path = [
text_type(item, "mbcs") if not isinstance(item, text_type) else item
for item in path
]
# convert to full paths
path = [op.abspath(item) if not op.isabs(item) else item for item in path]
# create instance of file operation object
fileop = pythoncom.CoCreateInstance(
shell.CLSID_FileOperation,
None,
pythoncom.CLSCTX_ALL,
shell.IID_IFileOperation,
)
# default flags to use
flags = (
shellcon.FOF_NOCONFIRMATION
| shellcon.FOF_NOERRORUI
| shellcon.FOF_SILENT
| shellcon.FOFX_EARLYFAILURE
)
# determine rest of the flags based on OS version
# use newer recommended flags if available
if int(version().split(".", 1)[0]) >= 8 and False:
flags |= (
0x20000000 # FOFX_ADDUNDORECORD win 8+
| 0x00080000 # FOFX_RECYCLEONDELETE win 8+
)
else:
flags |= shellcon.FOF_ALLOWUNDO
# set the flags
fileop.SetOperationFlags(flags)
# actually try to perform the operation, this section may throw a
# pywintypes.com_error which does not seem to create as nice of an
# error as OSError so wrapping with try to convert
try:
for itemPath in path:
item = shell.SHCreateItemFromParsingName(
itemPath, None, shell.IID_IShellItem
)
fileop.DeleteItem(item)
result = fileop.PerformOperations()
aborted = fileop.GetAnyOperationsAborted()
# if non-zero result or aborted throw an exception
if result or aborted:
raise OSError(None, None, path, result)
except pywintypes.com_error as error:
# convert to standard OS error, allows other code to get a
# normal errno
raise OSError(None, error.strerror, path, error.hresult)
class SHFILEOPSTRUCTW(Structure): except ImportError:
_fields_ = [ from ctypes import (
("hwnd", HWND), windll,
("wFunc", UINT), Structure,
("pFrom", LPCWSTR), byref,
("pTo", LPCWSTR), c_uint,
("fFlags", c_uint), create_unicode_buffer,
("fAnyOperationsAborted", BOOL), addressof,
("hNameMappings", c_uint), GetLastError,
("lpszProgressTitle", LPCWSTR), FormatError,
)
from ctypes.wintypes import HWND, UINT, LPCWSTR, BOOL
kernel32 = windll.kernel32
GetShortPathNameW = kernel32.GetShortPathNameW
shell32 = windll.shell32
SHFileOperationW = shell32.SHFileOperationW
class SHFILEOPSTRUCTW(Structure):
_fields_ = [
("hwnd", HWND),
("wFunc", UINT),
("pFrom", LPCWSTR),
("pTo", LPCWSTR),
("fFlags", c_uint),
("fAnyOperationsAborted", BOOL),
("hNameMappings", c_uint),
("lpszProgressTitle", LPCWSTR),
] ]
FO_MOVE = 1
FO_COPY = 2
FO_DELETE = 3
FO_RENAME = 4
FO_MOVE = 1 FOF_MULTIDESTFILES = 1
FO_COPY = 2 FOF_SILENT = 4
FO_DELETE = 3 FOF_NOCONFIRMATION = 16
FO_RENAME = 4 FOF_ALLOWUNDO = 64
FOF_NOERRORUI = 1024
FOF_MULTIDESTFILES = 1 def get_short_path_name(long_name):
FOF_SILENT = 4 if not long_name.startswith("\\\\?\\"):
FOF_NOCONFIRMATION = 16 long_name = "\\\\?\\" + long_name
FOF_ALLOWUNDO = 64 buf_size = GetShortPathNameW(long_name, None, 0)
FOF_NOERRORUI = 1024 # FIX: https://github.com/hsoft/send2trash/issues/31
# If buffer size is zero, an error has occurred.
if not buf_size:
err_no = GetLastError()
raise WindowsError(err_no, FormatError(err_no), long_name[4:])
output = create_unicode_buffer(buf_size)
GetShortPathNameW(long_name, output, buf_size)
return output.value[4:] # Remove '\\?\' for SHFileOperationW
def send2trash(path):
def get_short_path_name(long_name): if not isinstance(path, text_type):
if not long_name.startswith('\\\\?\\'): path = text_type(path, "mbcs")
long_name = '\\\\?\\' + long_name if not op.isabs(path):
buf_size = GetShortPathNameW(long_name, None, 0) path = op.abspath(path)
# FIX: https://github.com/hsoft/send2trash/issues/31 path = get_short_path_name(path)
# If buffer size is zero, an error has occurred. fileop = SHFILEOPSTRUCTW()
if not buf_size: fileop.hwnd = 0
err_no = GetLastError() fileop.wFunc = FO_DELETE
raise WindowsError(err_no, FormatError(err_no), long_name[4:]) # FIX: https://github.com/hsoft/send2trash/issues/17
output = create_unicode_buffer(buf_size) # Starting in python 3.6.3 it is no longer possible to use:
GetShortPathNameW(long_name, output, buf_size) # LPCWSTR(path + '\0') directly as embedded null characters are no longer
return output.value[4:] # Remove '\\?\' for SHFileOperationW # allowed in strings
# Workaround
# - create buffer of c_wchar[] (LPCWSTR is based on this type)
def send2trash(path): # - buffer is two c_wchar characters longer (double null terminator)
if not isinstance(path, text_type): # - cast the address of the buffer to a LPCWSTR
path = text_type(path, 'mbcs') # NOTE: based on how python allocates memory for these types they should
if not op.isabs(path): # always be zero, if this is ever not true we can go back to explicitly
path = op.abspath(path) # setting the last two characters to null using buffer[index] = '\0'.
path = get_short_path_name(path) buffer = create_unicode_buffer(path, len(path) + 2)
fileop = SHFILEOPSTRUCTW() fileop.pFrom = LPCWSTR(addressof(buffer))
fileop.hwnd = 0 fileop.pTo = None
fileop.wFunc = FO_DELETE fileop.fFlags = FOF_ALLOWUNDO | FOF_NOCONFIRMATION | FOF_NOERRORUI | FOF_SILENT
# FIX: https://github.com/hsoft/send2trash/issues/17 fileop.fAnyOperationsAborted = 0
# Starting in python 3.6.3 it is no longer possible to use: fileop.hNameMappings = 0
# LPCWSTR(path + '\0') directly as embedded null characters are no longer fileop.lpszProgressTitle = None
# allowed in strings result = SHFileOperationW(byref(fileop))
# Workaround if result:
# - create buffer of c_wchar[] (LPCWSTR is based on this type) raise WindowsError(result, FormatError(result), path)
# - buffer is two c_wchar characters longer (double null terminator)
# - cast the address of the buffer to a LPCWSTR
# NOTE: based on how python allocates memory for these types they should
# always be zero, if this is ever not true we can go back to explicitly
# setting the last two characters to null using buffer[index] = '\0'.
buffer = create_unicode_buffer(path, len(path)+2)
fileop.pFrom = LPCWSTR(addressof(buffer))
fileop.pTo = None
fileop.fFlags = FOF_ALLOWUNDO | FOF_NOCONFIRMATION | FOF_NOERRORUI | FOF_SILENT
fileop.fAnyOperationsAborted = 0
fileop.hNameMappings = 0
fileop.lpszProgressTitle = None
result = SHFileOperationW(byref(fileop))
if result:
raise WindowsError(result, FormatError(result), path)