• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeutils API Reference
  • KDE Home
  • Contact Us
 

kfloppy

  • sources
  • kde-4.12
  • kdeutils
  • kfloppy
format.cpp
Go to the documentation of this file.
1 /*
2 
3  This file is part of the KFloppy program, part of the KDE project
4 
5  Copyright (C) 2002 Adriaan de Groot <groot@kde.org>
6  Copyright (C) 2004, 2005 Nicolas GOUTTE <goutte@kde.org>
7 
8  This program is free software; you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation, version 2.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 
21 */
22 
23 #include "format.h"
24 
25 #include <unistd.h>
26 #include <stdlib.h>
27 
28 #include <QTimer>
29 #include <QRegExp>
30 
31 #include <klocale.h>
32 #include <k3process.h>
33 #include <kdebug.h>
34 #include <kstandarddirs.h>
35 
36 static QString extPath = QString();
37 
38 /* static */ QString findExecutable(const QString &e)
39 {
40  if (extPath.isEmpty())
41  {
42  QString path = QLatin1String( qgetenv("PATH") );
43  if (!path.isEmpty()) path.append(QLatin1String( ":" ));
44  path.append(QLatin1String( "/usr/sbin:/sbin" ));
45  extPath = path;
46  }
47 
48  return KGlobal::dirs()->findExe(e, extPath);
49 }
50 
51 
52 KFAction::KFAction(QObject *parent) :
53  QObject(parent)
54 {
55  DEBUGSETUP;
56 }
57 
58 KFAction::~KFAction()
59 {
60  DEBUGSETUP;
61  quit();
62 }
63 
64 /* slot */ void KFAction::quit()
65 {
66  DEBUGSETUP;
67 }
68 
69 /* slot */ void KFAction::exec()
70 {
71  DEBUGSETUP;
72 }
73 
74 class KFActionQueue_p
75 {
76 public:
77  QList<KFAction*> list;
78 } ;
79 
80 KFActionQueue::KFActionQueue(QObject *parent) :
81  KFAction(parent),
82  d(new KFActionQueue_p)
83 {
84  DEBUGSETUP;
85 }
86 
87 KFActionQueue::~KFActionQueue()
88 {
89  DEBUGSETUP;
90  qDeleteAll(d->list);
91  d->list.clear();
92  delete d;
93 }
94 
95 void KFActionQueue::queue(KFAction *p)
96 {
97  DEBUGSETUP;
98 
99  d->list.append(p);
100  DEBUGS(p->objectName());
101 }
102 
103 /* virtual */ void KFActionQueue::exec()
104 {
105  DEBUGSETUP;
106 
107  actionDone(0L,true);
108 }
109 
110 /* slot */ void KFActionQueue::actionDone(KFAction *p,bool success)
111 {
112  DEBUGSETUP;
113 
114  if (p)
115  {
116  if (!d->list.isEmpty() && d->list.first()==p)
117  {
118  d->list.removeFirst();
119  delete p;
120  }
121  else
122  {
123  DEBUGS( "Strange pointer received.");
124  emit done(this,false);
125  return;
126  }
127  }
128  else
129  {
130  DEBUGS("Starting action queue.");
131  }
132 
133  if (!success)
134  {
135  DEBUGS("Action failed.");
136  emit done(this,false);
137  return;
138  }
139 
140  KFAction *next = d->list.isEmpty() ? 0 : d->list.first();
141  if (!next)
142  {
143  emit done(this,true);
144  }
145  else
146  {
147  kDebug(KFAREA) << "Running action " << next->objectName() ;
148  QObject::connect(next,SIGNAL(done(KFAction*,bool)),
149  this,SLOT(actionDone(KFAction*,bool)));
150  // Propagate signals
151  QObject::connect(next,SIGNAL(status(QString,int)),
152  this,SIGNAL(status(QString,int)));
153  QTimer::singleShot(0,next,SLOT(exec()));
154  }
155 }
156 
157 
158 // Here we have names of devices. The variable
159 // names are basically the linux device names,
160 // replace with whatever your OS needs instead.
161 //
162 //
163 #ifdef ANY_LINUX
164 
165 const char * const fd0H1440[] = { "/dev/fd0u1440", "/dev/floppy/0u1440", "/dev/fd0h1440", "/dev/fd0H1440", 0L } ;
166 const char * const fd0D720[] = { "/dev/fd0u720", "/dev/floppy/0u720", "/dev/fd0D720", "/dev/fd0h720", 0L };
167 const char * const fd0h1200[] = { "/dev/fd0h1200", "/dev/floppy/0h1200", 0L };
168 const char * const fd0h360[] = { "/dev/fd0u360", "/dev/floppy/0u360", "/dev/fd0h360", "/dev/fd0d360", 0L };
169 
170 const char * const fd1H1440[] = { "/dev/fd1u1440", "/dev/floppy/1u1440","/dev/fd1h1440", "/dev/fd1H1440", 0L } ;
171 const char * const fd1D720[] = { "/dev/fd1u720", "/dev/floppy/1u720", "/dev/fd1D720", "/dev/fd1h720", 0L };
172 const char * const fd1h1200[] = { "/dev/fd1h1200", "/dev/floppy/1h1200", 0L };
173 const char * const fd1h360[] = { "/dev/fd1u360", "/dev/floppy/1u360","/dev/fd1h360", "/dev/fd1d360", 0L };
174 
175 const char * const fd0auto[] = { "/dev/fd0", 0L };
176 const char * const fd1auto[] = { "/dev/fd1", 0L };
177 
178 #endif
179 
180 
181 #ifdef ANY_BSD
182 const char * const fd0[] = { "/dev/fd0", 0L } ;
183 const char * const fd1[] = { "/dev/fd1", 0L } ;
184 #endif
185 
186 // Next we have a table of device names and characteristics.
187 // These are ordered according to 2*densityIndex+deviceIndex,
188 // ie. primary (0) 1440K (0) is first, then secondary (1) 1440K is
189 // second, down to secondary (1) 360k (4) in position 3*2+1=7.
190 //
191 //
192 // Note that the data originally contained in KFloppy was
193 // patently false, so most of this is fake. I guess no one ever
194 // formatted a 5.25" floppy.
195 //
196 // The flags field is unused in this implementation.
197 //
198 //
199 const fdinfo fdtable[] =
200 {
201 #ifdef ANY_LINUX
202  // device drv blks trk flg
203  { fd0H1440, 0, 1440, 80, 0 },
204  { fd1H1440, 1, 1440, 80, 0 },
205  { fd0D720, 0, 720, 80, 0 },
206  { fd1D720, 1, 720, 80, 0 },
207  { fd0h1200, 0, 1200, 80, 0 },
208  { fd1h1200, 1, 1200, 80, 0 },
209  { fd0h360, 0, 360, 40, 0 },
210  { fd1h360, 1, 360, 40, 0 },
211  { fd0auto, 0, 0, 80, 0 },
212  { fd1auto, 1, 0, 80, 0 },
213 #endif
214 
215 #ifdef ANY_BSD
216  // Instead of the number of tracks, which is
217  // unneeded, we record the
218  // number of F's printed during an fdformat
219  { fd0, 0, 1440, 40, 0 },
220  { fd1, 1, 1440, 40, 0 },
221  { fd0, 0, 720, 40, 0 },
222  { fd1, 1, 720, 40, 0 },
223  { fd0, 0, 1200, 40, 0},
224  { fd1, 1, 1200, 40, 0},
225  { fd0, 0, 360, 40, 0},
226  { fd1, 1, 360, 40, 0},
227 #endif
228  { 0L, 0, 0, 0, 0 }
229 } ;
230 
231 
232 FloppyAction::FloppyAction(QObject *p) :
233  KFAction(p),
234  deviceInfo(0L),
235  theProcess(0L)
236 {
237  DEBUGSETUP;
238 }
239 
240 void FloppyAction::quit()
241 {
242  DEBUGSETUP;
243  delete theProcess;
244  theProcess=0L;
245 
246  KFAction::quit();
247 }
248 
249 bool FloppyAction::configureDevice( const QString& newDeviceName )
250 {
251  deviceInfo = 0; // We have not any idea what the device is
252  deviceName = newDeviceName;
253  return true; // No problem!
254 }
255 
256 bool FloppyAction::configureDevice(int drive,int density)
257 {
258  DEBUGSETUP;
259  const char *devicename = 0L;
260 
261  deviceInfo=0L;
262  deviceName.clear();
263 
264  if ((drive<0) || (drive>1))
265  {
266  emit status(i18n("Unexpected drive number %1.", drive),-1);
267  return false;
268  }
269 
270  const fdinfo *deviceinfo = fdtable;
271  for ( ; deviceinfo && (deviceinfo->devices) ; deviceinfo++)
272  {
273  if (deviceinfo->blocks != density)
274  continue;
275  }
276  if (!deviceinfo)
277  {
278  emit status(i18n("Unexpected density number %1.", density),-1);
279  return false;
280  }
281 
282  deviceinfo = fdtable;
283  for ( ; deviceinfo && (deviceinfo->devices) ; deviceinfo++)
284  {
285  if (deviceinfo->blocks != density)
286  continue;
287  if (deviceinfo->drive == drive)
288  break;
289  }
290 
291  if (!deviceinfo || !deviceinfo->devices)
292  {
293  emit status(i18n("Cannot find a device for drive %1 and density %2.",
294  drive, density),-1);
295  return false;
296  }
297 
298  for (const char* const* devices=deviceinfo->devices ;
299  *devices ; devices++)
300  {
301  if (access(*devices,W_OK)>=0)
302  {
303  kDebug(KFAREA) << "Found device " << *devices ;
304  devicename=*devices;
305  break;
306  }
307  }
308 
309  if (!devicename)
310  {
311  const QString str = i18n(
312  "Cannot access %1\nMake sure that the device exists and that "
313  "you have write permission to it.",QLatin1String( deviceinfo->devices[0] ));
314  emit status(str,-1);
315  return false;
316  }
317 
318  deviceName = QLatin1String( devicename );
319  deviceInfo = deviceinfo;
320 
321  return true;
322 }
323 
324 void FloppyAction::processDone(K3Process *p)
325 {
326  DEBUGSETUP;
327 
328  if (p!=theProcess)
329  {
330  DEBUGS(" Strange process exited.");
331  return;
332  }
333 
334  if (p->normalExit())
335  {
336  if (p->exitStatus() == 0)
337  {
338  emit status(QString::null,100); //krazy:exclude=nullstrassign for old broken gcc
339  emit done(this,true);
340  }
341  else
342  {
343  emit status(i18n("The program %1 terminated with an error.", theProcessName),100);
344  emit done(this,false);
345  }
346  }
347  else
348  {
349  emit status(i18n("The program %1 terminated abnormally.", theProcessName),100);
350  emit done(this,false);
351  }
352 }
353 
354 void FloppyAction::processStdOut(K3Process *, char *b, int l)
355 {
356  Q_UNUSED(b);
357  Q_UNUSED(l);
358  kDebug(KFAREA) << "stdout:" << QString::fromLatin1(b,l) ;
359 }
360 
361 void FloppyAction::processStdErr(K3Process *p, char *b, int l)
362 {
363  processStdOut(p,b,l);
364 }
365 
366 bool FloppyAction::startProcess()
367 {
368  DEBUGSETUP;
369 
370  connect(theProcess,SIGNAL(processExited(K3Process*)),
371  this,SLOT(processDone(K3Process*)));
372  connect(theProcess,SIGNAL(receivedStdout(K3Process*,char*,int)),
373  this,SLOT(processStdOut(K3Process*,char*,int)));
374  connect(theProcess,SIGNAL(receivedStderr(K3Process*,char*,int)),
375  this,SLOT(processStdErr(K3Process*,char*,int)));
376 
377  theProcess->setEnvironment( QLatin1String( "LC_ALL" ), QLatin1String( "C" ) ); // We need the untranslated output of the tool
378  return theProcess->start(K3Process::NotifyOnExit,
379  K3Process::AllOutput);
380 }
381 
382 
383 /* static */ QString FDFormat::fdformatName = QString();
384 
385 FDFormat::FDFormat(QObject *p) :
386  FloppyAction(p),
387  doVerify(true)
388 {
389  DEBUGSETUP;
390  theProcessName = QString::fromLatin1("fdformat");
391  setObjectName( QLatin1String("FDFormat" ));
392 }
393 
394 /* static */ bool FDFormat::runtimeCheck()
395 {
396  fdformatName = findExecutable(QLatin1String( "fdformat" ));
397  return (!fdformatName.isEmpty());
398 }
399 
400 bool FDFormat::configure(bool v)
401 {
402  doVerify=v;
403  return true;
404 }
405 
406 /* virtual */ void FDFormat::exec()
407 {
408  DEBUGSETUP;
409 
410  if ( !deviceInfo || deviceName.isEmpty() )
411  {
412  emit status( i18n("Internal error: device not correctly defined."), -1 );
413  emit done(this,false);
414  return;
415  }
416 
417  if (fdformatName.isEmpty())
418  {
419  emit status(i18n("Cannot find fdformat."),-1);
420  emit done(this,false);
421  return;
422  }
423 
424  delete theProcess;
425  theProcess = new K3Process;
426 
427  formatTrackCount=0;
428 
429  *theProcess << fdformatName ;
430 
431  // Common to Linux and BSD, others may differ
432  if (!doVerify)
433  {
434  *theProcess << QLatin1String( "-n" );
435  }
436 
437 #ifdef ANY_BSD
438  *theProcess
439  << QLatin1String( "-y" )
440  << QLatin1String( "-f" )
441  << QString::number(deviceInfo->blocks) ;
442 #elif defined(ANY_LINUX)
443  // No Linux-specific flags
444 #endif
445 
446  // Common to Linux and BSD, others may differ
447  *theProcess << deviceName;
448 
449  if (!startProcess())
450  {
451  emit status(i18n("Could not start fdformat."),-1);
452  emit done(this,false);
453  }
454 
455  // Now depend on fdformat running and producing output.
456 }
457 
458 // Parse some output from the fdformat process. Lots of
459 // #ifdefs here to account for variations in the basic
460 // fdformat. Uses gotos to branch to whatever error message we
461 // need, since the messages can be standardized across OSsen.
462 //
463 //
464 void FDFormat::processStdOut(K3Process *, char *b, int l)
465 {
466  DEBUGSETUP;
467  QString s;
468 
469 #ifdef ANY_BSD
470  if (b[0]=='F')
471  {
472  formatTrackCount++;
473  emit status(QString::null, //krazy:exclude=nullstrassign for old broken gcc
474  formatTrackCount * 100 / deviceInfo->tracks);
475  }
476  else if (b[0]=='E')
477  {
478  emit status(i18n("Error formatting track %1.", formatTrackCount),-1);
479  }
480  else
481  {
482  s = QString::fromLatin1(b,l);
483  if (s.contains(QLatin1String( "ioctl(FD_FORM)" )))
484  {
485  emit status (i18n(
486  "Cannot access floppy or floppy drive.\n"
487  "Please insert a floppy and make sure that you "
488  "have selected a valid floppy drive."),-1);
489  return;
490  }
491  if (s.indexOf(QLatin1String( "/dev/" ))>=0)
492  {
493  emit status(s,-1);
494  return;
495  }
496  DEBUGS(s);
497  }
498 #elif defined(ANY_LINUX)
499  s = QString::fromLatin1(b,l);
500  DEBUGS(s);
501  QRegExp regexp( QLatin1String( "([0-9]+)" ) );
502  if ( s.startsWith( QLatin1String( "bad data at cyl" ) ) || s.contains( QLatin1String( "Problem reading cylinder" ) ) )
503  {
504  if ( regexp.indexIn( s ) > -1 )
505  {
506  const int track = regexp.cap(1).toInt();
507  emit status(i18n("Low-level formatting error at track %1.", track), -1);
508  }
509  else
510  {
511  // This error should not happen
512  emit status(i18n("Low-level formatting error: %1", s), -1);
513  }
514  return;
515  }
516  else if (s.contains(QLatin1String( "ioctl(FDFMTBEG)" )))
517  {
518  emit status (i18n(
519  "Cannot access floppy or floppy drive.\n"
520  "Please insert a floppy and make sure that you "
521  "have selected a valid floppy drive."),-1);
522  return;
523  }
524  else if (s.contains(QLatin1String( "busy" ))) // "Device or resource busy"
525  {
526  emit status(i18n("Device busy.\nPerhaps you need to unmount the floppy first."),-1);
527  return;
528  }
529  // Be careful to leave "iotcl" as last before checking numbers
530  else if (s.contains(QLatin1String( "ioctl" )))
531  {
532  emit status(i18n("Low-level format error: %1", s),-1);
533  return;
534  }
535  // Check for numbers at last (as /dev/fd0u1440 has numbers too)
536  else if ( regexp.indexIn(s) > -1 )
537  {
538  // Normal track number (formatting or verifying)
539  const int p = regexp.cap(1).toInt();
540  if ((p>=0) && (p<deviceInfo->tracks))
541  {
542  emit status(QString::null, //krazy:exclude=nullstrassign for old broken gcc
543  p * 100 / deviceInfo->tracks);
544  }
545  }
546 #endif
547  return;
548 }
549 
550 
551 /* static */ QString DDZeroOut::m_ddName = QString();
552 
553 DDZeroOut::DDZeroOut(QObject *p) :
554  FloppyAction(p)
555 {
556  kDebug(KFAREA) << k_funcinfo ;
557  theProcessName = QString::fromLatin1("dd");
558  setObjectName( QLatin1String("DD" ));
559 }
560 
561 /* static */ bool DDZeroOut::runtimeCheck()
562 {
563  m_ddName = findExecutable(QLatin1String( "dd" ));
564  return (!m_ddName.isEmpty());
565 }
566 
567 /* virtual */ void DDZeroOut::exec()
568 {
569  kDebug(KFAREA) << k_funcinfo ;
570 
571  if ( deviceName.isEmpty() )
572  {
573  emit status( i18n("Internal error: device not correctly defined."), -1 );
574  emit done( this, false );
575  return;
576  }
577 
578  if ( m_ddName.isEmpty() )
579  {
580  emit status( i18n("Cannot find dd."), -1 );
581  emit done( this, false );
582  return;
583  }
584 
585  delete theProcess;
586  theProcess = new K3Process;
587 
588  *theProcess << m_ddName ;
589 
590  *theProcess << QLatin1String( "if=/dev/zero" ) ;
591  *theProcess << QLatin1String( "of=" )+deviceName;
592 
593  if ( !startProcess() )
594  {
595  emit status( i18n("Could not start dd."), -1 );
596  emit done( this, false );
597  }
598 
599 }
600 
601 void DDZeroOut::processDone(K3Process *p)
602 {
603  kDebug(KFAREA) << k_funcinfo ;
604 
605  if (p!=theProcess)
606  {
607  kDebug(KFAREA) << "Strange process exited." ;
608  return;
609  }
610 
617  emit status(QString::null,100); //krazy:exclude=nullstrassign for old broken gcc
618  emit done(this,true);
619 }
620 
621 
622 /* static */ QString FATFilesystem::newfs_fat = QString();
623 
624 FATFilesystem::FATFilesystem(QObject *parent) :
625  FloppyAction(parent)
626 {
627  DEBUGSETUP;
628  runtimeCheck();
629  theProcessName=newfs_fat;
630  setObjectName( QLatin1String("FATFilesystem" ));
631 }
632 
633 /* static */ bool FATFilesystem::runtimeCheck()
634 {
635  DEBUGSETUP;
636 
637 #ifdef ANY_BSD
638  newfs_fat = findExecutable(QLatin1String( "newfs_msdos" ));
639 #elif defined(ANY_LINUX)
640  newfs_fat = findExecutable(QLatin1String( "mkdosfs" ));
641 #else
642  return false;
643 #endif
644 
645  return !newfs_fat.isEmpty();
646 }
647 
648 bool FATFilesystem::configure(bool v,bool l,const QString &lbl)
649 {
650  doVerify=v;
651  doLabel=l;
652  if (l)
653  label=lbl.simplified();
654  else
655  label.clear();
656 
657  return true;
658 }
659 
660 void FATFilesystem::exec()
661 {
662  DEBUGSETUP;
663 
664  if (
665 #ifdef ANY_BSD // BSD needs the deviceInfo for the block count
666  !deviceInfo ||
667 #endif
668  deviceName.isEmpty())
669  {
670  emit status( i18n("Internal error: device not correctly defined."), -1 );
671  emit done(this,false);
672  return;
673  }
674 
675  if (newfs_fat.isEmpty())
676  {
677  emit status(i18n("Cannot find a program to create FAT filesystems."),-1);
678  emit done(this,false);
679  return;
680  }
681 
682  delete theProcess;
683  K3Process *p = theProcess = new K3Process;
684 
685  *p << newfs_fat;
686 #ifdef ANY_BSD
687  *p << QLatin1String( "-f" ) << QString::number(deviceInfo->blocks);
688  if (doLabel)
689  {
690  *p << QLatin1String( "-L" ) << label ;
691  }
692 #else
693 #ifdef ANY_LINUX
694  if (doLabel)
695  {
696  *p << QLatin1String( "-n" ) << label ;
697  }
698  if (doVerify)
699  {
700  *p << QLatin1String( "-c" );
701  }
702 #endif
703 #endif
704  *p << deviceName ;
705 
706  if (!startProcess())
707  {
708  emit status(i18n("Cannot start FAT format program."),-1);
709  emit done(this,false);
710  }
711 }
712 
713 void FATFilesystem::processStdOut(K3Process *, char *b, int l)
714 {
715 #ifdef ANY_BSD
716  // ### TODO: do some checks
717 #elif defined(ANY_LINUX)
718  QString s ( QString::fromLatin1( b, l ) );
719  kDebug(KFAREA) << s ;
720  if (s.contains(QLatin1String( "mounted file system" ))) // "/dev/fd0 contains a mounted file system
721  {
722  emit status(i18n("Floppy is mounted.\nYou need to unmount the floppy first."),-1);
723  return;
724  }
725  else if (s.contains(QLatin1String( "busy" ))) // "Device or resource busy"
726  {
727  emit status(i18n("Device busy.\nPerhaps you need to unmount the floppy first."),-1);
728  return;
729  }
730 # if 0
731  else if ( s.find( "mkdosfs" ) != -1 ) // DEBUG: get the program header and show it!
732  {
733  emit status( s, -1 );
734  return;
735  }
736 # endif
737 #endif
738 }
739 
740 
741 #ifdef ANY_BSD
742 
743 /* static */ QString UFSFilesystem::newfs = QString();
744 
745 UFSFilesystem::UFSFilesystem(QObject *parent) :
746  FloppyAction(parent)
747 {
748  DEBUGSETUP;
749  runtimeCheck();
750  theProcessName=newfs;
751  setObjectName( QLatin1String("UFSFilesystem" ));
752 }
753 
754 /* static */ bool UFSFilesystem::runtimeCheck()
755 {
756  DEBUGSETUP;
757 
758  newfs = findExecutable(QLatin1String( "newfs" ));
759 
760  return !newfs.isEmpty();
761 }
762 
763 void UFSFilesystem::exec()
764 {
765  DEBUGSETUP;
766 
767  if ( deviceName.isEmpty() )
768  {
769  emit status( i18n("Internal error: device not correctly defined."), -1 );
770  emit done(this,false);
771  return;
772  }
773 
774  if (newfs.isEmpty())
775  {
776  emit status(i18nc("BSD", "Cannot find a program to create UFS filesystems."),-1);
777  emit done(this,false);
778  return;
779  }
780 
781  delete theProcess;
782  K3Process *p = theProcess = new K3Process;
783 
784  *p << newfs;
785 
786  // ### TODO: is it still needed? (FreeBSD 5.3's man page says: "For backward compatibility.")
787  if ( deviceInfo )
788  *p << "-T" << QString("fd%1").arg(deviceInfo->blocks);
789 
790  *p << deviceName;
791 
792  if (!startProcess())
793  {
794  emit status(i18nc("BSD", "Cannot start UFS format program."),-1);
795  emit done(this,false);
796  }
797 }
798 #endif
799 
800 
801 /* static */ QString Ext2Filesystem::newfs = QString();
802 
803 Ext2Filesystem::Ext2Filesystem(QObject *parent) :
804  FloppyAction(parent)
805 {
806  DEBUGSETUP;
807  runtimeCheck();
808  theProcessName=QLatin1String( "mke2fs" );
809  setObjectName( QLatin1String("Ext2Filesystem" ));
810 }
811 
812 /* static */ bool Ext2Filesystem::runtimeCheck()
813 {
814  DEBUGSETUP;
815 
816  newfs = findExecutable(QLatin1String( "mke2fs" ));
817 
818  return !newfs.isEmpty();
819 }
820 
821 bool Ext2Filesystem::configure(bool v,bool l,const QString &lbl)
822 {
823  doVerify=v;
824  doLabel=l;
825  if (l)
826  {
827  label=lbl.trimmed();
828  }
829  else
830  {
831  label.clear();
832  }
833 
834  return true;
835 }
836 
837 void Ext2Filesystem::exec()
838 {
839  DEBUGSETUP;
840 
841  if (
842 #ifdef ANY_BSD // BSD needs the deviceInfo for the block count
843  !deviceInfo ||
844 #endif
845  deviceName.isEmpty() )
846  {
847  emit status( i18n("Internal error: device not correctly defined."), -1 );
848  emit done(this,false);
849  return;
850  }
851 
852  if (newfs.isEmpty())
853  {
854  emit status(i18n("Cannot find a program to create ext2 filesystems."),-1);
855  emit done(this,false);
856  return;
857  }
858 
859  delete theProcess;
860  K3Process *p = theProcess = new K3Process;
861 
862  *p << newfs;
863  *p << "-q";
864  if (doVerify) *p << QLatin1String( "-c" ) ;
865  if (doLabel) *p << QLatin1String( "-L" ) << label ;
866 
867  *p << deviceName ;
868 
869  if (!startProcess())
870  {
871  emit status(i18n("Cannot start ext2 format program."),-1);
872  emit done(this,false);
873  }
874 }
875 
876 void Ext2Filesystem::processStdOut(K3Process *, char *b, int l)
877 {
878 #ifdef ANY_BSD
879  // ### TODO: do some checks
880 #elif defined(ANY_LINUX)
881  QString s ( QString::fromLatin1( b, l ) );
882  kDebug(KFAREA) << s ;
883  if (s.contains(QLatin1String( "mounted" ))) // "/dev/fd0 is mounted; will not make a filesystem here!"
884  {
885  emit status(i18n("Floppy is mounted.\nYou need to unmount the floppy first."),-1);
886  return;
887  }
888  else if (s.contains(QLatin1String( "busy" ))) // "Device or resource busy"
889  {
890  emit status(i18n("Device busy.\nPerhaps you need to unmount the floppy first."),-1);
891  return;
892  }
893 #endif
894 }
895 
896 
897 
898 #ifdef ANY_LINUX
899 /* static */ QString MinixFilesystem::newfs = QString();
900 
901 MinixFilesystem::MinixFilesystem(QObject *parent) :
902  FloppyAction(parent)
903 {
904  DEBUGSETUP;
905  runtimeCheck();
906  theProcessName=QLatin1String( "mkfs.minix" );
907  setObjectName( QLatin1String("Minix2Filesystem" ));
908 }
909 
910 /* static */ bool MinixFilesystem::runtimeCheck()
911 {
912  DEBUGSETUP;
913 
914  newfs = findExecutable(QLatin1String( "mkfs.minix" ));
915 
916  return !newfs.isEmpty();
917 }
918 
919 bool MinixFilesystem::configure(bool v,bool l,const QString &lbl)
920 {
921  doVerify=v;
922  doLabel=l;
923  if (l)
924  {
925  label=lbl.trimmed();
926  }
927  else
928  {
929  label.clear();
930  }
931 
932  return true;
933 }
934 
935 void MinixFilesystem::exec()
936 {
937  DEBUGSETUP;
938 
939  if ( deviceName.isEmpty() )
940  {
941  emit status( i18n("Internal error: device not correctly defined."), -1 );
942  emit done(this,false);
943  return;
944  }
945 
946  if (newfs.isEmpty())
947  {
948  emit status(i18n("Cannot find a program to create Minix filesystems."),-1);
949  emit done(this,false);
950  return;
951  }
952 
953  delete theProcess;
954  K3Process *p = theProcess = new K3Process;
955 
956  *p << newfs;
957 
958  // Labeling is not possible
959  if (doVerify) *p << QLatin1String( "-c" ) ;
960 
961  *p << deviceName ;
962 
963  if (!startProcess())
964  {
965  emit status(i18n("Cannot start Minix format program."),-1);
966  emit done(this,false);
967  }
968 }
969 
970 void MinixFilesystem::processStdOut(K3Process *, char *b, int l)
971 {
972  QString s ( QString::fromLatin1( b, l ) );
973  kDebug(KFAREA) << s ;
974  if (s.contains(QLatin1String( "mounted" ))) // "mkfs.minix: /dev/fd0 is mounted; will not make a filesystem here!"
975  {
976  emit status(i18n("Floppy is mounted.\nYou need to unmount the floppy first."),-1);
977  return;
978  }
979  else if (s.contains(QLatin1String( "busy" ))) // "Device or resource busy"
980  {
981  emit status(i18n("Device busy.\nPerhaps you need to unmount the floppy first."),-1);
982  return;
983  }
984 }
985 
986 #endif
987 
988 #include "format.moc"
FloppyAction::processStdOut
virtual void processStdOut(K3Process *, char *, int)
Provide handling of stdout.
Definition: format.cpp:354
KFActionQueue::actionDone
void actionDone(KFAction *, bool)
Definition: format.cpp:110
KFAction::quit
virtual void quit()
Quit aborts the action.
Definition: format.cpp:64
FATFilesystem::processStdOut
virtual void processStdOut(K3Process *, char *b, int l)
Parse output.
Definition: format.cpp:713
FATFilesystem::doLabel
bool doLabel
Definition: format.h:338
FloppyAction::quit
virtual void quit()
Kills the running process, if one exists.
Definition: format.cpp:240
extPath
static QString extPath
Definition: format.cpp:36
KFActionQueue::queue
void queue(KFAction *)
Add a KFAction to the queue.
Definition: format.cpp:95
KFAction::done
void done(KFAction *me, bool success)
done() should always be emitted with this as first parameter, to avoid sender() magic and the like...
fdinfo::devices
const char *const * devices
Definition: format.h:162
KFAction::KFAction
KFAction(QObject *parent=0L)
Definition: format.cpp:52
FloppyAction::processDone
virtual void processDone(K3Process *)
Provide handling of the exit of the external program.
Definition: format.cpp:324
DDZeroOut::processDone
virtual void processDone(K3Process *)
Provide handling of the exit of the external program.
Definition: format.cpp:601
FloppyAction::startProcess
bool startProcess()
Sets up connections, calls K3Process::run().
Definition: format.cpp:366
FDFormat::processStdOut
virtual void processStdOut(K3Process *, char *, int)
Provide handling of stdout.
Definition: format.cpp:464
DDZeroOut::m_ddName
static QString m_ddName
path to executable.
Definition: format.h:308
FATFilesystem::configure
bool configure(bool verify, bool label, const QString &l)
newfs_msdos(1) doesn't support an additional verify, but Linux mkdosfs(1) does.
Definition: format.cpp:648
fdtable
const fdinfo fdtable[]
Definition: format.cpp:199
QObject
Ext2Filesystem::doVerify
bool doVerify
Definition: format.h:364
Ext2Filesystem::doLabel
bool doLabel
Definition: format.h:364
KFActionQueue::~KFActionQueue
virtual ~KFActionQueue()
Definition: format.cpp:87
FloppyAction::processStdErr
virtual void processStdErr(K3Process *, char *, int)
Provide handling stderr.
Definition: format.cpp:361
format.h
This file defines a hierarchy of classes that can run a sequence of external programs (like fdformat...
Ext2Filesystem::Ext2Filesystem
Ext2Filesystem(QObject *parent=0L)
Definition: format.cpp:803
FATFilesystem::label
QString label
Definition: format.h:339
KFActionQueue::exec
virtual void exec()
Exec() should return quickly to ensire that the GUI thread stays alive.
Definition: format.cpp:103
fdinfo::blocks
int blocks
Definition: format.h:164
fdinfo
Description structure for floppy devices.
Definition: format.h:162
FDFormat::formatTrackCount
int formatTrackCount
How many tracks formatted.
Definition: format.h:277
fdinfo::drive
int drive
Definition: format.h:163
KFAREA
#define KFAREA
Definition: debug.h:39
DDZeroOut::DDZeroOut
DDZeroOut(QObject *parent=0L)
Definition: format.cpp:553
KFAction::~KFAction
virtual ~KFAction()
Definition: format.cpp:58
FDFormat::configure
bool configure(bool verify)
verify instructs fdformat(1) to verify the medium as well.
Definition: format.cpp:400
findExecutable
QString findExecutable(const QString &e)
Utility function that looks for executables in $PATH and in /sbin and /usr/sbin.
Definition: format.cpp:38
KFAction::status
void status(const QString &msg, int progress)
Emit this signal to inform the user of interesting changes; setting msg to an empty string doesn't ch...
FloppyAction::deviceName
QString deviceName
Name of the device.
Definition: format.h:213
DEBUGSETUP
#define DEBUGSETUP
Definition: debug.h:42
FloppyAction::configureDevice
bool configureDevice(int driveno, int density)
ConfigureDevice() needs to be called prior to exec() or exec() will fail; this indicates which drive ...
Definition: format.cpp:256
FATFilesystem::runtimeCheck
static bool runtimeCheck()
Definition: format.cpp:633
Ext2Filesystem::label
QString label
Definition: format.h:365
Ext2Filesystem::processStdOut
virtual void processStdOut(K3Process *, char *b, int l)
Parse output.
Definition: format.cpp:876
FloppyAction::theProcess
K3Process * theProcess
Definition: format.h:234
DEBUGS
#define DEBUGS(a)
Definition: debug.h:43
FATFilesystem::newfs_fat
static QString newfs_fat
Definition: format.h:336
FloppyAction::deviceInfo
const fdinfo * deviceInfo
Configuration info (Pointer into list of "/dev/..." entries)
Definition: format.h:212
FloppyAction::FloppyAction
FloppyAction(QObject *parent=0L)
Definition: format.cpp:232
KFAction::exec
virtual void exec()
Exec() should return quickly to ensire that the GUI thread stays alive.
Definition: format.cpp:69
FATFilesystem::doVerify
bool doVerify
Definition: format.h:338
KFActionQueue::KFActionQueue
KFActionQueue(QObject *parent=0L)
Definition: format.cpp:80
FDFormat::FDFormat
FDFormat(QObject *parent=0L)
Definition: format.cpp:385
fdinfo::tracks
int tracks
Definition: format.h:165
FATFilesystem::exec
virtual void exec()
Exec() should return quickly to ensire that the GUI thread stays alive.
Definition: format.cpp:660
Ext2Filesystem::newfs
static QString newfs
Definition: format.h:362
FloppyAction
Concrete action for running a single external program.
Definition: format.h:174
Ext2Filesystem::runtimeCheck
static bool runtimeCheck()
Definition: format.cpp:812
Ext2Filesystem::configure
bool configure(bool verify, bool label, const QString &l)
Same args as FATFilesystem::configure.
Definition: format.cpp:821
FloppyAction::theProcessName
QString theProcessName
human-readable
Definition: format.h:235
Ext2Filesystem::exec
virtual void exec()
Exec() should return quickly to ensire that the GUI thread stays alive.
Definition: format.cpp:837
FATFilesystem::FATFilesystem
FATFilesystem(QObject *parent=0L)
Definition: format.cpp:624
KFAction
Abstract base class of actions to be undertaken.
Definition: format.h:66
DDZeroOut::exec
virtual void exec()
Exec() should return quickly to ensire that the GUI thread stays alive.
Definition: format.cpp:567
FDFormat::fdformatName
static QString fdformatName
path to executable.
Definition: format.h:276
FDFormat::exec
virtual void exec()
Exec() should return quickly to ensire that the GUI thread stays alive.
Definition: format.cpp:406
FDFormat::runtimeCheck
static bool runtimeCheck()
Concrete classes can provide a runtimeCheck function (heck, this is static, so the name is up to you)...
Definition: format.cpp:394
DDZeroOut::runtimeCheck
static bool runtimeCheck()
Concrete classes can provide a runtimeCheck function (heck, this is static, so the name is up to you)...
Definition: format.cpp:561
FDFormat::doVerify
bool doVerify
Definition: format.h:278
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:08:02 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kfloppy

Skip menu "kfloppy"
  • Main Page
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdeutils API Reference

Skip menu "kdeutils API Reference"
  • ark
  • filelight
  • kcalc
  • kcharselect
  • kdf
  • kfloppy
  • kgpg
  • kremotecontrol
  • ktimer
  • kwallet
  • superkaramba
  • sweeper

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal