File indexing completed on 2024-09-29 12:07:43

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 2003 Waldo Bastian <bastian@kde.org>
0004     SPDX-FileCopyrightText: 2007 David Faure <faure@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-only
0007 */
0008 
0009 #ifndef KMOUNTPOINT_H
0010 #define KMOUNTPOINT_H
0011 
0012 #include "kiocore_export.h"
0013 
0014 #include <QExplicitlySharedDataPointer>
0015 #include <QStringList>
0016 
0017 #include <memory>
0018 #include <sys/types.h> // dev_t among other definitions
0019 
0020 class KMountPointPrivate;
0021 
0022 /**
0023  * @class KMountPoint kmountpoint.h <KMountPoint>
0024  *
0025  * The KMountPoint class provides information about mounted and unmounted disks.
0026  * It provides a system independent interface to fstab.
0027  *
0028  * @author Waldo Bastian <bastian@kde.org>
0029  */
0030 class KIOCORE_EXPORT KMountPoint : public QSharedData
0031 {
0032 public:
0033     using Ptr = QExplicitlySharedDataPointer<KMountPoint>;
0034 
0035     /**
0036      * List of mount points.
0037      */
0038     class KIOCORE_EXPORT List : public QList<Ptr>
0039     {
0040     public:
0041         List();
0042         /**
0043          * Find the mountpoint on which resides @p path
0044          * For instance if /home is a separate partition, findByPath("/home/user/blah")
0045          * will return /home
0046          * @param path the path to check
0047          * @return the mount point of the given file
0048          */
0049         Ptr findByPath(const QString &path) const;
0050 
0051         /**
0052          * Returns the mount point associated with @p device,
0053          * i.e. the one where mountedFrom() == @p device
0054          * (after symlink resolution).
0055          * @return the mountpoint, or @c nullptr if this device doesn't exist or isn't mounted
0056          */
0057         Ptr findByDevice(const QString &device) const;
0058     };
0059 
0060 public:
0061     /**
0062      * Flags that specify which additional details should be fetched for each mountpoint.
0063      * @see DetailsNeededFlags
0064      */
0065     enum DetailsNeededFlag {
0066         /**
0067          * Only the basic details: mountedFrom, mountPoint, mountType.
0068          */
0069         BasicInfoNeeded = 0,
0070         /**
0071          * Also fetch the options used when mounting, see KMountPoint::mountOptions().
0072          */
0073         NeedMountOptions = 1,
0074         /**
0075          * Also fetch the device name (with symlinks resolved), see KMountPoint::realDeviceName().
0076          */
0077         NeedRealDeviceName = 2,
0078     };
0079     /**
0080      * Stores a combination of #DetailsNeededFlag values.
0081      */
0082     Q_DECLARE_FLAGS(DetailsNeededFlags, DetailsNeededFlag)
0083 
0084     /**
0085      * This function gives a list of all possible mountpoints. (fstab)
0086      * @param infoNeeded Flags that specify which additional information
0087      * should be fetched.
0088      */
0089     static List possibleMountPoints(DetailsNeededFlags infoNeeded = BasicInfoNeeded);
0090 
0091     /**
0092      * Returns a list of all current mountpoints.
0093      *
0094      * @param infoNeeded Flags that specify which additional information
0095      * should be fetched.
0096      *
0097      * @note This method will return an empty list on @c Android
0098      */
0099     static List currentMountPoints(DetailsNeededFlags infoNeeded = BasicInfoNeeded);
0100 
0101     /**
0102      * Where this filesystem gets mounted from.
0103      * This can refer to a device, a remote server or something else.
0104      */
0105     QString mountedFrom() const;
0106 
0107     /**
0108      * Returns @c true if this mount point represents a network filesystem (e.g.\ NFS,
0109      * CIFS, etc.), otherwise returns @c false.
0110      *
0111      * @since 5.86
0112      */
0113     bool isOnNetwork() const;
0114 
0115     /**
0116      * Returns the device ID (dev_t, major, minor) of this mount point. This
0117      * ID is unique per device (including network mounts).
0118      *
0119      * @since 5.86
0120      */
0121     dev_t deviceId() const;
0122 
0123     /**
0124      * Canonical name of the device where the filesystem got mounted from.
0125      * (Or empty, if not a device)
0126      * Only available when the NeedRealDeviceName flag was set.
0127      */
0128     QString realDeviceName() const;
0129 
0130     /**
0131      * Path where the filesystem is mounted (if you used @ref currentMountPoints()),
0132      * or can be mounted (if you used @ref possibleMountPoints()).
0133      */
0134     QString mountPoint() const;
0135 
0136     /**
0137      * Type of filesystem
0138      */
0139     QString mountType() const;
0140 
0141     /**
0142      * Options used to mount the filesystem.
0143      * Only available if the @ref NeedMountOptions flag was set.
0144      */
0145     QStringList mountOptions() const;
0146 
0147     /**
0148      * Returns @c true if the filesystem is "probably" slow, e.g.\ a network mount,
0149      * @c false otherwise.
0150      */
0151     bool probablySlow() const;
0152 
0153     enum FileSystemFlag {
0154         SupportsChmod,
0155         SupportsChown,
0156         SupportsUTime,
0157         SupportsSymlinks,
0158         CaseInsensitive,
0159     };
0160 
0161     /**
0162      * Checks the capabilities of the filesystem.
0163      * @param flag the flag to check
0164      * @return true if the filesystem has that flag, false if not
0165      *
0166      * The available flags are:
0167      * @li SupportsChmod: returns true if the filesystem supports chmod
0168      * (e.g. msdos filesystems return false)
0169      * @li SupportsChown: returns true if the filesystem supports chown
0170      * (e.g. msdos filesystems return false)
0171      * @li SupportsUtime: returns true if the filesystems supports utime
0172      * (e.g. msdos filesystems return false)
0173      * @li SupportsSymlinks: returns true if the filesystems supports symlinks
0174      * (e.g. msdos filesystems return false)
0175      * @li CaseInsensitive: returns true if the filesystem treats
0176      * "foo" and "FOO" as being the same file (true for msdos filesystems)
0177      *
0178      */
0179     bool testFileSystemFlag(FileSystemFlag flag) const;
0180 
0181     /**
0182      * Destructor
0183      */
0184     ~KMountPoint();
0185 
0186 private:
0187     /**
0188      * Constructor
0189      */
0190     KIOCORE_NO_EXPORT KMountPoint();
0191 
0192     friend KIOCORE_EXPORT QDebug operator<<(QDebug debug, const Ptr &mp);
0193 
0194     friend KMountPointPrivate;
0195     std::unique_ptr<KMountPointPrivate> d;
0196 };
0197 
0198 KIOCORE_EXPORT QDebug operator<<(QDebug debug, const KMountPoint::Ptr &mp);
0199 
0200 Q_DECLARE_OPERATORS_FOR_FLAGS(KMountPoint::DetailsNeededFlags)
0201 
0202 #endif // KMOUNTPOINT_H