28#include "kstandarddirs.h"
30#include "kmountpoint.h"
35#ifdef HAVE_SYS_MNTTAB_H
37#include <sys/mnttab.h>
41#elif defined(HAVE_SYS_MNTENT_H)
42#include <sys/mntent.h>
46#ifdef HAVE_SYS_MOUNT_H
47#ifdef HAVE_SYS_TYPES_H
50#ifdef HAVE_SYS_PARAM_H
60#include <sys/mntctl.h>
61#include <sys/vmount.h>
66int mntctl(
int command,
int size,
void* buffer);
69extern "C" struct vfs_ent *getvfsbytype(
int vfsType);
70extern "C" void endvfsent( );
74#ifndef HAVE_GETMNTINFO
78# define MNTTAB _PATH_MOUNTED
82# define MNTTAB MTAB_FILE
84# define MNTTAB "/etc/mnttab"
93#define FSTAB "/etc/vfstab"
95#define FSTAB "/etc/fstab"
100KMountPoint::KMountPoint()
109#define SETMNTENT setmntent
110#define ENDMNTENT endmntent
111#define STRUCT_MNTENT struct mntent *
112#define STRUCT_SETMNTENT FILE *
113#define GETMNTENT(file, var) ((var = getmntent(file)) != 0)
114#define MOUNTPOINT(var) var->mnt_dir
115#define MOUNTTYPE(var) var->mnt_type
116#define MOUNTOPTIONS(var) var->mnt_opts
117#define FSNAME(var) var->mnt_fsname
119#define SETMNTENT fopen
120#define ENDMNTENT fclose
121#define STRUCT_MNTENT struct mnttab
122#define STRUCT_SETMNTENT FILE *
123#define GETMNTENT(file, var) (getmntent(file, &var) == 0)
124#define MOUNTPOINT(var) var.mnt_mountp
125#define MOUNTTYPE(var) var.mnt_fstype
126#define MOUNTOPTIONS(var) var.mnt_mntopts
127#define FSNAME(var) var.mnt_special
132 KMountPoint::List result;
135 STRUCT_SETMNTENT fstab;
136 if ((fstab = SETMNTENT(FSTAB,
"r")) == 0)
140 while (GETMNTENT(fstab, fe))
143 mp->m_mountedFrom = TQFile::decodeName(FSNAME(fe));
145 mp->m_mountPoint = TQFile::decodeName(MOUNTPOINT(fe));
146 mp->m_mountType = TQFile::decodeName(MOUNTTYPE(fe));
150 if (infoNeeded & NeedMountOptions || (mp->m_mountType ==
"supermount"))
152 TQString options = TQFile::decodeName(MOUNTOPTIONS(fe));
153 mp->m_mountOptions = TQStringList::split(
',', options);
156 if(mp->m_mountType ==
"supermount")
159 if (infoNeeded & NeedRealDeviceName)
161 if (mp->m_mountedFrom.startsWith(
"/"))
170 if ( !f.open(IO_ReadOnly) )
178 s=t.readLine().simplifyWhiteSpace();
179 if ( s.isEmpty() || (s[0] ==
'#'))
183 TQStringList item = TQStringList::split(
' ', s);
186 if (item.count() < 5)
189 if (item.count() < 4)
196 mp->m_mountedFrom = item[i++];
201 mp->m_mountPoint = item[i++];
202 mp->m_mountType = item[i++];
203 TQString options = item[i++];
205 if (infoNeeded & NeedMountOptions)
207 mp->m_mountOptions = TQStringList::split(
',', options);
210 if (infoNeeded & NeedRealDeviceName)
212 if (mp->m_mountedFrom.startsWith(
"/"))
226 KMountPoint::List result;
228#ifdef HAVE_GETMNTINFO
230#ifdef GETMNTINFO_USES_STATVFS
231 struct statvfs *mounted;
233 struct statfs *mounted;
236 int num_fs = getmntinfo(&mounted, MNT_NOWAIT);
238 for (
int i=0;i<num_fs;i++)
241 mp->m_mountedFrom = TQFile::decodeName(mounted[i].f_mntfromname);
242 mp->m_mountPoint = TQFile::decodeName(mounted[i].f_mntonname);
245 mp->m_mountType = TQFile::decodeName(mnt_names[mounted[i].f_type]);
247 mp->m_mountType = TQFile::decodeName(mounted[i].f_fstypename);
250 if (infoNeeded & NeedMountOptions)
252 struct fstab *ft = getfsfile(mounted[i].f_mntonname);
253 TQString options = TQFile::decodeName(ft->fs_mntops);
254 mp->m_mountOptions = TQStringList::split(
',', options);
257 if (infoNeeded & NeedRealDeviceName)
259 if (mp->m_mountedFrom.startsWith(
"/"))
268 struct vmount *mntctl_buffer;
275 mntctl_buffer = (
struct vmount*)malloc(buf_sz);
276 num = mntctl(MCTL_QUERY, buf_sz, mntctl_buffer);
279 buf_sz = *(
int*)mntctl_buffer;
281 mntctl_buffer = (
struct vmount*)malloc(buf_sz);
282 num = mntctl(MCTL_QUERY, buf_sz, mntctl_buffer);
288 vm = (
struct vmount *)mntctl_buffer;
289 for ( ; num > 0; num-- )
292 fsname_len = vmt2datasize(vm, VMT_STUB);
293 mountedto = (
char*)malloc(fsname_len + 1);
294 mountedto[fsname_len] =
'\0';
295 strncpy(mountedto, (
char *)vmt2dataptr(vm, VMT_STUB), fsname_len);
297 fsname_len = vmt2datasize(vm, VMT_OBJECT);
298 mountedfrom = (
char*)malloc(fsname_len + 1);
299 mountedfrom[fsname_len] =
'\0';
300 strncpy(mountedfrom, (
char *)vmt2dataptr(vm, VMT_OBJECT), fsname_len);
306 struct vfs_ent* ent = getvfsbytype(vm->vmt_gfstype);
309 mp->m_mountedFrom = TQFile::decodeName(mountedfrom);
310 mp->m_mountPoint = TQFile::decodeName(mountedto);
311 mp->m_mountType = TQFile::decodeName(ent->vfsent_name);
316 if (infoNeeded & NeedMountOptions)
321 if (infoNeeded & NeedRealDeviceName)
323 if (mp->m_mountedFrom.startsWith(
"/"))
330 vm = (
struct vmount *)((
char *)vm + vm->vmt_length);
336 free( mntctl_buffer );
337#elif defined(TQ_WS_WIN)
340 STRUCT_SETMNTENT mnttab;
341 if ((mnttab = SETMNTENT(MNTTAB,
"r")) == 0)
345 while (GETMNTENT(mnttab, fe))
348 mp->m_mountedFrom = TQFile::decodeName(FSNAME(fe));
350 mp->m_mountPoint = TQFile::decodeName(MOUNTPOINT(fe));
351 mp->m_mountType = TQFile::decodeName(MOUNTTYPE(fe));
355 if (infoNeeded & NeedMountOptions || (mp->m_mountType ==
"supermount"))
357 TQString options = TQFile::decodeName(MOUNTOPTIONS(fe));
358 mp->m_mountOptions = TQStringList::split(
',', options);
361 if (mp->m_mountType ==
"supermount")
364 if (infoNeeded & NeedRealDeviceName)
366 if (mp->m_mountedFrom.startsWith(
"/"))
380 for ( TQStringList::ConstIterator it = options.begin(); it != options.end(); ++it)
382 if( (*it).startsWith(
"dev="))
383 return TQString(*it).remove(
"dev=");
385 return TQString(
"none");
The KMountPoint class provides information about mounted and unmounted disks.
static TQString devNameFromOptions(const TQStringList &options)
When using supermount, the device name is in the options field as dev=/my/device.
static KMountPoint::List currentMountPoints(int infoNeeded=0)
This function gives a list of all currently used mountpoints.
~KMountPoint()
Destructor.
static KMountPoint::List possibleMountPoints(int infoNeeded=0)
This function gives a list of all possible mountpoints.
static TQString realPath(const TQString &dirname)
Expands all symbolic links and resolves references to '/.