summaryrefslogtreecommitdiffstats
path: root/kfloppy/format.cpp
blob: 35ed93335c27863e5bf7b36d2009eacb9bc821aa (plain)
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
/*

    This file is part of the KFloppy program, part of the KDE project
    
    Copyright (C) 2002 Adriaan de Groot <groot@kde.org>
    Copyright (C) 2004, 2005 Nicolas GOUTTE <goutte@kde.org>

    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, version 2.

    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 <unistd.h>
#include <stdlib.h>

#include <tqtimer.h>
#include <tqregexp.h>

#include <klocale.h>
#include <kprocess.h>
#include <kdebug.h>
#include <kstandarddirs.h>

#include "format.h"

static TQString extPath = TQString();

/* static */ TQString findExecutable(const TQString &e)
{
	if (extPath.isEmpty())
	{
		TQString path = getenv("PATH");
		if (!path.isEmpty()) path.append(":");
		path.append("/usr/sbin:/sbin");
		extPath = path;
	}

	return KGlobal::dirs()->findExe(e, extPath);
}



KFAction::KFAction(TQObject *parent) :
	TQObject(parent)
{
	DEBUGSETUP;
}

KFAction::~KFAction()
{
	DEBUGSETUP;
	quit();
}

/* slot */ void KFAction::quit()
{
	DEBUGSETUP;
}

/* slot */ void KFAction::exec()
{
	DEBUGSETUP;
}

class KFActionQueue_p
{
public:
	TQPtrList<KFAction> list;
} ;

KFActionQueue::KFActionQueue(TQObject *parent) :
	KFAction(parent),
	d(new KFActionQueue_p)
{
	DEBUGSETUP;
	d->list.setAutoDelete(true);
}

KFActionQueue::~KFActionQueue()
{
	DEBUGSETUP;
	delete d;
}

void KFActionQueue::queue(KFAction *p)
{
	DEBUGSETUP;

	d->list.append(p);
	DEBUGS(p->name());
}

/* virtual */ void KFActionQueue::exec()
{
	DEBUGSETUP;

	actionDone(0L,true);
}

/* slot */ void KFActionQueue::actionDone(KFAction *p,bool success)
{
	DEBUGSETUP;

	if (p)
	{
		if (d->list.first()==p)
		{
			d->list.removeFirst();
			// delete p;           /* auto-delete */
		}
		else
		{
			DEBUGS(  "Strange pointer received.");
			emit done(this,false);
			return;
		}
	}
	else
	{
		DEBUGS("Starting action queue.");
	}

	if (!success)
	{
		DEBUGS("Action failed.");
		emit done(this,false);
		return;
	}

	KFAction *next = d->list.first();
	if (!next)
	{
		emit done(this,true);
	}
	else
	{
		kdDebug(KFAREA) << "Running action " << next->name() << endl;
		TQObject::connect(next,TQT_SIGNAL(done(KFAction *,bool)),
			this,TQT_SLOT(actionDone(KFAction *,bool)));
		// Propagate signals
		TQObject::connect(next,TQT_SIGNAL(status(const TQString &,int)),
			this,TQT_SIGNAL(status(const TQString &,int)));
		TQTimer::singleShot(0,next,TQT_SLOT(exec()));
	}
}




// Here we have names of devices. The variable
// names are basically the linux device names,
// replace with whatever your OS needs instead.
//
//
#ifdef ANY_LINUX

const char *fd0H1440[] = { "/dev/fd0u1440", "/dev/floppy/0u1440", "/dev/fd0h1440", "/dev/fd0H1440", "/dev/fd0", 0L } ;
const char *fd0D720[] = { "/dev/fd0u720", "/dev/floppy/0u720", "/dev/fd0D720", "/dev/fd0h720", "/dev/fd0", 0L };
const char *fd0h1200[] = { "/dev/fd0h1200", "/dev/floppy/0h1200", "/dev/fd0", 0L };
const char *fd0h360[] = { "/dev/fd0u360", "/dev/floppy/0u360", "/dev/fd0h360", "/dev/fd0d360", "/dev/fd0", 0L };

