kdecore Library API Documentation

kmountpoint.cpp

00001 /* 00002 * 00003 * This file is part of the KDE libraries 00004 * Copyright (c) 2003 Waldo Bastian <bastian@kde.org> 00005 * 00006 * $Id: kmountpoint.cpp,v 1.8 2003/09/13 11:18:31 coolo Exp $ 00007 * 00008 * This library is free software; you can redistribute it and/or 00009 * modify it under the terms of the GNU Library General Public 00010 * License version 2 as published by the Free Software Foundation. 00011 * 00012 * This library is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 * Library General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU Library General Public License 00018 * along with this library; see the file COPYING.LIB. If not, write to 00019 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 00020 * Boston, MA 02111-1307, USA. 00021 **/ 00022 00023 #include <config.h> 00024 #include <stdlib.h> 00025 00026 #include <qfile.h> 00027 00028 #include "kstandarddirs.h" 00029 00030 #include "kmountpoint.h" 00031 00032 #ifdef HAVE_VOLMGT 00033 #include <volmgt.h> 00034 #endif 00035 #ifdef HAVE_SYS_MNTTAB_H 00036 #include <sys/mnttab.h> 00037 #endif 00038 #ifdef HAVE_MNTENT_H 00039 #include <mntent.h> 00040 #elif HAVE_SYS_MNTENT_H 00041 #include <sys/mntent.h> 00042 #endif 00043 00044 // This is the *BSD branch 00045 #ifdef HAVE_SYS_MOUNT_H 00046 #ifdef HAVE_SYS_TYPES_H 00047 #include <sys/types.h> 00048 #endif 00049 #ifdef HAVE_SYS_PARAM_H 00050 #include <sys/param.h> 00051 #endif 00052 #include <sys/mount.h> 00053 #endif 00054 00055 #ifdef HAVE_FSTAB_H 00056 #include <fstab.h> 00057 #endif 00058 #if defined(_AIX) 00059 #include <sys/mntctl.h> 00060 #include <sys/vmount.h> 00061 #include <sys/vfs.h> 00062 /* AIX does not prototype mntctl anywhere that I can find */ 00063 #ifndef mntctl 00064 extern "C" { 00065 int mntctl(int command, int size, void* buffer); 00066 } 00067 #endif 00068 extern "C" struct vfs_ent *getvfsbytype(int vfsType); 00069 extern "C" void endvfsent( ); 00070 #endif 00071 00072 00073 #ifndef HAVE_GETMNTINFO 00074 # ifdef _PATH_MOUNTED 00075 // On some Linux, MNTTAB points to /etc/fstab ! 00076 # undef MNTTAB 00077 # define MNTTAB _PATH_MOUNTED 00078 # else 00079 # ifndef MNTTAB 00080 # ifdef MTAB_FILE 00081 # define MNTTAB MTAB_FILE 00082 # else 00083 # define MNTTAB "/etc/mnttab" 00084 # endif 00085 # endif 00086 # endif 00087 #endif 00088 00089 00090 00091 #ifdef _OS_SOLARIS_ 00092 #define FSTAB "/etc/vfstab" 00093 #else 00094 #define FSTAB "/etc/fstab" 00095 #endif 00096 00097 00098 00099 KMountPoint::KMountPoint() 00100 { 00101 } 00102 00103 KMountPoint::~KMountPoint() 00104 { 00105 } 00106 00107 #ifdef HAVE_SETMNTENT 00108 #define SETMNTENT setmntent 00109 #define ENDMNTENT endmntent 00110 #define STRUCT_MNTENT struct mntent * 00111 #define STRUCT_SETMNTENT FILE * 00112 #define GETMNTENT(file, var) ((var = getmntent(file)) != 0) 00113 #define MOUNTPOINT(var) var->mnt_dir 00114 #define MOUNTTYPE(var) var->mnt_type 00115 #define MOUNTOPTIONS(var) var->mnt_opts 00116 #define FSNAME(var) var->mnt_fsname 00117 #else 00118 #define SETMNTENT fopen 00119 #define ENDMNTENT fclose 00120 #define STRUCT_MNTENT struct mnttab 00121 #define STRUCT_SETMNTENT FILE * 00122 #define GETMNTENT(file, var) (getmntent(file, &var) == 0) 00123 #define MOUNTPOINT(var) var.mnt_mountp 00124 #define MOUNTTYPE(var) var.mnt_fstype 00125 #define MOUNTOPTIONS(var) var.mnt_mntopts 00126 #define FSNAME(var) var.mnt_special 00127 #endif 00128 00129 KMountPoint::List KMountPoint::possibleMountPoints(int infoNeeded) 00130 { 00131 KMountPoint::List result; 00132 00133 #ifdef HAVE_SETMNTENT 00134 STRUCT_SETMNTENT fstab; 00135 if ((fstab = SETMNTENT(FSTAB, "r")) == 0) 00136 return result; 00137 00138 STRUCT_MNTENT fe; 00139 while (GETMNTENT(fstab, fe)) 00140 { 00141 KMountPoint *mp = new KMountPoint(); 00142 mp->m_mountedFrom = QFile::decodeName(FSNAME(fe)); 00143 00144 mp->m_mountPoint = QFile::decodeName(MOUNTPOINT(fe)); 00145 mp->m_mountType = QFile::decodeName(MOUNTTYPE(fe)); 00146 if (infoNeeded & NeedMountOptions) 00147 { 00148 QString options = QFile::decodeName(MOUNTOPTIONS(fe)); 00149 mp->m_mountOptions = QStringList::split(',', options); 00150 } 00151 00152 if (infoNeeded & NeedRealDeviceName) 00153 { 00154 if (mp->m_mountedFrom.startsWith("/")) 00155 mp->m_device = KStandardDirs::realPath(mp->m_mountedFrom); 00156 } 00157 // TODO: Strip trailing '/' ? 00158 result.append(mp); 00159 } 00160 ENDMNTENT(fstab); 00161 #else 00162 QFile f(FSTAB); 00163 if ( !f.open(IO_ReadOnly) ) 00164 return result; 00165 00166 QTextStream t (&f); 00167 QString s; 00168 00169 while (! t.eof()) 00170 { 00171 s=t.readLine().simplifyWhiteSpace(); 00172 if ( s.isEmpty() || (s[0] == '#')) 00173 continue; 00174 00175 // not empty or commented out by '#' 00176 QStringList item = QStringList::split(' ', s); 00177 00178 #ifdef _OS_SOLARIS_ 00179 if (item.count() < 5) 00180 continue; 00181 #else 00182 if (item.count() < 4) 00183 continue; 00184 #endif 00185 00186 KMountPoint *mp = new KMountPoint(); 00187 00188 int i = 0; 00189 mp->m_mountedFrom = item[i++]; 00190 #ifdef _OS_SOLARIS_ 00191 //device to fsck 00192 i++; 00193 #endif 00194 mp->m_mountPoint = item[i++]; 00195 mp->m_mountType = item[i++]; 00196 QString options = item[i++]; 00197 if (infoNeeded & NeedMountOptions) 00198 { 00199 mp->m_mountOptions = QStringList::split(',', options); 00200 } 00201 00202 if (infoNeeded & NeedRealDeviceName) 00203 { 00204 if (mp->m_mountedFrom.startsWith("/")) 00205 mp->m_device = KStandardDirs::realPath(mp->m_mountedFrom); 00206 } 00207 // TODO: Strip trailing '/' ? 00208 result.append(mp); 00209 } //while 00210 00211 f.close(); 00212 #endif 00213 return result; 00214 } 00215 00216 KMountPoint::List KMountPoint::currentMountPoints(int infoNeeded) 00217 { 00218 KMountPoint::List result; 00219 00220 #ifdef HAVE_GETMNTINFO 00221 00222 struct statfs *mounted; 00223 00224 int num_fs = getmntinfo(&mounted, MNT_NOWAIT); 00225 00226 for (int i=0;i<num_fs;i++) 00227 { 00228 KMountPoint *mp = new KMountPoint(); 00229 mp->m_mountedFrom = QFile::decodeName(mounted[i].f_mntfromname); 00230 mp->m_mountPoint = QFile::decodeName(mounted[i].f_mntonname); 00231 00232 #ifdef __osf__ 00233 mp->m_mountType = QFile::decodeName(mnt_names[mounted[i].f_type]); 00234 #else 00235 mp->m_mountType = QFile::decodeName(mounted[i].f_fstypename); 00236 #endif 00237 00238 if (infoNeeded & NeedMountOptions) 00239 { 00240 struct fstab *ft = getfsfile(mounted[i].f_mntonname); 00241 QString options = QFile::decodeName(ft->fs_mntops); 00242 mp->m_mountOptions = QStringList::split(',', options); 00243 } 00244 00245 if (infoNeeded & NeedRealDeviceName) 00246 { 00247 if (mp->m_mountedFrom.startsWith("/")) 00248 mp->m_device = KStandardDirs::realPath(mp->m_mountedFrom); 00249 } 00250 // TODO: Strip trailing '/' ? 00251 result.append(mp); 00252 } 00253 00254 #elif defined(_AIX) 00255 00256 struct vmount *mntctl_buffer; 00257 struct vmount *vm; 00258 char *mountedfrom; 00259 char *mountedto; 00260 int fsname_len, num; 00261 int buf_sz = 4096; 00262 00263 mntctl_buffer = (struct vmount*)malloc(buf_sz); 00264 num = mntctl(MCTL_QUERY, buf_sz, mntctl_buffer); 00265 if (num == 0) 00266 { 00267 buf_sz = *(int*)mntctl_buffer; 00268 free(mntctl_buffer); 00269 mntctl_buffer = (struct vmount*)malloc(buf_sz); 00270 num = mntctl(MCTL_QUERY, buf_sz, mntctl_buffer); 00271 } 00272 00273 if (num > 0) 00274 { 00275 /* iterate through items in the vmount structure: */ 00276 vm = (struct vmount *)mntctl_buffer; 00277 for ( ; num > 0; num-- ) 00278 { 00279 /* get the name of the mounted file systems: */ 00280 fsname_len = vmt2datasize(vm, VMT_STUB); 00281 mountedto = (char*)malloc(fsname_len + 1); 00282 mountedto[fsname_len] = '\0'; 00283 strncpy(mountedto, (char *)vmt2dataptr(vm, VMT_STUB), fsname_len); 00284 00285 fsname_len = vmt2datasize(vm, VMT_OBJECT); 00286 mountedfrom = (char*)malloc(fsname_len + 1); 00287 mountedfrom[fsname_len] = '\0'; 00288 strncpy(mountedfrom, (char *)vmt2dataptr(vm, VMT_OBJECT), fsname_len); 00289 00290 /* Look up the string for the file system type, 00291 * as listed in /etc/vfs. 00292 * ex.: nfs,jfs,afs,cdrfs,sfs,cachefs,nfs3,autofs 00293 */ 00294 struct vfs_ent* ent = getvfsbytype(vm->vmt_gfstype); 00295 00296 KMountPoint *mp = new KMountPoint(); 00297 mp->m_mountedFrom = QFile::decodeName(mountedfrom); 00298 mp->m_mountPoint = QFile::decodeName(mountedto); 00299 mp->m_mountType = QFile::decodeName(ent->vfsent_name); 00300 00301 free(mountedfrom); 00302 free(mountedto); 00303 00304 if (infoNeeded & NeedMountOptions) 00305 { 00306 // TODO 00307 } 00308 00309 if (infoNeeded & NeedRealDeviceName) 00310 { 00311 if (mp->m_mountedFrom.startsWith("/")) 00312 mp->m_device = KStandardDirs::realPath(mp->m_mountedFrom); 00313 } 00314 00315 result.append(mp); 00316 00317 /* goto the next vmount structure: */ 00318 vm = (struct vmount *)((char *)vm + vm->vmt_length); 00319 } 00320 00321 endvfsent( ); 00322 } 00323 00324 free( mntctl_buffer ); 00325 #else 00326 STRUCT_SETMNTENT mnttab; 00327 if ((mnttab = SETMNTENT(MNTTAB, "r")) == 0) 00328 return result; 00329 00330 STRUCT_MNTENT fe; 00331 while (GETMNTENT(mnttab, fe)) 00332 { 00333 KMountPoint *mp = new KMountPoint(); 00334 mp->m_mountedFrom = QFile::decodeName(FSNAME(fe)); 00335 00336 mp->m_mountPoint = QFile::decodeName(MOUNTPOINT(fe)); 00337 mp->m_mountType = QFile::decodeName(MOUNTTYPE(fe)); 00338 if (infoNeeded & NeedMountOptions) 00339 { 00340 QString options = QFile::decodeName(MOUNTOPTIONS(fe)); 00341 mp->m_mountOptions = QStringList::split(',', options); 00342 } 00343 00344 if (infoNeeded & NeedRealDeviceName) 00345 { 00346 if (mp->m_mountedFrom.startsWith("/")) 00347 mp->m_device = KStandardDirs::realPath(mp->m_mountedFrom); 00348 } 00349 // TODO: Strip trailing '/' ? 00350 result.append(mp); 00351 } 00352 ENDMNTENT(mnttab); 00353 #endif 00354 return result; 00355 } 00356 00357
KDE Logo
This file is part of the documentation for kdecore Library Version 3.2.3.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Mon Aug 30 22:53:31 2004 by doxygen 1.3.8 written by Dimitri van Heesch, © 1997-2003