• 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 <stdio.h>
37 #include <sys/mnttab.h>
38 #endif
39 #ifdef HAVE_MNTENT_H
40 #include <mntent.h>
41 #elif defined(HAVE_SYS_MNTENT_H)
42 #include <sys/mntent.h>
43 #endif
44 
45 // This is the *BSD branch
46 #ifdef HAVE_SYS_MOUNT_H
47 #ifdef HAVE_SYS_TYPES_H
48 #include <sys/types.h>
49 #endif
50 #ifdef HAVE_SYS_PARAM_H
51 #include <sys/param.h>
52 #endif
53 #include <sys/mount.h>
54 #endif
55 
56 #ifdef HAVE_FSTAB_H
57 #include <fstab.h>
58 #endif
59 #if defined(_AIX)
60 #include <sys/mntctl.h>
61 #include <sys/vmount.h>
62 #include <sys/vfs.h>
63 /* AIX does not prototype mntctl anywhere that I can find */
64 #ifndef mntctl
65 extern "C" {
66 int mntctl(int command, int size, void* buffer);
67 }
68 #endif
69 extern "C" struct vfs_ent *getvfsbytype(int vfsType);
70 extern "C" void endvfsent( );
71 #endif
72 
73 
74 #ifndef HAVE_GETMNTINFO
75 # ifdef _PATH_MOUNTED
76 // On some Linux, MNTTAB points to /etc/fstab !
77 # undef MNTTAB
78 # define MNTTAB _PATH_MOUNTED
79 # else
80 # ifndef MNTTAB
81 # ifdef MTAB_FILE
82 # define MNTTAB MTAB_FILE
83 # else
84 # define MNTTAB "/etc/mnttab"
85 # endif
86 # endif
87 # endif
88 #endif
89 
90 
91 
92 #ifdef _OS_SOLARIS_
93 #define FSTAB "/etc/vfstab"
94 #else
95 #define FSTAB "/etc/fstab"
96 #endif
97 
98 
99 
100 KMountPoint::KMountPoint()
101 {
102 }
103 
104 KMountPoint::~KMountPoint()
105 {
106 }
107 
108 #ifdef HAVE_SETMNTENT
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
118 #else
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
128 #endif
129 
130 KMountPoint::List KMountPoint::possibleMountPoints(int infoNeeded)
131 {
132  KMountPoint::List result;
133 
134 #ifdef HAVE_SETMNTENT
135  STRUCT_SETMNTENT fstab;
136  if ((fstab = SETMNTENT(FSTAB, "r")) == 0)
137  return result;
138 
139  STRUCT_MNTENT fe;
140  while (GETMNTENT(fstab, fe))
141  {
142  KMountPoint *mp = new KMountPoint();
143  mp->m_mountedFrom = TQFile::decodeName(FSNAME(fe));
144 
145  mp->m_mountPoint = TQFile::decodeName(MOUNTPOINT(fe));
146  mp->m_mountType = TQFile::decodeName(MOUNTTYPE(fe));
147 
148  //Devices using supermount have their device names in the mount options
149  //instead of the device field. That's why we need to read the mount options
150  if (infoNeeded & NeedMountOptions || (mp->m_mountType == "supermount"))
151  {
152  TQString options = TQFile::decodeName(MOUNTOPTIONS(fe));
153  mp->m_mountOptions = TQStringList::split(',', options);
154  }
155 
156  if(mp->m_mountType == "supermount")
157  mp->m_mountedFrom = devNameFromOptions(mp->m_mountOptions);
158 
159  if (infoNeeded & NeedRealDeviceName)
160  {
161  if (mp->m_mountedFrom.startsWith("/"))
162  mp->m_device = TDEStandardDirs::realPath(mp->m_mountedFrom);
163  }
164  // TODO: Strip trailing '/' ?
165  result.append(mp);
166  }
167  ENDMNTENT(fstab);
168 #else
169  TQFile f(FSTAB);
170  if ( !f.open(IO_ReadOnly) )
171  return result;
172 
173  TQTextStream t (&f);
174  TQString s;
175 
176  while (! t.eof())
177  {
178  s=t.readLine().simplifyWhiteSpace();
179  if ( s.isEmpty() || (s[0] == '#'))
180  continue;
181 
182  // not empty or commented out by '#'
183  TQStringList item = TQStringList::split(' ', s);
184 
185 #ifdef _OS_SOLARIS_
186  if (item.count() < 5)
187  continue;
188 #else
189  if (item.count() < 4)
190  continue;
191 #endif
192 
193  KMountPoint *mp = new KMountPoint();
194 
195  int i = 0;
196  mp->m_mountedFrom = item[i++];
197 #ifdef _OS_SOLARIS_
198  //device to fsck
199  i++;
200 #endif
201  mp->m_mountPoint = item[i++];
202  mp->m_mountType = item[i++];
203  TQString options = item[i++];
204 
205  if (infoNeeded & NeedMountOptions)
206  {
207  mp->m_mountOptions = TQStringList::split(',', options);
208  }
209 
210  if (infoNeeded & NeedRealDeviceName)
211  {
212  if (mp->m_mountedFrom.startsWith("/"))
213  mp->m_device = TDEStandardDirs::realPath(mp->m_mountedFrom);
214  }
215  // TODO: Strip trailing '/' ?
216  result.append(mp);
217  } //while
218 
219  f.close();
220 #endif
221  return result;
222 }
223 
224 KMountPoint::List KMountPoint::currentMountPoints(int infoNeeded)
225 {
226  KMountPoint::List result;
227 
228 #ifdef HAVE_GETMNTINFO
229 
230 #ifdef GETMNTINFO_USES_STATVFS
231  struct statvfs *mounted;
232 #else
233  struct statfs *mounted;
234 #endif
235 
236  int num_fs = getmntinfo(&mounted, MNT_NOWAIT);
237 
238  for (int i=0;i<num_fs;i++)
239  {
240  KMountPoint *mp = new KMountPoint();
241  mp->m_mountedFrom = TQFile::decodeName(mounted[i].f_mntfromname);
242  mp->m_mountPoint = TQFile::decodeName(mounted[i].f_mntonname);
243  mp->m_mountType = TQFile::decodeName(mounted[i].f_fstypename);
244 
245  if (infoNeeded & NeedMountOptions)
246  {
247  struct fstab *ft = getfsfile(mounted[i].f_mntonname);
248  TQString options = TQFile::decodeName(ft->fs_mntops);
249  mp->m_mountOptions = TQStringList::split(',', options);
250  }
251 
252  if (infoNeeded & NeedRealDeviceName)
253  {
254  if (mp->m_mountedFrom.startsWith("/"))
255  mp->m_device = TDEStandardDirs::realPath(mp->m_mountedFrom);
256  }
257  // TODO: Strip trailing '/' ?
258  result.append(mp);
259  }
260 
261 #elif defined(_AIX)
262 
263  struct vmount *mntctl_buffer;
264  struct vmount *vm;
265  char *mountedfrom;
266  char *mountedto;
267  int fsname_len, num;
268  int buf_sz = 4096;
269 
270  mntctl_buffer = (struct vmount*)malloc(buf_sz);
271  num = mntctl(MCTL_QUERY, buf_sz, mntctl_buffer);
272  if (num == 0)
273  {
274  buf_sz = *(int*)mntctl_buffer;
275  free(mntctl_buffer);
276  mntctl_buffer = (struct vmount*)malloc(buf_sz);
277  num = mntctl(MCTL_QUERY, buf_sz, mntctl_buffer);
278  }
279 
280  if (num > 0)
281  {
282  /* iterate through items in the vmount structure: */
283  vm = (struct vmount *)mntctl_buffer;
284  for ( ; num > 0; num-- )
285  {
286  /* get the name of the mounted file systems: */
287  fsname_len = vmt2datasize(vm, VMT_STUB);
288  mountedto = (char*)malloc(fsname_len + 1);
289  mountedto[fsname_len] = '\0';
290  strncpy(mountedto, (char *)vmt2dataptr(vm, VMT_STUB), fsname_len);
291 
292  fsname_len = vmt2datasize(vm, VMT_OBJECT);
293  mountedfrom = (char*)malloc(fsname_len + 1);
294  mountedfrom[fsname_len] = '\0';
295  strncpy(mountedfrom, (char *)vmt2dataptr(vm, VMT_OBJECT), fsname_len);
296 
297  /* Look up the string for the file system type,
298  * as listed in /etc/vfs.
299  * ex.: nfs,jfs,afs,cdrfs,sfs,cachefs,nfs3,autofs
300  */
301  struct vfs_ent* ent = getvfsbytype(vm->vmt_gfstype);
302 
303  KMountPoint *mp = new KMountPoint();
304  mp->m_mountedFrom = TQFile::decodeName(mountedfrom);
305  mp->m_mountPoint = TQFile::decodeName(mountedto);
306  mp->m_mountType = TQFile::decodeName(ent->vfsent_name);
307 
308  free(mountedfrom);
309  free(mountedto);
310 
311  if (infoNeeded & NeedMountOptions)
312  {
313  // TODO
314  }
315 
316  if (infoNeeded & NeedRealDeviceName)
317  {
318  if (mp->m_mountedFrom.startsWith("/"))
319  mp->m_device = TDEStandardDirs::realPath(mp->m_mountedFrom);
320  }
321 
322  result.append(mp);
323 
324  /* goto the next vmount structure: */
325  vm = (struct vmount *)((char *)vm + vm->vmt_length);
326  }
327 
328  endvfsent( );
329  }
330 
331  free( mntctl_buffer );
332 #elif defined(TQ_WS_WIN)
333  //TODO?
334 #else
335  STRUCT_SETMNTENT mnttab;
336  if ((mnttab = SETMNTENT(MNTTAB, "r")) == 0)
337  return result;
338 
339  STRUCT_MNTENT fe;
340  while (GETMNTENT(mnttab, fe))
341  {
342  KMountPoint *mp = new KMountPoint();
343  mp->m_mountedFrom = TQFile::decodeName(FSNAME(fe));
344 
345  mp->m_mountPoint = TQFile::decodeName(MOUNTPOINT(fe));
346  mp->m_mountType = TQFile::decodeName(MOUNTTYPE(fe));
347 
348  //Devices using supermount have their device names in the mount options
349  //instead of the device field. That's why we need to read the mount options
350  if (infoNeeded & NeedMountOptions || (mp->m_mountType == "supermount"))
351  {
352  TQString options = TQFile::decodeName(MOUNTOPTIONS(fe));
353  mp->m_mountOptions = TQStringList::split(',', options);
354  }
355 
356  if (mp->m_mountType == "supermount")
357  mp->m_mountedFrom = devNameFromOptions(mp->m_mountOptions);
358 
359  if (infoNeeded & NeedRealDeviceName)
360  {
361  if (mp->m_mountedFrom.startsWith("/"))
362  mp->m_device = TDEStandardDirs::realPath(mp->m_mountedFrom);
363  }
364  // TODO: Strip trailing '/' ?
365  result.append(mp);
366  }
367  ENDMNTENT(mnttab);
368 #endif
369  return result;
370 }
371 
372 TQString KMountPoint::devNameFromOptions(const TQStringList &options)
373 {
374  // Search options to find the device name
375  for ( TQStringList::ConstIterator it = options.begin(); it != options.end(); ++it)
376  {
377  if( (*it).startsWith("dev="))
378  return TQString(*it).remove("dev=");
379  }
380  return TQString("none");
381 }
KMountPoint
The KMountPoint class provides information about mounted and unmounted disks.
Definition: kmountpoint.h:36
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:372
KMountPoint::currentMountPoints
static KMountPoint::List currentMountPoints(int infoNeeded=0)
This function gives a list of all currently used mountpoints.
Definition: kmountpoint.cpp:224
KMountPoint::~KMountPoint
~KMountPoint()
Destructor.
Definition: kmountpoint.cpp:104
KMountPoint::possibleMountPoints
static KMountPoint::List possibleMountPoints(int infoNeeded=0)
This function gives a list of all possible mountpoints.
Definition: kmountpoint.cpp:130
TDEStandardDirs::realPath
static TQString realPath(const TQString &dirname)
Expands all symbolic links and resolves references to '/.
Definition: kstandarddirs.cpp:689

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
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdecore by doxygen 1.9.1
This website is maintained by Timothy Pearson.