const char *fd1H1440[] = { "/dev/fd1u1440", "/dev/floppy/1u1440","/dev/fd1h1440", "/dev/fd1H1440", "/dev/fd1", 0L } ;
const char *fd1D720[] = { "/dev/fd1u720", "/dev/floppy/1u720", "/dev/fd1D720", "/dev/fd1h720", "/dev/fd1", 0L };
const char *fd1h1200[] = { "/dev/fd1h1200", "/dev/floppy/1h1200", "/dev/fd1", 0L };
const char *fd1h360[] = { "/dev/fd1u360", "/dev/floppy/1u360","/dev/fd1h360", "/dev/fd1d360", "/dev/fd1", 0L };

const char *fd0auto[] = { "/dev/fd0", 0L };
const char *fd1auto[] = { "/dev/fd1", 0L };

#endif


#ifdef ANY_BSD
const char *fd0[] = { "/dev/fd0", 0L } ;
const char *fd1[] = { "/dev/fd1", 0L } ;
#endif

// Next we have a table of device names and characteristics.
// These are ordered according to 2*densityIndex+deviceIndex,
// ie. primary (0) 1440K (0) is first, then secondary (1) 1440K is
// second, down to secondary (1) 360k (4) in position 3*2+1=7.
//
//
// Note that the data originally contained in KFloppy was
// patently false, so most of this is fake. I guess noone ever
// formatted a 5.25" floppy.
//
// The flags field is unused in this implementation.
//
//
fdinfo fdtable[] =
{
#ifdef ANY_LINUX
        // device  drv blks trk flg
	{ fd0H1440, 0, 1440, 80, 0 },
	{ fd1H1440, 1, 1440, 80, 0 },
	{ fd0D720,  0,  720, 80, 0 },
	{ fd1D720,  1,  720, 80, 0 },
	{ fd0h1200, 0, 1200, 80, 0 },
	{ fd1h1200, 1, 1200, 80, 0 },
	{ fd0h360,  0,  360, 40, 0 },
	{ fd1h360,  1,  360, 40, 0 },
	{ fd0auto,  0,    0, 80, 0 },
	{ fd1auto,  1,    0, 80, 0 },
#endif

#ifdef ANY_BSD
	// Instead of the number of tracks, which is
	// unneeded, we record the
	// number of F's printed during an fdformat
	{ fd0, 0, 1440, 40, 0 },
	{ fd1, 1, 1440, 40, 0 },
	{ fd0, 0,  720, 40, 0 },
	{ fd1, 1,  720, 40, 0 },
	{ fd0, 0, 1200, 40, 0},
	{ fd1, 1, 1200, 40, 0},
	{ fd0, 0,  360, 40, 0},
	{ fd1, 1,  360, 40, 0},
#endif
	{ 0L, 0, 0, 0, 0 }
} ;


FloppyAction::FloppyAction(TQObject *p) :
	KFAction(p),
	deviceInfo(0L),
	theProcess(0L)
{
	DEBUGSETUP;
}

void FloppyAction::quit()
{
	DEBUGSETUP;
        delete theProcess;
        theProcess=0L;

	KFAction::quit();
}

bool FloppyAction::configureDevice( const TQString& newDeviceName )
{
    deviceInfo = 0; // We have not any idea what the device is
    deviceName = newDeviceName;
    return true; // No problem!
}

bool FloppyAction::configureDevice(int drive,int density)
{
	DEBUGSETUP;
	const char *devicename = 0L;

	deviceInfo=0L;
	deviceName = TQString();

	if ((drive<0) || (drive>1))
	{
		emit status(i18n("Unexpected drive number %1.").arg(drive),-1);
		return false;
	}

	fdinfo *deviceinfo = fdtable;
	for ( ; deviceinfo && (deviceinfo->devices) ; deviceinfo++)
	{
		if (deviceinfo->blocks != density)
			continue;
        }
	if (!deviceinfo)
	{
		emit status(i18n("Unexpected density number %1.").arg(density),-1);
		return false;
	}

	deviceinfo = fdtable;
	for ( ; deviceinfo && (deviceinfo->devices) ; deviceinfo++)
	{
		if (deviceinfo->blocks != density)
			continue;
		if (deviceinfo->drive == drive)
			break;
	}

	if (!deviceinfo || !deviceinfo->devices)
	{
		emit status(i18n("Cannot find a device for drive %1 and density %2.")
			.arg(drive).arg(density),-1);
		return false;
	}

	for (const char **devices=deviceinfo->devices ;
		*devices ; devices++)
	{
		if (access(*devices,W_OK)>=0)
		{
			kdDebug(KFAREA) << "Found device " << *devices << endl;
			devicename=*devices;
			break;
		}
	}

	if (!devicename)
	{
		const TQString str = i18n(
			"Cannot access %1\nMake sure that the device exists and that "
			"you have write permission to it.").arg(deviceinfo->devices[0]);
		emit status(str,-1);
		return false;
	}

	deviceName = devicename;
	deviceInfo = deviceinfo;

	return true;
}

