gio: Add API for identifying system FS types and device paths

This is needed by gnome-control-center and gnome-settings-daemon; it
makes existing checks from gunixmounts.c public.

Signed-off-by: Philip Withnall <withnall@endlessm.com>

https://bugzilla.gnome.org/show_bug.cgi?id=788927
This commit is contained in:
Philip Withnall 2017-10-25 15:03:17 +01:00
parent f43babfea3
commit 1e4221a3f7
6 changed files with 127 additions and 9 deletions

View File

@ -1571,6 +1571,8 @@ g_unix_mount_monitor_get
g_unix_mount_monitor_new
g_unix_mount_monitor_set_rate_limit
g_unix_is_mount_path_system_internal
g_unix_is_system_fs_type
g_unix_is_system_device_path
<SUBSECTION Standard>
GUnixMountMonitorClass
G_UNIX_MOUNT_MONITOR

View File

@ -281,10 +281,23 @@ g_unix_is_mount_path_system_internal (const char *mount_path)
return FALSE;
}
static gboolean
guess_system_internal (const char *mountpoint,
const char *fs,
const char *device)
/**
* g_unix_is_system_fs_type:
* @fs_type: a file system type, e.g. `procfs` or `tmpfs`
*
* Determines if @fs_type is considered a type of file system which is only
* used in implementation of the OS. This is primarily used for hiding
* mounted volumes that are intended as APIs for programs to read, and system
* administrators at a shell; rather than something that should, for example,
* appear in a GUI. For example, the Linux `/proc` filesystem.
*
* The list of file system types considered system ones may change over time.
*
* Returns: %TRUE if @fs_type is considered an implementation detail of the OS.
* Since: 2.56
*/
gboolean
g_unix_is_system_fs_type (const char *fs_type)
{
const char *ignore_fs[] = {
"adfs",
@ -336,6 +349,31 @@ guess_system_internal (const char *mountpoint,
"zfs",
NULL
};
g_return_val_if_fail (fs_type != NULL && *fs_type != '\0', FALSE);
return is_in (fs_type, ignore_fs);
}
/**
* g_unix_is_system_device_path:
* @fs_type: a device path, e.g. `/dev/loop0` or `nfsd`
*
* Determines if @device_path is considered a block device path which is only
* used in implementation of the OS. This is primarily used for hiding
* mounted volumes that are intended as APIs for programs to read, and system
* administrators at a shell; rather than something that should, for example,
* appear in a GUI. For example, the Linux `/proc` filesystem.
*
* The list of device paths considered system ones may change over time.
*
* Returns: %TRUE if @device_path is considered an implementation detail of
* the OS.
* Since: 2.56
*/
gboolean
g_unix_is_system_device_path (const char *device_path)
{
const char *ignore_devices[] = {
"none",
"sunrpc",
@ -345,11 +383,21 @@ guess_system_internal (const char *mountpoint,
"/dev/vn",
NULL
};
if (is_in (fs, ignore_fs))
g_return_val_if_fail (device_path != NULL && *device_path != '\0', FALSE);
return is_in (device_path, ignore_devices);
}
static gboolean
guess_system_internal (const char *mountpoint,
const char *fs,
const char *device)
{
if (g_unix_is_system_fs_type (fs))
return TRUE;
if (is_in (device, ignore_devices))
if (g_unix_is_system_device_path (device))
return TRUE;
if (g_unix_is_mount_path_system_internal (mountpoint))
@ -2105,9 +2153,14 @@ g_unix_mount_is_readonly (GUnixMountEntry *mount_entry)
/**
* g_unix_mount_is_system_internal:
* @mount_entry: a #GUnixMount.
*
* Checks if a Unix mount is a system mount. This is the Boolean OR of
* g_unix_is_system_fs_type(), g_unix_is_system_device_path() and
* g_unix_is_mount_path_system_internal() on @mount_entrys properties.
*
* Checks if a unix mount is a system path.
*
* The definition of what a system mount entry is may change over time as new
* file system types and device paths are ignored.
*
* Returns: %TRUE if the unix mount is for a system path.
*/
gboolean

View File

@ -151,6 +151,10 @@ void g_unix_mount_monitor_set_rate_limit (GUnixMountMonitor *mount
GLIB_AVAILABLE_IN_ALL
gboolean g_unix_is_mount_path_system_internal (const char *mount_path);
GLIB_AVAILABLE_IN_2_56
gboolean g_unix_is_system_fs_type (const char *fs_type);
GLIB_AVAILABLE_IN_2_56
gboolean g_unix_is_system_device_path (const char *device_path);
G_END_DECLS

View File

@ -266,6 +266,7 @@ test_programs += \
socket-address \
stream-rw_all \
unix-fd \
unix-mounts \
unix-streams \
g-file-info-filesystem-readonly \
$(NULL)

View File

@ -113,6 +113,7 @@ if host_machine.system() != 'windows'
'socket-address',
'stream-rw_all',
'unix-fd',
'unix-mounts',
'unix-streams',
'g-file-info-filesystem-readonly',
'gschema-compile',

57
gio/tests/unix-mounts.c Normal file
View File

@ -0,0 +1,57 @@
/* GLib testing framework examples and tests
*
* Copyright © 2017 Endless Mobile, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
#include <glib.h>
#ifndef G_OS_UNIX
#error This is a Unix-specific test
#endif
#include <errno.h>
#include <locale.h>
#include <glib/gstdio.h>
#include <gio/gio.h>
#include <gio/gunixmounts.h>
static void
test_is_system_fs_type (void)
{
g_assert_true (g_unix_is_system_fs_type ("tmpfs"));
g_assert_false (g_unix_is_system_fs_type ("ext4"));
}
static void
test_is_system_device_path (void)
{
g_assert_true (g_unix_is_system_device_path ("devpts"));
g_assert_false (g_unix_is_system_device_path ("/"));
}
int
main (int argc,
char *argv[])
{
setlocale (LC_ALL, "");
g_test_init (&argc, &argv, NULL);
g_test_add_func ("/unix-mounts/is-system-fs-type", test_is_system_fs_type);
g_test_add_func ("/unix-mounts/is-system-device-path", test_is_system_device_path);
return g_test_run ();
}