int FileTransferServer::stopThread(const std::string& transferid,const int& pid) { int result=0; if (pid != -1) { result = kill(pid, SIGKILL); if (result) { updateStatus(vishnu::TRANSFER_FAILED, transferid, strerror(errno)); throw FMSVishnuException(ERRCODE_RUNTIME_ERROR,strerror(errno)); } // get the user responsible for the stop request std::string sessionId = msessionServer.getAttribut("where sessionkey='"+FileTransferServer::getDatabaseInstance()->escapeData((msessionServer.getData()).getSessionKey())+"'", "vsessionid"); std::string sqlCommand="SELECT userid,vsessionid " " FROM users,vsession " " WHERE vsession.users_numuserid=users.numuserid" " AND vsessionid='"+ FileTransferServer::getDatabaseInstance()->escapeData(sessionId)+"'"; boost::scoped_ptr<DatabaseResult> dbResult(FileTransferServer::getDatabaseInstance()->getResult(sqlCommand)); std::string logMsg= "by: "+ dbResult->getFirstElement(); updateStatus(vishnu::TRANSFER_CANCELLED, transferid, logMsg); } return result; }
// A file copy thread int FileTransferServer::addCpThread(const std::string& srcUser, const std::string& srcMachineName, const std::string& srcUserKey, const std::string& destUser, const std::string& destMachineName, const FMS_Data::CpFileOptions& options) { mtransferType=File::copy; addTransferThread(srcUser,srcMachineName,srcUserKey, destUser, destMachineName, options); waitThread(); std::string errorMsg(getErrorFromDatabase(mfileTransfer.getTransferId())); if (false == errorMsg.empty()) { throw FMSVishnuException (ERRCODE_RUNTIME_ERROR,errorMsg); } return 0; }
// Cancel file transfers int FileTransferServer::stopThread(const FMS_Data::StopTransferOptions& options) { if (! options.getTransferId().empty() || ! options.getUserId().empty() || ! options.getFromMachineId().empty()) { std::string sqlListOfPid = "SELECT transferId,processId " " FROM filetransfer, vsession " " WHERE vsession.numsessionid=filetransfer.vsession_numsessionid " " AND filetransfer.status=0"; std::string transferid; std::vector<std::string>::iterator iter; std::vector<std::string> results; msessionServer.check(); processOptions(options, sqlListOfPid); boost::scoped_ptr<DatabaseResult> ListOfPid (FileTransferServer::getDatabaseInstance()->getResult(sqlListOfPid.c_str())); if (ListOfPid->getNbTuples() != 0){ for (size_t i = 0; i < ListOfPid->getNbTuples(); ++i) { int pid; results.clear(); results = ListOfPid->get(i); iter = results.begin(); transferid = *iter; ++iter; pid = vishnu::convertToInt(*iter); stopThread(transferid,pid); ++iter; } } else { throw FMSVishnuException (ERRCODE_RUNTIME_ERROR, "There is no file transfer in progress "); } } return 0; }
// To add a new file transfer thread int FileTransferServer::addTransferThread(const std::string& srcUser, const std::string& srcMachineName, const std::string& srcUserKey, const std::string& destUser, const std::string& destMachineName, const FMS_Data::CpFileOptions& options) { updateData(); // update datas and get the vishnu transfer id int direction; if (vishnu::ifLocalTransferInvolved(srcMachineName, destMachineName, direction)) { mfileTransfer.setStatus(vishnu::TRANSFER_WAITING_CLIENT_RESPONSE); updateDatabaseRecord(); if (destUser.empty()) { std::string tmp = mfileTransfer.getSourceFilePath(); mfileTransfer.setSourceFilePath(srcUser+"@"+srcMachineName+":"+tmp); } else { std::string tmp = mfileTransfer.getDestinationFilePath(); mfileTransfer.setDestinationFilePath(destUser+"@"+destMachineName+":"+tmp); } LOG("[INFO] request transfer from client size", 1); return 0; } FMS_Data::CpFileOptions optionsCopy(options); int timeout (0); //FIXE: get timeout from config // if (options.getTrCommand() == vishnu::UNDEFINED_TRANSFER_MANAGER) { // std::string sessionId = msessionServer.getAttribut("where sessionkey='"+FileTransferServer::getDatabaseInstance()->escapeData((msessionServer.getData()).getSessionKey())+"'", "vsessionid"); // std::string query="SELECT users.numuserid,users_numuserid,vsessionid from users,vsession " // " WHERE vsession.users_numuserid=users.numuserid " // " AND vsessionid='"+ FileTransferServer::getDatabaseInstance()->escapeData(sessionId)+"'"; // boost::scoped_ptr<DatabaseResult> dbResult(FileTransferServer::getDatabaseInstance()->getResult(query)); // if (dbResult->getNbTuples() != 0) { // std::string numuserId= dbResult->getFirstElement(); // OptionValueServer optionValueServer; // optionsCopy.setTrCommand(optionValueServer.getOptionValueForUser(numuserId, TRANSFERCMD_OPT)); // timeout = optionValueServer.getOptionValueForUser(numuserId, TRANSFER_TIMEOUT_OPT); // } // } boost::scoped_ptr<FileTransferCommand> transferManager( FileTransferCommand::getTransferManager(optionsCopy, timeout)); boost::scoped_ptr<SSHFile> srcFileServer( new SSHFile(msessionServer, mfileTransfer.getSourceFilePath(), srcMachineName, srcUser, "", srcUserKey, "", FileTransferServer::getSSHPort(), FileTransferServer::getSSHCommand(), transferManager->getLocation())); mfileTransfer.setTrCommand(optionsCopy.getTrCommand()); mfileTransfer.setStatus(vishnu::TRANSFER_INPROGRESS); //INPPROGRESS if (! srcFileServer->exists() || ! srcFileServer->getErrorMsg().empty()) { mfileTransfer.setStatus(vishnu::TRANSFER_FAILED); mfileTransfer.setSize(0); mfileTransfer.setStartTime(0); mfileTransfer.setErrorMsg(srcFileServer->getErrorMsg()); updateDatabaseRecord(); throw FMSVishnuException(ERRCODE_RUNTIME_ERROR,srcFileServer->getErrorMsg()); } mfileTransfer.setSize(srcFileServer->getSize()); mfileTransfer.setStartTime(0); if ( srcUser==destUser && srcMachineName == destMachineName && mfileTransfer.getSourceFilePath() == mfileTransfer.getDestinationFilePath()) { mfileTransfer.setStatus(vishnu::TRANSFER_FAILED); //failed mfileTransfer.setErrorMsg("same source and destination"); updateDatabaseRecord(); throw FMSVishnuException(ERRCODE_RUNTIME_ERROR, "same source and destination "); } // create a TransferExec instance TransferExec transferExec(msessionServer, srcUser, srcMachineName, mfileTransfer.getSourceFilePath(), srcUserKey, destUser, destMachineName, mfileTransfer.getDestinationFilePath(), mfileTransfer.getTransferId()); updateDatabaseRecord(); // create the thread to perform the copy if (mtransferType == File::copy) { mthread = boost::thread(&FileTransferServer::copy, this, transferExec, transferManager->getCommand()); } else if (mtransferType == File::move) { mthread = boost::thread(&FileTransferServer::move, this, transferExec, transferManager->getCommand()); } return 0; }
/** * \brief Function to check the remote file path * \param path the remote file path * \return raises an exception on error */ void vishnu::checkRemotePath(const std::string& path){ if (path.find(":")==std::string::npos){ throw FMSVishnuException(ERRCODE_INVALID_PATH, "The path must be a remote file and in the form machineId:path"); } }
/* The function returns all the information about a file: * - The local owner * - The local group * - The local uid & gid * - The creation, modification and acces time. * - The file type. */ int solveGetInfos(diet_profile_t* profile) { std::string path = ""; std::string host = ""; std::string sessionKey = ""; std::string localPath, userKey, machineName; std::string cmd = ""; std::string fileStatSerialized = ""; diet_string_get(profile, 0, sessionKey); diet_string_get(profile, 1, path); diet_string_get(profile, 2, host); // reset the profile to handle result diet_profile_reset(profile, 2); localPath = path; SessionServer sessionServer (sessionKey); try { int mapperkey; //MAPPER CREATION Mapper *mapper = MapperRegistry::getInstance()->getMapper(vishnu::FMSMAPPERNAME); mapperkey = mapper->code("vishnu_stat"); mapper->code(host + ":" + path, mapperkey); cmd = mapper->finalize(mapperkey); // check the sessionKey sessionServer.check(); UMS_Data::Machine_ptr machine = new UMS_Data::Machine(); machine->setMachineId(host); MachineServer machineServer(machine); // check the machine machineServer.checkMachine(); // get the machineName machineName = machineServer.getMachineName(); delete machine; std::string acLogin = UserServer(sessionServer).getUserAccountLogin(host); FileFactory ff; ff.setSSHServer(machineName); boost::scoped_ptr<File> file (ff.getFileServer(sessionServer,localPath, acLogin, userKey)); boost::scoped_ptr<FMS_Data::FileStat> fileStat_ptr (new FMS_Data::FileStat()); if ( file->exists()) { *fileStat_ptr=file->getFileStat(); ::ecorecpp::serializer::serializer _ser; fileStatSerialized = _ser.serialize_str(const_cast<FMS_Data::FileStat_ptr>(fileStat_ptr.get())); } else { throw FMSVishnuException(ERRCODE_RUNTIME_ERROR, static_cast<SSHFile*>(file.get())->getErrorMsg()); } // set success result diet_string_set(profile, 1, fileStatSerialized); diet_string_set(profile, 0, "success"); //To register the command sessionServer.finish(cmd, vishnu::FMS, vishnu::CMDSUCCESS); } catch (VishnuException& err) { try { sessionServer.finish(cmd, vishnu::FMS, vishnu::CMDFAILED); } catch (VishnuException& fe) { err.appendMsgComp(fe.what()); } // set error result diet_string_set(profile, 0, "error"); diet_string_set(profile, 1, err.what()); } return 0; }