void FloppyAction::processDone(KProcess *p)
{
	DEBUGSETUP;

	if (p!=theProcess)
	{
		DEBUGS("  Strange process exited.");
		return;
	}

	if (p->normalExit())
	{
	        if (p->exitStatus() == 0)
	        {
			emit status(TQString(),100);
			emit done(this,true);
		}
		else
		{
			emit status(i18n("The program %1 terminated with an error.").arg(theProcessName),100);
			emit done(this,false);
		}
	}
	else
	{
		emit status(i18n("The program %1 terminated abnormally.").arg(theProcessName),100);
		emit done(this,false);
	}
}

void FloppyAction::processStdOut(KProcess *, char *b, int l)
{
	Q_UNUSED(b);
	Q_UNUSED(l);
	kdDebug(KFAREA) << "stdout:" << TQString::fromLatin1(b,l) << endl;
}

void FloppyAction::processStdErr(KProcess *p, char *b, int l)
{
	processStdOut(p,b,l);
}

bool FloppyAction::startProcess()
{
	DEBUGSETUP;

	connect(theProcess,TQT_SIGNAL(processExited(KProcess *)),
		this,TQT_SLOT(processDone(KProcess *)));
	connect(theProcess,TQT_SIGNAL(receivedStdout(KProcess *,char *,int)),
		this,TQT_SLOT(processStdOut(KProcess *,char *,int)));
	connect(theProcess,TQT_SIGNAL(receivedStderr(KProcess *,char *,int)),
		this,TQT_SLOT(processStdErr(KProcess *,char *,int)));

        theProcess->setEnvironment( "LC_ALL", "C" ); // We need the untranslated output of the tool
	return theProcess->start(KProcess::NotifyOnExit,
		KProcess::AllOutput);
}


/* static */ TQString FDFormat::fdformatName = TQString();

FDFormat::FDFormat(TQObject *p) :
	FloppyAction(p),
	doVerify(true)
{
	DEBUGSETUP;
	theProcessName = TQString::fromLatin1("fdformat");
	setName("FDFormat");
}

/* static */ bool FDFormat::runtimeCheck()
{
	fdformatName = findExecutable("fdformat");
	return (!fdformatName.isEmpty());
}

bool FDFormat::configure(bool v)
{
	doVerify=v;
	return true;
}

/* virtual */ void FDFormat::exec()
{
	DEBUGSETUP;

	if ( !deviceInfo || deviceName.isEmpty() )
	{
                emit status( i18n("Internal error: device not correctly defined."), -1 );
		emit done(this,false);
		return;
	}

	if (fdformatName.isEmpty())
	{
		emit status(i18n("Cannot find fdformat."),-1);
		emit done(this,false);
		return;
	}

	if (theProcess) delete theProcess;
	theProcess = new KProcess;

	formatTrackCount=0;

	*theProcess << fdformatName ;

	// Common to Linux and BSD, others may differ
	if (!doVerify)
	{
		*theProcess << "-n";
	}

#ifdef ANY_BSD
	*theProcess
		<< "-y"
		<< "-f"
		<< TQString::number(deviceInfo->blocks) ;
#elif defined(ANY_LINUX)
	// No Linux-specific flags
#endif

	// Common to Linux and BSD, others may differ
	*theProcess << deviceName;

	if (!startProcess())
	{
		emit status(i18n("Could not start fdformat."),-1);
		emit done(this,false);
	}

	// Now depend on fdformat running and producing output.
}

