LockedFile::LockedFile(const QString &filePath) { m_fileHandle = NULL; QFileInfo existingFile(filePath); existingFile.setCaching(false); //Make sure the file exists, before we try to lock it if(!existingFile.exists()) { char error_msg[256]; strcpy_s(error_msg, 256, QString("File '%1' does not exist!").arg(existingFile.fileName()).toLatin1().constData()); throw error_msg; } //Remember file path m_filePath = existingFile.canonicalFilePath(); //Now lock the file for(int i = 0; i < 64; i++) { m_fileHandle = CreateFileW(QWCHAR(QDir::toNativeSeparators(filePath)), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, NULL, NULL); if((m_fileHandle != NULL) && (m_fileHandle != INVALID_HANDLE_VALUE)) break; if(!i) qWarning("Failed to lock file on first attemp, retrying..."); Sleep(100); } //Locked successfully? if((m_fileHandle == NULL) || (m_fileHandle == INVALID_HANDLE_VALUE)) { THROW(QString("File '%1' could not be locked!").arg(existingFile.fileName()).toLatin1().constData()); } }
void BackupListWidget::addItemWithUrl(QUrl url) { if(url.isEmpty()) return; QString fileUrl = url.toLocalFile(); if(fileUrl.isEmpty()) return; QFileInfo file(fileUrl); if(!file.exists()) return; QList<QUrl> urls = itemUrls(); bool matches = false; foreach(QUrl existingUrl, urls) { if(url == existingUrl) { matches = true; break; } QFileInfo existingFile(existingUrl.toLocalFile()); if(existingFile.isDir() && fileUrl.startsWith(existingFile.absoluteFilePath())) { matches = true; break; } } if(matches) { auto confirm = QMessageBox::question(this, tr("Confirm action"), tr("The file or directory:\n %1\n" "was already in the backup list;" " adding it again will have no effect.\n" "Add anyway?").arg(url.toLocalFile())); if(confirm == QMessageBox::No) return; } BackupListWidgetItem *item = new BackupListWidgetItem(url); connect(item, &BackupListWidgetItem::requestDelete, this, &BackupListWidget::removeItems); connect(item, &BackupListWidgetItem::requestUpdate, this, &BackupListWidget::recomputeListTotals); insertItem(count(), item); setItemWidget(item, item->widget()); emit itemWithUrlAdded(url); }
bool addExistingFile(string fileName, string address, string owner) { string fileDirectory = getFilePath(fileName); if (!isSaved(fileName)) throw accessError(NOT_SAVED); if (!isExisting(address)) throw accessError(NO_FILE); if (!isAccessable(fileName, owner)) throw accessError(NOT_USER); diffTree tree(fileName); diffNode* start = tree.getTreePathByID(tree.currentVersion); diffNode* finish = tree.getTreePath(); modernizeFile(fileName, tree.getVersionsPath(start, finish)); vector<diffString> exf; vector<diffString> newf; ifstream existingFile(fileDirectory + '\\' + fileName); ifstream newFile(address); while (!existingFile.eof()) { diffString buf; buf.text = new char[MAX]; existingFile.getline(buf.text, MAX); buf.length = strlen(buf.text); exf.push_back(buf); } while (!newFile.eof()) { diffString buf; buf.text = new char[MAX]; newFile.getline(buf.text, MAX); buf.length = strlen(buf.text); newf.push_back(buf); } vector<diffString> difference = diff(exf, newf); showChangesVector(difference); diffNode* child = new diffNode; child->diffLine = difference; tree.addNode(finish, child); ofstream diffFile(fileDirectory + '\\' + fileName + "-diff.dat"); diffFile.close(); tree.printDiffTreeFile(fileName); deleteFile(fileName); addFile(fileName, address); return true; }
void AsyncIO::startCopy() { //same file, no need to copy if(existingFilePath.compare(newFilePath) == 0) { emit finished(); return; } //load both files QFile existingFile(existingFilePath); QFile newFile(newFilePath); bool openExisting = existingFile.open( QIODevice::ReadOnly ); bool openNew = newFile.open( QIODevice::WriteOnly ); //if either file fails to open exit if(!openExisting || !openNew) { emit error("Failed to open file"); return; } double existingFileSize = existingFile.size(); double bytesCopied = 0; //copy contents uint BUFFER_SIZE = 16000; char* buffer = new char[BUFFER_SIZE]; while(!existingFile.atEnd()) { qint64 len = existingFile.read( buffer, BUFFER_SIZE ); newFile.write( buffer, len ); bytesCopied += BUFFER_SIZE; int copyProgress = (bytesCopied / existingFileSize) * 100; if(copyProgress!=progress) { progress = copyProgress; emit progressUpdate(progress); } } //deallocate buffer delete[] buffer; buffer = NULL; emit finished(); return; }
void FilesTest::run() { trj::File existingFile("../../torrijas/nanovg/example/images/image1.jpg"); std::cout << "existingFile path: " << existingFile.getPath().getCharArray() << std::endl; std::cout << "existingFile name: " << existingFile.getName().getCharArray() << std::endl; std::cout << "existingFile extension: " << existingFile.getExtension().getCharArray() << std::endl; std::cout << "existingFile absolute path: " << existingFile.getAbsolutePath().getCharArray() << std::endl; trj::Folder existingFolder = existingFile.getParentFolder(); std::cout << "existingFolder path: " << existingFolder.getPath().getCharArray() << std::endl; std::cout << "existingFolder name: " << existingFolder.getName().getCharArray() << std::endl; std::cout << "existingFolder absolute path: " << existingFolder.getAbsolutePath().getCharArray() << std::endl; std::cout << "existingFolder parent: " << existingFolder.getParentFolder().getPath().getCharArray() << std::endl; trj::File nonExistingFile("../../torrijas/nanovg/example/images/image456.jpg"); std::cout << "nonExistingFile path: " << nonExistingFile.getPath().getCharArray() << std::endl; trj::File noPathFile("torrijas-test"); std::cout << "noPathFile path: " << noPathFile.getPath().getCharArray() << std::endl; std::cout << "noPathFile name: " << noPathFile.getName().getCharArray() << std::endl; std::cout << "noPathFile extension: " << noPathFile.getExtension().getCharArray() << std::endl; std::cout << "noPathFile absolute path: " << noPathFile.getAbsolutePath().getCharArray() << std::endl; std::cout << "noPathFile parent: " << noPathFile.getParentFolder().getPath().getCharArray() << std::endl; trj::Folder currentFolder = trj::Folder::getCurrentFolder(); std::cout << "currentFolder path: " << currentFolder.getPath().getCharArray() << std::endl; trj::Folder parentCurrentFolder = currentFolder.getParentFolder(); std::cout << "parentCurrentFolder path: " << parentCurrentFolder.getPath().getCharArray() << std::endl; std::vector<trj::File> files; std::vector<trj::Folder> folders; parentCurrentFolder.getContent(files, folders); for(const trj::File& file : files) { std::cout << "parentCurrentFolder file path: " << file.getPath().getCharArray() << std::endl; } for(const trj::Folder& folder : folders) { std::cout << "parentCurrentFolder folder path: " << folder.getPath().getCharArray() << std::endl; } }
/*! Opens the file using OpenMode \a mode, returning true if successful; otherwise false. Important: the \a mode must include QIODevice::WriteOnly. It may also have additional flags, such as QIODevice::Text and QIODevice::Unbuffered. QIODevice::ReadWrite and QIODevice::Append are not supported at the moment. \sa QIODevice::OpenMode, setFileName() */ bool QSaveFile::open(OpenMode mode) { Q_D(QSaveFile); if (isOpen()) { qWarning("QSaveFile::open: File (%s) already open", qPrintable(fileName())); return false; } unsetError(); if ((mode & (ReadOnly | WriteOnly)) == 0) { qWarning("QSaveFile::open: Open mode not specified"); return false; } // In the future we could implement ReadWrite by copying from the existing file to the temp file... if ((mode & ReadOnly) || (mode & Append)) { qWarning("QSaveFile::open: Unsupported open mode 0x%x", int(mode)); return false; } // check if existing file is writable QFileInfo existingFile(d->fileName); if (existingFile.exists() && !existingFile.isWritable()) { d->setError(QFileDevice::WriteError, QSaveFile::tr("Existing file %1 is not writable").arg(d->fileName)); d->writeError = QFileDevice::WriteError; return false; } if (existingFile.isDir()) { d->setError(QFileDevice::WriteError, QSaveFile::tr("Filename refers to a directory")); d->writeError = QFileDevice::WriteError; return false; } // Resolve symlinks. Don't use QFileInfo::canonicalFilePath so it still give the expected // target even if the file does not exist d->finalFileName = d->fileName; if (existingFile.isSymLink()) { int maxDepth = 128; while (--maxDepth && existingFile.isSymLink()) existingFile.setFile(existingFile.symLinkTarget()); if (maxDepth > 0) d->finalFileName = existingFile.filePath(); } d->fileEngine = new QTemporaryFileEngine; static_cast<QTemporaryFileEngine *>(d->fileEngine)->initialize(d->finalFileName, 0666); // Same as in QFile: QIODevice provides the buffering, so there's no need to request it from the file engine. if (!d->fileEngine->open(mode | QIODevice::Unbuffered)) { QFileDevice::FileError err = d->fileEngine->error(); #ifdef Q_OS_UNIX if (d->directWriteFallback && err == QFileDevice::OpenError && errno == EACCES) { delete d->fileEngine; d->fileEngine = QAbstractFileEngine::create(d->finalFileName); if (d->fileEngine->open(mode | QIODevice::Unbuffered)) { d->useTemporaryFile = false; QFileDevice::open(mode); return true; } err = d->fileEngine->error(); } #endif if (err == QFileDevice::UnspecifiedError) err = QFileDevice::OpenError; d->setError(err, d->fileEngine->errorString()); delete d->fileEngine; d->fileEngine = 0; return false; } d->useTemporaryFile = true; QFileDevice::open(mode); if (existingFile.exists()) setPermissions(existingFile.permissions()); return true; }
TVerdict CTestImportContactMergeModeStep::doTestStepL() /** * @return - TVerdict code * Override of base class pure virtual */ { TPtrC fieldName, fieldVal; TInt merge; if(!GetStringFromConfig(ConfigSection(), KIniFieldName, fieldName)) { ERR_PRINTF1(_L("Unable to read fieldname from ini file")); SetTestStepResult(EFail); } else { TBuf<50> existingFile(KNullDesC); TBuf<50> newFile; TBuf<2> drive; #ifdef __WINS__ drive.Append(_L("c:")); #else TFileName processFileName = RProcess().FileName(); TParsePtrC parse(processFileName); drive.Append(parse.Drive()); #endif if(KErrNone == fieldName.Compare(KAssistance)) { GetIntFromConfig(ConfigSection(), KIniFlagvalue, merge); if(merge==1) { // field present with data existingFile = KExportAsstWithDataFile; // new file with field present used for vCard newFile = KExportAsstWithNewFile; } if(merge==2) { //field absent existingFile.Append(drive); existingFile.Append(KImportAsstMergeMode2File); // new file with field present used for vCard newFile = KExportAsstWithNewFile; } if(merge==3) { // field present existingFile = KExportAsstWithDataFile; // new file with field present but does not have value, used for vCard newFile.Append(drive); newFile.Append(KImportAsstMergeMode3File); } ImportFieldsInMergeModeL(existingFile, newFile, iStorage[0],iFieldUid[0], iVcardUid[0]); } else if(KErrNone == fieldName.Compare(KAssistanceTel)) { GetIntFromConfig(ConfigSection(), KIniFlagvalue, merge); if(merge==1) { // field present with data existingFile = KExportAsstTelWithDataFile; // new file with field present used for vCard newFile = KExportAsstTelWithNewFile; } if(merge==2) { //field absent existingFile.Append(drive); existingFile.Append(KImportAsstTelMergeMode2File); // new file with field present used for vCard newFile.Append(KExportAsstTelWithNewFile); } if(merge==3) { // field present existingFile = KExportAsstTelWithDataFile; // new file with field present but does not have value, used for vCard newFile.Append(drive); newFile.Append(KImportAsstTelMergeMode3File); } ImportFieldsInMergeModeL(existingFile, newFile, iStorage[1],iFieldUid[1], iVcardUid[1]); } else if(KErrNone == fieldName.Compare(KAnniversary)) { GetIntFromConfig(ConfigSection(), KIniFlagvalue, merge); if(merge==1) { // field present with data existingFile = KExportAnniWithDataFile; // new file with field present used for vCard newFile = KExportAnniWithNewFile; } if(merge==2) { //field absent existingFile.Append(drive); existingFile.Append(KImportAnniMergeMode2File); // new file with field present used for vCard newFile = KExportAnniWithNewFile; } if(merge==3) { // field present existingFile = KExportAnniWithDataFile; // new file with field present but does not have value, used for vCard newFile.Append(drive); newFile.Append(KImportAnniMergeMode3File); } ImportFieldsInMergeModeL(existingFile, newFile, iStorage[2],iFieldUid[2], iVcardUid[2]); } else if(KErrNone == fieldName.Compare(KSpouse)) { GetIntFromConfig(ConfigSection(), KIniFlagvalue, merge); if(merge==1) { // field present with data existingFile = KExportSpouseWithDataFile; //new file with field present used for vCard newFile = KExportSpouseWithNewFile; } if(merge==2) { //field absent existingFile.Append(drive); existingFile.Append(KImportSpouseMergeMode2File); //new file with field present used for vCard newFile = KExportSpouseWithNewFile; } if(merge==3) { // field present existingFile = KExportSpouseWithDataFile; //new file with field present but does not have value, used for vCard newFile.Append(drive); newFile.Append(KImportSpouseMergeMode3File); } ImportFieldsInMergeModeL(existingFile, newFile, iStorage[3],iFieldUid[3], iVcardUid[3]); } else if(KErrNone == fieldName.Compare(KChildren)) { GetIntFromConfig(ConfigSection(), KIniFlagvalue, merge); if(merge==1) { // field present with data existingFile = KExportChildrenWithDataFile; //new file with field present used for vCard newFile = KExportChildrenWithNewFile; } if(merge==2) { //field absent existingFile.Append(drive); existingFile.Append(KImportChildrenMergeMode2File); //new file with field present used for vCard newFile = KExportChildrenWithNewFile; } if(merge==3) { // field present existingFile = KExportChildrenWithDataFile; //new file with field present but does not have value, used for vCard newFile.Append(drive); newFile.Append(KImportChildrenMergeMode3File); } ImportFieldsInMergeModeL(existingFile, newFile, iStorage[4],iFieldUid[4], iVcardUid[4]); } else if(KErrNone == fieldName.Compare(KClass)) { GetIntFromConfig(ConfigSection(), KIniFlagvalue, merge); if(merge==1) { // field present with data existingFile = KExportClassWithDataFile; //new file with field present used for vCard newFile = KExportClassWithNewFile; } if(merge==2) { //field absent existingFile.Append(drive); existingFile.Append(KImportClassMergeMode2File); //new file with field present used for vCard newFile = KExportClassWithNewFile; } if(merge==3) { // field present with data existingFile = KExportClassWithDataFile; //new file with field present but does not have value, used for vCard newFile.Append(drive); newFile.Append(KImportClassMergeMode3File); } ImportFieldsInMergeModeL(existingFile, newFile, iStorage[5],iFieldUid[5], iVcardUid[5]); } else if(KErrNone == fieldName.Compare(KCompanyName)) { GetIntFromConfig(ConfigSection(), KIniFlagvalue, merge); if(merge==1) { // field present existingFile = KExportCompanyWithDataFile; //new file with field present used for vCard newFile = KExportCompanyWithNewFile; } if(merge==2) { //field absent existingFile.Append(drive); existingFile.Append(KImportCompanyMergeMode2File); //new file with field present used for vCard newFile = KExportCompanyWithNewFile; } if(merge==3) { // field present existingFile = KExportCompanyWithDataFile; //new file with field present used for vCard newFile.Append(drive); newFile.Append(KImportCompanyMergeMode3File); } ImportFieldsInMergeModeL(existingFile, newFile, iStorage[6],iFieldUid[6], iVcardUid[6]); } else if(KErrNone == fieldName.Compare(KDepartment)) { GetIntFromConfig(ConfigSection(), KIniFlagvalue, merge); if(merge==1) { // field present existingFile = KExportDeptWithDataFile; //new file with field present used for vCard newFile = KExportDeptWithNewFile; } if(merge==2) { //field absent existingFile.Append(drive); existingFile.Append(KImportDeptMergeMode2File); //new file with field present used for vCard newFile = KExportDeptWithNewFile; } if(merge==3) { // field present existingFile = KExportDeptWithDataFile; //new file with field present used for vCard newFile.Append(drive); newFile.Append(KImportDeptMergeMode3File); } ImportFieldsInMergeModeL(existingFile, newFile, iStorage[7],iFieldUid[7], iVcardUid[7]); } else { ERR_PRINTF1(_L("Invalid fieldname")); SetTestStepResult(EFail); } } return TestStepResult(); }