Sleds/cppcore/component/storagenode.h

486 lines
15 KiB
C++

// Copyright (c) 2013, IOnU Security, Inc.
// Copyright (c) 2016, Sequence Logic, Inc. All rights reserved.
/**
* A StorageNode object.
*/
#ifndef STORAGE_NODE
#define STORAGE_NODE
#include <string>
#include <set>
#include <vector>
#include <memory>
#include "storagenodestatus.h"
#include "storagenodetype.h"
#include "../../cppjson/jsonobject.h"
#ifdef WIN32
#ifdef SL_CPPCORE_DLLEXPORT
#define SL_CPPCORE_EXPORT __declspec(dllexport)
#else
#define SL_CPPCORE_EXPORT __declspec(dllimport)
#endif
#else
#define SL_CPPCORE_EXPORT
#endif
namespace sequencelogic
{
class CloudGuardURN;
class Permissions;
class DocumentServiceIF;
class EyeTime;
class StorageLocation;
#if defined(WIN32) || defined(_WIN32)
#pragma warning(push)
#pragma warning(disable:4251 4275) // Disable the dumbass XXXX needs to have dll-interface.
#endif
class StorageNode;
typedef std::shared_ptr<StorageNode> StorageNodePtr;
typedef std::vector<StorageNodePtr> StorageNodeVector;
typedef std::shared_ptr<Permissions> PermissionsPtr;
/*
* The StorageNode object. Contains a rapidjson::Document object.
*/
class SL_CPPCORE_EXPORT StorageNode // : public JSONObject
{
public:
/*
* Empty constructor. Create an empty storage node.
*/
StorageNode ();
/*
* Copy constructor.
*/
StorageNode (const StorageNodePtr &pNode);
StorageNode (const StorageNode &node);
/*
* Copy constructor.
*/
StorageNode (const JSONObjectPtr &pNode);
StorageNode (const JSONObject &node);
/*
* Create a StorageNode from a JSON string.
*/
StorageNode (const std::string &nodeString);
/*
* Destructor. Destroys the JSON object.
*/
~StorageNode() {}
StorageNode& operator=(const StorageNode &rhs);
/*
* Create a pending node! Caller owns the return pointer, and is responsible for cleanup!
*/
static StorageNodePtr createPendingNode(const CloudGuardURN &pendingUrn, const std::string &path, const std::string &name);
/*
* Create an empty node! Caller owns the return pointer, and is responsible for cleanup!
*/
static StorageNodePtr createEmptyNode();
/*
* Set the name of this node.
*/
void setName (const std::string &name);
/*
* Get the name of this node.
*/
std::string getName () const;
/*
* Set the version of this node.
*/
void setVersion (int nVer);
/*
* Get the version of this node.
*/
int getVersion () const;
/**
* @details
* Node names are not guaranteed to be unique. The end user is allowed
* to create document 'd1.txt' on device x and create document 'd1.txt'
* on device y. If the given device Id does not match the node device
* Id, returns uuid plus node name. For example, on device x the
* unique names are 'd1.txt' and '{a uuid}.d1.txt' respectively.
*/
std::string getUniqueName(const std::string& deviceId) const;
/*
* Set the mime type of this node.
*/
void setMimeType (const std::string &mimeType);
/*
* Get the mime type of this node.
*/
std::string getMimeType () const;
/*
* Set the length of this node.
*/
void setLength (unsigned int length);
/*
* Get the length of this node.
*/
unsigned int getLength () const;
/*
* Set the last access time for this node.
*/
void setLastAccessed (const EyeTime &lastAccessed);
void setLastAccessed (const std::string &lastAccessed);
/*
* Get the last access time for this node.
*/
EyeTime getLastAccessed () const;
/*
* Set the last modified time for this node.
*/
void setLastModified (const EyeTime &lastModified);
void setLastModified (const std::string &lastModified);
/*
* Get the last modified time for this node.
*/
EyeTime getLastModified () const;
/*
* Set the utime for this node.
*/
void setutime (const EyeTime &utime);
void setutime (const std::string &lastModified);
/*
* Get the mtime for this node.
*/
sequencelogic::EyeTime getmtime () const;
/*
* Set the mtime for this node.
*/
void setmtime (const EyeTime &mtime);
void setmtime (const std::string &lastModified);
/*
* Is 'mtime' set on this node?
*/
bool hasmtime() const;
/*
* Get the utime for this node.
*/
EyeTime getutime() const;
/*
* Set the creation time for this node.
*/
void setCreation (const EyeTime &created);
void setCreation (const std::string &created);
/*
* Get the creation time for this node.
*/
EyeTime getCreation () const;
/*
* Set the host for this node.
*/
void setHost (const std::string &host);
/*
* Get the host for this node.
*/
std::string getHost () const;
/*
* Set the URN.
*/
void setURN (const std::string &mount);
/*
* Get the URN.
*/
std::string getURN () const;
/*
* Set the mount.
*/
void setMount (const std::string &mount);
/*
* Get the mount.
*/
std::string getMount () const;
/*
* Set the source.
*/
void setSource (const std::string &source);
/*
* Get the source.
*/
std::string getSource () const;
/*
* Set the path.
*/
void setPath (const std::string &path);
/*
* Get the path.
*/
std::string getPath () const;
/*
* Set the folder.
*/
void setFolder (const std::string &folder);
/*
* Get the folder.
*/
std::string getFolder () const;
/*
* Set the parent.
*/
void setParent (const std::string &parent);
/*
* Set the parents from a JSONArray.
*/
void setParents(const JSONArray &parents);
/*
* Get the parent.
*/
std::string getParent () const;
/*
* Set this node's status.
*/
void setStatus (StorageNodeStatus status);
void setStatus (const std::string &status);
/*
* Get this node's status.
*/
StorageNodeStatus getStatus () const;
/*
* Set this node's type.
*/
void setType (StorageNodeType type);
void setType (const std::string &type);
/*
* Get this node's type.
*/
StorageNodeType getType () const;
/*
* Set this node's meta type.
*/
void setMetaType (const std::string &metaType);
/*
* Get this node's meta type.
*/
std::string getMetaType () const;
/*
* Set this node's sync status.
*/
void setSync (bool sync);
/*
* Get this node's sync status.
*/
bool getSync () const;
void setSignature(const std::string &sig);
/*
* Set and get 'hash' for the node.
*/
void setHash(const std::string &hash);
std::string getHash() const;
/*
* Get this node's permissions.
*/
std::string getSignature() const;
/**
get children storage node objects
*/
StorageNodeVector getChildren() const;
/**
add children to this node
@param children vector of StorageNodePtrs
*/
void setChildren(const StorageNodeVector & children);
/**
add a child to the array of children
@param child - the child
*/
void addChild(const StorageNode & child);
/**
Get metadata associated with EyeFile pointed to by StorageNode
@returns metadata as string
*/
JSONObject getMetadata() const;
/**
Set metadata associated with EyeFile pointed to by StorageNode
@param metaData - metadata as string
*/
void setMetadata(const JSONObject & metadata);
/**
Set/get the size of the node.
*/
void setSize(int nSize);
int getSize() const;
/**
Set/get the absolute path of the node.
*/
void setAbsPath(const std::string &absPath);
std::string getAbsPath() const;
/**
Set/get the description of the node.
*/
void setDescription(const std::string &desc);
std::string getDescription() const;
/**
Set/get the view for the node.
*/
void setView(const std::string &view);
std::string getView() const;
/**
Set/get the port for the node.
*/
void setPort(int nPort);
int getPort() const;
/**
Set/get the isDirectory bool for the node.
*/
void setIsDir(bool bIsDir);
bool getIsDir() const;
/**
Set/get the filename for the node.
*/
void setFilename(const std::string &filename);
std::string getFilename() const;
/**
* Set/get permisisons.
*/
/*
void setPermissions(const Permissions &perms);
PermissionsPtr getPermissions() const;
*/
/**
* @brief Returns the L1 location data for this node
*/
StorageLocation getLocation() const;
/**
* @brief Returns the remote location data for this node
*/
StorageLocation getRemoteLocation() const;
/**
* @brief Sets local host and remote location data
* @details
* Overrides all existing location data.
*/
void setLocations(std::pair<StorageLocation,StorageLocation>& p);
std::string toDebugString() const;
std::string toString() const;
/**
Is this node empty?
@returns true if empty
*/
bool isEmpty() const;
/**
* Merge this node with the node passed in.
*/
void mergeWith(const StorageNode &node);
void mergeWith(const JSONObject &node);
static std::string name (const StorageNode &obj) { return obj.getName(); }
static StorageNodeStatus status (const StorageNode &obj) { return obj.getStatus(); }
static std::string description (const StorageNode &obj) { return obj.getDescription(); }
static StorageNodeType type (const StorageNode &obj) { return obj.getType(); }
static bool isDirectory (const StorageNode &obj) { return StorageNodeTypeUtil::isDirectory(type(obj)); }
static bool isFile (const StorageNode &obj) { return StorageNodeTypeUtil::isFile(type(obj)); }
static bool isRemoteFileOrDirectory (const StorageNode &obj);
/**
Add member to StorageNode JSON
@param key - the key
@param value - the value
*/
void setKeyValue(const char *pKey, int nVal) { _pData->setJSONValue(pKey, nVal); }
void setKeyValue(const char *pKey, unsigned int nVal) { _pData->setJSONValue(pKey, nVal); }
void setKeyValue(const char *pKey, long long nVal) { _pData->setJSONValue(pKey, nVal); }
void setKeyValue(const char *pKey, unsigned long long nVal) { _pData->setJSONValue(pKey, nVal); }
void setKeyValue(const char *pKey, double nVal) { _pData->setJSONValue(pKey, nVal); }
void setKeyValue(const char *pKey, const char *pVal) { _pData->setJSONValue(pKey, pVal); }
void setKeyValue(const char *pKey, bool bVal) { _pData->setJSONValue(pKey, bVal); }
void setKeyValue(const std::string &key, int nVal) { _pData->setJSONValue(key, nVal); }
void setKeyValue(const std::string &key, unsigned int nVal) { _pData->setJSONValue(key, nVal); }
void setKeyValue(const std::string &key, long long nVal) { _pData->setJSONValue(key, nVal); }
void setKeyValue(const std::string &key, unsigned long long nVal) { _pData->setJSONValue(key, nVal); }
void setKeyValue(const std::string &key, double nVal) { _pData->setJSONValue(key, nVal); }
void setKeyValue(const std::string &key, bool bVal) { _pData->setJSONValue(key, bVal); }
/**
Get member from StorageNode JSON
@param key - the key
@returns the value
*/
std::string getString(const char *pKey) { return _pData->getJSONString(pKey); }
int getInt(const char *pKey) const { return _pData->getJSONInt(pKey); }
unsigned int getUInt(const char *pKey) const { return _pData->getJSONUInt(pKey); }
long long getInt64(const char *pKey) const { return _pData->getJSONInt64(pKey); }
unsigned long long getUInt64(const char *pKey) const { return _pData->getJSONUInt64(pKey); }
double getDouble(const char *pKey) const { return _pData->getJSONDouble(pKey); }
std::string getString(const std::string &key) { return _pData->getJSONString(key.c_str()); }
int getInt(const std::string &key) const { return _pData->getJSONInt(key); }
unsigned int getUInt(const std::string &key) const { return _pData->getJSONUInt(key); }
long long getInt64(const std::string &key) const { return _pData->getJSONInt64(key); }
unsigned long long getUInt64(const std::string &key) const { return _pData->getJSONUInt64(key); }
double getDouble(const std::string &key) const { return _pData->getJSONDouble(key); }
private:
std::unique_ptr<JSONObject> _pData;
};
#if defined(WIN32) || defined(_WIN32)
#pragma warning(pop)
#endif
SL_CPPCORE_EXPORT std::ostream& operator<<(std::ostream &out, const StorageNode &node);
};
#endif