From 00c00e2f3f3194202877e54a2429bd1a4f9c5526 Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Mon, 9 Jan 2012 21:49:08 -0500 Subject: [PATCH] Add G_FILE_ATTRIBUTE_FILESYSTEM_USED to get exact used space This is implemented by with statfs_buffer.f_bavail (free blocks for unprivileged users) as a default way to retrieve real free space. Based on a patch by Marcus Carlson, bug 625751. --- docs/reference/gio/gio-sections.txt | 1 + gio/gfileattribute.c | 1 + gio/gfileinfo.h | 11 +++++++++++ gio/glocalfile.c | 19 +++++++++++++++++++ 4 files changed, 32 insertions(+) diff --git a/docs/reference/gio/gio-sections.txt b/docs/reference/gio/gio-sections.txt index 62b233af8..8c59f3b69 100644 --- a/docs/reference/gio/gio-sections.txt +++ b/docs/reference/gio/gio-sections.txt @@ -295,6 +295,7 @@ G_FILE_ATTRIBUTE_THUMBNAILING_FAILED G_FILE_ATTRIBUTE_PREVIEW_ICON G_FILE_ATTRIBUTE_FILESYSTEM_SIZE G_FILE_ATTRIBUTE_FILESYSTEM_FREE +G_FILE_ATTRIBUTE_FILESYSTEM_USED G_FILE_ATTRIBUTE_FILESYSTEM_TYPE G_FILE_ATTRIBUTE_FILESYSTEM_READONLY G_FILE_ATTRIBUTE_GVFS_BACKEND diff --git a/gio/gfileattribute.c b/gio/gfileattribute.c index 8e4b8bf1e..bca38cc7b 100644 --- a/gio/gfileattribute.c +++ b/gio/gfileattribute.c @@ -194,6 +194,7 @@ * %G_FILE_ATTRIBUTE_PREVIEW_ICONpreview::iconobject (#GIcon) * %G_FILE_ATTRIBUTE_FILESYSTEM_SIZEfilesystem::sizeuint64 * %G_FILE_ATTRIBUTE_FILESYSTEM_FREEfilesystem::freeuint64 + * %G_FILE_ATTRIBUTE_FILESYSTEM_USEDfilesystem::useduint64 * %G_FILE_ATTRIBUTE_FILESYSTEM_TYPEfilesystem::typestring * %G_FILE_ATTRIBUTE_FILESYSTEM_READONLYfilesystem::readonlyboolean * %G_FILE_ATTRIBUTE_GVFS_BACKENDgvfs::backendstring diff --git a/gio/gfileinfo.h b/gio/gfileinfo.h index 952dade01..375a078ec 100644 --- a/gio/gfileinfo.h +++ b/gio/gfileinfo.h @@ -728,6 +728,17 @@ typedef struct _GFileInfoClass GFileInfoClass; **/ #define G_FILE_ATTRIBUTE_FILESYSTEM_FREE "filesystem::free" /* uint64 */ +/** + * G_FILE_ATTRIBUTE_FILESYSTEM_USED: + * + * A key in the "filesystem" namespace for getting the number of bytes of used on the + * file system. Corresponding #GFileAttributeType is + * %G_FILE_ATTRIBUTE_TYPE_UINT64. + * + * Since: 2.32 + */ +#define G_FILE_ATTRIBUTE_FILESYSTEM_USED "filesystem::used" /* uint64 */ + /** * G_FILE_ATTRIBUTE_FILESYSTEM_TYPE: * diff --git a/gio/glocalfile.c b/gio/glocalfile.c index 7c1b05bfc..cfd805a1b 100644 --- a/gio/glocalfile.c +++ b/gio/glocalfile.c @@ -990,6 +990,25 @@ g_local_file_query_filesystem_info (GFile *file, #if defined(USE_STATFS) || defined(USE_STATVFS) g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_SIZE, block_size * statfs_buffer.f_blocks); #endif +#endif /* G_OS_WIN32 */ + } + + if (!no_size && + g_file_attribute_matcher_matches (attribute_matcher, + G_FILE_ATTRIBUTE_FILESYSTEM_USED)) + { +#ifdef G_OS_WIN32 + gchar *localdir = g_path_get_dirname (local->filename); + wchar_t *wdirname = g_utf8_to_utf16 (localdir, -1, NULL, NULL, NULL); + ULARGE_INTEGER li_free; + ULARGE_INTEGER li_total; + + g_free (localdir); + if (GetDiskFreeSpaceExW (wdirname, &li_free, &li_total, NULL)) + g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_USED, (guint64)li_total.QuadPart - (guint64)li_free.QuadPart); + g_free (wdirname); +#else + g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_USED, block_size * (statfs_buffer.f_blocks - statfs_buffer.f_bfree)); #endif /* G_OS_WIN32 */ }