26 #include <QtCore/QFile>
27 #include <QtCore/QTextIStream>
39 #ifdef HAVE_SYS_MNTTAB_H
40 #include <sys/mnttab.h>
44 #elif defined(HAVE_SYS_MNTENT_H)
45 #include <sys/mntent.h>
49 #ifdef HAVE_SYS_MOUNT_H
50 #ifdef HAVE_SYS_TYPES_H
51 #include <sys/types.h>
53 #ifdef HAVE_SYS_PARAM_H
54 #include <sys/param.h>
56 #include <sys/mount.h>
63 #include <sys/mntctl.h>
64 #include <sys/vmount.h>
68 extern "C" int mntctl(
int command,
int size,
void* buffer);
70 extern "C" struct vfs_ent *getvfsbytype(
int vfsType);
71 extern "C" void endvfsent( );
75 #ifndef HAVE_GETMNTINFO
79 # define MNTTAB _PATH_MOUNTED
83 # define MNTTAB MTAB_FILE
85 # define MNTTAB "/etc/mnttab"
95 #define FSTAB "/etc/vfstab"
97 #define FSTAB "/etc/fstab"
100 class KMountPoint::Private {
102 void finalizePossibleMountPoint(DetailsNeededFlags infoNeeded);
103 void finalizeCurrentMountPoint(DetailsNeededFlags infoNeeded);
112 KMountPoint::KMountPoint()
129 #ifdef HAVE_SETMNTENT
130 #define SETMNTENT setmntent
131 #define ENDMNTENT endmntent
132 #define STRUCT_MNTENT struct mntent *
133 #define STRUCT_SETMNTENT FILE *
134 #define GETMNTENT(file, var) ((var = getmntent(file)) != 0)
135 #define MOUNTPOINT(var) var->mnt_dir
136 #define MOUNTTYPE(var) var->mnt_type
137 #define MOUNTOPTIONS(var) var->mnt_opts
138 #define FSNAME(var) var->mnt_fsname
140 #define SETMNTENT fopen
141 #define ENDMNTENT fclose
142 #define STRUCT_MNTENT struct mnttab
143 #define STRUCT_SETMNTENT FILE *
144 #define GETMNTENT(file, var) (getmntent(file, &var) == 0)
145 #define MOUNTPOINT(var) var.mnt_mountp
146 #define MOUNTTYPE(var) var.mnt_fstype
147 #define MOUNTOPTIONS(var) var.mnt_mntopts
148 #define FSNAME(var) var.mnt_special
158 for ( QStringList::ConstIterator it = options.begin(); it != options.end(); ++it)
160 if( (*it).startsWith(QLatin1String(
"dev=")))
163 return QString::fromLatin1(
"none");
166 void KMountPoint::Private::finalizePossibleMountPoint(DetailsNeededFlags infoNeeded)
168 if (mountType == QLatin1String(
"supermount")) {
172 if (mountedFrom.startsWith(QLatin1String(
"UUID="))) {
173 const QString uuid = mountedFrom.mid(5);
174 const QString potentialDevice = QFile::symLinkTarget(QString::fromLatin1(
"/dev/disk/by-uuid/") + uuid);
175 if (QFile::exists(potentialDevice)) {
176 mountedFrom = potentialDevice;
179 if (mountedFrom.startsWith(QLatin1String(
"LABEL="))) {
180 const QString label = mountedFrom.mid(6);
181 const QString potentialDevice = QFile::symLinkTarget(QString::fromLatin1(
"/dev/disk/by-label/") + label);
182 if (QFile::exists(potentialDevice)) {
183 mountedFrom = potentialDevice;
187 if (infoNeeded & NeedRealDeviceName) {
188 if (mountedFrom.startsWith(QLatin1Char(
'/')))
194 void KMountPoint::Private::finalizeCurrentMountPoint(DetailsNeededFlags infoNeeded)
196 if (infoNeeded & NeedRealDeviceName) {
197 if (mountedFrom.startsWith(QLatin1Char(
'/')))
210 #ifdef HAVE_SETMNTENT
219 mp->
d->mountedFrom = QFile::decodeName(
FSNAME(fe));
221 mp->
d->mountPoint = QFile::decodeName(
MOUNTPOINT(fe));
222 mp->
d->mountType = QFile::decodeName(
MOUNTTYPE(fe));
226 if (infoNeeded &
NeedMountOptions || (mp->
d->mountType == QLatin1String(
"supermount")))
229 mp->
d->mountOptions = options.split( QLatin1Char(
',') );
232 mp->
d->finalizePossibleMountPoint(infoNeeded);
238 QFile f(QLatin1String(
FSTAB));
239 if ( !f.open(QIODevice::ReadOnly) )
247 s=t.readLine().simplified();
248 if ( s.isEmpty() || (s[0] == QLatin1Char(
'#')))
252 const QStringList item = s.split(QLatin1Char(
' '));
255 if (item.count() < 5)
258 if (item.count() < 4)
265 mp->
d->mountedFrom = item[i++];
270 mp->
d->mountPoint = item[i++];
271 mp->
d->mountType = item[i++];
276 mp->
d->mountOptions = options.split(QLatin1Char(
','));
279 mp->
d->finalizePossibleMountPoint(infoNeeded);
293 #ifdef HAVE_GETMNTINFO
295 #ifdef GETMNTINFO_USES_STATVFS
296 struct statvfs *mounted;
298 struct statfs *mounted;
301 int num_fs = getmntinfo(&mounted, MNT_NOWAIT);
303 for (
int i=0;i< num_fs;i++)
306 mp->
d->mountedFrom = QFile::decodeName(mounted[i].f_mntfromname);
307 mp->
d->mountPoint = QFile::decodeName(mounted[i].f_mntonname);
310 mp->
d->mountType = QFile::decodeName(mnt_names[mounted[i].f_type]);
312 mp->
d->mountType = QFile::decodeName(mounted[i].f_fstypename);
317 struct fstab *ft = getfsfile(mounted[i].f_mntonname);
319 QString options = QFile::decodeName(ft->fs_mntops);
320 mp->
d->mountOptions = options.split(QLatin1Char(
','));
326 mp->
d->finalizeCurrentMountPoint(infoNeeded);
333 struct vmount *mntctl_buffer;
340 mntctl_buffer = (
struct vmount*)malloc(buf_sz);
341 num = mntctl(MCTL_QUERY, buf_sz, mntctl_buffer);
344 buf_sz = *(
int*)mntctl_buffer;
346 mntctl_buffer = (
struct vmount*)malloc(buf_sz);
347 num = mntctl(MCTL_QUERY, buf_sz, mntctl_buffer);
353 vm = (
struct vmount *)mntctl_buffer;
354 for ( ; num > 0; --num )
357 fsname_len = vmt2datasize(vm, VMT_STUB);
358 mountedto = (
char*)malloc(fsname_len + 1);
359 mountedto[fsname_len] =
'\0';
360 strncpy(mountedto, (
char *)vmt2dataptr(vm, VMT_STUB), fsname_len);
362 fsname_len = vmt2datasize(vm, VMT_OBJECT);
363 mountedfrom = (
char*)malloc(fsname_len + 1);
364 mountedfrom[fsname_len] =
'\0';
365 strncpy(mountedfrom, (
char *)vmt2dataptr(vm, VMT_OBJECT), fsname_len);
371 struct vfs_ent* ent = getvfsbytype(vm->vmt_gfstype);
374 mp->d->mountedFrom = QFile::decodeName(mountedfrom);
375 mp->d->mountPoint = QFile::decodeName(mountedto);
376 mp->d->mountType = QFile::decodeName(ent->vfsent_name);
386 mp->d->finalizeCurrentMountPoint(infoNeeded);
390 vm = (
struct vmount *)((
char *)vm + vm->vmt_length);
396 free( mntctl_buffer );
397 #elif defined(Q_WS_WIN) && !defined(_WIN32_WCE)
399 DWORD bits = GetLogicalDrives();
403 for(
int i = 0; i < 26; i++)
408 mp->
d->mountPoint =
QString(QLatin1Char(
'A' + i) + QLatin1String(
":/"));
413 #elif defined(_WIN32_WCE)
427 mp->
d->mountedFrom = QFile::decodeName(
FSNAME(fe));
429 mp->
d->mountPoint = QFile::decodeName(
MOUNTPOINT(fe));
430 mp->
d->mountType = QFile::decodeName(
MOUNTTYPE(fe));
434 if (infoNeeded &
NeedMountOptions || (mp->
d->mountType == QLatin1String(
"supermount")))
437 mp->
d->mountOptions = options.split( QLatin1Char(
',') );
439 mp->
d->finalizeCurrentMountPoint(infoNeeded);
450 return d->mountedFrom;
460 return d->mountPoint;
470 return d->mountOptions;
480 const QLatin1Char slash(
'/');
482 if (child.startsWith(parent)) {
490 return parent == child || parent.endsWith(slash) || child.at(parent.length()) == slash;
493 return parent.endsWith(slash) && (parent.length() == child.length() + 1) && parent.startsWith(child);
503 const QString realname = QDir::fromNativeSeparators(QDir(path).absolutePath());
508 for (const_iterator it = begin(); it != end(); ++it) {
509 const QString mountpoint = (*it)->d->mountPoint;
510 const int length = mountpoint.length();
523 if (realDevice.isEmpty())
525 for (const_iterator it = begin(); it != end(); ++it) {
526 if ((*it)->d->device == realDevice ||
527 (*it)->d->mountedFrom == realDevice)
535 bool nfs = d->mountType == QLatin1String(
"nfs");
536 bool autofs = d->mountType == QLatin1String(
"autofs") || d->mountType == QLatin1String(
"subfs");
550 const bool isMsDos = ( d->mountType == QLatin1String(
"msdos") || d->mountType == QLatin1String(
"fat") || d->mountType == QLatin1String(
"vfat") );
551 const bool isNtfs = d->mountType.contains(QLatin1String(
"fuse.ntfs")) || d->mountType.contains(QLatin1String(
"fuseblk.ntfs"))
553 || d->mountType == QLatin1String(
"fuseblk");
554 const bool isSmb = d->mountType == QLatin1String(
"cifs") || d->mountType == QLatin1String(
"smbfs");
561 return !isMsDos && !isNtfs && !isSmb;
Can be used to control the lifetime of an object that has derived QSharedData.
bool probablySlow() const
Checks if the filesystem that is probably slow (nfs mounts).
The KMountPoint class provides information about mounted and unmounted disks.
#define GETMNTENT(file, var)
static QString realFilePath(const QString &filename)
Expands all symbolic links and resolves references to '/.
static List currentMountPoints(DetailsNeededFlags infoNeeded=BasicInfoNeeded)
This function gives a list of all currently used mountpoints.
QString mountedFrom() const
Where this filesystem gets mounted from.
~KMountPoint()
Destructor.
QString realDeviceName() const
Canonical name of the device where the filesystem got mounted from.
Ptr findByDevice(const QString &device) const
Returns the mount point associated with device, i.e.
QString mountType() const
Type of filesystem.
bool testFileSystemFlag(FileSystemFlag flag) const
Checks the capabilities of the filesystem.
static QString devNameFromOptions(const QStringList &options)
When using supermount, the device name is in the options field as dev=/my/device. ...
QString mountPoint() const
Path where the filesystem is mounted or can be mounted.
KSharedPtr< KMountPoint > Ptr
QStringList mountOptions() const
Options used to mount the filesystem.
static List possibleMountPoints(DetailsNeededFlags infoNeeded=BasicInfoNeeded)
This function gives a list of all possible mountpoints.
#define MOUNTOPTIONS(var)
Ptr findByPath(const QString &path) const
Find the mountpoint on which resides path For instance if /home is a separate partition, findByPath("/home/user/blah") will return /home.
static bool pathsAreParentAndChildOrEqual(const QString &parent, const QString &child)