KNetworkMounts Class

Header: #include <KNetworkMounts>
CMake: find_package(KF6 REQUIRED COMPONENTS CoreAddons)
target_link_libraries(mytarget PRIVATE KF6::CoreAddons)
Since: 5.85
Inherits: QObject

Public Types

enum KNetworkMountOption { LowSideEffectsOptimizations, MediumSideEffectsOptimizations, StrongSideEffectsOptimizations, KDirWatchDontAddWatches, SymlinkPathsUseCache }
enum KNetworkMountsType { NfsPaths, SmbPaths, SymlinkDirectory, SymlinkToNetworkMount, Any }

Public Functions

void addPath(const QString &path, KNetworkMounts::KNetworkMountsType type)
QString canonicalSymlinkPath(const QString &path)
void clearCache()
bool isEnabled() const
bool isOptionEnabled(const KNetworkMounts::KNetworkMountOption option, const bool defaultValue = false) const
bool isOptionEnabledForPath(const QString &path, KNetworkMounts::KNetworkMountOption option)
bool isSlowPath(const QString &path, KNetworkMounts::KNetworkMountsType type = Any)
QStringList paths(KNetworkMounts::KNetworkMountsType type = Any) const
void setEnabled(bool value)
void setOption(const KNetworkMounts::KNetworkMountOption option, const bool value)
void setPaths(const QStringList &paths, KNetworkMounts::KNetworkMountsType type)
void sync()

Static Public Members

KNetworkMounts *self()

Detailed Description

Performance control on network mounts.

This class provides methods for deciding whether operations on slow network mounts should be performed or not.

Configuration is read from a configuration file network_mounts in the user's QStandardPaths::ConfigLocation. This file can be filled by using the network mounts performance configuration module or directly via setEnabled, setPaths, addPath and setOption

KNetworkMounts::self()->setEnabled(true);
KNetworkMounts::self()->setOption(KNetworkMounts::LowSideEffectsOptimizations, true);
KNetworkMounts::self()->addPath(path1, KNetworkMounts::NfsPaths);
KNetworkMounts::self()->addPath(path2, KNetworkMounts::NfsPaths);
KNetworkMounts::self()->setPaths(listOfPaths, KNetworkMounts::SmbPaths);

Use KNetworkMounts like this to check if the given url is on a configured slow path and the KNetworkMountOption LowSideEffectsOptimizations is enabled:

if (KNetworkMounts::self()->isOptionEnabledForPath(url.toLocalFile(),
    KNetworkMounts::LowSideEffectsOptimizations))
{
    // skip operations which are slow on the given url if
    // KNetworkMountOption LowSideEffectsOptimizations is enabled
} else {
    // given url is not configured being slow or the KNetworkMountOption
    // LowSideEffectsOptimizations is not enabled
}

If called for the first time, this creates a singleton instance and reads the config file. Subsequent calls just use this instance without reading the config file again.

Member Type Documentation

enum KNetworkMounts::KNetworkMountOption

The KNetworkMountOption enum

ConstantValueDescription
KNetworkMounts::LowSideEffectsOptimizations0Don't run KDiskFreeSpaceInfo if slow path. Don't check for manually mounted drives. Don't check with QFileInfo::isWritable if it is writable, if not yet known, return true. Don't check with QFileInfo::isReadable if it is readable, return false. Don't check for desktop files just return false. Ignore .hidden files on slow paths. Don't read mime comment from .desktop or .directory files. Don't get the size with QFileInfo::size, just return 0, if not yet known. Don't determine mime type from file content, use file extension. Don't check for desktop files just return false. Don't call KFileSystemType::fileSystemType to check if the filesystem is slow, just return true. Don't count files/directories in subdirectories. Don't calculate sizes of subdirectories. Avoid check for dir at Kate startup.
KNetworkMounts::MediumSideEffectsOptimizations1Don't return project for dir, avoid QFileInfo().absoluteDir(). Don't search for .kateconfig recursively Ignore recent files on slow paths
KNetworkMounts::StrongSideEffectsOptimizations2Turn off symbolic link resolution
KNetworkMounts::KDirWatchDontAddWatches3Disables dir watching completely for slow paths, avoids stat() calls on added dirs and subdirs
KNetworkMounts::SymlinkPathsUseCache4Cache resolved symlink paths

enum KNetworkMounts::KNetworkMountsType

The KNetworkMountsType enum

