• 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
65extern "C" {
66int mntctl(int command, int size, void* buffer);
67}
68#endif
69extern "C" struct vfs_ent *getvfsbytype(int vfsType);
70extern "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
100KMountPoint::KMountPoint()
101{
102}
103
104KMountPoint::~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
130KMountPoint::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
224KMountPoint::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
244#ifdef __osf__
245 mp->m_mountType = TQFile::decodeName(mnt_names[mounted[i].f_type]);
246#else
247 mp->m_mountType = TQFile::decodeName(mounted[i].f_fstypename);
248#endif
249
250 if (infoNeeded & NeedMountOptions)
251 {
252 struct fstab *ft = getfsfile(mounted[i].f_mntonname);
253 TQString options = TQFile::decodeName(ft->fs_mntops);
254 mp->m_mountOptions = TQStringList::split(',', options);
255 }
256
257 if (infoNeeded & NeedRealDeviceName)
258 {
259 if (mp->m_mountedFrom.startsWith("/"))
260 mp->m_device = TDEStandardDirs::realPath(mp->m_mountedFrom);
261 }
262 // TODO: Strip trailing '/' ?
263 result.append(mp);
264 }
265
266#elif defined(_AIX)
267
268 struct vmount *mntctl_buffer;
269 struct vmount *vm;
270 char *mountedfrom;
271 char *mountedto;
272 int fsname_len, num;
273 int buf_sz = 4096;
274
275 mntctl_buffer = (struct vmount*)malloc(buf_sz);
276 num = mntctl(MCTL_QUERY, buf_sz, mntctl_buffer);
277 if (num == 0)
278 {
279 buf_sz = *(int*)mntctl_buffer;
280 free(mntctl_buffer);
281 mntctl_buffer = (struct vmount*)malloc(buf_sz);
282 num = mntctl(MCTL_QUERY, buf_sz, mntctl_buffer);
283 }
284
285 if (num > 0)
286 {
287 /* iterate through items in the vmount structure: */
288 vm = (struct vmount *)mntctl_buffer;
289 for ( ; num > 0; num-- )
290 {
291 /* get the name of the mounted file systems: */
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);
296
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);
301
302 /* Look up the string for the file system type,
303 * as listed in /etc/vfs.
304 * ex.: nfs,jfs,afs,cdrfs,sfs,cachefs,nfs3,autofs
305 */
306 struct vfs_ent* ent = getvfsbytype(vm->vmt_gfstype);
307
308 KMountPoint *mp = new KMountPoint();
309 mp->m_mountedFrom = TQFile::decodeName(mountedfrom);
310 mp->m_mountPoint = TQFile::decodeName(mountedto);
311 mp->m_mountType = TQFile::decodeName(ent->vfsent_name);
312
313 free(mountedfrom);
314 free(mountedto);
315
316 if (infoNeeded & NeedMountOptions)
317 {
318 // TODO
319 }
320
321 if (infoNeeded & NeedRealDeviceName)
322 {
323 if (mp->m_mountedFrom.startsWith("/"))
324 mp->m_device = TDEStandardDirs::realPath(mp->m_mountedFrom);
325 }
326
327 result.append(mp);
328
329 /* goto the next vmount structure: */
330 vm = (struct vmount *)((char *)vm + vm->vmt_length);
331 }
332
333 endvfsent( );
334 }
335
336 free( mntctl_buffer );
337#elif defined(TQ_WS_WIN)
338 //TODO?
339#else
340 STRUCT_SETMNTENT mnttab;
341 if ((mnttab = SETMNTENT(MNTTAB, "r")) == 0)
342 return result;
343
344 STRUCT_MNTENT fe;
345 while (GETMNTENT(mnttab, fe))
346 {
347 KMountPoint *mp = new KMountPoint();
348 mp->m_mountedFrom = TQFile::decodeName(FSNAME(fe));
349
350 mp->m_mountPoint = TQFile::decodeName(MOUNTPOINT(fe));
351 mp->m_mountType = TQFile::decodeName(MOUNTTYPE(fe));
352
353 //Devices using supermount have their device names in the mount options
354 //instead of the device field. That's why we need to read the mount options
355 if (infoNeeded & NeedMountOptions || (mp->m_mountType == "supermount"))
356 {
357 TQString options = TQFile::decodeName(MOUNTOPTIONS(fe));
358 mp->m_mountOptions = TQStringList::split(',', options);
359 }
360
361 if (mp->m_mountType == "supermount")
362 mp->m_mountedFrom = devNameFromOptions(mp->m_mountOptions);
363
364 if (infoNeeded & NeedRealDeviceName)
365 {
366 if (mp->m_mountedFrom.startsWith("/"))
367 mp->m_device = TDEStandardDirs::realPath(mp->m_mountedFrom);
368 }
369 // TODO: Strip trailing '/' ?
370 result.append(mp);
371 }
372 ENDMNTENT(mnttab);
373#endif
374 return result;
375}
376
377TQString KMountPoint::devNameFromOptions(const TQStringList &options)
378{
379 // Search options to find the device name
380 for ( TQStringList::ConstIterator it = options.begin(); it != options.end(); ++it)
381 {
382 if( (*it).startsWith("dev="))
383 return TQString(*it).remove("dev=");
384 }
385 return TQString("none");
386}
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:377
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
  • 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.8
This website is maintained by Timothy Pearson.