Wittyshare  0.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
WsFsTreeModification.cpp
Go to the documentation of this file.
1 /*
2  *
3  * Filename: WsFsTreeModification.cpp
4  *
5  * Description:
6  *
7  * Created: 03/21/2013 04:18:54 AM
8  * Revision: none
9  *
10  * Author: Benoit Daccache (BD), ben.daccache@gmail.com
11  * Company:
12  *
13  */
14 
15 #include "WsFsTreeModification.h"
16 
18 #include <Search/WsSearch.h>
19 #include <boost/lexical_cast.hpp>
20 
21 
23  m_updater(updater)
24 {
26 }
27 
28 int WsFsTreeModification::saveProperties( const std::set<std::string>& groups, const std::string& json, const std::string& path)
29 {
31  NodePtr n;
32  n = ft->eatPath(path);
33  if ( n.get() == 0) {
34  /* No access */
35  return ErrorCode::NotFound;
36  }
37  if (!canEdit(n, groups)) return ErrorCode::NoAccess;
38  /* Set the root of the properties to the new Json */
39  n.get()->getProperties().get()->setRoot(json);
40  /* save it */
41  if (n.get()->getProperties().get()->save() == ErrorCode::Failure ) {
42  return ErrorCode::Failure;
43  }
44  return ErrorCode::Success;
45 }
46 
47 int WsFsTreeModification::saveProperty( const std::set<std::string>& groups, const std::string& path, const std::string& section, const std::string& attr, const std::string& val)
48 {
50  NodePtr n;
51  n = ft->eatPath(path);
52  if ( n.get() == 0) {
53  /* No access */
54  return ErrorCode::NotFound;
55  }
56  if (!canEdit(n, groups)) return ErrorCode::NoAccess;
57  /* Set property */
58  n.get()->getProperties().get()->set(section, attr, val);
59  /* save it */
60  if (n.get()->getProperties().get()->save() == ErrorCode::Failure ) {
61  return ErrorCode::Failure;
62  }
63  return ErrorCode::Success;
64 }
65 
66 int WsFsTreeModification::createNode( const std::set<std::string>& groups, const string& uid, const string& p, int type)
67 {
68  NodePtr n;
69  boost::filesystem::path parent = p;
70  boost::filesystem::path root = m_updater->getLastTree()->getRootPath();
71  parent = parent.parent_path();
73  /* Test if parent directory exists */
74  n = ft->eatPath(parent.string());
75  if ( n.get() == 0) {
76  LOG(ERROR) << "WsFsTreeClient::createNode() : Parent Node not found " << parent.string();
77  return ErrorCode::NotFound;
78  }
79  /* Test if parent is a dirNode */
80  if ( !n.get()->isDirectory()) {
81  LOG(ERROR) << "WsFsTreeClient::createNode() : Parent Node is not a directory " << parent.string();
82  return ErrorCode::Failure;
83  }
84 
85  if (!canEdit(n, groups)) return ErrorCode::NoAccess;
86 
87  /* Check if new node already exit */
88  if (ft->eatPath(p).get() != 0) {
89  LOG(INFO) << "WsFsTreeClient::createNode() : Node already exist" << p;
90  return ErrorCode::Failure;
91  }
92  switch (type) {
93  case 0: { /* Create a FileNode */
94  fstream f;
95  /* Create the file on disk */
96  f.open( string(root.string() + p).c_str(), ios::out );
97  if (!f.is_open())
98  return ErrorCode::Failure;
99  f.close();
100  /* Create Node and properties */
101  WsFileNode* fn = new WsFileNode(root / p, root);
102  NodePropertiesPtr props(new WsNodeProperties(root / path(p), WsNodeProperties::File));
103  fn->setProperties(props);
104  NodePtr nn = NodePtr(fn);
105  /* Insert Node */
106  m_updater->getLastTree()->insertNode(nn);
107  return ErrorCode::Success;
108  }
109  case 1: { /* DirNode */
110  /* Create Node */
111  try {
112  /* Create the directory on disk */
113  boost::filesystem::create_directory(root / path(p));
114  } catch (std::exception& e) {
115  LOG(ERROR) << "WsFsTreeClient::createNode() : could not create node " << e.what();
116  return ErrorCode::Failure;
117  }
118  /* Create Config Files */
119  try {
120  boost::filesystem::create_directory(root / path(p) / GlobalConfig::PathToConfig);
121  NodePropertiesPtr fp(new WsNodeProperties( root / path(p), WsNodeProperties::Dir));
122  set<string> groups;
123  groups.insert(uid);
124  fp->setGroups(groups);
125  fp->save();
126  WsDirNode* dn = new WsDirNode(root / p, root);
127  dn->setProperties(fp);
128  NodePtr nn = NodePtr(dn);
129  /* Insert the new Node */
130  m_updater->getLastTree()->insertNode(nn);
131  return ErrorCode::Success;
132  } catch (std::exception& e) {
133  LOG(ERROR) << "WsFsTreeClient::createNode() : could not create node " << e.what();
134  return ErrorCode::Failure;
135  }
136  }
137  default:
138  return ErrorCode::Failure;
139  }
140 }
141 
142 int WsFsTreeModification::deleteNode( const std::set<std::string>& groups, const string& uid, const string& p)
143 {
144  NodePtr n;
146  n = ft->eatPath(p);
147  if ( n.get() == 0) {
148  LOG(ERROR) << "WsFsTreeClient::deleteNode() : Node not found " << p;
149  return ErrorCode::NotFound;
150  }
151  if (!canEdit(n, groups)) return ErrorCode::Failure;
152  /* Check if file or directory exist */
153  if ( !boost::filesystem::exists(ft->getRootPath() / p)) {
154  LOG(ERROR) << "WsFsTreeClient::deleteNode() : Node do not exist" << p;
155  return ErrorCode::NoAccess;
156  }
157  if ( n.get()->isRegularFile()) {
158  /* Remove config file if it exists */
159  std::string propFileName = n.get()->getName() + ".json";
160  if (exists(ft->getRootPath() / n.get()->getPath().parent_path() / GlobalConfig::PathToNodeProperties / propFileName)) {
161  try {
162  boost::filesystem::remove_all(ft->getRootPath() / n.get()->getPath().parent_path() / GlobalConfig::PathToNodeProperties / propFileName );
163  } catch (std::exception& e) {
164  LOG(ERROR) << "WsFsTreeClient::deleteNode() : could not delete node " << propFileName << " : " << e.what();
165  return ErrorCode::Failure;
166  }
167  LOG(INFO) << "WsFsTreeClient::deleteNode() : Removing config files for " << p;
168  }
169  std::string lockFileName = n.get()->getName() + ".lock";
170  if (exists(ft->getRootPath() / n.get()->getPath().parent_path() / GlobalConfig::PathToNodeLock / lockFileName)) {
171  try {
172  boost::filesystem::remove_all(ft->getRootPath() / n.get()->getPath().parent_path() / GlobalConfig::PathToNodeLock / lockFileName );
173  } catch (std::exception& e) {
174  LOG(ERROR) << "WsFsTreeClient::deleteNode() : could not delete node " << lockFileName << " : " << e.what();
175  return ErrorCode::Failure;
176  }
177  LOG(INFO) << "WsFsTreeClient::deleteNode() : Removing lock files for " << p;
178  }
179  }
180  /* Remove Node */
181  try {
182  boost::filesystem::remove_all(ft->getRootPath() / p);
183  } catch (std::exception& e) {
184  LOG(ERROR) << "WsFsTreeClient::deleteNode() : could not create node " << e.what();
185  return ErrorCode::Failure;
186  }
187  LOG(INFO) << "WsFsTreeClient :: Removing Node " << p;
188  return ErrorCode::Success;
189 }
190 
191 int WsFsTreeModification::renameNode( const std::set<std::string>& groups, const string& uid, const string& p, const string& newPath)
192 {
193  path root = m_updater->getLastTree()->getRootPath();
194  NodePtr n;
196  n = ft->eatPath(p);
197  if ( n.get() == 0) {
198  LOG(ERROR) << "WsFsTreeClient::renameNode() : Node not found " << p;
199  return ErrorCode::NotFound;
200  }
201  /* Check if parent is a dir */
202  if (!n.get()->getParent()->isDirectory()) {
203  LOG(DEBUG) << "WsFsDaemon::renameNode() : Rename dest parent not a dir";
204  return ErrorCode::Failure;
205  }
206  if (!canEdit(n, groups)) return ErrorCode::Failure;
207  /* Check if node exist */
208  if ( !boost::filesystem::exists(root / p)) {
209  LOG(ERROR) << "WsFsTreeClient::renameNode() : Node do not exist" << p;
210  return ErrorCode::NotFound;
211  }
212  /* Check if destination folder exist */
213  if ( !boost::filesystem::exists(root / path(newPath).parent_path())) {
214  LOG(ERROR) << "WsFsTreeClient::renameNode() : Parent dest node do not exist" << p;
215  return ErrorCode::NotFound;
216  }
217  /* Check if destination path already exist */
218  if ( boost::filesystem::exists(root / newPath)) {
219  LOG(ERROR) << "WsFsTreeClient::renameNode() : Destination already exist" << newPath;
220  return ErrorCode::Failure;
221  }
222  if (n.get()->isRegularFile() ) {
223  /* Move config if exist */
224  string oldConfFilename = path(p).filename().string() + GlobalConfig::ConfExt;
225  path oldConfPath = root / path(p).parent_path() / path(GlobalConfig::PathToNodeProperties);
226  string newConfFilename = path(newPath).filename().string() + GlobalConfig::ConfExt;
227  path newConfPath = root / path(newPath).parent_path() / path(GlobalConfig::PathToNodeProperties);
228  if (exists(oldConfPath / oldConfFilename)) {
229  try {
230  /* Move configuraton file */
231  rename(oldConfPath / oldConfFilename , newConfPath / newConfFilename);
232  } catch (std::exception& e) {
233  LOG(ERROR) << "WsFsTreeClient::renameNode() : could not create node " << e.what();
234  return ErrorCode::Failure;
235  }
236  }
237  }
238  /* Rename node */
239  try {
240  boost::filesystem::rename(ft->getRootPath() / p, ft->getRootPath() / newPath );
241  if (n->isDirectory()) {
242  /* Create a new DirNode and properties */
243  NodePropertiesPtr props(new WsNodeProperties(root / newPath, WsNodeProperties::Dir));
244  WsDirNode* dn = new WsDirNode(root / newPath, root);
245  dn->setProperties(props);
246  NodePtr nn = NodePtr(dn);
247  /* Insert it in tree */
248  if ( m_updater->getLastTree()->insertNode(nn) == ErrorCode::Failure)
249  return ErrorCode::Failure;
250  return ErrorCode::Success;
251  } else {
252  /* Create a new FileNode and properties */
253  NodePropertiesPtr props(new WsNodeProperties(root / newPath, WsNodeProperties::File));
254  WsFileNode* dn = new WsFileNode(root / newPath, root);
255  dn->setProperties(props);
256  NodePtr nn = NodePtr(dn);
257  /* Insert it in tree */
258  if ( m_updater->getLastTree()->insertNode(nn) == ErrorCode::Failure)
259  return ErrorCode::Failure;
260  return ErrorCode::Success;
261  }
262  } catch (std::exception& e) {
263  LOG(ERROR) << "WsFsTreeClient::renameNode() : could not create node " << e.what();
264  return ErrorCode::Failure;
265  }
266  return ErrorCode::Success;
267 }
268 
269 bool WsFsTreeModification::isEditor(const std::set<std::string>& groups)
270 {
271  return groups.count(m_conf->get("global", "editor_group", "editor")) > 0;
272 }
273 
274 bool WsFsTreeModification::isAdministrator(const std::set<std::string>& groups)
275 {
276  return groups.count(m_conf->get("global", "admin_group", "administrator")) > 0;
277 }
278 
279 bool WsFsTreeModification::canEdit(NodePtr node, std::set<std::string> groups)
280 {
281  /* If allowed and editor or if admin return true */
282  if ((node.get()->isAllowed(groups) && groups.count(m_conf->get("global", "editor_group", "editor")) > 0)
283  || groups.count(m_conf->get("global", "admin_group", "administrator")) > 0){
284  LOG(DEBUG)<<"WsFsTreeModification::canEdit() : user can edit ";
285  return true;
286  }
287  LOG(DEBUG)<<"WsFsTreeModification::canEdit() : user cannot edit ";
288  return false;
289 }
Global properties.
int renameNode(const std::set< std::string > &groups, const std::string &uid, const string &path, const string &newPath)
renames a node
const std::string PathToConfig
WsFsTreeModification(WsFsTreeUpdater *updater)
Constructor.
boost::shared_ptr< WsAbstractNode > NodePtr
#define DEBUG
Definition: WsLogger.h:27
int saveProperty(const std::set< std::string > &groups, const std::string &path, const std::string &section, const std::string &attr, const std::string &val)
save a property of the node. The user must have access and edit rights for the node.
Reprensents a Directory on disk.
Definition: WsDirNode.h:25
const int Failure
int createNode(const std::set< std::string > &groups, const std::string &uid, const string &path, int type)
create a directory or File. If the node is a WsDirNode than it will be only accessible to the Admin a...
Reprensents a File on disk.
Definition: WsFileNode.h:23
Properties of a WsNode.
handles the update of the FileSystemTree
const std::string PathToNodeProperties
const int NotFound
FileSystemTreePtr getLastTree()
returns the last created tree
void setProperties(NodePropertiesPtr properties)
set the properties of the node. The properties are a WsAbstractProperties object. ...
Definition: WsNode.cpp:165
#define LOG
Definition: WsLogger.h:22
WsFsTreeUpdater * m_updater
WsGlobalProperties * m_conf
#define INFO
Definition: WsLogger.h:32
const int NoAccess
int deleteNode(const std::set< std::string > &groups, const std::string &uid, const string &path)
delete a node. The user must be an Admin on editor to remove the node
boost::shared_ptr< WsFileSystemTree > FileSystemTreePtr
std::string get(const std::string &section, const std::string &id, const std::string &def)
const std::string PathToNodeLock
bool isEditor(const std::set< std::string > &groups)
bool isAdministrator(const std::set< std::string > &groups)
boost::shared_ptr< WsNodeProperties > NodePropertiesPtr
static WsGlobalProperties * instance()
int saveProperties(const std::set< std::string > &groups, const std::string &json, const std::string &path)
save the properties of the node. The user must have access and edit rights for the node...
const int Success
const std::string ConfExt
#define ERROR
Definition: WsLogger.h:42
bool canEdit(NodePtr node, std::set< std::string > groups)
return true if the user can edit false otherwise