// Parse some output from the fdformat process. Lots of
// #ifdefs here to account for variations in the basic
// fdformat. Uses gotos to branch to whatever error message we
// need, since the messages can be standardized across OSsen.
//
//
void FDFormat::processStdOut(KProcess *, char *b, int l)
{
	DEBUGSETUP;
	TQString s;

#ifdef ANY_BSD
	if (b[0]=='F')
	{
		formatTrackCount++;
		emit status(TQString(),
			formatTrackCount * 100 / deviceInfo->tracks);
	}
	else if (b[0]=='E')
	{
		emit status(i18n("Error formatting track %1.").arg(formatTrackCount),-1);
	}
	else
	{
		s = TQString::fromLatin1(b,l);
		if (s.contains("ioctl(FD_FORM)"))
		{
                    emit status (i18n(
                            "Cannot access floppy or floppy drive.\n"
                            "Please insert a floppy and make sure that you "
                            "have selected a valid floppy drive."),-1);
                    return;
		}
		if (s.find("/dev/")>=0)
		{
			emit status(s,-1);
			return;
		}
		DEBUGS(s);
	}
#elif defined(ANY_LINUX)
	s = TQString::fromLatin1(b,l);
	DEBUGS(s);
        TQRegExp regexp( "([0-9]+)" );
        if ( s.startsWith( "bad data at cyl" ) || ( s.find( "Problem reading cylinder" ) != -1 ) )
        {
            if ( regexp.search( s ) > -1 )
            {
                const int track = regexp.cap(1).toInt();
                emit status(i18n("Low-level formatting error at track %1.").arg(track), -1);
            }
            else
            {
                // This error should not happen
                emit status(i18n("Low-level formatting error: %1").arg(s), -1);
            }
            return;
        }
	else if (s.find("ioctl(FDFMTBEG)")!=-1)
	{
            emit status (i18n(
                    "Cannot access floppy or floppy drive.\n"
                    "Please insert a floppy and make sure that you "
                    "have selected a valid floppy drive."),-1);
            return;
	}
        else if (s.find("busy")!=-1) // "Device or resource busy"
        {
            emit status(i18n("Device busy.\nPerhaps you need to unmount the floppy first."),-1);
            return;
        }
        // Be careful to leave "iotcl" as last before checking numbers
        else if (s.find("ioctl")!=-1)
        {
            emit status(i18n("Low-level format error: %1").arg(s),-1);
            return;
        }
        // Check for numbers at last (as /dev/fd0u1440 has numbers too)
        else if ( regexp.search(s) > -1 )
        {
            // Normal track number (formatting or verifying)
            const int p = regexp.cap(1).toInt();
            if ((p>=0) && (p<deviceInfo->tracks))
            {
                    emit status(TQString(),
                            p * 100 / deviceInfo->tracks);
            }
        }
#endif
	return;
}


/* static */ TQString DDZeroOut::m_ddName = TQString();

DDZeroOut::DDZeroOut(TQObject *p) :
    FloppyAction(p)
{
    kdDebug(KFAREA) << (__PRETTY_FUNCTION__) << endl;
    theProcessName = TQString::fromLatin1("dd");
    setName("DD");
}

/* static */ bool DDZeroOut::runtimeCheck()
{
    m_ddName = findExecutable("dd");
    return (!m_ddName.isEmpty());
}

/* virtual */ void DDZeroOut::exec()
{
    kdDebug(KFAREA) << (__PRETTY_FUNCTION__) << endl;

    if ( deviceName.isEmpty() )
    {
        emit status( i18n("Internal error: device not correctly defined."), -1 );
        emit done( this, false );
        return;
    }

    if ( m_ddName.isEmpty() )
    {
        emit status( i18n("Cannot find dd."), -1 );
        emit done( this, false );
        return;
    }

    delete theProcess;
    theProcess = new KProcess;

    *theProcess << m_ddName ;

    *theProcess << "if=/dev/zero" ;
    *theProcess << "of="+deviceName;

    if ( !startProcess() )
    {
            emit status( i18n("Could not start dd."), -1 );
            emit done( this, false );
    }

}

void DDZeroOut::processDone(KProcess *p)
{
    kdDebug(KFAREA) << (__PRETTY_FUNCTION__) << endl;

    if (p!=theProcess)
    {
            kdDebug(KFAREA) << "Strange process exited." << endl;
            return;
    }

    /**
     * As we do not give a number of blocks to dd(1), it will stop
     * with the error "No space left on device"
     *
     * ### TODO: really check if the exit is not on an other error and then abort the formatting
     */
    emit status(TQString(),100);
    emit done(this,true);
}



/* static */ TQString FATFilesystem::newfs_fat = TQString() ;

FATFilesystem::FATFilesystem(TQObject *parent) :
	FloppyAction(parent)
{
	DEBUGSETUP;
	runtimeCheck();
	theProcessName=newfs_fat;
	setName("FATFilesystem");
}

