Fix maxfile attribute dropped in psutil 6.0.0 causing an error to be printed

This commit is contained in:
allegroai 2024-08-28 19:46:26 +03:00
parent 9611e5c486
commit 696af6e76b

View File

@ -7,19 +7,22 @@ import psutil
def get_filename_max_length(dir_path):
# type: (str) -> int
default = 255 # Common filesystems like NTFS, EXT4 and HFS+ limited with 255
try:
dir_path = pathlib2.Path(os.path.abspath(dir_path))
if platform == "win32":
# noinspection PyBroadException
dir_drive = dir_path.drive
for drv in psutil.disk_partitions():
if drv.device.startswith(dir_drive):
return drv.maxfile
# The maxfile attribute is only available in psutil >=5.7.4,<6.0.0
return getattr(drv, "maxfile", default)
elif platform in ("linux", "darwin"):
return os.statvfs(dir_path).f_namemax
except Exception as err:
print(err)
return 255 # Common filesystems like NTFS, EXT4 and HFS+ limited with 255
return default
def is_path_traversal(target_folder, relative_path):