00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include <qpe/storage.h>
00024
00025 #include <qcopchannel_qws.h>
00026
00027 #include <stdio.h>
00028
00029 #if defined(_OS_LINUX_) || defined(Q_OS_LINUX)
00030 #include <sys/vfs.h>
00031 #include <mntent.h>
00032 #endif
00033
00034 #ifdef Q_OS_MACX
00035 # include <sys/param.h>
00036 # include <sys/ucred.h>
00037 # include <sys/mount.h>
00038 # include <stdio.h>
00039 # include <errno.h>
00040 #endif
00041
00042
00043
00044
00045
00046
00047
00048 static bool isCF(const QString& m)
00049 {
00050
00051 #ifndef Q_OS_MACX
00052 FILE* f = fopen("/var/run/stab", "r");
00053 if (!f) f = fopen("/var/state/pcmcia/stab", "r");
00054 if (!f) f = fopen("/var/lib/pcmcia/stab", "r");
00055 if ( f )
00056 {
00057 char line[1024];
00058 char devtype[80];
00059 char devname[80];
00060 while ( fgets( line, 1024, f ) )
00061 {
00062
00063 if ( sscanf(line,"%*d %s %*s %*s %s", devtype, devname )==2 )
00064 {
00065 if ( QString(devtype) == "ide" && m.find(devname)>0 )
00066 {
00067 fclose(f);
00068 return TRUE;
00069 }
00070 }
00071 }
00072 fclose(f);
00073 }
00074 #endif
00075 return FALSE;
00076 }
00077
00093 StorageInfo::StorageInfo( QObject *parent )
00094 : QObject( parent )
00095 {
00096 mFileSystems.setAutoDelete( TRUE );
00097 channel = new QCopChannel( "QPE/Card", this );
00098 connect( channel, SIGNAL(received(const QCString&,const QByteArray&)),
00099 this, SLOT(cardMessage(const QCString&,const QByteArray&)) );
00100 update();
00101 }
00102
00106 const FileSystem *StorageInfo::fileSystemOf( const QString &filename )
00107 {
00108 for (QListIterator<FileSystem> i(mFileSystems); i.current(); ++i)
00109 {
00110 if ( filename.startsWith( (*i)->path() ) )
00111 return (*i);
00112 }
00113 return 0;
00114 }
00115
00116
00117 void StorageInfo::cardMessage( const QCString& msg, const QByteArray& )
00118 {
00119 if ( msg == "mtabChanged()" )
00120 update();
00121 }
00122
00123
00128
00129
00130 void StorageInfo::update()
00131 {
00132
00133 #if defined(_OS_LINUX_) || defined(Q_OS_LINUX)
00134 struct mntent *me;
00135 FILE *mntfp = setmntent( "/etc/mtab", "r" );
00136
00137 QStringList curdisks;
00138 QStringList curopts;
00139 QStringList curfs;
00140 bool rebuild = FALSE;
00141 int n=0;
00142 if ( mntfp )
00143 {
00144 while ( (me = getmntent( mntfp )) != 0 )
00145 {
00146 QString fs = me->mnt_fsname;
00147 if ( fs.left(7)=="/dev/hd" || fs.left(7)=="/dev/sd"
00148 || fs.left(8)=="/dev/mtd" || fs.left(9) == "/dev/mmcd"
00149 || fs.left( 14 ) == "/dev/mmc/part1"
00150 || fs.left(5)=="tmpfs" || fs.left(11)=="/dev/mmcblk"
00151 || fs.left(9)=="/dev/root" )
00152 {
00153 n++;
00154 curdisks.append(fs);
00155 curopts.append( me->mnt_opts );
00156
00157 curfs.append( me->mnt_dir );
00158 bool found = FALSE;
00159 for (QListIterator<FileSystem> i(mFileSystems); i.current(); ++i)
00160 {
00161 if ( (*i)->disk() == fs )
00162 {
00163 found = TRUE;
00164 break;
00165 }
00166 }
00167 if ( !found )
00168 rebuild = TRUE;
00169 }
00170 }
00171 endmntent( mntfp );
00172 }
00173 if ( rebuild || n != (int)mFileSystems.count() )
00174 {
00175 mFileSystems.clear();
00176 QStringList::ConstIterator it=curdisks.begin();
00177 QStringList::ConstIterator fsit=curfs.begin();
00178 QStringList::ConstIterator optsIt=curopts.begin();
00179 for (; it!=curdisks.end(); ++it, ++fsit, ++optsIt)
00180 {
00181 QString opts = *optsIt;
00182
00183 QString disk = *it;
00184 QString humanname;
00185 bool removable = FALSE;
00186 if ( isCF(disk) )
00187 {
00188 humanname = tr("CF Card");
00189 removable = TRUE;
00190 }
00191 else if ( disk == "/dev/hda1" )
00192 {
00193 humanname = tr("Hard Disk");
00194 }
00195 else if ( disk.left(9) == "/dev/mmcd" )
00196 {
00197 humanname = tr("SD Card");
00198 removable = TRUE;
00199 }
00200 else if ( disk.left( 14 ) == "/dev/mmc/part1" || disk.left(11) == "/dev/mmcblk" )
00201 {
00202 humanname = tr("MMC Card");
00203 removable = TRUE;
00204 }
00205 else if ( disk.left(7) == "/dev/hd" )
00206 humanname = tr("Hard Disk") + " " + disk;
00207 else if ( disk.left(7) == "/dev/sd" ) {
00208 humanname = tr("SCSI Hard Disk") + " " + disk;
00209 removable = TRUE;
00210 } else if ( disk.left(14) == "/dev/mtdblock6" )
00211 humanname = tr("Internal Memory");
00212 else if ( disk == "/dev/mtdblock1" || humanname == "/dev/mtdblock/1" )
00213 humanname = tr("Internal Storage");
00214 else if ( disk.left(14) == "/dev/mtdblock/" )
00215 humanname = tr("Internal Storage") + " " + disk;
00216 else if ( disk.left(13) == "/dev/mtdblock" )
00217 humanname = tr("Internal Storage") + " " + disk;
00218 else if ( disk.left(9) == "/dev/root" )
00219 humanname = tr("Internal Storage") + " " + disk;
00220 else if ( disk.left(5) == "tmpfs" )
00221 humanname = tr("Internal Memory");
00222 FileSystem *fs = new FileSystem( disk, *fsit, humanname, removable, opts );
00223 mFileSystems.append( fs );
00224 }
00225 emit disksChanged();
00226 }
00227 else
00228 {
00229
00230 for (QListIterator<FileSystem> i(mFileSystems); i.current(); ++i)
00231 i.current()->update();
00232 }
00233 #endif
00234 }
00235
00236 bool deviceTab( const char *device)
00237 {
00238 QString name = device;
00239 bool hasDevice=false;
00240
00241 #ifdef Q_OS_MACX
00242
00243 struct statfs** mntbufp;
00244 int count = 0;
00245 if ( ( count = getmntinfo( mntbufp, MNT_WAIT ) ) == 0 )
00246 {
00247 qWarning("deviceTab: Error in getmntinfo(): %s",strerror( errno ) );
00248 hasDevice = false;
00249 }
00250 for( int i = 0; i < count; i++ )
00251 {
00252 QString deviceName = mntbufp[i]->f_mntfromname;
00253 qDebug(deviceName);
00254 if( deviceName.left( name.length() ) == name )
00255 hasDevice = true;
00256 }
00257 #else
00258
00259 struct mntent *me;
00260 FILE *mntfp = setmntent( "/etc/mtab", "r" );
00261 if ( mntfp )
00262 {
00263 while ( (me = getmntent( mntfp )) != 0 )
00264 {
00265 QString deviceName = me->mnt_fsname;
00266
00267 if( deviceName.left(name.length()) == name)
00268 {
00269 hasDevice = true;
00270 }
00271 }
00272 }
00273 endmntent( mntfp );
00274 #endif
00275
00276
00277 return hasDevice;
00278 }
00279
00285 bool StorageInfo::hasCf()
00286 {
00287 return deviceTab("/dev/hd");
00288 }
00289
00295 bool StorageInfo::hasSd()
00296 {
00297 return deviceTab("/dev/mmcd");
00298 }
00299
00305 bool StorageInfo::hasMmc()
00306 {
00307 bool hasMmc=false;
00308 if( deviceTab("/dev/mmc/part"))
00309 hasMmc=true;
00310 if( deviceTab("/dev/mmcd"))
00311 hasMmc=true;
00312 return hasMmc;
00313 }
00314
00325
00326
00327 FileSystem::FileSystem( const QString &disk, const QString &path, const QString &name, bool rem, const QString &o )
00328 : fsdisk( disk ), fspath( path ), humanname( name ), blkSize(512), totalBlks(0), availBlks(0), removable( rem ), opts( o )
00329 {
00330 update();
00331 }
00332
00333 void FileSystem::update()
00334 {
00335 #if defined(_OS_LINUX_) || defined(Q_OS_LINUX)
00336 struct statfs fs;
00337 if ( !statfs( fspath.latin1(), &fs ) )
00338 {
00339 blkSize = fs.f_bsize;
00340 totalBlks = fs.f_blocks;
00341 availBlks = fs.f_bavail;
00342 }
00343 else
00344 {
00345 blkSize = 0;
00346 totalBlks = 0;
00347 availBlks = 0;
00348 }
00349 #endif
00350 }
00351