Make a branch to make krunner Good Enough For Aaron™.
[kdebase/uwolfer.git] / runtime / kioslave / floppy / kio_floppy.cpp
blob402cacf4cf4da0371538957028f11dac47319be5
1 /* This file is part of the KDE project
3 Copyright (C) 2000 Alexander Neundorf <neundorf@kde.org>
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
21 #include "kio_floppy.h"
23 #include <config-runtime.h>
25 #ifdef HAVE_STRING_H
26 #include <string.h>
27 #else
28 #include <strings.h>
29 #endif
31 #include <errno.h>
32 #include <stdlib.h>
33 #include <unistd.h>
34 #include <sys/stat.h>
35 #include <time.h>
36 #include <sys/time.h>
37 #include <sys/types.h>
39 #include <QByteArray>
40 #include <QTextStream>
41 #include <QDateTime>
42 #include <QFile>
44 #include <kcomponentdata.h>
45 #include <kdebug.h>
46 #include <kio/global.h>
47 #include <klocale.h>
49 using namespace KIO;
51 extern "C" { KDE_EXPORT int kdemain(int argc, char **argv); }
53 int kdemain( int argc, char **argv )
55 KComponentData componentData( "kio_floppy" );
57 if (argc != 4)
59 fprintf(stderr, "Usage: kio_floppy protocol domain-socket1 domain-socket2\n");
60 exit(-1);
62 kDebug(7101) << "Floppy: kdemain: starting";
64 FloppyProtocol slave(argv[2], argv[3]);
65 slave.dispatchLoop();
66 return 0;
69 void getDriveAndPath(const QString& path, QString& drive, QString& rest)
71 drive=QString();
72 rest=QString();
73 QStringList list=path.split("/");
74 for (QStringList::Iterator it=list.begin(); it!=list.end(); ++it)
76 if (it==list.begin())
77 drive=(*it)+':';
78 else
79 rest=rest+'/'+(*it);
83 FloppyProtocol::FloppyProtocol (const QByteArray &pool, const QByteArray &app )
84 :SlaveBase( "floppy", pool, app )
85 ,m_mtool(0)
86 ,m_stdoutBuffer(0)
87 ,m_stderrBuffer(0)
88 ,m_stdoutSize(0)
89 ,m_stderrSize(0)
91 kDebug(7101)<<"Floppy::Floppy: -"<<pool<<"-";
94 FloppyProtocol::~FloppyProtocol()
96 delete [] m_stdoutBuffer;
97 delete [] m_stderrBuffer;
98 delete m_mtool;
99 m_mtool=0;
100 m_stdoutBuffer=0;
101 m_stderrBuffer=0;
104 int FloppyProtocol::readStdout()
106 //kDebug(7101)<<"Floppy::readStdout";
107 if (m_mtool==0) return 0;
109 char buffer[16*1024];
110 int length=::read(m_mtool->stdoutFD(),buffer,16*1024);
111 if (length<=0) return 0;
113 //+1 gives us room for a terminating 0
114 char *newBuffer=new char[length+m_stdoutSize+1];
115 kDebug(7101)<<"Floppy::readStdout(): length: "<<length<<" m_tsdoutSize: "<<m_stdoutSize<<" +1="<<length+m_stdoutSize+1;
116 if (m_stdoutBuffer!=0)
118 memcpy(newBuffer, m_stdoutBuffer, m_stdoutSize);
120 memcpy(newBuffer+m_stdoutSize, buffer, length);
121 m_stdoutSize+=length;
122 newBuffer[m_stdoutSize]='\0';
124 delete [] m_stdoutBuffer;
125 m_stdoutBuffer=newBuffer;
126 //kDebug(7101)<<"Floppy::readStdout(): -"<<m_stdoutBuffer<<"-";
128 //kDebug(7101)<<"Floppy::readStdout ends";
129 return length;
132 int FloppyProtocol::readStderr()
134 //kDebug(7101)<<"Floppy::readStderr";
135 if (m_mtool==0) return 0;
137 /*struct timeval tv;
138 tv.tv_sec=0;
139 tv.tv_usec=1000*300;
140 ::select(0,0,0,0,&tv);*/
142 char buffer[16*1024];
143 int length=::read(m_mtool->stderrFD(),buffer,16*1024);
144 kDebug(7101)<<"Floppy::readStderr(): read "<<length<<" bytes";
145 if (length<=0) return 0;
147 //+1 gives us room for a terminating 0
148 char *newBuffer=new char[length+m_stderrSize+1];
149 memcpy(newBuffer, m_stderrBuffer, m_stderrSize);
150 memcpy(newBuffer+m_stderrSize, buffer, length);
151 m_stderrSize+=length;
152 newBuffer[m_stderrSize]='\0';
153 delete [] m_stderrBuffer;
154 m_stderrBuffer=newBuffer;
155 kDebug(7101)<<"Floppy::readStderr(): -"<<m_stderrBuffer<<"-";
157 return length;
160 void FloppyProtocol::clearBuffers()
162 kDebug(7101)<<"Floppy::clearBuffers()";
163 m_stdoutSize=0;
164 m_stderrSize=0;
165 delete [] m_stdoutBuffer;
166 m_stdoutBuffer=0;
167 delete [] m_stderrBuffer;
168 m_stderrBuffer=0;
169 //kDebug(7101)<<"Floppy::clearBuffers() ends";
172 void FloppyProtocol::terminateBuffers()
174 //kDebug(7101)<<"Floppy::terminateBuffers()";
175 //append a terminating 0 to be sure
176 if (m_stdoutBuffer!=0)
178 m_stdoutBuffer[m_stdoutSize]='\0';
180 if (m_stderrBuffer!=0)
182 m_stderrBuffer[m_stderrSize]='\0';
184 //kDebug(7101)<<"Floppy::terminateBuffers() ends";
187 bool FloppyProtocol::stopAfterError(const KUrl& url, const QString& drive)
189 if (m_stderrSize==0)
190 return true;
191 //m_stderrBuffer[m_stderrSize]='\0';
193 QString outputString(m_stderrBuffer);
194 QTextStream output(&outputString, QIODevice::ReadOnly);
195 QString line=output.readLine();
196 kDebug(7101)<<"line: -"<<line<<"-";
197 if (line.indexOf("resource busy") > -1)
199 error( KIO::ERR_SLAVE_DEFINED, i18n("Could not access drive %1.\nThe drive is still busy.\nWait until it is inactive and then try again.", drive));
201 else if ((line.indexOf("Disk full") > -1) || (line.indexOf("No free cluster") > -1))
203 error( KIO::ERR_SLAVE_DEFINED, i18n("Could not write to file %1.\nThe disk in drive %2 is probably full.", url.prettyUrl(), drive));
205 //file not found
206 else if (line.indexOf("not found") > -1)
208 error( KIO::ERR_DOES_NOT_EXIST, url.prettyUrl());
210 //no disk
211 else if (line.indexOf("not configured") > -1)
213 error( KIO::ERR_SLAVE_DEFINED, i18n("Could not access %1.\nThere is probably no disk in the drive %2", url.prettyUrl(), drive));
215 else if (line.indexOf("No such device") > -1)
217 error( KIO::ERR_SLAVE_DEFINED, i18n("Could not access %1.\nThere is probably no disk in the drive %2 or you do not have enough permissions to access the drive.", url.prettyUrl(), drive));
219 else if (line.indexOf("not supported") > -1)
221 error( KIO::ERR_SLAVE_DEFINED, i18n("Could not access %1.\nThe drive %2 is not supported.", url.prettyUrl(), drive));
223 //not supported or no such drive
224 else if (line.indexOf("Permission denied") > -1)
226 error( KIO::ERR_SLAVE_DEFINED, i18n("Could not access %1.\nMake sure the floppy in drive %2 is a DOS-formatted floppy disk \nand that the permissions of the device file (e.g. /dev/fd0) are set correctly (e.g. rwxrwxrwx).", url.prettyUrl(), drive));
228 else if (line.indexOf("non DOS media") > -1)
230 error( KIO::ERR_SLAVE_DEFINED, i18n("Could not access %1.\nThe disk in drive %2 is probably not a DOS-formatted floppy disk.", url.prettyUrl(), drive));
232 else if (line.indexOf("Read-only") > -1)
234 error( KIO::ERR_SLAVE_DEFINED, i18n("Access denied.\nCould not write to %1.\nThe disk in drive %2 is probably write-protected.", url.prettyUrl(), drive));
236 else if ((outputString.indexOf("already exists") > -1) || (outputString.indexOf("Skipping ") > -1))
238 error( KIO::ERR_FILE_ALREADY_EXIST,url.prettyUrl());
239 //return false;
241 else if (outputString.indexOf("could not read boot sector") > -1)
243 error( KIO::ERR_SLAVE_DEFINED, i18n("Could not read boot sector for %1.\nThere is probably not any disk in drive %2.", url.prettyUrl(), drive));
244 //return false;
246 else
248 error( KIO::ERR_UNKNOWN, outputString);
250 return true;
253 void FloppyProtocol::listDir( const KUrl& _url)
255 kDebug(7101)<<"Floppy::listDir() "<<_url.path();
256 KUrl url(_url);
257 QString path(url.path());
259 if ((path.isEmpty()) || (path=="/"))
261 url.setPath("/a/");
262 redirection(url);
263 finished();
264 return;
266 QString drive;
267 QString floppyPath;
268 getDriveAndPath(path,drive,floppyPath);
270 QStringList args;
272 args<<"mdir"<<"-a"<<(drive+floppyPath);
273 if (m_mtool!=0)
274 delete m_mtool;
275 m_mtool=new Program(args);
277 clearBuffers();
279 if (!m_mtool->start())
281 delete m_mtool;
282 m_mtool=0;
283 errorMissingMToolsProgram("mdir");
284 return;
287 int result;
288 bool loopFinished(false);
289 bool errorOccured(false);
292 bool stdoutEvent;
293 bool stderrEvent;
294 result=m_mtool->select(1,0,stdoutEvent, stderrEvent);
295 if (stdoutEvent)
296 if (readStdout()==0)
297 loopFinished=true;
298 if (stderrEvent)
300 if (readStderr()==0)
301 loopFinished=true;
302 else
303 if (stopAfterError(url,drive))
305 loopFinished=true;
306 errorOccured=true;
309 } while (!loopFinished);
311 delete m_mtool;
312 m_mtool=0;
313 //now mdir has finished
314 //let's parse the output
315 terminateBuffers();
317 if (errorOccured)
318 return;
320 QString outputString(m_stdoutBuffer);
321 QTextStream output(&outputString, QIODevice::ReadOnly);
322 QString line;
324 int totalNumber(0);
325 int mode(0);
326 UDSEntry entry;
328 while (!output.atEnd())
330 line=output.readLine();
331 kDebug(7101)<<"Floppy::listDir(): line: -"<<line<<"- length: "<<line.length();
333 if (mode==0)
335 if (line.isEmpty())
337 kDebug(7101)<<"Floppy::listDir(): switching to mode 1";
338 mode=1;
341 else if (mode==1)
343 if (line[0]==' ')
345 kDebug(7101)<<"Floppy::listDir(): ende";
346 totalSize(totalNumber);
347 break;
349 entry.clear();
350 StatInfo info=createStatInfo(line);
351 if (info.isValid)
353 createUDSEntry(info,entry);
354 //kDebug(7101)<<"Floppy::listDir(): creating UDSEntry";
355 listEntry( entry, false);
356 totalNumber++;
360 listEntry( entry, true ); // ready
361 finished();
362 //kDebug(7101)<<"Floppy::listDir() ends";
365 void FloppyProtocol::errorMissingMToolsProgram(const QString& name)
367 error(KIO::ERR_SLAVE_DEFINED,i18n("Could not start program \"%1\".\nEnsure that the mtools package is installed correctly on your system.", name));
370 void FloppyProtocol::createUDSEntry(const StatInfo& info, UDSEntry& entry)
372 entry.insert( KIO::UDSEntry::UDS_NAME, info.name );
373 entry.insert( KIO::UDSEntry::UDS_SIZE, info.size );
374 entry.insert( KIO::UDSEntry::UDS_MODIFICATION_TIME, info.time );
375 entry.insert( KIO::UDSEntry::UDS_ACCESS, info.mode );
376 entry.insert( KIO::UDSEntry::UDS_FILE_TYPE, info.isDir?S_IFDIR:S_IFREG );
379 StatInfo FloppyProtocol::createStatInfo(const QString line, bool makeStat, const QString& dirName)
381 //kDebug(7101)<<"Floppy::createUDSEntry()";
382 QString name;
383 QString size;
384 bool isDir(false);
385 QString day,month, year;
386 QString hour, minute;
387 StatInfo info;
389 if (line.length()==41)
391 int nameLength=line.indexOf(' ');
392 kDebug(7101)<<"Floppy::createStatInfo: line find: "<<nameLength <<"= -"<<line<<"-";
393 if (nameLength>0)
395 name=line.mid(0,nameLength);
396 QString ext=line.mid(9,3);
397 ext=ext.trimmed();
398 if (!ext.isEmpty())
399 name+='.'+ext;
401 kDebug(7101)<<"Floppy::createStatInfo() name 8.3= -"<<name<<"-";
403 else if (line.length()>41)
405 name=line.mid(42);
406 kDebug(7101)<<"Floppy::createStatInfo() name vfat: -"<<name<<"-";
408 if ((name==".") || (name==".."))
410 if (makeStat)
411 name=dirName;
412 else
414 info.isValid=false;
415 return info;
419 if (line.mid(13,5)=="<DIR>")
421 //kDebug(7101)<<"Floppy::createUDSEntry() isDir";
422 size="1024";
423 isDir=true;
425 else
427 size=line.mid(13,9);
428 //kDebug(7101)<<"Floppy::createUDSEntry() size: -"<<size<<"-";
431 //TEEKANNE JPG 70796 01-02-2003 17:47 Teekanne.jpg
432 if (line[25]=='-')
434 month=line.mid(23,2);
435 day=line.mid(26,2);
436 year=line.mid(29,4);
438 else //SETUP PKG 1019 1997-09-25 10:31 setup.pkg
440 year=line.mid(23,4);
441 month=line.mid(28,2);
442 day=line.mid(31,2);
444 hour=line.mid(35,2);
445 minute=line.mid(38,2);
446 //kDebug(7101)<<"Floppy::createUDSEntry() day: -"<<day<<"-"<<month<<"-"<<year<<"- -"<<hour<<"-"<<minute<<"-";
448 if (name.isEmpty())
450 info.isValid=false;
451 return info;
454 info.name=name;
455 info.size=size.toInt();
457 QDateTime date(QDate(year.toInt(),month.toInt(),day.toInt()),QTime(hour.toInt(),minute.toInt()));
458 info.time=date.toTime_t();
460 if (isDir)
461 info.mode = S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH| S_IWOTH|S_IWGRP|S_IWUSR ;
462 else
463 info.mode = S_IRUSR | S_IRGRP | S_IROTH| S_IWOTH|S_IWGRP|S_IWUSR;
465 info.isDir=isDir;
467 info.isValid=true;
468 //kDebug(7101)<<"Floppy::createUDSEntry() ends";
469 return info;
472 StatInfo FloppyProtocol::_stat(const KUrl& url)
474 StatInfo info;
476 QString path(url.path());
477 QString drive;
478 QString floppyPath;
479 getDriveAndPath(path,drive,floppyPath);
481 if (floppyPath.isEmpty())
483 kDebug(7101)<<"Floppy::_stat(): floppyPath.isEmpty()";
484 info.name=path;
485 info.size=1024;
486 info.time=0;
487 info.mode=S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH| S_IWOTH|S_IWGRP|S_IWUSR;
488 info.isDir=true;
489 info.isValid=true;
491 return info;
494 //kDebug(7101)<<"Floppy::_stat(): delete m_mtool";
495 if (m_mtool!=0)
496 delete m_mtool;
498 QStringList args;
499 args<<"mdir"<<"-a"<<(drive+floppyPath);
501 //kDebug(7101)<<"Floppy::_stat(): create m_mtool";
502 m_mtool=new Program(args);
504 if (!m_mtool->start())
506 delete m_mtool;
507 m_mtool=0;
508 errorMissingMToolsProgram("mdir");
509 return info;
513 clearBuffers();
515 int result;
516 bool loopFinished(false);
517 bool errorOccured(false);
520 bool stdoutEvent;
521 bool stderrEvent;
522 result=m_mtool->select(1,0,stdoutEvent, stderrEvent);
523 if (stdoutEvent)
524 if (readStdout()==0)
525 loopFinished=true;
526 if (stderrEvent)
528 if (readStderr()==0)
529 loopFinished=true;
530 else
531 if (stopAfterError(url,drive))
533 loopFinished=true;
534 errorOccured=true;
537 } while (!loopFinished);
539 //kDebug(7101)<<"Floppy::_stat(): delete m_mtool";
540 delete m_mtool;
541 m_mtool=0;
542 //now mdir has finished
543 //let's parse the output
544 terminateBuffers();
546 if (errorOccured)
548 info.isValid=false;
549 return info;
552 if (m_stdoutSize==0)
554 info.isValid=false;
555 error( KIO::ERR_COULD_NOT_STAT, url.prettyUrl());
556 return info;
559 kDebug(7101)<<"Floppy::_stat(): parse stuff";
560 QString outputString(m_stdoutBuffer);
561 QTextStream output(&outputString, QIODevice::ReadOnly);
562 QString line;
563 for (int lineNumber=0; !output.atEnd(); lineNumber++)
565 line=output.readLine();
566 if ( (lineNumber<3) || (line.isEmpty()) )
567 continue;
568 StatInfo info=createStatInfo(line,true,url.fileName());
569 if (info.isValid==false)
570 error( KIO::ERR_COULD_NOT_STAT, url.prettyUrl());
571 return info;
573 if (info.isValid==false)
574 error( KIO::ERR_COULD_NOT_STAT, url.prettyUrl());
575 return info;
578 int FloppyProtocol::freeSpace(const KUrl& url)
580 QString path(url.path());
581 QString drive;
582 QString floppyPath;
583 getDriveAndPath(path,drive,floppyPath);
585 //kDebug(7101)<<"Floppy::freeSpace(): delete m_mtool";
586 if (m_mtool!=0)
587 delete m_mtool;
589 QStringList args;
590 args<<"mdir"<<"-a"<<drive;
592 //kDebug(7101)<<"Floppy::freeSpace(): create m_mtool";
593 m_mtool=new Program(args);
595 if (!m_mtool->start())
597 delete m_mtool;
598 m_mtool=0;
599 errorMissingMToolsProgram("mdir");
600 return -1;
604 clearBuffers();
606 int result;
607 bool loopFinished(false);
608 bool errorOccured(false);
611 bool stdoutEvent;
612 bool stderrEvent;
613 result=m_mtool->select(1,0,stdoutEvent, stderrEvent);
614 if (stdoutEvent)
615 if (readStdout()==0)
616 loopFinished=true;
617 if (stderrEvent)
619 if (readStderr()==0)
620 loopFinished=true;
621 else
622 if (stopAfterError(url,drive))
624 loopFinished=true;
625 errorOccured=true;
628 } while (!loopFinished);
630 //kDebug(7101)<<"Floppy::freeSpace(): delete m_mtool";
631 delete m_mtool;
632 m_mtool=0;
633 //now mdir has finished
634 //let's parse the output
635 terminateBuffers();
637 if (errorOccured)
639 return -1;
642 if (m_stdoutSize==0)
644 error( KIO::ERR_COULD_NOT_STAT, url.prettyUrl());
645 return -1;
648 kDebug(7101)<<"Floppy::freeSpace(): parse stuff";
649 QString outputString(m_stdoutBuffer);
650 QTextStream output(&outputString, QIODevice::ReadOnly);
651 QString line;
652 int lineNumber(0);
653 while (!output.atEnd())
655 line=output.readLine();
656 if (line.indexOf("bytes free")==36)
658 QString tmp=line.mid(24,3);
659 tmp=tmp.trimmed();
660 tmp+=line.mid(28,3);
661 tmp=tmp.trimmed();
662 tmp+=line.mid(32,3);
663 tmp=tmp.trimmed();
665 return tmp.toInt();
667 lineNumber++;
669 return -1;
672 void FloppyProtocol::stat( const KUrl & _url)
674 kDebug(7101)<<"Floppy::stat() "<<_url.path();
675 KUrl url(_url);
676 QString path(url.path());
678 if ((path.isEmpty()) || (path=="/"))
680 url.setPath("/a/");
681 redirection(url);
682 finished();
683 return;
685 StatInfo info=this->_stat(url);
686 if (info.isValid)
688 UDSEntry entry;
689 createUDSEntry(info,entry);
690 statEntry( entry );
691 finished();
692 //kDebug(7101)<<"Floppy::stat(): ends";
693 return;
695 //otherwise the error() was already reported in _stat()
698 void FloppyProtocol::mkdir( const KUrl& url, int)
700 kDebug(7101)<<"FloppyProtocol::mkdir()";
701 QString path(url.path());
703 if ((path.isEmpty()) || (path=="/"))
705 KUrl newUrl(url);
706 newUrl.setPath("/a/");
707 redirection(newUrl);
708 finished();
709 return;
711 QString drive;
712 QString floppyPath;
713 getDriveAndPath(path,drive,floppyPath);
714 if (floppyPath.isEmpty())
716 finished();
717 return;
719 if (m_mtool!=0)
720 delete m_mtool;
721 //kDebug(7101)<<"Floppy::stat(): create args";
722 QStringList args;
724 args<<"mmd"<<(drive+floppyPath);
725 kDebug(7101)<<"Floppy::mkdir(): executing: mmd -"<<(drive+floppyPath)<<"-";
727 m_mtool=new Program(args);
728 if (!m_mtool->start())
730 delete m_mtool;
731 m_mtool=0;
732 errorMissingMToolsProgram("mmd");
733 return;
737 clearBuffers();
738 int result;
739 bool loopFinished(false);
740 bool errorOccured(false);
743 bool stdoutEvent;
744 bool stderrEvent;
745 result=m_mtool->select(1,0,stdoutEvent, stderrEvent);
746 if (stdoutEvent)
747 if (readStdout()==0)
748 loopFinished=true;
749 if (stderrEvent)
751 if (readStderr()==0)
752 loopFinished=true;
753 else
754 if (stopAfterError(url,drive))
756 loopFinished=true;
757 errorOccured=true;
760 } while (!loopFinished);
762 delete m_mtool;
763 m_mtool=0;
764 terminateBuffers();
765 if (errorOccured)
766 return;
767 finished();
770 void FloppyProtocol::del( const KUrl& url, bool isfile)
772 kDebug(7101)<<"FloppyProtocol::del()";
773 QString path(url.path());
775 if ((path.isEmpty()) || (path=="/"))
777 KUrl newUrl(url);
778 newUrl.setPath("/a/");
779 redirection(newUrl);
780 finished();
781 return;
783 QString drive;
784 QString floppyPath;
785 getDriveAndPath(path,drive,floppyPath);
786 if (floppyPath.isEmpty())
788 finished();
789 return;
792 if (m_mtool!=0)
793 delete m_mtool;
794 //kDebug(7101)<<"Floppy::stat(): create args";
795 QStringList args;
797 bool usingmdel;
799 if (isfile)
801 args<<"mdel"<<(drive+floppyPath);
802 usingmdel=true;
804 else
806 args<<"mrd"<<(drive+floppyPath);
807 usingmdel=false;
810 kDebug(7101)<<"Floppy::del(): executing: " << (usingmdel ? QString("mdel") : QString("mrd") ) << "-"<<(drive+floppyPath)<<"-";
812 m_mtool=new Program(args);
813 if (!m_mtool->start())
815 delete m_mtool;
816 m_mtool=0;
817 errorMissingMToolsProgram(usingmdel ? QString("mdel") : QString("mrd"));
818 return;
822 clearBuffers();
823 int result;
824 bool loopFinished(false);
825 bool errorOccured(false);
828 bool stdoutEvent;
829 bool stderrEvent;
830 result=m_mtool->select(1,0,stdoutEvent, stderrEvent);
831 if (stdoutEvent)
832 if (readStdout()==0)
833 loopFinished=true;
834 if (stderrEvent)
836 if (readStderr()==0)
837 loopFinished=true;
838 else
839 if (stopAfterError(url,drive))
841 loopFinished=true;
842 errorOccured=true;
845 } while (!loopFinished);
847 delete m_mtool;
848 m_mtool=0;
849 terminateBuffers();
850 if (errorOccured)
851 return;
852 finished();
855 void FloppyProtocol::rename( const KUrl &src, const KUrl &dest, KIO::JobFlags flags )
857 QString srcPath(src.path());
858 QString destPath(dest.path());
860 kDebug(7101)<<"Floppy::rename() -"<<srcPath<<"- to -"<<destPath<<"-";
862 if ((srcPath.isEmpty()) || (srcPath=="/"))
863 srcPath="/a/";
865 if ((destPath.isEmpty()) || (destPath=="/"))
866 destPath="/a/";
868 QString srcDrive;
869 QString srcFloppyPath;
870 getDriveAndPath(srcPath,srcDrive,srcFloppyPath);
871 if (srcFloppyPath.isEmpty())
873 finished();
874 return;
877 QString destDrive;
878 QString destFloppyPath;
879 getDriveAndPath(destPath,destDrive,destFloppyPath);
880 if (destFloppyPath.isEmpty())
882 finished();
883 return;
886 if (m_mtool!=0)
887 delete m_mtool;
888 //kDebug(7101)<<"Floppy::stat(): create args";
889 QStringList args;
891 if (flags & KIO::Overwrite)
892 args<<"mren"<<"-o"<<(srcDrive+srcFloppyPath)<<(destDrive+destFloppyPath);
893 else
894 args<<"mren"<<"-D"<<"s"<<(srcDrive+srcFloppyPath)<<(destDrive+destFloppyPath);
896 kDebug(7101)<<"Floppy::move(): executing: mren -"<<(srcDrive+srcFloppyPath)<<" "<<(destDrive+destFloppyPath);
898 m_mtool=new Program(args);
899 if (!m_mtool->start())
901 delete m_mtool;
902 m_mtool=0;
903 errorMissingMToolsProgram("mren");
904 return;
908 clearBuffers();
909 int result;
910 bool loopFinished(false);
911 bool errorOccured(false);
914 bool stdoutEvent;
915 bool stderrEvent;
916 result=m_mtool->select(1,0,stdoutEvent, stderrEvent);
917 if (stdoutEvent)
918 if (readStdout()==0)
919 loopFinished=true;
920 if (stderrEvent)
922 if (readStderr()==0)
923 loopFinished=true;
924 else
925 if (stopAfterError(src,srcDrive))
927 loopFinished=true;
928 errorOccured=true;
931 } while (!loopFinished);
933 delete m_mtool;
934 m_mtool=0;
935 terminateBuffers();
936 if (errorOccured)
937 return;
938 finished();
941 void FloppyProtocol::get( const KUrl& url )
943 QString path(url.path());
944 kDebug(7101)<<"Floppy::get() -"<<path<<"-";
946 if ((path.isEmpty()) || (path=="/"))
948 KUrl newUrl(url);
949 newUrl.setPath("/a/");
950 redirection(newUrl);
951 finished();
952 return;
954 StatInfo info=this->_stat(url);
955 //the error was already reported in _stat()
956 if (info.isValid==false)
957 return;
959 totalSize( info.size);
961 QString drive;
962 QString floppyPath;
963 getDriveAndPath(path,drive,floppyPath);
964 if (floppyPath.isEmpty())
966 finished();
967 return;
970 if (m_mtool!=0)
971 delete m_mtool;
972 //kDebug(7101)<<"Floppy::stat(): create args";
973 QStringList args;
974 args<<"mcopy"<<(drive+floppyPath)<<"-";
976 kDebug(7101)<<"Floppy::get(): executing: mcopy -"<<(drive+floppyPath)<<"-";
978 m_mtool=new Program(args);
979 if (!m_mtool->start())
981 delete m_mtool;
982 m_mtool=0;
983 errorMissingMToolsProgram("mcopy");
984 return;
987 clearBuffers();
988 int result;
989 int bytesRead(0);
990 bool loopFinished(false);
991 bool errorOccured(false);
994 bool stdoutEvent;
995 bool stderrEvent;
996 result=m_mtool->select(1,0,stdoutEvent, stderrEvent);
997 if (stdoutEvent)
999 delete [] m_stdoutBuffer;
1000 m_stdoutBuffer=0;
1001 m_stdoutSize=0;
1002 if (readStdout()>0)
1004 kDebug(7101)<<"Floppy::get(): m_stdoutSize:"<<m_stdoutSize;
1005 bytesRead+=m_stdoutSize;
1006 data( QByteArray::fromRawData(m_stdoutBuffer, m_stdoutSize) );
1008 else
1010 loopFinished=true;
1013 if (stderrEvent)
1015 if (readStderr()==0)
1016 loopFinished=true;
1017 else
1018 if (stopAfterError(url,drive))
1020 errorOccured=true;
1021 loopFinished=true;
1024 } while (!loopFinished);
1026 //kDebug(7101)<<"Floppy::get(): deleting m_mtool";
1027 delete m_mtool;
1028 m_mtool=0;
1029 if (errorOccured)
1030 return;
1032 //kDebug(7101)<<"Floppy::get(): finishing";
1033 data( QByteArray() );
1034 finished();
1037 void FloppyProtocol::put( const KUrl& url, int , JobFlags flags )
1039 QString path(url.path());
1040 kDebug(7101)<<"Floppy::put() -"<<path<<"-";
1042 if ((path.isEmpty()) || (path=="/"))
1044 KUrl newUrl(url);
1045 newUrl.setPath("/a/");
1046 redirection(newUrl);
1047 finished();
1048 return;
1050 QString drive;
1051 QString floppyPath;
1052 getDriveAndPath(path,drive,floppyPath);
1053 if (floppyPath.isEmpty())
1055 finished();
1056 return;
1058 int freeSpaceLeft=freeSpace(url);
1059 if (freeSpaceLeft==-1)
1060 return;
1062 if (m_mtool!=0)
1063 delete m_mtool;
1064 //kDebug(7101)<<"Floppy::stat(): create args";
1065 QStringList args;
1066 if (flags & KIO::Overwrite)
1067 args<<"mcopy"<<"-o"<<"-"<<(drive+floppyPath);
1068 else
1069 args<<"mcopy"<<"-s"<<"-"<<(drive+floppyPath);
1071 kDebug(7101)<<"Floppy::put(): executing: mcopy -"<<(drive+floppyPath)<<"-";
1073 m_mtool=new Program(args);
1074 if (!m_mtool->start())
1076 delete m_mtool;
1077 m_mtool=0;
1078 errorMissingMToolsProgram("mcopy");
1079 return;
1083 clearBuffers();
1084 int result(0);
1085 int bytesRead(0);
1087 //from file.cc
1088 // Loop until we got 0 (end of data)
1091 bool stdoutEvent;
1092 bool stderrEvent;
1093 kDebug(7101)<<"Floppy::put(): select()...";
1094 m_mtool->select(0,100,stdoutEvent, stderrEvent);
1095 if (stdoutEvent)
1097 if (readStdout()==0)
1098 result=0;
1100 if (stderrEvent)
1102 if (readStderr()==0)
1103 result=0;
1104 else
1105 if (stopAfterError(url,drive))
1106 result=-1;
1107 kDebug(7101)<<"Floppy::put(): error: result=="<<result;
1109 else
1111 QByteArray buffer;
1112 dataReq(); // Request for data
1113 //kDebug(7101)<<"Floppy::put(): after dataReq()";
1114 result = readData( buffer );
1115 //kDebug(7101)<<"Floppy::put(): after readData(), read "<<result<<" bytes";
1116 if (result > 0)
1118 bytesRead+=result;
1119 kDebug(7101)<<"Floppy::put() bytesRead: "<<bytesRead<<" space: "<<freeSpaceLeft;
1120 if (bytesRead>freeSpaceLeft)
1122 result=0;
1123 error( KIO::ERR_SLAVE_DEFINED, i18n("Could not write to file %1.\nThe disk in drive %2 is probably full.", url.prettyUrl(), drive));
1125 else
1127 //kDebug(7101)<<"Floppy::put(): writing...";
1128 result=::write(m_mtool->stdinFD(),buffer.data(), buffer.size());
1129 kDebug(7101)<<"Floppy::put(): after write(), wrote "<<result<<" bytes";
1134 while ( result > 0 );
1136 if (result<0)
1138 perror("writing to stdin");
1139 error( KIO::ERR_CANNOT_OPEN_FOR_WRITING, url.prettyUrl());
1140 return;
1143 delete m_mtool;
1144 m_mtool=0;
1146 finished();