00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <sys/types.h>
00025 #include <signal.h>
00026
00027 #include <stdlib.h>
00028 #include <ctype.h>
00029
00030 #include "debug.h"
00031 #include "zip.moc"
00032
00033 #include <QCheckBox>
00034 #include <QTimer>
00035 #include <QGridLayout>
00036
00037 #include <klocale.h>
00038 #include <k3process.h>
00039 #include <kconfig.h>
00040
00041 ZipFormat::ZipFormat(QWidget *w,const char *n) :
00042 DiskFormat(w,n),
00043 zeroWholeDisk(0L),
00044 p(0L),
00045 formatStep(0),
00046 statusTimer(0L)
00047 {
00048 DEBUGSETUP;
00049
00050 QGridLayout *grid = new QGridLayout(this);
00051 grid->setSpacing(10);
00052
00053 zeroWholeDisk = new QCheckBox(i18n("Zero entire disk"),this);
00054 zeroWholeDisk->setWhatsThis(
00055 i18n("Try to write zeroes to the entire disk "
00056 "before adding a filesystem, in order "
00057 "to check the disk's integrity."));
00058 grid->addWidget(zeroWholeDisk,0,0);
00059 enableSoftUpdates = new QCheckBox(i18n("Enable softupdates"),this);
00060 grid->addWidget(enableSoftUpdates,1,0);
00061
00062
00063
00064
00065
00066 grid->addItem( new QSpacerItem( 0,10), 2, 0 );
00067 grid->setRowStretch(2,100);
00068
00069 endInit();
00070 }
00071
00072 const char fslabel[] = I18N_NOOP("UFS Zip100");
00073
00074 FilesystemList ZipFormat::FSLabels() const
00075 {
00076 FilesystemList l;
00077
00078 l.append(new FilesystemData(i18n(fslabel),0xefc87329,(DiskFormat *)this));
00079 return l;
00080 }
00081
00082 bool ZipFormat::runtimeCheck()
00083 {
00084 DEBUGSETUP;
00085 dd = findExecutable("dd");
00086 newfs = findExecutable("newfs");
00087 return !newfs.isEmpty() && !dd.isEmpty();
00088 }
00089
00090 QString ZipFormat::dd;
00091 QString ZipFormat::newfs;
00092
00093 void ZipFormat::setEnabled(bool b)
00094 {
00095 zeroWholeDisk->setEnabled(b);
00096 enableSoftUpdates->setEnabled(b);
00097 }
00098
00099 void ZipFormat::readSettings(KConfig *c)
00100 {
00101 c->setGroup(fslabel);
00102 zeroWholeDisk->setChecked(
00103 c->readBoolEntry("ZeroDisk",false));
00104 enableSoftUpdates->setChecked(
00105 c->readBoolEntry("SoftUpdates",false));
00106 }
00107
00108 void ZipFormat::writeSettings(KConfig *c)
00109 {
00110 c->setGroup(fslabel);
00111 c->writeEntry("ZeroDisk",zeroWholeDisk->isChecked());
00112 c->writeEntry("SoftUpdates",enableSoftUpdates->isChecked());
00113 }
00114
00115 void ZipFormat::quit()
00116 {
00117 DEBUGSETUP;
00118 delete p;
00119 delete statusTimer;
00120
00121 p=0L;
00122 statusTimer=0L;
00123 }
00124
00125 void ZipFormat::format(FilesystemData *f)
00126 {
00127 DEBUGSETUP;
00128
00129 if (f->magic()!=0xefc87329)
00130 {
00131 complainAboutFormat(f);
00132 return;
00133 }
00134
00135 formatStep=0;
00136
00137 if (p) delete p;
00138 p = new K3Process();
00139
00140 if (statusTimer) delete statusTimer;
00141 statusTimer = new QTimer(this);
00142
00143 connect(p,SIGNAL(processExited(K3Process *)),
00144 this,SLOT(transition()));
00145 connect(p,SIGNAL(receivedStdout(K3Process *,char *,int)),
00146 this,SLOT(processResult(K3Process *,char *,int)));
00147 connect(p,SIGNAL(receivedStderr(K3Process *,char *,int)),
00148 this,SLOT(processResult(K3Process *,char *,int)));
00149 connect(statusTimer,SIGNAL(timeout()),
00150 this,SLOT(statusRequest()));
00151
00152 transition();
00153 }
00154
00155 void ZipFormat::transition()
00156 {
00157 DEBUGSETUP;
00158
00159 switch(formatStep)
00160 {
00161 case 0 :
00162
00163
00164
00165
00166
00167
00168 if (zeroWholeDisk->isChecked())
00169 {
00170
00171
00172 statusTimer->start(10000);
00173 QTimer::singleShot(1000,this,
00174 SLOT(statusRequest()));
00175 totalBlocks=12288;
00176 }
00177 else
00178 {
00179
00180 statusTimer->start(1000);
00181 totalBlocks=100;
00182 }
00183
00184 *p << dd
00185 << "if=/dev/zero"
00186 << "of=/dev/afd0c"
00187 << "bs=8192" ;
00188 *p << QString("count=%1").arg(totalBlocks);
00189 if (!p->start(K3Process::NotifyOnExit,K3Process::AllOutput))
00190 {
00191 emit statusMessage(i18n("Cannot start dd to zero disk."));
00192 emit formatDone(-1);
00193 delete statusTimer;
00194 delete p;
00195 statusTimer=0L;
00196 p=0L;
00197 return;
00198 }
00199
00200 formatStep=1;
00201 emit statusMessage(i18n("Zeroing disk..."));
00202 break;
00203 case 1 :
00204 statusTimer->stop();
00205
00206 if (p->exitStatus())
00207 {
00208 emit statusMessage(i18n("Zeroing disk failed."));
00209 emit formatDone(-1);
00210 return;
00211 }
00212
00213 totalBlocks=196608;
00214
00215 p->clearArguments();
00216 *p << newfs << "-T" << "zip100" ;
00217 if (enableSoftUpdates->isChecked())
00218 {
00219 *p << "-U" ;
00220 }
00221 *p << "/dev/afd0c" ;
00222 if (!p->start(K3Process::NotifyOnExit,K3Process::AllOutput))
00223 {
00224 emit statusMessage(i18n("Cannot start newfs."));
00225 emit formatDone(-1);
00226 };
00227 formatStep=2;
00228 emit statusMessage(i18n("Making filesystem..."));
00229 break;
00230 case 2 :
00231 if (p->exitStatus())
00232 {
00233 emit statusMessage(i18n("newfs failed."));
00234 emit formatDone(-1);
00235 }
00236 else
00237 {
00238 emit statusMessage(i18n("Disk formatted successfully."));
00239 emit formatDone(0);
00240 }
00241 break;
00242 }
00243 }
00244
00245 void ZipFormat::processResult(K3Process *, char *b, int l)
00246 {
00247 DEBUGSETUP;
00248
00249 #ifdef DEBUG
00250 QString o = QString::fromLatin1(b,l);
00251 DEBUGS(QString(" %1").arg(o).latin1());
00252 #endif
00253
00254 switch(formatStep)
00255 {
00256 case 1 :
00257 if (strchr(b,'+'))
00258 {
00259 int currentblock=atoi(b);
00260 emit setProgress(currentblock*100/totalBlocks);
00261 if (totalBlocks>10000)
00262 {
00263 emit statusMessage(i18n("Zeroing block %1 of %2...",
00264 currentblock,
00265 totalBlocks));
00266 }
00267 }
00268 break;
00269 case 2 :
00270 if ((b[0]==' ') && isdigit(b[1]))
00271 {
00272 int currentblock=atoi(b+1);
00273 emit setProgress(currentblock*100/totalBlocks);
00274 }
00275 else
00276 {
00277
00278
00279
00280
00281 emit setProgress(1);
00282
00283
00284
00285 }
00286 break;
00287 }
00288 }
00289
00290 void ZipFormat::statusRequest()
00291 {
00292 if (formatStep!=1)
00293 return;
00294 if (!p)
00295 return;
00296
00297 #ifdef ANY_BSD
00298 p->kill(SIGINFO);
00299 #endif
00300 }
00301
00302