Wittyshare  0.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
WsAbstractTree.cpp
Go to the documentation of this file.
1 
15 #include "WsAbstractTree.h"
16 #include <Include/WsGlobalConfig.h>
17 
18 WsAbstractTree::WsAbstractTree(FileSystemTreePtr fs, const set<string>& gid, const int& md, const path& rootPath, const string& stamp):
19  WsTreeTraversal(fs->getRoot()),
20  m_gid(gid),
21  m_rootPath(rootPath),
22  m_maxDepth(md),
23  m_currentDepth(0),
24  m_stamp(stamp)
25 {
26 }
27 
28 WsAbstractTree::WsAbstractTree(FileSystemTreePtr fs, const set<string>& gid, const int& md, const set<string>& names, const set<string>& ext, const path& rootPath, const string& stamp):
29  WsTreeTraversal(fs->getRoot()),
30  m_gid(gid),
31  m_exclNames(names),
32  m_exclExt(ext),
33  m_rootPath(rootPath),
34  m_maxDepth(md),
35  m_currentDepth(0),
36  m_stamp(stamp)
37 {
38 }
39 
40 WsAbstractTree::WsAbstractTree(NodePtr n, const set<string>& gid, const int& md, const path& rootPath, const string& stamp):
41  WsTreeTraversal(n),
42  m_gid(gid),
43  m_rootPath(rootPath),
44  m_maxDepth(md),
45  m_currentDepth(0),
46  m_stamp(stamp)
47 {
48 }
49 
50 WsAbstractTree::WsAbstractTree(NodePtr n, const set<string>& gid, const int& md, const set<string>& names, const set<string>& ext, const path& rootPath, const string& stamp):
51  WsTreeTraversal(n),
52  m_gid(gid),
53  m_exclNames(names),
54  m_exclExt(ext),
55  m_rootPath(rootPath),
56  m_maxDepth(md),
57  m_currentDepth(0),
58  m_stamp(stamp)
59 {
60 }
61 
63 {
64 }
65 
67 {
69 }
70 
72 {
73  return m_root;
74 }
75 
77 {
78  path rootPath = this->m_rootPath;
79  /* First iteration, no root node */
80  if (m_root.get() == 0 ) {
81  /* Check if user has access to root node */
82  if (!dynamic_cast<WsDirNode*>(n.get())->isAllowed(m_gid)) {
83  return ErrorCode::Failure;
84  }
85  /* We create a new node because children are not necessarly the same for each user, depends on rights */
86  m_root = NodePtr(new WsDirNode(WsTreeTraversal::m_root->getFullPath(), rootPath));
87  m_root.get()->setProperties(dynamic_cast<WsDirNode*>(WsTreeTraversal::m_root.get())->getProperties());
88  m_current = m_root;
89  return ErrorCode::Success;
90  }
91  if (m_maxDepth == 0 || m_currentDepth < m_maxDepth) {
92  /* Check if user can access this node */
93  if (dynamic_cast<WsDirNode*>(n.get())->isAllowed(m_gid)) {
94  /* Check if this name is excluded */
95  if (m_exclNames.count(n.get()->getFullPath().stem().string()) > 0) {
96  LOG(DEBUG) << "WsAbstractTree::beginTraverseDir() : Excluding node " << n.get()->getFullPath();
97  return ErrorCode::Failure;
98  }
99  NodePtr nn(new WsDirNode(n.get()->getFullPath(), rootPath));
100  nn.get()->setProperties(n.get()->getProperties());
101  if (m_current->addChildDirectory(nn) == ErrorCode::Failure) {
102  LOG(DEBUG) << "WsAbstractTree::beginTraverseDir() : Excluding node " << n.get()->getFullPath();
103  return ErrorCode::Failure;
104  }
105  nn.get()->setParent(m_current);
106  m_current = nn;
107  }
108  /* The user cannot access this node, return ErrorCode::Failure
109  * to stop recursion inside subdirs */
110  else {
111  LOG(DEBUG) << "WsAbstractTree::beginTraverseDir() : Not allowed to access " << n->getPath();
112  return ErrorCode::Failure;
113  }
114  m_currentDepth++;
115  } else
116  /* Return Failure to stop recursion into subdirs */
117  return ErrorCode::Failure;
118  return ErrorCode::Success;
119 }
120 
122 {
123  if (m_current.get() != 0 && m_current.get()->getParent() != 0) {
124  m_current = m_current.get()->getParent();
125  }
126  return ErrorCode::Success;
127 }
128 
130 {
131  path rootPath = this->m_rootPath;
132  /* Check if the filename or extension is excluded */
133  if (m_exclNames.count(n.get()->getFullPath().stem().string()) > 0) {
134  LOG(DEBUG) << "WsAbstractTree::traverseFile() : Excluding node " << n.get()->getFullPath();
135  return ErrorCode::Success;
136  }
137  if (m_exclExt.count(n.get()->getFullPath().extension().string()) > 0) {
138  LOG(DEBUG) << "WsAbstractTree::traverseFile() : Excluding node " << n.get()->getFullPath();
139  return ErrorCode::Success;
140  }
141  NodePtr nn(new WsFileNode(n.get()->getFullPath(), rootPath));
142  nn.get()->setProperties(n.get()->getProperties());
143  nn.get()->setCreateDate(dynamic_cast<WsFileNode*>(n.get())->getCreateDate());
144  nn.get()->setModifyDate(dynamic_cast<WsFileNode*>(n.get())->getModifyDate());
145  nn.get()->setSize(dynamic_cast<WsFileNode*>(n.get())->getSize());
146  /* Check if there is no root */
147  if (m_root.get() == 0) {
148  m_root = nn;
149  return ErrorCode::Success;
150  }
151  if (m_current->addChildFile(nn) == ErrorCode::Failure)
152  return ErrorCode::Failure;
153  nn->setParent(m_current);
154  return ErrorCode::Success;
155 }
156 
158 {
159  return this->m_rootPath;
160 }
161 
163 {
164  return m_stamp;
165 }
boost::shared_ptr< WsAbstractNode > NodePtr
virtual const std::string & getStamp()
Get the stamp of the tree.
#define DEBUG
Definition: WsLogger.h:27
std::set< std::string > m_gid
the gids of the user
Reprensents a Directory on disk.
Definition: WsDirNode.h:25
virtual path & getRootPath()
get the root path
path m_rootPath
The rootPath.
const int Failure
int m_maxDepth
The max depth of the tree (ie depth we don't want to exceed.
Reprensents a File on disk.
Definition: WsFileNode.h:23
Used to traverse the tree.
virtual int build()
Parses the fileSystemTree and builds the Tree containing only the nodes where the user has access...
Reprensents the tree of a group. If a group does not have access to a Node, this Node will not show i...
virtual int traverseFile(NodePtr n)
Called when traversing a fileNode.
~WsAbstractTree()
Destructor.
#define LOG
Definition: WsLogger.h:22
int start()
start traversing the Tree.
virtual int beginTraverseDir(NodePtr n)
Called when a directory is being traversed.
WsAbstractTree(FileSystemTreePtr fs, const std::set< std::string > &gid, const int &md, const path &rootPath, const std::string &stamp)
Constructor for the Tree class.
boost::shared_ptr< WsFileSystemTree > FileSystemTreePtr
std::set< std::string > m_exclNames
The excluded names.
virtual NodePtr getRoot()
Returns the root node of the menuTree.
virtual int endTraverseDir(NodePtr n)
Called when a directory has been completely traversed. ie all subnodes(recursively all sub nodes of s...
int m_currentDepth
The current depth of the tree (ie number of levels)
std::set< std::string > m_exclExt
The excluded extensions.
const int Success
NodePtr m_current
The current node (the current level):w.
NodePtr m_root
the root of the tree
std::string m_stamp
The stamp of the tree.