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
| /***************************************************************************
WriteThread.cpp
-------------------
Begin : Web May 6 2009 19:11 alpha_one_x86
Project : Ultracopier
Email : alpha.super-one@laposte.net
Note : See README for copyright and developer
Target : Define the class of the writeThread
****************************************************************************/
#include "WriteThread.h"
WriteThread::WriteThread(copyThread *parent)
{
this->parent=parent;
theCurrentStat=WriteThread::Stopped;
stopIt=false;
endOfSource=false;
source=NULL;
destination=NULL;
theItem.id=0;
movingMode=false;
mutexWaitControlR.lock();
mutexWaitControlW.lock();
debugRcount=0;
debugWcount=0;
}
WriteThread::~WriteThread()
{
mutexWaitControlW.unlock();
mutexWaitControlR.unlock();
}
void WriteThread::setFiles(QFile * source,QFile * destination,copyItemInternal theItem)
{
DEBUGCONSOLE(50,"WriteThread::setFiles","source: "+source->fileName()+", destination: "+destination->fileName());
this->source=source;
this->destination=destination;
this->theItem=theItem;
controlMutexR.wakeAll();
}
void WriteThread::stop()
{
stopIt=true;
controlMutexR.wakeAll();
controlMutexW.wakeAll();
}
void WriteThread::addNewBlock(QByteArray newBlock)
{
DEBUGCONSOLE(50,"WriteThread::addNewBlock","newBlock size: "+QString::number(newBlock.size()));
debugRcount+=newBlock.size();
//wait if list is full
if(theBlockList.size()>LISTBLOCKSIZE)
controlMutexW.wait(&mutexWaitControlW);
{
QMutexLocker lock_mutex(&pipe);
theBlockList.append(newBlock);
}
//wake if list is becoming not empty
controlMutexR.wakeAll();
}
/// \brief end of source detected
void WriteThread::endOfSourceDetected()
{
endOfSource=true;
controlMutexR.wakeAll();
}
/// \brief set action on error
void WriteThread::errorAction(int action)
{
DEBUGCONSOLE(50,"WriteThread::fileExistsAction","action: "+QString::number(action));
actionAfterUnlock=action;
if(action==ERRORACTION_CLOSE)
stop();
else
{
controlMutexR.wakeAll();
theCurrentStat=WriteThread::Running;
}
}
/// \brief return the statut
WriteThread::currentStat WriteThread::getCurrentStat()
{
return theCurrentStat;
}
void WriteThread::setKeepDate(bool keepDate)
{
this->keepDate=keepDate;
}
void WriteThread::setMovingMode(bool movingMode)
{
this->movingMode=movingMode;
}
void WriteThread::run()
{
DEBUGCONSOLE(50,"WriteThread::run","start");
if(source==NULL || destination==NULL || theItem.id==0)
{
DEBUGCONSOLE(10,"WriteThread::run","item not loaded!");
return;
}
do
{
endOfSource=false;
theCurrentStat=WriteThread::Running;
QByteArray blockArray;
qint64 positionInDestination=0,bytesWriten=0;
DEBUGCONSOLE(50,"WriteThread::run","start loop");
do
{
//wait if list is empty then wait
if(theBlockList.size()==0 && !endOfSource)
controlMutexR.wait(&mutexWaitControlR);
{
QMutexLocker lock_mutex(&pipe);
//read one block
if(theBlockList.size()==0)
blockArray.clear();
else
{
blockArray=theBlockList.first();
theBlockList.removeFirst();
}
}
controlMutexW.wakeAll();
//write one block
if(!stopIt && !blockArray.isEmpty())
{
positionInDestination=destination->pos();
retryWriteThisBlock:
bytesWriten=destination->write(blockArray);
if(destination->error()!=QFile::NoError || bytesWriten!=blockArray.size())
{
retrySeekDestination:
actionAfterUnlock=-1;
DEBUGCONSOLE(90,"WriteThread::run","Error in writing: "+destination->errorString());
emit errorOnFile(ERROR_DEF_ALL,destination->fileName(),tr("Error in writing destination: ")+destination->errorString(),theItem.id);
theCurrentStat=WriteThread::Paused;
if(actionAfterUnlock==-1)
controlMutexR.wait(&mutexWaitControlR);
theCurrentStat=WriteThread::Running;
if(actionAfterUnlock==ERRORACTION_RETRY)
{
if(!destination->seek(positionInDestination))
goto retrySeekDestination;
goto retryWriteThisBlock;
}
else if(actionAfterUnlock==ERRORACTION_SKIP)
goto SkipFile;
else if(actionAfterUnlock==ERRORACTION_CLOSE)
goto SkipFile;
else
{
DEBUGCONSOLE(10,"WriteThread::run","Unknow action at destination write error ("+QString::number(actionAfterUnlock)+")");
goto SkipFile;
}
}
}
}
while(!blockArray.isEmpty() && bytesWriten==blockArray.size() && !stopIt && (theBlockList.size() || !endOfSource));
DEBUGCONSOLE(70,"WriteThread::run","blockArray.isEmpty(): "+QString::number(blockArray.isEmpty()));
DEBUGCONSOLE(70,"WriteThread::run","bytesWriten: "+QString::number(bytesWriten));
DEBUGCONSOLE(70,"WriteThread::run","blockArray.size(): "+QString::number(blockArray.size()));
DEBUGCONSOLE(70,"WriteThread::run","stopIt: "+QString::number(stopIt));
DEBUGCONSOLE(70,"WriteThread::run","theBlockList.size(): "+QString::number(theBlockList.size()));
DEBUGCONSOLE(70,"WriteThread::run","endOfSource: "+QString::number(endOfSource));
endOfSource=false;
DEBUGCONSOLE(90,"WriteThread::run","closing source done");
if(destination->isOpen())
destination->close();
DEBUGCONSOLE(90,"WriteThread::run","closing destination done");
//set the time if no write thread used
if(keepDate)
{
retrySetDate:
DEBUGCONSOLE(90,"WriteThread::run","set date...");
if(!changeFileDateTime(destination->fileName(),source->fileName()))
{
actionAfterUnlock=-1;
DEBUGCONSOLE(90,"WriteThread::run","Date cannot be modified!");
emit errorOnFile(ERROR_DEF_ALL,source->fileName(),tr("Date cannot be modified!"),theItem.id);
theCurrentStat=WriteThread::Paused;
if(actionAfterUnlock==-1)
controlMutexR.wait(&mutexWaitControlR);
theCurrentStat=WriteThread::Running;
if(actionAfterUnlock==ERRORACTION_RETRY)
goto retrySetDate;
else if(actionAfterUnlock==ERRORACTION_SKIP)
goto SkipFile;
else if(actionAfterUnlock==ERRORACTION_CLOSE)
goto SkipFile;
else
{
DEBUGCONSOLE(10,"WriteThread::run","Unknow action at change file time ("+QString::number(actionAfterUnlock)+")");
goto SkipFile;
}
}
DEBUGCONSOLE(90,"WriteThread::run","set date done");
}
//if query to stop, directly go to SkipFile
if(stopIt)
goto SkipFile;
//remove source in moving mode
if(movingMode)
{
DEBUGCONSOLE(90,"WriteThread::run","try remove source...");
if(!source->remove())
{
actionAfterUnlock=-1;
DEBUGCONSOLE(10,"WriteThread::run","source->errorString():\""+source->errorString()+"\" while removing the source");
emit errorOnFile(ERROR_DEF_ALL,destination->fileName(),tr("Unable to remove the source file in moving mode: ")+source->errorString(),theItem.id);
theCurrentStat=WriteThread::Paused;
if(actionAfterUnlock==-1)
controlMutexR.wait(&mutexWaitControlR);
theCurrentStat=WriteThread::Running;
if(actionAfterUnlock==ERRORACTION_RETRY)
goto retrySetDate;
else if(actionAfterUnlock==ERRORACTION_SKIP)
goto SkipFile;
else if(actionAfterUnlock==ERRORACTION_CLOSE)
goto SkipFile;
else
{
DEBUGCONSOLE(10,"WriteThread::run","Unknow action at remove source file ("+QString::number(actionAfterUnlock)+")");
goto SkipFile;
}
}
DEBUGCONSOLE(90,"WriteThread::run","try remove source done");
}
DEBUGCONSOLE(90,"WriteThread::run","now is in SkipFile point!");
SkipFile:
//close file
DEBUGCONSOLE(90,"WriteThread::run","closing source and destination...");
if(destination->isOpen())
destination->close();
DEBUGCONSOLE(90,"WriteThread::run","closing destination done");
if((actionAfterUnlock==ERRORACTION_SKIP || actionAfterUnlock==ERRORACTION_CLOSE || stopIt) && destination->exists())
{
DEBUGCONSOLE(50,"WriteThread::run","try remove destination: "+destination->fileName());
destination->remove();
}
parent->deleteFileObject(source,destination);
source=NULL;
destination=NULL;
theItem.id=0;
theCurrentStat=WriteThread::Stopped;
controlMutexR.wait(&mutexWaitControlR);
} while(true);
} |
Partager