void ToersteBaseWorker::insertFileInfo(const QString &path) { if( !fileDatabase.isOpen() ) { qDebug() << "ToersteBase: Error: Database is not open"; return; } QFileInfo fileInfo(path); if ( isFileIndexed(path) ) { return; } QSqlQuery query; QString sqlError; QString autoComplete = fileInfo.fileName() + " ( " + QDir::toNativeSeparators(fileInfo.canonicalPath()) + QDir::separator() + fileInfo.fileName() + " )"; QString queryString = "INSERT INTO file (filePath, autoComplete) VALUES ('" + path + "', '" + autoComplete + "')"; qDebug() << "ToersteBase: inserting file info for" << fileInfo.fileName() << "with path" << fileInfo.canonicalPath(); qDebug() << "ToersteBase: SQL query:" << queryString; query.prepare(queryString); query.exec(); sqlError = query.lastError().databaseText(); if ( !sqlError.isEmpty() ) { qDebug() << "ToersteBase: SQL error:" << sqlError; } }
void Plugin::addFile(const std::string filepath) { logger().debug("Adding file"); // Check if file exists and it's not a directory File file(filepath); if (!file.exists()) { std::cout << "Failed to add file " << filepath.c_str() << ": file does not exist" << std::endl; return; } if (file.isDirectory()) { std::cout << "Failed to add file " << filepath.c_str() << ": file is a directory" << std::endl; return; } File index(Plugin::GIT_BIN_INDEX); if (!index.exists()) { index.createFile(); } // Find this file in the index if (isFileIndexed(filepath) && file.isLink()) { logger().debug("File already in cache"); auto entry = getIndexEntry(filepath); if (entry == nullptr) { std::cout << "Failed to retrieve index meta data for " << filepath << std::endl; return; } // Not fresh. Update existing index file // Remove file from index before recalculating everything for (auto it = _index.begin(); it != _index.end();) { if ((*it).filepath == filepath) { // Remove link std::cout << "Replacing link with original file" << std::endl; file.remove(); Process::Args restoreArgs; std::string restorePath(Plugin::GIT_CACHE_DIR); restorePath.append("/wf/"); restorePath.append((*it).uuid); restoreArgs.push_back(restorePath); restoreArgs.push_back(filepath); Process::launch("mv", restoreArgs, 0, 0, 0); it = _index.erase(it); } else { it++; } } } // Add new file into index UUIDGenerator gen; IndexEntry e; e.filepath = filepath; e.md5 = getFileMd5(filepath); e.uuid = gen.createRandom().toString(); do { e.uuid = gen.createRandom().toString(); } while (!isUuidUnique(e.uuid)); _index.push_back(e); writeIndex(); // Place two copies of this file into cache // One copy is original file and don't tracked in any way // Second copy is a file we will create link to Process::Args args; args.push_back(filepath); std::string origPath(Plugin::GIT_CACHE_DIR); origPath.append("/of/").append(e.uuid); args.push_back(origPath); Poco::ProcessHandle copyProcess = Process::launch("cp", args, 0, 0, 0); if (copyProcess.wait() != 0) { std::cout << "Failed to move file into git-bin cache" << std::endl; return; } // Second stage copy args.clear(); args.push_back(filepath); std::string workPath(Plugin::GIT_CACHE_DIR); workPath.append("/wf/").append(e.uuid); args.push_back(workPath); copyProcess = Process::launch("mv", args, 0, 0, 0); if (copyProcess.wait() != 0) { std::cout << "Failed to move file on stage 2" << std::endl; return; } // Make a link args.clear(); args.push_back("-s"); Path rel(filepath); std::string relPath; for (int i = 0; i < rel.depth(); i++) { relPath.append("../"); } relPath.append(workPath); args.push_back(relPath); args.push_back(filepath); Poco::ProcessHandle linkProcess = Process::launch("ln", args, 0, 0, 0); if (linkProcess.wait() != 0) { std::cout << "Failed to create link to file" << std::endl; return; } args.clear(); args.push_back("add"); args.push_back(filepath); Poco::ProcessHandle gitAddProcess = Process::launch("git", args, 0, 0, 0); if (gitAddProcess.wait() != 0) { std::cout << "Failed to add file into git index (git add)" << std::endl; return; } }