/* static */ bool FATFilesystem::runtimeCheck()
{
	DEBUGSETUP;

#ifdef ANY_BSD
	newfs_fat = findExecutable("newfs_msdos");
#elif defined(ANY_LINUX)
	newfs_fat = findExecutable("mkdosfs");
#else
	return false;
#endif

	return !newfs_fat.isEmpty();
}

bool FATFilesystem::configure(bool v,bool l,const TQString &lbl)
{
	doVerify=v;
	doLabel=l;
	if (l)
		label=lbl.simplifyWhiteSpace();
	else
		label=TQString();

	return true;
}

void FATFilesystem::exec()
{
	DEBUGSETUP;

        
	if (
#ifdef ANY_BSD // BSD needs the deviceInfo for the block count
            !deviceInfo ||
#endif
            deviceName.isEmpty())
	{
                emit status( i18n("Internal error: device not correctly defined."), -1 );
		emit done(this,false);
		return;
	}

	if (newfs_fat.isEmpty())
	{
		emit status(i18n("Cannot find a program to create FAT filesystems."),-1);
		emit done(this,false);
		return;
	}

	if (theProcess) delete theProcess;
	KProcess *p = theProcess = new KProcess;

	*p << newfs_fat;
#ifdef ANY_BSD
	*p << "-f" << TQString::number(deviceInfo->blocks);
	if (doLabel)
	{
		*p << "-L" << label ;
	}
#else
#ifdef ANY_LINUX
	if (doLabel)
	{
		*p << "-n" << label ;
	}
	if (doVerify)
	{
		*p << "-c";
	}
#endif
#endif
	*p << deviceName ;

	if (!startProcess())
	{
		emit status(i18n("Cannot start FAT format program."),-1);
		emit done(this,false);
	}
}

void FATFilesystem::processStdOut(KProcess *, char *b, int l)
{
#ifdef ANY_BSD
    // ### TODO: do some checks
#elif defined(ANY_LINUX)
    TQString s ( TQString::fromLatin1( b, l ) );
    kdDebug(KFAREA) << s << endl;
    if (s.find("mounted file system")!=-1) // "/dev/fd0 contains a mounted file system
    {
        emit status(i18n("Floppy is mounted.\nYou need to unmount the floppy first."),-1);
        return;
    }
    else if (s.find("busy")!=-1) // "Device or resource busy"
    {
        emit status(i18n("Device busy.\nPerhaps you need to unmount the floppy first."),-1);
        return;
    }
# if 0
    else if ( s.find( "mkdosfs" ) != -1 ) // DEBUG: get the program header and show it!
    {
        emit status( s, -1 );
        return;
    }
# endif
#endif
}




#ifdef ANY_BSD

/* static */ TQString UFSFilesystem::newfs = TQString() ;

UFSFilesystem::UFSFilesystem(TQObject *parent) :
	FloppyAction(parent)
{
	DEBUGSETUP;
	runtimeCheck();
	theProcessName=newfs;
	setName("UFSFilesystem");
}

/* static */ bool UFSFilesystem::runtimeCheck()
{
	DEBUGSETUP;

	newfs = findExecutable("newfs");

	return !newfs.isEmpty();
}

void UFSFilesystem::exec()
{
	DEBUGSETUP;

	if ( deviceName.isEmpty() )
	{
                emit status( i18n("Internal error: device not correctly defined."), -1 );
		emit done(this,false);
		return;
	}

	if (newfs.isEmpty())
	{
		emit status(i18n("BSD", "Cannot find a program to create UFS filesystems."),-1);
		emit done(this,false);
		return;
	}

	if (theProcess) delete theProcess;
	KProcess *p = theProcess = new KProcess;

	*p << newfs;
        
        // ### TODO: is it still needed? (FreeBSD 5.3's man page says: "For backward compatibility.")
        if ( deviceInfo )
           *p << "-T" << TQString("fd%1").arg(deviceInfo->blocks);

        *p << deviceName;

	if (!startProcess())
	{
		emit status(i18n("BSD", "Cannot start UFS format program."),-1);
		emit done(this,false);
	}
}
#endif



/* static */ TQString Ext2Filesystem::newfs = TQString() ;

Ext2Filesystem::Ext2Filesystem(TQObject *parent) :
	FloppyAction(parent)
{
	DEBUGSETUP;
	runtimeCheck();
	theProcessName="mke2fs";
	setName("Ext2Filesystem");
}

