Use winutil to get disk free space

This commit is contained in:
Kovid Goyal 2020-10-16 14:51:10 +05:30
parent c9df881fd9
commit 5701a6c2c5
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
4 changed files with 20 additions and 17 deletions

View File

@ -21,11 +21,11 @@ if iswindows:
drive_ok_lock = Lock()
def drive_is_ok(letter, max_tries=10, debug=False):
import win32file
from calibre_extensions import winutil
with drive_ok_lock:
for i in range(max_tries):
try:
win32file.GetDiskFreeSpaceEx(letter+':\\')
winutil.get_disk_free_space(letter+':\\')
return True
except Exception as e:
if i >= max_tries - 1 and debug:

View File

@ -146,20 +146,17 @@ class Device(DeviceConfig, DevicePlugin):
if not prefix:
return 0, 0
prefix = prefix[:-1]
import win32file, winerror
from calibre_extensions import winutil
try:
sectors_per_cluster, bytes_per_sector, free_clusters, total_clusters = \
win32file.GetDiskFreeSpace(prefix)
except Exception as err:
if getattr(err, 'args', [None])[0] == winerror.ERROR_NOT_READY:
available_space, total_space, free_space = winutil.get_disk_free_space(prefix)
except OSError as err:
if err.winerror == winutil.ERROR_NOT_READY:
# Disk not ready
time.sleep(3)
sectors_per_cluster, bytes_per_sector, free_clusters, total_clusters = \
win32file.GetDiskFreeSpace(prefix)
available_space, total_space, free_space = winutil.get_disk_free_space(prefix)
else:
raise
mult = sectors_per_cluster * bytes_per_sector
return total_clusters * mult, free_clusters * mult
return total_space, available_space
def total_space(self, end_session=True):
msz = casz = cbsz = 0

View File

@ -408,7 +408,11 @@ winutil_get_disk_free_space(PyObject *self, PyObject *args) {
wchar_raii path;
if (!PyArg_ParseTuple(args, "O&", py_to_wchar, &path)) return NULL;
ULARGE_INTEGER bytes_available_to_caller, total_bytes, total_free_bytes;
if (!GetDiskFreeSpaceEx(path.ptr(), &bytes_available_to_caller, &total_bytes, &total_free_bytes)) return PyErr_SetExcFromWindowsErrWithFilenameObject(PyExc_OSError, 0, PyTuple_GET_ITEM(args, 0));
BOOL ok;
Py_BEGIN_ALLOW_THREADS
ok = GetDiskFreeSpaceEx(path.ptr(), &bytes_available_to_caller, &total_bytes, &total_free_bytes);
Py_END_ALLOW_THREADS
if (!ok) return PyErr_SetExcFromWindowsErrWithFilenameObject(PyExc_OSError, 0, PyTuple_GET_ITEM(args, 0));
return Py_BuildValue("KKK", bytes_available_to_caller.QuadPart, total_bytes.QuadPart, total_free_bytes.QuadPart);
}
@ -1281,6 +1285,8 @@ CALIBRE_MODINIT_FUNC PyInit_winutil(void) {
PyModule_AddIntConstant(m, "ERROR_MORE_DATA", ERROR_MORE_DATA);
PyModule_AddIntConstant(m, "ERROR_NO_MORE_ITEMS", ERROR_NO_MORE_ITEMS);
PyModule_AddIntConstant(m, "ERROR_FILE_NOT_FOUND", ERROR_FILE_NOT_FOUND);
PyModule_AddIntConstant(m, "ERROR_GEN_FAILURE ", ERROR_GEN_FAILURE);
PyModule_AddIntConstant(m, "ERROR_INSUFFICIENT_BUFFER", ERROR_INSUFFICIENT_BUFFER);
PyModule_AddIntConstant(m, "ERROR_BAD_COMMAND", ERROR_BAD_COMMAND);
PyModule_AddIntConstant(m, "ERROR_INVALID_DATA", ERROR_INVALID_DATA);
PyModule_AddIntConstant(m, "ERROR_NOT_READY", ERROR_NOT_READY);

View File

@ -8,13 +8,13 @@ __copyright__ = '2015, Kovid Goyal <kovid at kovidgoyal.net>'
import os, sys, time, traceback
from threading import Thread
import winerror
from calibre import guess_type, prints
from calibre.constants import is64bit, isportable, isfrozen, __version__, DEBUG, plugins
from calibre.utils.winreg.lib import Key, HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE
from calibre.utils.lock import singleinstance
from polyglot.builtins import iteritems, itervalues
from calibre_extensions import winutil
# See https://msdn.microsoft.com/en-us/library/windows/desktop/cc144154(v=vs.85).aspx
@ -197,7 +197,7 @@ def get_prog_id_map(base, key_path):
try:
k = Key(open_at=key_path, root=base)
except OSError as err:
if err.winerror == winerror.ERROR_FILE_NOT_FOUND:
if err.winerror == winutil.ERROR_FILE_NOT_FOUND:
return desc, ans
raise
with k:
@ -213,7 +213,7 @@ def get_open_data(base, prog_id):
try:
k = Key(open_at=r'Software\Classes\%s' % prog_id, root=base)
except OSError as err:
if err.winerror == winerror.ERROR_FILE_NOT_FOUND:
if err.winerror == winutil.ERROR_FILE_NOT_FOUND:
return None, None, None
with k:
cmd = k.get(sub_key=r'shell\open\command')
@ -249,7 +249,7 @@ def find_programs(extensions):
try:
k = Key(open_at=r'Software\RegisteredApplications', root=base)
except OSError as err:
if err.winerror == winerror.ERROR_FILE_NOT_FOUND:
if err.winerror == winutil.ERROR_FILE_NOT_FOUND:
continue
raise
with k:
@ -274,7 +274,7 @@ def find_programs(extensions):
try:
k = Key(open_at=r'Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.%s\OpenWithProgIDs' % ext, root=HKEY_CURRENT_USER)
except OSError as err:
if err.winerror == winerror.ERROR_FILE_NOT_FOUND:
if err.winerror == winutil.ERROR_FILE_NOT_FOUND:
continue
for prog_id in itervalues(k):
if prog_id and prog_id not in seen_prog_ids: