2016-02-19 15:19:15 -06:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
|
|
|
|
/*
|
|
|
|
* This file is part of the LibreOffice project.
|
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Storage abstraction.
|
|
|
|
#ifndef INCLUDED_STORAGE_HPP
|
|
|
|
#define INCLUDED_STORAGE_HPP
|
|
|
|
|
2016-04-16 17:12:10 -05:00
|
|
|
#include <set>
|
2016-10-29 20:15:00 -05:00
|
|
|
#include <string>
|
2016-02-19 15:19:15 -06:00
|
|
|
|
2016-03-31 01:48:34 -05:00
|
|
|
#include <Poco/URI.h>
|
2016-10-29 20:15:00 -05:00
|
|
|
#include <Poco/Util/Application.h>
|
2016-03-03 20:05:30 -06:00
|
|
|
|
2016-02-19 15:19:15 -06:00
|
|
|
#include "Auth.hpp"
|
loolwsd: include cleanup and organization
A source file (.cpp) must include its own header first.
This insures that the header is self-contained and
doesn't depend on arbitrary (and accidental) includes
before it to compile.
Furthermore, system headers should go next, followed by
C then C++ headers, then libraries (Poco, etc) and, finally,
project headers come last.
This makes sure that headers and included in the same dependency
order to avoid side-effects. For example, Poco should never rely on
anything from our project in the same way that a C header should
never rely on anything in C++, Poco, or project headers.
Also, includes ought to be sorted where possible, to improve
readability and avoid accidental duplicates (of which there
were a few).
Change-Id: I62cc1343e4a091d69195e37ed659dba20cfcb1ef
Reviewed-on: https://gerrit.libreoffice.org/25262
Reviewed-by: Ashod Nakashian <ashnakash@gmail.com>
Tested-by: Ashod Nakashian <ashnakash@gmail.com>
2016-05-21 09:23:07 -05:00
|
|
|
#include "Log.hpp"
|
2016-02-19 15:19:15 -06:00
|
|
|
#include "Util.hpp"
|
|
|
|
|
|
|
|
/// Base class of all Storage abstractions.
|
|
|
|
class StorageBase
|
|
|
|
{
|
|
|
|
public:
|
2016-10-26 06:15:28 -05:00
|
|
|
/// Represents basic file's attributes.
|
2016-08-13 23:01:13 -05:00
|
|
|
/// Used for local and network files.
|
2016-03-21 18:12:00 -05:00
|
|
|
class FileInfo
|
|
|
|
{
|
|
|
|
public:
|
2016-09-26 01:44:40 -05:00
|
|
|
FileInfo(const std::string& filename,
|
|
|
|
const Poco::Timestamp& modifiedTime,
|
2016-10-26 06:15:28 -05:00
|
|
|
size_t size)
|
2016-09-26 01:44:40 -05:00
|
|
|
: _filename(filename),
|
|
|
|
_modifiedTime(modifiedTime),
|
2016-10-26 06:15:28 -05:00
|
|
|
_size(size)
|
2016-09-26 01:44:40 -05:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-04-06 23:32:28 -05:00
|
|
|
bool isValid() const
|
|
|
|
{
|
2016-04-19 02:10:07 -05:00
|
|
|
return !_filename.empty() && _size > 0;
|
2016-04-06 23:32:28 -05:00
|
|
|
}
|
|
|
|
|
2016-04-19 02:10:07 -05:00
|
|
|
std::string _filename;
|
|
|
|
Poco::Timestamp _modifiedTime;
|
|
|
|
size_t _size;
|
2016-03-21 18:12:00 -05:00
|
|
|
};
|
|
|
|
|
2016-03-05 09:27:30 -06:00
|
|
|
/// localStorePath the absolute root path of the chroot.
|
|
|
|
/// jailPath the path within the jail that the child uses.
|
2016-10-14 05:09:43 -05:00
|
|
|
StorageBase(const Poco::URI& uri,
|
|
|
|
const std::string& localStorePath,
|
|
|
|
const std::string& jailPath) :
|
|
|
|
_uri(uri),
|
2016-03-05 09:27:30 -06:00
|
|
|
_localStorePath(localStorePath),
|
2016-03-09 17:36:25 -06:00
|
|
|
_jailPath(jailPath),
|
2016-10-26 06:15:28 -05:00
|
|
|
_fileInfo("", Poco::Timestamp(), 0),
|
2016-10-14 05:09:43 -05:00
|
|
|
_isLoaded(false)
|
2016-03-03 20:05:30 -06:00
|
|
|
{
|
2016-10-14 04:28:19 -05:00
|
|
|
Log::debug("Storage ctor: " + uri.toString());
|
2016-03-03 20:05:30 -06:00
|
|
|
}
|
|
|
|
|
2016-03-31 01:48:34 -05:00
|
|
|
std::string getLocalRootPath() const;
|
2016-03-08 18:21:16 -06:00
|
|
|
|
2016-10-14 04:28:19 -05:00
|
|
|
const std::string getUri() const { return _uri.toString(); }
|
2016-03-09 17:36:25 -06:00
|
|
|
|
2016-10-14 05:09:43 -05:00
|
|
|
bool isLoaded() const { return _isLoaded; }
|
|
|
|
|
2016-10-26 06:15:28 -05:00
|
|
|
/// Returns the basic information about the file.
|
|
|
|
virtual FileInfo getFileInfo() { return _fileInfo; }
|
2016-03-21 18:12:00 -05:00
|
|
|
|
|
|
|
/// Returns a local file path for the given URI.
|
2016-02-19 15:19:15 -06:00
|
|
|
/// If necessary copies the file locally first.
|
2016-03-10 21:01:34 -06:00
|
|
|
virtual std::string loadStorageFileToLocal() = 0;
|
2016-02-19 15:19:15 -06:00
|
|
|
|
2016-03-03 20:05:30 -06:00
|
|
|
/// Writes the contents of the file back to the source.
|
2016-10-16 11:40:52 -05:00
|
|
|
virtual bool saveLocalFileToStorage(const Poco::URI& uriPublic) = 0;
|
2016-02-19 15:19:15 -06:00
|
|
|
|
2016-10-29 20:15:00 -05:00
|
|
|
static size_t getFileSize(const std::string& filename);
|
2016-03-11 12:45:13 -06:00
|
|
|
|
2016-04-16 07:13:59 -05:00
|
|
|
/// Must be called at startup to configure.
|
|
|
|
static void initialize();
|
|
|
|
|
|
|
|
/// Storage object creation factory.
|
2016-10-14 05:09:43 -05:00
|
|
|
static std::unique_ptr<StorageBase> create(const Poco::URI& uri,
|
|
|
|
const std::string& jailRoot,
|
|
|
|
const std::string& jailPath);
|
2016-04-07 15:59:27 -05:00
|
|
|
|
2016-03-03 20:05:30 -06:00
|
|
|
protected:
|
2016-10-14 04:28:19 -05:00
|
|
|
const Poco::URI _uri;
|
2016-10-14 05:09:43 -05:00
|
|
|
std::string _localStorePath;
|
|
|
|
std::string _jailPath;
|
2016-03-09 17:36:25 -06:00
|
|
|
std::string _jailedFilePath;
|
2016-03-21 18:12:00 -05:00
|
|
|
FileInfo _fileInfo;
|
2016-10-14 05:09:43 -05:00
|
|
|
bool _isLoaded;
|
2016-04-16 07:13:59 -05:00
|
|
|
|
2016-06-07 02:18:49 -05:00
|
|
|
static bool FilesystemEnabled;
|
|
|
|
static bool WopiEnabled;
|
2016-04-18 20:13:17 -05:00
|
|
|
/// Allowed/denied WOPI hosts, if any and if WOPI is enabled.
|
2016-06-07 02:18:49 -05:00
|
|
|
static Util::RegexListMatcher WopiHosts;
|
2016-02-19 15:19:15 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
/// Trivial implementation of local storage that does not need do anything.
|
|
|
|
class LocalStorage : public StorageBase
|
|
|
|
{
|
|
|
|
public:
|
2016-10-14 05:09:43 -05:00
|
|
|
LocalStorage(const Poco::URI& uri,
|
|
|
|
const std::string& localStorePath,
|
|
|
|
const std::string& jailPath) :
|
|
|
|
StorageBase(uri, localStorePath, jailPath),
|
2016-10-26 06:15:28 -05:00
|
|
|
_isCopy(false)
|
2016-03-05 09:27:30 -06:00
|
|
|
{
|
2016-04-16 11:53:42 -05:00
|
|
|
Log::info("LocalStorage ctor with localStorePath: [" + localStorePath +
|
2016-10-14 04:28:19 -05:00
|
|
|
"], jailPath: [" + jailPath + "], uri: [" + uri.toString() + "].");
|
2016-03-05 09:27:30 -06:00
|
|
|
}
|
2016-02-19 15:19:15 -06:00
|
|
|
|
2016-10-26 06:15:28 -05:00
|
|
|
class LocalFileInfo {
|
|
|
|
public:
|
|
|
|
LocalFileInfo(const std::string& userid,
|
|
|
|
const std::string& username)
|
|
|
|
: _userid(userid),
|
|
|
|
_username(username)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string _userid;
|
|
|
|
std::string _username;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Returns the URI specific file data
|
|
|
|
/// Also stores the basic file information which can then be
|
|
|
|
/// obtained using getFileInfo method
|
|
|
|
LocalFileInfo getLocalFileInfo(const Poco::URI& uriPublic);
|
2016-03-05 09:27:30 -06:00
|
|
|
|
2016-03-31 01:48:34 -05:00
|
|
|
std::string loadStorageFileToLocal() override;
|
2016-03-05 09:27:30 -06:00
|
|
|
|
2016-10-16 11:40:52 -05:00
|
|
|
bool saveLocalFileToStorage(const Poco::URI& uriPublic) override;
|
2016-03-09 17:36:25 -06:00
|
|
|
|
|
|
|
private:
|
|
|
|
/// True if the jailed file is not linked but copied.
|
|
|
|
bool _isCopy;
|
2016-10-26 07:47:42 -05:00
|
|
|
static std::atomic<unsigned> LastLocalStorageId;
|
2016-02-19 15:19:15 -06:00
|
|
|
};
|
|
|
|
|
2016-08-13 23:01:13 -05:00
|
|
|
/// WOPI protocol backed storage.
|
2016-03-03 20:05:30 -06:00
|
|
|
class WopiStorage : public StorageBase
|
|
|
|
{
|
|
|
|
public:
|
2016-10-14 05:09:43 -05:00
|
|
|
WopiStorage(const Poco::URI& uri,
|
|
|
|
const std::string& localStorePath,
|
|
|
|
const std::string& jailPath) :
|
2016-10-14 07:46:49 -05:00
|
|
|
StorageBase(uri, localStorePath, jailPath),
|
|
|
|
_wopiLoadDuration(0)
|
2016-03-03 20:05:30 -06:00
|
|
|
{
|
2016-04-16 11:53:42 -05:00
|
|
|
Log::info("WopiStorage ctor with localStorePath: [" + localStorePath +
|
2016-10-14 04:28:19 -05:00
|
|
|
"], jailPath: [" + jailPath + "], uri: [" + uri.toString() + "].");
|
2016-03-03 20:05:30 -06:00
|
|
|
}
|
|
|
|
|
2016-10-29 20:15:00 -05:00
|
|
|
class WOPIFileInfo
|
|
|
|
{
|
2016-10-26 06:15:28 -05:00
|
|
|
public:
|
|
|
|
WOPIFileInfo(const std::string& userid,
|
|
|
|
const std::string& username,
|
|
|
|
const bool userCanWrite,
|
2016-10-25 02:13:00 -05:00
|
|
|
const std::string& postMessageOrigin,
|
2016-10-26 06:15:28 -05:00
|
|
|
const std::chrono::duration<double> callDuration)
|
|
|
|
: _userid(userid),
|
|
|
|
_username(username),
|
|
|
|
_userCanWrite(userCanWrite),
|
2016-10-25 02:13:00 -05:00
|
|
|
_postMessageOrigin(postMessageOrigin),
|
2016-10-26 06:15:28 -05:00
|
|
|
_callDuration(callDuration)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/// User id of the user accessing the file
|
|
|
|
std::string _userid;
|
|
|
|
/// Display Name of user accessing the file
|
|
|
|
std::string _username;
|
|
|
|
/// If user accessing the file has write permission
|
|
|
|
bool _userCanWrite;
|
2016-10-25 02:13:00 -05:00
|
|
|
/// WOPI Post message property
|
|
|
|
std::string _postMessageOrigin;
|
2016-10-26 06:15:28 -05:00
|
|
|
/// Time it took to call WOPI's CheckFileInfo
|
|
|
|
std::chrono::duration<double> _callDuration;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Returns the response of CheckFileInfo WOPI call for given URI
|
|
|
|
/// Also extracts the basic file information from the response
|
|
|
|
/// which can then be obtained using getFileInfo()
|
|
|
|
WOPIFileInfo getWOPIFileInfo(const Poco::URI& uriPublic);
|
2016-03-21 18:12:00 -05:00
|
|
|
|
2016-03-03 20:05:30 -06:00
|
|
|
/// uri format: http://server/<...>/wopi*/files/<id>/content
|
2016-03-31 01:48:34 -05:00
|
|
|
std::string loadStorageFileToLocal() override;
|
2016-03-11 12:45:13 -06:00
|
|
|
|
2016-10-16 11:40:52 -05:00
|
|
|
bool saveLocalFileToStorage(const Poco::URI& uriPublic) override;
|
2016-10-14 07:46:49 -05:00
|
|
|
|
2016-10-23 07:05:46 -05:00
|
|
|
/// Total time taken for making WOPI calls during load
|
|
|
|
std::chrono::duration<double> getWopiLoadDuration() const { return _wopiLoadDuration; }
|
2016-10-14 07:46:49 -05:00
|
|
|
|
|
|
|
private:
|
2016-10-23 07:05:46 -05:00
|
|
|
// Time spend in loading the file from storage
|
2016-10-14 07:46:49 -05:00
|
|
|
std::chrono::duration<double> _wopiLoadDuration;
|
2016-03-03 20:05:30 -06:00
|
|
|
};
|
|
|
|
|
2016-08-13 23:01:13 -05:00
|
|
|
/// WebDAV protocol backed storage.
|
2016-02-19 15:19:15 -06:00
|
|
|
class WebDAVStorage : public StorageBase
|
|
|
|
{
|
|
|
|
public:
|
2016-10-14 05:09:43 -05:00
|
|
|
WebDAVStorage(const Poco::URI& uri,
|
|
|
|
const std::string& localStorePath,
|
2016-03-05 09:27:30 -06:00
|
|
|
const std::string& jailPath,
|
2016-03-03 20:05:30 -06:00
|
|
|
std::unique_ptr<AuthBase> authAgent) :
|
2016-10-14 05:09:43 -05:00
|
|
|
StorageBase(uri, localStorePath, jailPath),
|
2016-02-19 15:19:15 -06:00
|
|
|
_authAgent(std::move(authAgent))
|
|
|
|
{
|
2016-04-16 11:53:42 -05:00
|
|
|
Log::info("WebDAVStorage ctor with localStorePath: [" + localStorePath +
|
2016-10-14 04:28:19 -05:00
|
|
|
"], jailPath: [" + jailPath + "], uri: [" + uri.toString() + "].");
|
2016-02-19 15:19:15 -06:00
|
|
|
}
|
|
|
|
|
2016-10-26 06:15:28 -05:00
|
|
|
// Implement me
|
|
|
|
// WebDAVFileInfo getWebDAVFileInfo(const Poco::URI& uriPublic);
|
2016-03-21 18:12:00 -05:00
|
|
|
|
2016-03-31 01:48:34 -05:00
|
|
|
std::string loadStorageFileToLocal() override;
|
2016-02-19 15:19:15 -06:00
|
|
|
|
2016-10-16 11:40:52 -05:00
|
|
|
bool saveLocalFileToStorage(const Poco::URI& uriPublic) override;
|
2016-04-16 11:53:42 -05:00
|
|
|
|
2016-02-19 15:19:15 -06:00
|
|
|
private:
|
|
|
|
std::unique_ptr<AuthBase> _authAgent;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|
2016-04-08 03:08:41 -05:00
|
|
|
|
2016-02-19 15:19:15 -06:00
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|