/* static */ bool Ext2Filesystem::runtimeCheck()
{
	DEBUGSETUP;

	newfs = findExecutable("mke2fs");

	return !newfs.isEmpty();
}

bool Ext2Filesystem::configure(bool v,bool l,const TQString &lbl)
{
	doVerify=v;
	doLabel=l;
	if (l)
	{
		label=lbl.stripWhiteSpace();
	}
	else
	{
		label=TQString();
	}

	return true;
}

void Ext2Filesystem::exec()
{
	DEBUGSETUP;

	if (
#ifdef ANY_BSD // BSD needs the deviceInfo for the block count
            !deviceInfo ||
#endif
            deviceName.isEmpty() )
	{
                emit status( i18n("Internal error: device not correctly defined."), -1 );
		emit done(this,false);
		return;
	}

	if (newfs.isEmpty())
	{
		emit status(i18n("Cannot find a program to create ext2 filesystems."),-1);
		emit done(this,false);
		return;
	}

	if (theProcess) delete theProcess;
	KProcess *p = theProcess = new KProcess;

	*p << newfs;
	*p << "-q";
	if (doVerify) *p << "-c" ;
	if (doLabel) *p << "-L" << label ;

	*p << deviceName ;

	if (!startProcess())
	{
		emit status(i18n("Cannot start ext2 format program."),-1);
		emit done(this,false);
	}
}

void Ext2Filesystem::processStdOut(KProcess *, char *b, int l)
{
#ifdef ANY_BSD
    // ### TODO: do some checks
#elif defined(ANY_LINUX)
    TQString s ( TQString::fromLatin1( b, l ) );
    kdDebug(KFAREA) << s << endl;
    if (s.find("mounted")!=-1) // "/dev/fd0 is mounted; will not make a filesystem here!"
    {
        emit status(i18n("Floppy is mounted.\nYou need to unmount the floppy first."),-1);
        return;
    }
    else if (s.find("busy")!=-1) // "Device or resource busy"
    {
        emit status(i18n("Device busy.\nPerhaps you need to unmount the floppy first."),-1);
        return;
    }
#endif
}



#ifdef ANY_LINUX
/* static */ TQString MinixFilesystem::newfs = TQString() ;

MinixFilesystem::MinixFilesystem(TQObject *parent) :
	FloppyAction(parent)
{
	DEBUGSETUP;
	runtimeCheck();
	theProcessName="mkfs.minix";
	setName("Minix2Filesystem");
}

/* static */ bool MinixFilesystem::runtimeCheck()
{
	DEBUGSETUP;

	newfs = findExecutable("mkfs.minix");

	return !newfs.isEmpty();
}

bool MinixFilesystem::configure(bool v,bool l,const TQString &lbl)
{
	doVerify=v;
	doLabel=l;
	if (l)
	{
		label=lbl.stripWhiteSpace();
	}
	else
	{
		label=TQString();
	}

	return true;
}

void MinixFilesystem::exec()
{
	DEBUGSETUP;

	if ( deviceName.isEmpty() )
	{
                emit status( i18n("Internal error: device not correctly defined."), -1 );
		emit done(this,false);
		return;
	}

	if (newfs.isEmpty())
	{
		emit status(i18n("Cannot find a program to create Minix filesystems."),-1);
		emit done(this,false);
		return;
	}

	if (theProcess) delete theProcess;
	KProcess *p = theProcess = new KProcess;

	*p << newfs;

        // Labeling is not possible
	if (doVerify) *p << "-c" ;

	*p << deviceName ;

	if (!startProcess())
	{
		emit status(i18n("Cannot start Minix format program."),-1);
		emit done(this,false);
	}
}

void MinixFilesystem::processStdOut(KProcess *, char *b, int l)
{
    TQString s ( TQString::fromLatin1( b, l ) );
    kdDebug(KFAREA) << s << endl;
    if (s.find("mounted")!=-1) // "mkfs.minix: /dev/fd0 is mounted; will not make a filesystem here!"
    {
        emit status(i18n("Floppy is mounted.\nYou need to unmount the floppy first."),-1);
        return;
    }
    else if (s.find("busy")!=-1) // "Device or resource busy"
    {
        emit status(i18n("Device busy.\nPerhaps you need to unmount the floppy first."),-1);
        return;
    }
}

#endif

#include "format.moc"