• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • tdecore
 

tdecore

  • tdecore
kmountpoint.cpp
1 /*
2  *
3  * This file is part of the KDE libraries
4  * Copyright (c) 2003 Waldo Bastian <bastian@kde.org>
5  *
6  * $Id$
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License version 2 as published by the Free Software Foundation.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public License
18  * along with this library; see the file COPYING.LIB. If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21  **/
22 
23 #include <config.h>
24 #include <stdlib.h>
25 
26 #include <tqfile.h>
27 
28 #include "kstandarddirs.h"
29 
30 #include "kmountpoint.h"
31 
32 #ifdef HAVE_VOLMGT
33 #include <volmgt.h>
34 #endif
35 #ifdef HAVE_SYS_MNTTAB_H
36 #include <sys/mnttab.h>
37 #endif
38 #ifdef HAVE_MNTENT_H
39 #include <mntent.h>
40 #elif defined(HAVE_SYS_MNTENT_H)
41 #include <sys/mntent.h>
42 #endif
43 
44 // This is the *BSD branch
45 #ifdef HAVE_SYS_MOUNT_H
46 #ifdef HAVE_SYS_TYPES_H
47 #include <sys/types.h>
48 #endif
49 #ifdef HAVE_SYS_PARAM_H
50 #include <sys/param.h>
51 #endif
52 #include <sys/mount.h>
53 #endif
54 
55 #ifdef HAVE_FSTAB_H
56 #include <fstab.h>
57 #endif
58 #if defined(_AIX)
59 #include <sys/mntctl.h>
60 #include <sys/vmount.h>
61 #include <sys/vfs.h>
62 /* AIX does not prototype mntctl anywhere that I can find */
63 #ifndef mntctl
64 extern "C" {
65 int mntctl(int command, int size, void* buffer);
66 }
67 #endif
68 extern "C" struct vfs_ent *getvfsbytype(int vfsType);
69 extern "C" void endvfsent( );
70 #endif
71 
72 
73 #ifndef HAVE_GETMNTINFO
74 # ifdef _PATH_MOUNTED
75 // On some Linux, MNTTAB points to /etc/fstab !
76 # undef MNTTAB
77 # define MNTTAB _PATH_MOUNTED
78 # else
79 # ifndef MNTTAB
80 # ifdef MTAB_FILE
81 # define MNTTAB MTAB_FILE
82 # else
83 # define MNTTAB "/etc/mnttab"
84 # endif
85 # endif
86 # endif
87 #endif
88 
89 
90 
91 #ifdef _OS_SOLARIS_
92 #define FSTAB "/etc/vfstab"
93 #else
94 #define FSTAB "/etc/fstab"
95 #endif
96 
97 
98 
99 KMountPoint::KMountPoint()
100 {
101 }
102 
103 KMountPoint::~KMountPoint()
104 {
105 }
106 
107 #ifdef HAVE_SETMNTENT
108 #define SETMNTENT setmntent
109 #define ENDMNTENT endmntent
110 #define STRUCT_MNTENT struct mntent *
111 #define STRUCT_SETMNTENT FILE *
112 #define GETMNTENT(file, var) ((var = getmntent(file)) != 0)
113 #define MOUNTPOINT(var) var->mnt_dir
114 #define MOUNTTYPE(var) var->mnt_type
115 #define MOUNTOPTIONS(var) var->mnt_opts
116 #define FSNAME(var) var->mnt_fsname
117 #else
118 #define SETMNTENT fopen
119 #define ENDMNTENT fclose
120 #define STRUCT_MNTENT struct mnttab
121 #define STRUCT_SETMNTENT FILE *
122 #define GETMNTENT(file, var) (getmntent(file, &var) == 0)
123 #define MOUNTPOINT(var) var.mnt_mountp
124 #define MOUNTTYPE(var) var.mnt_fstype
125 #define MOUNTOPTIONS(var) var.mnt_mntopts
126 #define FSNAME(var) var.mnt_special
127 #endif
128 
129 KMountPoint::List KMountPoint::possibleMountPoints(int infoNeeded)
130 {
131  KMountPoint::List result;
132 
133 #ifdef HAVE_SETMNTENT
134  STRUCT_SETMNTENT fstab;
135  if ((fstab = SETMNTENT(FSTAB, "r")) == 0)
136  return result;
137 
138  STRUCT_MNTENT fe;
139  while (GETMNTENT(fstab, fe))
140  {
141  KMountPoint *mp = new KMountPoint();
142  mp->m_mountedFrom = TQFile::decodeName(FSNAME(fe));
143 
144  mp->m_mountPoint = TQFile::decodeName(MOUNTPOINT(fe));
145  mp->m_mountType = TQFile::decodeName(MOUNTTYPE(fe));
146 
147  //Devices using supermount have their device names in the mount options
148  //instead of the device field. That's why we need to read the mount options
149  if (infoNeeded & NeedMountOptions || (mp->m_mountType == "supermount"))
150  {
151  TQString options = TQFile::decodeName(MOUNTOPTIONS(fe));
152  mp->m_mountOptions = TQStringList::split(',', options);
153  }
154 
155  if(mp->m_mountType == "supermount")
156  mp->m_mountedFrom = devNameFromOptions(mp->m_mountOptions);
157 
158  if (infoNeeded & NeedRealDeviceName)
159  {
160  if (mp->m_mountedFrom.startsWith("/"))
161  mp->m_device = TDEStandardDirs::realPath(mp->m_mountedFrom);
162  }
163  // TODO: Strip trailing '/' ?
164  result.append(mp);
165  }
166  ENDMNTENT(fstab);
167 #else
168  TQFile f(FSTAB);
169  if ( !f.open(IO_ReadOnly) )
170  return result;
171 
172  TQTextStream t (&f);
173  TQString s;
174 
175  while (! t.eof())
176  {
177  s=t.readLine().simplifyWhiteSpace();
178  if ( s.isEmpty() || (s[0] == '#'))
179  continue;
180 
181  // not empty or commented out by '#'
182  TQStringList item = TQStringList::split(' ', s);
183 
184 #ifdef _OS_SOLARIS_
185  if (item.count() < 5)
186  continue;
187 #else
188  if (item.count() < 4)
189  continue;
190 #endif
191 
192  KMountPoint *mp = new KMountPoint();
193 
194  int i = 0;
195  mp->m_mountedFrom = item[i++];
196 #ifdef _OS_SOLARIS_
197  //device to fsck
198  i++;
199 #endif
200  mp->m_mountPoint = item[i++];
201  mp->m_mountType = item[i++];
202  TQString options = item[i++];
203 
204  if (infoNeeded & NeedMountOptions)
205  {
206  mp->m_mountOptions = TQStringList::split(',', options);
207  }
208 
209  if (infoNeeded & NeedRealDeviceName)
210  {
211  if (mp->m_mountedFrom.startsWith("/"))
212  mp->m_device = TDEStandardDirs::realPath(mp->m_mountedFrom);
213  }
214  // TODO: Strip trailing '/' ?
215  result.append(mp);
216  } //while
217 
218  f.close();
219 #endif
220  return result;
221 }
222 
223 KMountPoint::List KMountPoint::currentMountPoints(int infoNeeded)
224 {
225  KMountPoint::List result;
226 
227 #ifdef HAVE_GETMNTINFO
228 
229 #ifdef GETMNTINFO_USES_STATVFS
230  struct statvfs *mounted;
231 #else
232  struct statfs *mounted;
233 #endif
234 
235  int num_fs = getmntinfo(&mounted, MNT_NOWAIT);
236 
237  for (int i=0;i<num_fs;i++)
238  {
239  KMountPoint *mp = new KMountPoint();
240  mp->m_mountedFrom = TQFile::decodeName(mounted[i].f_mntfromname);
241  mp->m_mountPoint = TQFile::decodeName(mounted[i].f_mntonname);
242 
243 #ifdef __osf__
244  mp->m_mountType = TQFile::decodeName(mnt_names[mounted[i].f_type]);
245 #else
246  mp->m_mountType = TQFile::decodeName(mounted[i].f_fstypename);
247 #endif
248 
249  if (infoNeeded & NeedMountOptions)
250  {
251  struct fstab *ft = getfsfile(mounted[i].f_mntonname);
252  TQString options = TQFile::decodeName(ft->fs_mntops);
253  mp->m_mountOptions = TQStringList::split(',', options);
254  }
255 
256  if (infoNeeded & NeedRealDeviceName)
257  {
258  if (mp->m_mountedFrom.startsWith("/"))
259  mp->m_device = TDEStandardDirs::realPath(mp->m_mountedFrom);
260  }
261  // TODO: Strip trailing '/' ?
262  result.append(mp);
263  }
264 
265 #elif defined(_AIX)
266 
267  struct vmount *mntctl_buffer;
268  struct vmount *vm;
269  char *mountedfrom;
270  char *mountedto;
271  int fsname_len, num;
272  int buf_sz = 4096;
273 
274  mntctl_buffer = (struct vmount*)malloc(buf_sz);
275  num = mntctl(MCTL_QUERY, buf_sz, mntctl_buffer);
276  if (num == 0)
277  {
278  buf_sz = *(int*)mntctl_buffer;
279  free(mntctl_buffer);
280  mntctl_buffer = (struct vmount*)malloc(buf_sz);
281  num = mntctl(MCTL_QUERY, buf_sz, mntctl_buffer);
282  }
283 
284  if (num > 0)
285  {
286  /* iterate through items in the vmount structure: */
287  vm = (struct vmount *)mntctl_buffer;
288  for ( ; num > 0; num-- )
289  {
290  /* get the name of the mounted file systems: */
291  fsname_len = vmt2datasize(vm, VMT_STUB);
292  mountedto = (char*)malloc(fsname_len + 1);
293  mountedto[fsname_len] = '\0';
294  strncpy(mountedto, (char *)vmt2dataptr(vm, VMT_STUB), fsname_len);
295 
296  fsname_len = vmt2datasize(vm, VMT_OBJECT);
297  mountedfrom = (char*)malloc(fsname_len + 1);
298  mountedfrom[fsname_len] = '\0';
299  strncpy(mountedfrom, (char *)vmt2dataptr(vm, VMT_OBJECT), fsname_len);
300 
301  /* Look up the string for the file system type,
302  * as listed in /etc/vfs.
303  * ex.: nfs,jfs,afs,cdrfs,sfs,cachefs,nfs3,autofs
304  */
305  struct vfs_ent* ent = getvfsbytype(vm->vmt_gfstype);
306 
307  KMountPoint *mp = new KMountPoint();
308  mp->m_mountedFrom = TQFile::decodeName(mountedfrom);
309  mp->m_mountPoint = TQFile::decodeName(mountedto);
310  mp->m_mountType = TQFile::decodeName(ent->vfsent_name);
311 
312  free(mountedfrom);
313  free(mountedto);
314 
315  if (infoNeeded & NeedMountOptions)
316  {
317  // TODO
318  }
319 
320  if (infoNeeded & NeedRealDeviceName)
321  {
322  if (mp->m_mountedFrom.startsWith("/"))
323  mp->m_device = TDEStandardDirs::realPath(mp->m_mountedFrom);
324  }
325 
326  result.append(mp);
327 
328  /* goto the next vmount structure: */
329  vm = (struct vmount *)((char *)vm + vm->vmt_length);
330  }
331 
332  endvfsent( );
333  }
334 
335  free( mntctl_buffer );
336 #elif defined(Q_WS_WIN)
337  //TODO?
338 #else
339  STRUCT_SETMNTENT mnttab;
340  if ((mnttab = SETMNTENT(MNTTAB, "r")) == 0)
341  return result;
342 
343  STRUCT_MNTENT fe;
344  while (GETMNTENT(mnttab, fe))
345  {
346  KMountPoint *mp = new KMountPoint();
347  mp->m_mountedFrom = TQFile::decodeName(FSNAME(fe));
348 
349  mp->m_mountPoint = TQFile::decodeName(MOUNTPOINT(fe));
350  mp->m_mountType = TQFile::decodeName(MOUNTTYPE(fe));
351 
352  //Devices using supermount have their device names in the mount options
353  //instead of the device field. That's why we need to read the mount options
354  if (infoNeeded & NeedMountOptions || (mp->m_mountType == "supermount"))
355  {
356  TQString options = TQFile::decodeName(MOUNTOPTIONS(fe));
357  mp->m_mountOptions = TQStringList::split(',', options);
358  }
359 
360  if (mp->m_mountType == "supermount")
361  mp->m_mountedFrom = devNameFromOptions(mp->m_mountOptions);
362 
363  if (infoNeeded & NeedRealDeviceName)
364  {
365  if (mp->m_mountedFrom.startsWith("/"))
366  mp->m_device = TDEStandardDirs::realPath(mp->m_mountedFrom);
367  }
368  // TODO: Strip trailing '/' ?
369  result.append(mp);
370  }
371  ENDMNTENT(mnttab);
372 #endif
373  return result;
374 }
375 
376 TQString KMountPoint::devNameFromOptions(const TQStringList &options)
377 {
378  // Search options to find the device name
379  for ( TQStringList::ConstIterator it = options.begin(); it != options.end(); ++it)
380  {
381  if( (*it).startsWith("dev="))
382  return TQString(*it).remove("dev=");
383  }
384  return TQString("none");
385 }
KMountPoint
The KMountPoint class provides information about mounted and unmounted disks.
Definition: kmountpoint.h:35
KMountPoint::~KMountPoint
~KMountPoint()
Destructor.
Definition: kmountpoint.cpp:103
KMountPoint::currentMountPoints
static KMountPoint::List currentMountPoints(int infoNeeded=0)
This function gives a list of all currently used mountpoints.
Definition: kmountpoint.cpp:223
KMountPoint::possibleMountPoints
static KMountPoint::List possibleMountPoints(int infoNeeded=0)
This function gives a list of all possible mountpoints.
Definition: kmountpoint.cpp:129
TDEStandardDirs::realPath
static TQString realPath(const TQString &dirname)
Expands all symbolic links and resolves references to '/.
Definition: kstandarddirs.cpp:689
KMountPoint::devNameFromOptions
static TQString devNameFromOptions(const TQStringList &options)
When using supermount, the device name is in the options field as dev=/my/device. ...
Definition: kmountpoint.cpp:376

tdecore

Skip menu "tdecore"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

tdecore

Skip menu "tdecore"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  •     tdecore
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  • tdeioslave
  •   http
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdecore by doxygen 1.8.8
This website is maintained by Timothy Pearson.