loolwsd: FileTransferHandler no longer need it.

This commit is contained in:
Henry Castro 2015-07-17 16:55:49 -04:00
parent 99ca81a944
commit 404b5edbc9
2 changed files with 32 additions and 105 deletions

View file

@ -504,29 +504,46 @@ void MasterProcessSession::dispatchChild()
if (!aUri.empty() && aUri.getScheme() == "file")
{
// The process is jail rooted, so it requests loolMain process to transfer files.
Path aSrcFile(aUri.getPath());
Path aDstFile(Path(getJailPath(childSession->_childId), jailDocumentURL.substr(1)), aSrcFile.getFileName());
Path aDstPath(getJailPath(childSession->_childId), jailDocumentURL.substr(1));
Path aJailFile(jailDocumentURL, aSrcFile.getFileName());
try
{
Path aSrcFile(aUri.getPath());
Path aDstFile(Path(getJailPath(childSession->_childId), jailDocumentURL.substr(1)), aSrcFile.getFileName());
std::string sCopy(aSrcFile.toString() + " " + aDstFile.toString());
std::string str;
DialogSocket ds;
ds.connect(SocketAddress("127.0.0.1", LOOLWSD::FILE_PORT_NUMBER));
ds.sendMessage(sCopy);
ds.receiveMessage(str);
if (str != "OK")
Application::instance().logger().error( Util::logPrefix() +
"DataSocket copyTo(\"" + aSrcFile.toString() + "\",\"" + aDstFile.toString() + "\") failed: " + str);
File(aDstPath).createDirectories();
}
catch (Exception& exc)
{
Application::instance().logger().error( Util::logPrefix() +
"FileTransferHanlder failed: " + exc.displayText());
"createDirectories(\"" + aDstPath.toString() + "\") failed: " + exc.displayText() );
}
#ifdef __linux
Application::instance().logger().information(Util::logPrefix() + "Linking " + aSrcFile.toString() + " to " + aDstFile.toString());
if (link(aSrcFile.toString().c_str(), aDstFile.toString().c_str()) == -1)
{
// Failed
Application::instance().logger().error( Util::logPrefix() +
"link(\"" + aSrcFile.toString() + "\",\"" + aDstFile.toString() + "\") failed: " + strerror(errno) );
}
#endif
try
{
//fallback
if (!File(aDstFile).exists())
{
Application::instance().logger().information(Util::logPrefix() + "Copying " + aSrcFile.toString() + " to " + aDstFile.toString());
File(aSrcFile).copyTo(aDstFile.toString());
}
}
catch (Exception& exc)
{
Application::instance().logger().error( Util::logPrefix() +
"copyTo(\"" + aSrcFile.toString() + "\",\"" + aDstFile.toString() + "\") failed: " + exc.displayText());
}
}
_peer = childSession;

View file

@ -411,92 +411,6 @@ private:
HTTPServer& _srv;
};
class FileTransferHandler : public Runnable
{
public:
FileTransferHandler() : _socket(ServerSocket(LOOLWSD::FILE_PORT_NUMBER))
{
}
void run() override
{
Poco::Timespan span(250000);
while (true)
{
if (_socket.poll(span, Socket::SELECT_READ))
{
DialogSocket ds = _socket.acceptConnection();
try
{
std::string command;
while (ds.receiveMessage(command))
{
FastMutex::ScopedLock lock(_mutex);
ds.sendMessage(transferFile(command));
}
}
catch (Poco::Exception& exc)
{
std::cerr << "FileTransferHandler: " << exc.displayText() << std::endl;
}
}
}
}
std::string transferFile(std::string command)
{
StringTokenizer tokens(command, " ", StringTokenizer::TOK_IGNORE_EMPTY | StringTokenizer::TOK_TRIM);
if ( tokens.count() != 2 )
return "Souce and Destination is needed :" + command;
Path aSrcFile(tokens[0]);
Path aDstFile(tokens[1]);
Path aDstPath(aDstFile.parent());
try
{
File(aDstPath).createDirectories();
}
catch (Exception& exc)
{
return exc.displayText();
}
#ifdef __linux
Application::instance().logger().information(Util::logPrefix() + "Linking " + aSrcFile.toString() + " to " + aDstFile.toString());
if (link(aSrcFile.toString().c_str(), aDstFile.toString().c_str()) == -1)
{
// Failed
Application::instance().logger().error( Util::logPrefix() +
"link(\"" + aSrcFile.toString() + "\",\"" + aDstFile.toString() + "\") failed: " + strerror(errno) );
}
#endif
try
{
//fallback
if (!File(aDstFile).exists())
{
Application::instance().logger().information(Util::logPrefix() + "Copying " + aSrcFile.toString() + " to " + aDstFile.toString());
File(aSrcFile).copyTo(aDstFile.toString());
}
}
catch (Exception& exc)
{
return exc.displayText();
}
return "OK";
}
private:
Poco::Net::ServerSocket _socket;
mutable Poco::FastMutex _mutex;
};
int LOOLWSD::portNumber = DEFAULT_CLIENT_PORT_NUMBER;
std::string LOOLWSD::cache = LOOLWSD_CACHEDIR;
std::string LOOLWSD::sysTemplate;
@ -1118,10 +1032,6 @@ void LOOLWSD::loolMain()
namedMutexLOOL.unlock();
Thread threadFile;
FileTransferHandler svrFile;
threadFile.start(svrFile);
while (MasterProcessSession::_childProcesses.size() > 0)
{
int status;