Example #1
0
static bool copyIfNotPresent
    ( const QString &sourcePath
    , const QString &destinationPath
    )
{
    QFile sourceFile(sourcePath);
    QFile destinationFile(destinationPath);

    if (destinationFile.exists()) return true;

    if (sourceFile.open(QIODevice::ReadOnly | QIODevice::Text) == false) {
        fprintf(stderr, "Failed to read template.\n");
        return false;
    }

    QFileInfo destinationInfo(destinationFile);
    QDir(destinationInfo.absolutePath()).mkpath(".");

    if (destinationFile.open(QIODevice::WriteOnly | QIODevice::Text) == false) {
        fprintf(stderr, "Failed to open user configuration file for writing.\n");
        return false;
    }

    QTextStream in(&sourceFile);
    QTextStream out(&destinationFile);
    out << in.readAll();

    return true;
}
Example #2
0
QString SyncProcess::updateEntry(const QString & path, const QDir & source, const QDir & destination)
{
  QFileInfo sourceInfo(path);
  QString relativePath = source.relativeFilePath(path);
  QString destinationPath = destination.absoluteFilePath(relativePath);
  QFileInfo destinationInfo(destinationPath);
  if (sourceInfo.isDir()) {
    if (!destinationInfo.exists()) {
      progress->addText(tr("Create directory %1\n").arg(destinationPath));
      if (!destination.mkdir(relativePath)) {
        return QObject::tr("Create '%1' failed").arg(destinationPath);
      }
    }
  }
  else {
    if (!destinationInfo.exists()) {
      // qDebug() << "Copy" << path << "to" << destinationPath;
      progress->addText(tr("Copy %1 to %2").arg(path).arg(destinationPath) + "\n");
      if (!QFile::copy(path, destinationPath)) {
        return QObject::tr("Copy '%1' to '%2' failed").arg(path).arg(destinationPath);
      }
    }
    else if (sourceInfo.lastModified() > destinationInfo.lastModified()) {
      // retrieve source contents
      QFile sourceFile(path);
      if (!sourceFile.open(QFile::ReadOnly)) {
        return QObject::tr("Open '%1' failed").arg(path);
      }
      QByteArray sourceContents = sourceFile.readAll();
      sourceFile.close();

      // retrieve destination contents
      QFile destinationFile(destinationPath);
      if (!destinationFile.open(QFile::ReadOnly)) {
        return QObject::tr("Open '%1' failed").arg(destinationPath);
      }
      QByteArray destinationContents = destinationFile.readAll();
      destinationFile.close();

      if (contentsDifferent(sourceContents, destinationContents)) {
        // copy contents
        if (!destinationFile.open(QFile::WriteOnly | QIODevice::Truncate)) {
          return QObject::tr("Write '%1' failed").arg(destinationPath);
        }
        progress->addText(tr("Write %1").arg(destinationPath) + "\n");
        // qDebug() << "Write" << destinationPath;
        destinationFile.write(sourceContents);
        destinationFile.close();
      }
    }
  }
  return QString();
}
void QuaZipImageSource::extractImage(QuaZip *archive, const QString &filePath)
{
    QFileInfo destinationInfo(filePath);
    QuaZipFile source(archive);
    QFile destination(destinationInfo.absoluteFilePath());

    // Create any directories that would need to exist.
    QDir().mkpath(destinationInfo.absolutePath());

    // Copy the data from the archive to the file.
    source.open(QIODevice::ReadOnly);
    destination.open(QIODevice::WriteOnly);
    destination.resize(source.size());
    while (!source.atEnd()) {
        destination.write(source.read(256));
    }

    destination.close();
    source.close();
}
void ScanFileOrFolder::addToList(const QStringList& sources,const QString& destination)
{
    stopIt=false;
    this->sources=parseWildcardSources(sources);
    this->destination=destination;
    QFileInfo destinationInfo(this->destination);
    ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,QStringLiteral("check symblink: %1").arg(destinationInfo.absoluteFilePath()));
    while(destinationInfo.isSymLink())
    {
        ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,QStringLiteral("resolv destination to: %1").arg(destinationInfo.symLinkTarget()));
        if(QFileInfo(destinationInfo.symLinkTarget()).isAbsolute())
            this->destination=destinationInfo.symLinkTarget();
        else
            this->destination=destinationInfo.absolutePath()+text_slash+destinationInfo.symLinkTarget();
        destinationInfo.setFile(this->destination);
    }
    if(sources.size()>1 || QFileInfo(destination).isDir())
        /* Disabled because the separator transformation product bug
         * if(!destination.endsWith(QDir::separator()))
            this->destination+=QDir::separator();*/
        if(!destination.endsWith(text_slash) && !destination.endsWith(text_antislash))
            this->destination+=text_slash;//put unix separator because it's transformed into that's under windows too
    ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"addToList("+sources.join(";")+","+this->destination+")");
}
Example #5
0
bool WriteThread::internalOpen()
{
	ULTRACOPIER_DEBUGCONSOLE(DebugLevel_Notice,"["+QString::number(id)+"] internalOpen destination: "+name);
	if(stopIt)
		return false;
	if(file.isOpen())
	{
		ULTRACOPIER_DEBUGCONSOLE(DebugLevel_Notice,"["+QString::number(id)+"] already open! destination: "+file.fileName());
		return false;
	}
	//set to LISTBLOCKSIZE
	while(freeBlock.available()<ULTRACOPIER_PLUGIN_MAXBUFFERBLOCK)
		freeBlock.release();
	if(freeBlock.available()>ULTRACOPIER_PLUGIN_MAXBUFFERBLOCK)
		freeBlock.acquire(freeBlock.available()-ULTRACOPIER_PLUGIN_MAXBUFFERBLOCK);
        stopIt=false;
	CurentCopiedSize=0;
	endDetected=false;
	#ifdef ULTRACOPIER_PLUGIN_DEBUG
	stat=InodeOperation;
	#endif
	file.setFileName(name);
	//mkpath check if exists and return true if already exists
	QFileInfo destinationInfo(file);
	QDir destinationFolder;
	{
		mkpathTransfer->acquire();
		if(!destinationFolder.exists(destinationInfo.absolutePath()))
		{
			ULTRACOPIER_DEBUGCONSOLE(DebugLevel_Notice,"["+QString::number(id)+"] "+QString("Try create the path: %1")
						 .arg(destinationInfo.absolutePath()));
			if(!destinationFolder.mkpath(destinationInfo.absolutePath()))
			{
				if(!destinationFolder.exists(destinationInfo.absolutePath()))
				{
					/// \todo do real folder error here
					errorString_internal="mkpath error on destination";
					ULTRACOPIER_DEBUGCONSOLE(DebugLevel_Warning,"["+QString::number(id)+"] "+QString("Unable create the folder: %1, error: %2")
								 .arg(destinationInfo.absolutePath())
								 .arg(errorString_internal));
					emit error();
					#ifdef ULTRACOPIER_PLUGIN_DEBUG
					stat=Idle;
					#endif
					return false;
				}
			}
		}
		mkpathTransfer->release();
	}
	if(stopIt)
		return false;
	//try open it
	QIODevice::OpenMode flags=QIODevice::ReadWrite;
	if(!buffer)
		flags|=QIODevice::Unbuffered;
	if(file.open(flags))
	{
		if(stopIt)
			return false;
		file.seek(0);
		if(stopIt)
			return false;
		file.resize(startSize);
		if(stopIt)
			return false;
		emit opened();
		#ifdef ULTRACOPIER_PLUGIN_DEBUG
		stat=Idle;
		#endif
		isOpen.acquire();
		needRemoveTheFile=false;
		return true;
	}
	else
	{
		if(stopIt)
			return false;
		errorString_internal=file.errorString();
		ULTRACOPIER_DEBUGCONSOLE(DebugLevel_Warning,"["+QString::number(id)+"] "+QString("Unable to open: %1, error: %2").arg(name).arg(errorString_internal));
		emit error();
		#ifdef ULTRACOPIER_PLUGIN_DEBUG
		stat=Idle;
		#endif
		return false;
	}
}
Example #6
0
bool WriteThread::internalOpen()
{
    //do a bug
    ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,QStringLiteral("[")+QString::number(id)+QStringLiteral("] internalOpen destination: ")+file.fileName());
    if(stopIt)
    {
        ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,QStringLiteral("[")+QString::number(id)+QStringLiteral("] close because stopIt is at true"));
        emit closed();
        return false;
    }
    if(file.isOpen())
    {
        ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,QStringLiteral("[")+QString::number(id)+QStringLiteral("] already open! destination: ")+file.fileName());
        return false;
    }
    if(file.fileName().isEmpty())
    {
        errorString_internal=tr("Path resolution error (Empty path)");
        ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Warning,QStringLiteral("[")+QString::number(id)+QStringLiteral("] ")+QStringLiteral("Unable to open: %1, error: %2").arg(file.fileName()).arg(errorString_internal));
        emit error();
        return false;
    }
    ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,QStringLiteral("[")+QString::number(id)+QStringLiteral("] before the mutex"));
    //set to LISTBLOCKSIZE
    if(sequential)
    {
        while(writeFull.available()<1)
            writeFull.release();
        if(writeFull.available()>1)
            writeFull.acquire(writeFull.available()-1);
    }
    else
    {
        while(writeFull.available()<numberOfBlock)
            writeFull.release();
        if(writeFull.available()>numberOfBlock)
            writeFull.acquire(writeFull.available()-numberOfBlock);
    }
    ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,QStringLiteral("[")+QString::number(id)+QStringLiteral("] after the mutex"));
    stopIt=false;
    endDetected=false;
    #ifdef ULTRACOPIER_PLUGIN_DEBUG
    stat=InodeOperation;
    #endif
    //mkpath check if exists and return true if already exists
    QFileInfo destinationInfo(file);
    QDir destinationFolder;
    {
        mkpathTransfer->acquire();
        if(!destinationFolder.exists(destinationInfo.absolutePath()))
        {
            ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,QStringLiteral("[")+QString::number(id)+QStringLiteral("] ")+QStringLiteral("Try create the path: %1")
                         .arg(destinationInfo.absolutePath()));
            if(!destinationFolder.mkpath(destinationInfo.absolutePath()))
            {
                if(!destinationFolder.exists(destinationInfo.absolutePath()))
                {
                    /// \todo do real folder error here
                    errorString_internal="mkpath error on destination";
                    ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Warning,QStringLiteral("[")+QString::number(id)+QStringLiteral("] ")+QStringLiteral("Unable create the folder: %1, error: %2")
                                 .arg(destinationInfo.absolutePath())
                                 .arg(errorString_internal));
                    emit error();
                    #ifdef ULTRACOPIER_PLUGIN_DEBUG
                    stat=Idle;
                    #endif
                    mkpathTransfer->release();
                    return false;
                }
            }
        }
        mkpathTransfer->release();
    }
    ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,QStringLiteral("[")+QString::number(id)+QStringLiteral("] after the mkpath"));
    if(stopIt)
    {
        ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,QStringLiteral("[")+QString::number(id)+QStringLiteral("] close because stopIt is at true"));
        emit closed();
        return false;
    }
    //try open it
    QIODevice::OpenMode flags=QIODevice::ReadWrite;
    if(!buffer)
        flags|=QIODevice::Unbuffered;
    {
        QMutexLocker lock_mutex(&writeFileListMutex);
        if(writeFileList.count(file.fileName(),this)==0)
        {
            writeFileList.insert(file.fileName(),this);
            if(writeFileList.count(file.fileName())>1)
            {
                ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Warning,QStringLiteral("[")+QString::number(id)+QStringLiteral("] in waiting because same file is found"));
                return false;
            }
        }
    }
    bool fileWasExists=file.exists();
    if(file.open(flags))
    {
        ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,QStringLiteral("[")+QString::number(id)+QStringLiteral("] after the open"));
        {
            QMutexLocker lock_mutex(&accessList);
            if(!theBlockList.isEmpty())
            {
                ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Warning,QStringLiteral("[")+QString::number(id)+QStringLiteral("] ")+QStringLiteral("General file corruption detected"));
                stopIt=true;
                file.close();
                resumeNotStarted();
                file.setFileName(QStringLiteral(""));
                return false;
            }
        }
        pauseMutex.tryAcquire(pauseMutex.available());
        ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,QStringLiteral("[")+QString::number(id)+QStringLiteral("] after the pause mutex"));
        if(stopIt)
        {
            ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,QStringLiteral("[")+QString::number(id)+QStringLiteral("] close because stopIt is at true"));
            file.close();
            resumeNotStarted();
            file.setFileName(QStringLiteral(""));
            emit closed();
            return false;
        }
        if(!file.seek(0))
        {
            file.close();
            resumeNotStarted();
            file.setFileName(QStringLiteral(""));
            errorString_internal=file.errorString();
            ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Warning,QStringLiteral("[")+QString::number(id)+QStringLiteral("] ")+QStringLiteral("Unable to seek after open: %1, error: %2").arg(file.fileName()).arg(errorString_internal));
            emit error();
            #ifdef ULTRACOPIER_PLUGIN_DEBUG
            stat=Idle;
            #endif
            return false;
        }
        if(stopIt)
        {
            ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,QStringLiteral("[")+QString::number(id)+QStringLiteral("] close because stopIt is at true"));
            file.close();
            resumeNotStarted();
            file.setFileName(QStringLiteral(""));
            emit closed();
            return false;
        }
        if(!file.resize(startSize))
        {
            file.close();
            resumeNotStarted();
            file.setFileName(QStringLiteral(""));
            errorString_internal=file.errorString();
            ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Warning,QStringLiteral("[")+QString::number(id)+QStringLiteral("] ")+QStringLiteral("Unable to resize to %1 after open: %2, error: %3").arg(startSize).arg(file.fileName()).arg(errorString_internal));
            emit error();
            #ifdef ULTRACOPIER_PLUGIN_DEBUG
            stat=Idle;
            #endif
            return false;
        }
        if(stopIt)
        {
            ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,QStringLiteral("[")+QString::number(id)+QStringLiteral("] close because stopIt is at true"));
            file.close();
            resumeNotStarted();
            file.setFileName(QStringLiteral(""));
            emit closed();
            return false;
        }
        isOpen.acquire();
        ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,QStringLiteral("[")+QString::number(id)+QStringLiteral("] emit opened()"));
        emit opened();
        #ifdef ULTRACOPIER_PLUGIN_DEBUG
        stat=Idle;
        #endif
        needRemoveTheFile=false;
        postOperationRequested=false;
        return true;
    }
    else
    {
        if(!fileWasExists && file.exists())
            if(!file.remove())
                ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Warning,QStringLiteral("[")+QString::number(id)+QStringLiteral("] file created but can't be removed"));
        if(stopIt)
        {
            ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,QStringLiteral("[")+QString::number(id)+QStringLiteral("] close because stopIt is at true"));
            resumeNotStarted();
            file.setFileName(QStringLiteral(""));
            emit closed();
            return false;
        }
        errorString_internal=file.errorString();
        ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Warning,QStringLiteral("[")+QString::number(id)+QStringLiteral("] ")+QStringLiteral("Unable to open: %1, error: %2").arg(file.fileName()).arg(errorString_internal));
        emit error();
        #ifdef ULTRACOPIER_PLUGIN_DEBUG
        stat=Idle;
        #endif
        return false;
    }
}