Qtopia library API Documentation

storage.cpp

00001 /**********************************************************************
00002 ** Copyright (C) Holger 'zecke' Freyther <freyther@kde.org>
00003 ** Copyright (C) Lorn Potter <llornkcor@handhelds.org>
00004 ** Copyright (C) 2000 Trolltech AS.  All rights reserved.
00005 **
00006 ** This file is part of Opie Environment.
00007 **
00008 ** This file may be distributed and/or modified under the terms of the
00009 ** GNU General Public License version 2 as published by the Free Software
00010 ** Foundation and appearing in the file LICENSE.GPL included in the
00011 ** packaging of this file.
00012 **
00013 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00014 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00015 **
00016 ** See http://www.trolltech.com/gpl/ for GPL licensing information.
00017 **
00018 ** Contact info@trolltech.com if any conditions of this licensing are
00019 ** not clear to you.
00020 **
00021 **********************************************************************/
00022 
00023 #include <qpe/storage.h>
00024 #include <qpe/custom.h>
00025 
00026 #include <qfile.h>
00027 #include <qtimer.h>
00028 #include <qcopchannel_qws.h>
00029 
00030 #include <stdio.h>
00031 
00032 #if defined(_OS_LINUX_) || defined(Q_OS_LINUX)
00033 #include <sys/vfs.h>
00034 #include <mntent.h>
00035 #endif
00036 
00037 #ifdef Q_OS_MACX
00038 #  include <sys/param.h>
00039 #  include <sys/ucred.h>
00040 #  include <sys/mount.h>
00041 #  include <stdio.h>  // For strerror()
00042 #  include <errno.h>
00043 #endif /* Q_OS_MACX */
00044 
00045 #include <qstringlist.h>
00046 
00047 // Shouldn't be here ! (eilers)
00048 // #include <sys/vfs.h>
00049 // #include <mntent.h>
00050 
00051 
00052 static bool isCF(const QString& m)
00053 {
00054 
00055 #ifndef Q_OS_MACX
00056     FILE* f = fopen("/var/run/stab", "r");
00057     if (!f) f = fopen("/var/state/pcmcia/stab", "r");
00058     if (!f) f = fopen("/var/lib/pcmcia/stab", "r");
00059     if ( f )
00060     {
00061         char line[1024];
00062         char devtype[80];
00063         char devname[80];
00064         while ( fgets( line, 1024, f ) )
00065         {
00066             // 0       ide     ide-cs  0       hda     3       0
00067             if ( sscanf(line,"%*d %s %*s %*s %s", devtype, devname )==2 )
00068             {
00069                 if ( QString(devtype) == "ide" && m.find(devname)>0 )
00070                 {
00071                     fclose(f);
00072                     return TRUE;
00073                 }
00074             }
00075         }
00076         fclose(f);
00077     }
00078 #endif /* Q_OS_MACX */
00079     return FALSE;
00080 }
00081 
00097 StorageInfo::StorageInfo( QObject *parent )
00098         : QObject( parent )
00099 {
00100     mFileSystems.setAutoDelete( TRUE );
00101     channel = new QCopChannel( "QPE/Card", this );
00102     connect( channel, SIGNAL(received(const QCString &, const QByteArray &)),
00103              this, SLOT(cardMessage( const QCString &, const QByteArray &)) );
00104     update();
00105 }
00106 
00110 const FileSystem *StorageInfo::fileSystemOf( const QString &filename )
00111 {
00112     for (QListIterator<FileSystem> i(mFileSystems); i.current(); ++i)
00113     {
00114         if ( filename.startsWith( (*i)->path() ) )
00115             return (*i);
00116     }
00117     return 0;
00118 }
00119 
00120 
00121 void StorageInfo::cardMessage( const QCString& msg, const QByteArray& )
00122 {
00123     if ( msg == "mtabChanged()" )
00124         update();
00125 }
00126 
00127 
00132 // cause of the lack of a d pointer we need
00133 // to store informations in a config file :(
00134 void StorageInfo::update()
00135 {
00136     //qDebug("StorageInfo::updating");
00137 #if defined(_OS_LINUX_) || defined(Q_OS_LINUX)
00138     struct mntent *me;
00139     FILE *mntfp = setmntent( "/etc/mtab", "r" );
00140 
00141     QStringList curdisks;
00142     QStringList curopts;
00143     QStringList curfs;
00144     bool rebuild = FALSE;
00145     int n=0;
00146     if ( mntfp )
00147     {
00148         while ( (me = getmntent( mntfp )) != 0 )
00149         {
00150             QString fs = me->mnt_fsname;
00151             if ( fs.left(7)=="/dev/hd" || fs.left(7)=="/dev/sd"
00152                     || fs.left(8)=="/dev/mtd" || fs.left(9) == "/dev/mmcd"
00153                     || fs.left( 14 ) == "/dev/mmc/part1"
00154                     || fs.left(5)=="tmpfs" || fs.left(9)=="/dev/root" )
00155             {
00156                 n++;
00157                 curdisks.append(fs);
00158                 curopts.append( me->mnt_opts );
00159                 //qDebug("-->fs %s opts %s", fs.latin1(), me->mnt_opts );
00160                 curfs.append( me->mnt_dir );
00161                 bool found = FALSE;
00162                 for (QListIterator<FileSystem> i(mFileSystems); i.current(); ++i)
00163                 {
00164                     if ( (*i)->disk() == fs )
00165                     {
00166                         found = TRUE;
00167                         break;
00168                     }
00169                 }
00170                 if ( !found )
00171                     rebuild = TRUE;
00172             }
00173         }
00174         endmntent( mntfp );
00175     }
00176     if ( rebuild || n != (int)mFileSystems.count() )
00177     {
00178         mFileSystems.clear();
00179         QStringList::ConstIterator it=curdisks.begin();
00180         QStringList::ConstIterator fsit=curfs.begin();
00181         QStringList::ConstIterator optsIt=curopts.begin();
00182         for (; it!=curdisks.end(); ++it, ++fsit, ++optsIt)
00183         {
00184             QString opts = *optsIt;
00185 
00186             QString disk = *it;
00187             QString humanname;
00188             bool removable = FALSE;
00189             if ( isCF(disk) )
00190             {
00191                 humanname = tr("CF Card");
00192                 removable = TRUE;
00193             }
00194             else if ( disk == "/dev/hda1" )
00195             {
00196                 humanname = tr("Hard Disk");
00197             }
00198             else if ( disk.left(9) == "/dev/mmcd" )
00199             {
00200                 humanname = tr("SD Card");
00201                 removable = TRUE;
00202             }
00203             else if ( disk.left( 14 ) == "/dev/mmc/part1" )
00204             {
00205                 humanname = tr("MMC Card");
00206                 removable = TRUE;
00207             }
00208             else if ( disk.left(7) == "/dev/hd" )
00209                 humanname = tr("Hard Disk") + " " + disk;
00210             else if ( disk.left(7) == "/dev/sd" )
00211                 humanname = tr("SCSI Hard Disk") + " " + disk;
00212             else if ( disk.left(14) == "/dev/mtdblock6" ) //openzaurus ramfs
00213                 humanname = tr("Internal Memory");
00214             else if ( disk == "/dev/mtdblock1" || humanname == "/dev/mtdblock/1" )
00215                 humanname = tr("Internal Storage");
00216             else if ( disk.left(14) == "/dev/mtdblock/" )
00217                 humanname = tr("Internal Storage") + " " + disk;
00218             else if ( disk.left(13) == "/dev/mtdblock" )
00219                 humanname = tr("Internal Storage") + " " + disk;
00220             else if ( disk.left(9) == "/dev/root" )
00221                 humanname = tr("Internal Storage") + " " + disk;
00222             else if ( disk.left(5) == "tmpfs" ) //ipaqs /mnt/ramfs
00223                 humanname = tr("Internal Memory");
00224             FileSystem *fs = new FileSystem( disk, *fsit, humanname, removable, opts );
00225             mFileSystems.append( fs );
00226         }
00227         emit disksChanged();
00228     }
00229     else
00230     {
00231         // just update them
00232         for (QListIterator<FileSystem> i(mFileSystems); i.current(); ++i)
00233             i.current()->update();
00234     }
00235 #endif
00236 }
00237 
00238 bool deviceTab( const char *device)
00239 {
00240     QString name = device;
00241     bool hasDevice=false;
00242 
00243 #ifdef Q_OS_MACX
00244     // Darwin (MacOS X)
00245     struct statfs** mntbufp;
00246     int count = 0;
00247     if ( ( count = getmntinfo( mntbufp, MNT_WAIT ) ) == 0 )
00248     {
00249         qWarning("deviceTab: Error in getmntinfo(): %s",strerror( errno ) );
00250         hasDevice = false;
00251     }
00252     for( int i = 0; i < count; i++ )
00253     {
00254         QString deviceName = mntbufp[i]->f_mntfromname;
00255         qDebug(deviceName);
00256         if( deviceName.left( name.length() ) == name )
00257             hasDevice = true;
00258     }
00259 #else
00260     // Linux
00261     struct mntent *me;
00262     FILE *mntfp = setmntent( "/etc/mtab", "r" );
00263     if ( mntfp )
00264     {
00265         while ( (me = getmntent( mntfp )) != 0 )
00266         {
00267             QString deviceName = me->mnt_fsname;
00268             //          qDebug(deviceName);
00269             if( deviceName.left(name.length()) == name)
00270             {
00271                 hasDevice = true;
00272             }
00273         }
00274     }
00275     endmntent( mntfp );
00276 #endif /* Q_OS_MACX */
00277 
00278 
00279     return hasDevice;
00280 }
00281 
00287 bool StorageInfo::hasCf()
00288 {
00289     return deviceTab("/dev/hd");
00290 }
00291 
00297 bool StorageInfo::hasSd()
00298 {
00299     return deviceTab("/dev/mmcd");
00300 }
00301 
00307 bool StorageInfo::hasMmc()
00308 {
00309     bool hasMmc=false;
00310     if( deviceTab("/dev/mmc/part"))
00311         hasMmc=true;
00312     if( deviceTab("/dev/mmcd"))
00313         hasMmc=true;
00314     return hasMmc;
00315 }
00316 
00327 //---------------------------------------------------------------------------
00328 
00329 FileSystem::FileSystem( const QString &disk, const QString &path, const QString &name, bool rem, const QString &o )
00330         : fsdisk( disk ), fspath( path ), humanname( name ), blkSize(512), totalBlks(0), availBlks(0), removable( rem ), opts( o )
00331 {
00332     update();
00333 }
00334 
00335 void FileSystem::update()
00336 {
00337 #if defined(_OS_LINUX_) || defined(Q_OS_LINUX)
00338     struct statfs fs;
00339     if ( !statfs( fspath.latin1(), &fs ) )
00340     {
00341         blkSize = fs.f_bsize;
00342         totalBlks = fs.f_blocks;
00343         availBlks = fs.f_bavail;
00344     }
00345     else
00346     {
00347         blkSize = 0;
00348         totalBlks = 0;
00349         availBlks = 0;
00350     }
00351 #endif
00352 }
00353 
KDE Logo
This file is part of the documentation for OPIE Version 1.5.5.
Documentation copyright © 1997-2003 the KDE developers. 2003 OPIE developers
Generated on Tue Feb 10 20:24:08 2004 by doxygen 1.3.5 written by Dimitri van Heesch, © 1997-2001