1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
|
/*
* disks.cpp
*
* Copyright (c) 1998 Michael Kropfberger <michael.kropfberger@gmx.net>
*
* Requires the Qt widget libraries, available at no cost at
* http://www.troll.no/
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <qfileinfo.h>
#include <qdir.h>
#include <kglobal.h>
#include <kdebug.h>
#include "disks.h"
#include "disks.moc"
/****************************************************/
/********************* DiskEntry ********************/
/****************************************************/
/**
* Constructor
**/
void DiskEntry::init()
{
device="";
type="";
mountedOn="";
options="";
size=0;
used=0;
avail=0;
isMounted=FALSE;
mntcmd="";
umntcmd="";
iconSetByUser=FALSE;
icoName="";
// BackgroundProcesses ****************************************
sysProc = new KShellProcess(); Q_CHECK_PTR(sysProc);
connect( sysProc, SIGNAL(receivedStdout(KProcess *, char *, int) ),
this, SLOT (receivedSysStdErrOut(KProcess *, char *, int)) );
connect( sysProc, SIGNAL(receivedStderr(KProcess *, char *, int) ),
this, SLOT (receivedSysStdErrOut(KProcess *, char *, int)) );
readingSysStdErrOut=FALSE;
}
DiskEntry::DiskEntry(QObject *parent, const char *name)
: QObject (parent, name)
{
init();
}
DiskEntry::DiskEntry(const QString & deviceName, QObject *parent, const char *name)
: QObject (parent, name)
{
init();
setDeviceName(deviceName);
}
DiskEntry::~DiskEntry()
{
disconnect(this);
delete sysProc;
}
int DiskEntry::toggleMount()
{
if (!mounted())
return mount();
else
return umount();
}
int DiskEntry::mount()
{
QString cmdS=mntcmd;
if (cmdS.isEmpty()) // generate default mount cmd
if (getuid()!=0 ) // user mountable
{
cmdS="mount %d";
}
else // root mounts with all params/options
{
// FreeBSD's mount(8) is picky: -o _must_ go before
// the device and mountpoint.
cmdS=QString::fromLatin1("mount -t%t -o%o %d %m");
}
cmdS.replace(QString::fromLatin1("%d"),deviceName());
cmdS.replace(QString::fromLatin1("%m"),mountPoint());
cmdS.replace(QString::fromLatin1("%t"),fsType());
cmdS.replace(QString::fromLatin1("%o"),mountOptions());
kdDebug() << "mount-cmd: [" << cmdS << "]" << endl;
int e=sysCall(cmdS);
if (!e) setMounted(TRUE);
kdDebug() << "mount-cmd: e=" << e << endl;
return e;
}
int DiskEntry::umount()
{
kdDebug() << "umounting" << endl;
QString cmdS=umntcmd;
if (cmdS.isEmpty()) // generate default umount cmd
cmdS="umount %d";
cmdS.replace(QString::fromLatin1("%d"),deviceName());
cmdS.replace(QString::fromLatin1("%m"),mountPoint());
kdDebug() << "umount-cmd: [" << cmdS << "]" << endl;
int e=sysCall(cmdS);
if (!e) setMounted(FALSE);
kdDebug() << "umount-cmd: e=" << e << endl;
return e;
}
int DiskEntry::remount()
{
if (mntcmd.isEmpty() && umntcmd.isEmpty() // default mount/umount commands
&& (getuid()==0)) // you are root
{
QString oldOpt=options;
if (options.isEmpty())
options="remount";
else
options+=",remount";
int e=mount();
options=oldOpt;
return e;
} else {
if (int e=umount())
return mount();
else return e;
}
}
void DiskEntry::setMountCommand(const QString & mnt)
{
mntcmd=mnt;
}
void DiskEntry::setUmountCommand(const QString & umnt)
{
umntcmd=umnt;
}
void DiskEntry::setIconName(const QString & iconName)
{
iconSetByUser=TRUE;
icoName=iconName;
if (icoName.right(6) == "_mount")
icoName.truncate(icoName.length()-6);
else if (icoName.right(8) == "_unmount")
icoName.truncate(icoName.length()-8);
emit iconNameChanged();
}
QString DiskEntry::iconName()
{
QString iconName=icoName;
if (iconSetByUser) {
mounted() ? iconName+="_mount" : iconName+="_unmount";
return iconName;
} else
return guessIconName();
}
QString DiskEntry::guessIconName()
{
QString iconName;
// try to be intelligent
if (-1!=mountPoint().find("cdrom",0,FALSE)) iconName+="cdrom";
else if (-1!=deviceName().find("cdrom",0,FALSE)) iconName+="cdrom";
else if (-1!=mountPoint().find("writer",0,FALSE)) iconName+="cdwriter";
else if (-1!=deviceName().find("writer",0,FALSE)) iconName+="cdwriter";
else if (-1!=mountPoint().find("mo",0,FALSE)) iconName+="mo";
else if (-1!=deviceName().find("mo",0,FALSE)) iconName+="mo";
else if (-1!=deviceName().find("fd",0,FALSE)) {
if (-1!=deviceName().find("360",0,FALSE)) iconName+="5floppy";
if (-1!=deviceName().find("1200",0,FALSE)) iconName+="5floppy";
else iconName+="3floppy";
}
else if (-1!=mountPoint().find("floppy",0,FALSE)) iconName+="3floppy";
else if (-1!=mountPoint().find("zip",0,FALSE)) iconName+="zip";
else if (-1!=fsType().find("nfs",0,FALSE)) iconName+="nfs";
else iconName+="hdd";
mounted() ? iconName+="_mount" : iconName+="_unmount";
// if ( -1==mountOptions().find("user",0,FALSE) )
// iconName.prepend("root_"); // special root icon, normal user cant mount
//debug("device %s is %s",deviceName().latin1(),iconName.latin1());
//emit iconNameChanged();
return iconName;
}
/***************************************************************************
* starts a command on the underlying system via /bin/sh
**/
int DiskEntry::sysCall(const QString & command)
{
if (readingSysStdErrOut || sysProc->isRunning() ) return -1;
sysStringErrOut=i18n("Called: %1\n\n").arg(command); // put the called command on ErrOut
sysProc->clearArguments();
(*sysProc) << command;
if (!sysProc->start( KProcess::Block, KProcess::AllOutput ))
kdFatal() << i18n("could not execute %1").arg(command.local8Bit().data()) << endl;
if (sysProc->exitStatus()!=0) emit sysCallError(this, sysProc->exitStatus());
return (sysProc->exitStatus());
}
/***************************************************************************
* is called, when the Sys-command writes on StdOut or StdErr
**/
void DiskEntry::receivedSysStdErrOut(KProcess *, char *data, int len)
{
QString tmp = QString::fromLocal8Bit(data, len);
sysStringErrOut.append(tmp);
}
float DiskEntry::percentFull() const
{
if (size != 0) {
return 100 - ( ((float)avail / (float)size) * 100 );
} else {
return -1;
}
}
void DiskEntry::setDeviceName(const QString & deviceName)
{
device=deviceName;
emit deviceNameChanged();
}
QString DiskEntry::deviceRealName() const
{
QFileInfo inf( device );
QDir dir( inf.dirPath( true ) );
QString relPath = inf.fileName();
if ( inf.isSymLink() ) {
QString link = inf.readLink();
if ( link.startsWith( "/" ) )
return link;
relPath = link;
}
return dir.canonicalPath() + "/" + relPath;
}
void DiskEntry::setMountPoint(const QString & mountPoint)
{
mountedOn=mountPoint;
emit mountPointChanged();
}
QString DiskEntry::realMountPoint() const
{
QDir dir( mountedOn );
return dir.canonicalPath();
}
void DiskEntry::setMountOptions(const QString & mountOptions)
{
options=mountOptions;
emit mountOptionsChanged();
}
void DiskEntry::setFsType(const QString & fsType)
{
type=fsType;
emit fsTypeChanged();
}
void DiskEntry::setMounted(bool nowMounted)
{
isMounted=nowMounted;
emit mountedChanged();
}
void DiskEntry::setKBSize(int kb_size)
{
size=kb_size;
emit kBSizeChanged();
}
void DiskEntry::setKBUsed(int kb_used)
{
used=kb_used;
if ( size < (used+avail) ) { //adjust kBAvail
kdWarning() << "device " << device << ": kBAvail(" << avail << ")+*kBUsed(" << used << ") exceeds kBSize(" << size << ")" << endl;
setKBAvail(size-used);
}
emit kBUsedChanged();
}
void DiskEntry::setKBAvail(int kb_avail)
{
avail=kb_avail;
if ( size < (used+avail) ) { //adjust kBUsed
kdWarning() << "device " << device << ": *kBAvail(" << avail << ")+kBUsed(" << used << ") exceeds kBSize(" << size << ")" << endl;
setKBUsed(size-avail);
}
emit kBAvailChanged();
}
|