ConstantValueDescription
KNetworkMounts::NfsPaths0NFS paths
KNetworkMounts::SmbPaths1SMB paths
KNetworkMounts::SymlinkDirectory2Paths to directories which contain symbolic links to network mounts
KNetworkMounts::SymlinkToNetworkMount3Paths which are symbolic links to network mounts
KNetworkMounts::Any4Any slow path type. Do not use with setPaths or addPath

Member Function Documentation

void KNetworkMounts::addPath(const QString &path, KNetworkMounts::KNetworkMountsType type)

Add a path for which optimizations are to take place

path the path to add

type the type of the path. Do not use Any

See also KNetworkMountsType.

QString KNetworkMounts::canonicalSymlinkPath(const QString &path)

Resolves a path that may contain symbolic links to mounted network shares.

A symlink path is either a directory which contains symbolic links to slow network mounts (SymlinkDirectory) or a direct symbolic link to a slow network mount ( * SymlinkToNfsOrSmbPaths).

Example: There are some Samba shares mounted below /mnt. These are paths of type SmbPaths

/mnt/server1/share1
/mnt/server1/share2
/mnt/server2/share3

A (logged in) user may have symbolic links to them in his home directory below netshares. The directory /home/user/netshares is a SymlinkDirectory:

/home/user/netshares/share1 -> /mnt/server1/share1
/home/user/netshares/share2 -> /mnt/server1/share2
/home/user/netshares/share3 -> /mnt/server2/share3

There is a direct symbolic link from /home/user/share1 to /mnt/server1/share1. This is of type SymlinkToNfsOrSmbPaths:

/home/user/share1 -> /mnt/server1/share1

Both types of symbolic links from symlink paths to the real mounted shares are resolved even if KNetworkMountOption StrongSideEffectsOptimizations is enabled.

If the setup is like above a path

/home/user/netshares/share1/Documents/file.txt

would be resolved to

/mnt/server1/share1/Documents/file.txt

and a path

/home/user/share1/Documents/file.txt

would also be resolved to

/mnt/server1/share1/Documents/file.txt

Resolved paths are cached in a hash.

path the path to resolve

Returns the resolved path or path if path is not a symlink path or no symlink found

See also KNetworkMountsType, clearCache, and isSlowPath.

void KNetworkMounts::clearCache()

Clears the canonical symlink path cache

Call this if directory structures on mounted network drives changed. Don't enable the cache (SymlinkPathsUseCache) if this happens often and the drives are usually accessed via the symlinks. This method exists mainly for the KCM.

See also canonicalSymlinkPath.

bool KNetworkMounts::isEnabled() const

Query if the performance optimizations are switched on

Returns true if on, false otherwise

bool KNetworkMounts::isOptionEnabled(const KNetworkMounts::KNetworkMountOption option, const bool defaultValue = false) const

Query a performance option

option the option to query

defaultValue the value to return if the option is not configured

Returns true if option is on, false if not

See also KNetworkMountOption.

bool KNetworkMounts::isOptionEnabledForPath(const QString &path, KNetworkMounts::KNetworkMountOption option)

Query if path is configured to be a slow path and option is enabled

path the path to query

option the option to query

Returns true if path is a configured slow path and option option is enabled

bool KNetworkMounts::isSlowPath(const QString &path, KNetworkMounts::KNetworkMountsType type = Any)

Query if path is configured to be a slow path of type type

path the path to query

type the type to query. If omitted, any type matches

Returns true if path is a configured slow path of type type

This function is also used to determine the filesystem type in KFileSystemType::fileSystemType (KFileSystemType::Smb or KFileSystemType::Nfs) without an expensive call to stafs(). For this to work the types of paths need to be correctly assigned in setPath or addPath

QStringList KNetworkMounts::paths(KNetworkMounts::KNetworkMountsType type = Any) const

Query the configured paths for which optimizations are to take place

Returns a list of paths

See also setPaths().

[static] KNetworkMounts *KNetworkMounts::self()

Returns (and creates if necessary) the singleton instance

void KNetworkMounts::setEnabled(bool value)

Switch the performance optimizations on or off

value the value to set

See also isEnabled().

void KNetworkMounts::setOption(const KNetworkMounts::KNetworkMountOption option, const bool value)

Switch a performance option on or off

option the option to change

value the value to set

See also KNetworkMountOption.

void KNetworkMounts::setPaths(const QStringList &paths, KNetworkMounts::KNetworkMountsType type)

Set the paths for which optimizations are to take place

paths the paths to set

type the type of paths. Do not use Any

See also paths() and KNetworkMountsType.

void KNetworkMounts::sync()

Synchronizes to config file

QSettings synchronization also takes place automatically at regular intervals and from QSettings destructor, see QSettings::sync() documentation.

Calls QSettings::sync()