namespace WrtDeviceApis {
namespace Filesystem {
-
using namespace Api;
Manager::Locations Manager::m_locations;
}
Manager::~Manager()
-{
-}
+{}
IPathPtr Manager::getBasePath() const
{
}
void Manager::find(const IPathPtr& path,
- const FiltersMap& filters,
- NodeList& result,
- const EventFindPtr& event)
+ const FiltersMap& filters,
+ NodeList& result,
+ const EventFindPtr& event)
{
Try {
Assert(path && "path is NULL");
FTS *fts;
FTSENT *ftsent;
- std::string pth=path->getFullPath();
- char * const paths[] = {const_cast<char * const>(pth.c_str()), NULL};
+ std::string pth = path->getFullPath();
+ char * const paths[] = { const_cast<char * const>(pth.c_str()), NULL };
- if ((fts = fts_open(paths, FTS_PHYSICAL|FTS_NOCHDIR, NULL)) == NULL) {
+ if ((fts =
+ fts_open(paths, FTS_PHYSICAL | FTS_NOCHDIR, NULL)) == NULL)
+ {
//ERROR
int error = errno;
LogError(__PRETTY_FUNCTION__ << ": fts_open on "
- << pth
- << " failed with error: "
- << strerror(error));
+ << pth
+ << " failed with error: "
+ << strerror(error));
return;
}
while ((ftsent = fts_read(fts)) != NULL) {
- if(event && event->checkCancelled()) break;
+ if (event && event->checkCancelled()) {
+ break;
+ }
switch (ftsent->fts_info) {
- case FTS_DP:
- //directory in postorder - do nothing
- break;
- case FTS_D:
- case FTS_DC:
- case FTS_F:
- case FTS_SL:
- case FTS_SLNONE:
- case FTS_DEFAULT:
- //regular files, symbolic links, directories in preorder
- //and other file entries that can be processed further
- if (matchFilters(ftsent->fts_name, *ftsent->fts_statp, filters)) {
- IPathPtr childPath=IPath::create(ftsent->fts_path);
- result.push_back(Node::resolve(childPath));
- }
- break;
- case FTS_NS:
- case FTS_NSOK:
- case FTS_DOT:
- case FTS_DNR:
- case FTS_ERR:
- default:
- LogWarning(__PRETTY_FUNCTION__
- << ": traversal failed with error: "
- << strerror(ftsent->fts_errno));
- ThrowMsg(Commons::PlatformException,
- "Error reading directory");
+ case FTS_DP:
+ //directory in postorder - do nothing
+ break;
+ case FTS_D:
+ case FTS_DC:
+ case FTS_F:
+ case FTS_SL:
+ case FTS_SLNONE:
+ case FTS_DEFAULT:
+ //regular files, symbolic links, directories in preorder
+ //and other file entries that can be processed further
+ if (matchFilters(ftsent->fts_name, *ftsent->fts_statp,
+ filters))
+ {
+ IPathPtr childPath = IPath::create(ftsent->fts_path);
+ result.push_back(Node::resolve(childPath));
+ }
+ break;
+ case FTS_NS:
+ case FTS_NSOK:
+ case FTS_DOT:
+ case FTS_DNR:
+ case FTS_ERR:
+ default:
+ LogWarning(__PRETTY_FUNCTION__
+ << ": traversal failed with error: "
+ << strerror(ftsent->fts_errno));
+ ThrowMsg(Commons::PlatformException,
+ "Error reading directory");
}
}
if (fts_close(fts) == -1) {
int error = errno;
LogWarning(__PRETTY_FUNCTION__ << ": fts_close failed with error: "
- << strerror(error));
+ << strerror(error));
ThrowMsg(Commons::PlatformException,
"Could not close platform node.");
}
}
- Catch(Commons::Exception) {
- }
+ Catch(Commons::Exception) {}
}
void Manager::copyElement(
- const std::string &src, const std::string &dest, bool recursive) const
+ const std::string &src, const std::string &dest, bool recursive) const
{
LogDebug("Copying src: " << src << " to: " << dest);
free(data);
}
}
-
}
bool Manager::access(const IPathPtr& path,
- int accessType) const
+ int accessType) const
{
int amode = 0;
- if (accessType & AT_EXISTS) { amode |= F_OK; }
- if (accessType & AT_READ) { amode |= R_OK; }
- if (accessType & AT_WRITE) { amode |= W_OK; }
- if (accessType & AT_EXEC) { amode |= X_OK; }
+ if (accessType & AT_EXISTS) {
+ amode |= F_OK;
+ }
+ if (accessType & AT_READ) {
+ amode |= R_OK;
+ }
+ if (accessType & AT_WRITE) {
+ amode |= W_OK;
+ }
+ if (accessType & AT_EXEC) {
+ amode |= X_OK;
+ }
return (::access(path->getFullPath().c_str(), amode) == 0);
}
bool Manager::matchFilters(const std::string& name,
- const struct stat& info,
- const FiltersMap& filters)
+ const struct stat& info,
+ const FiltersMap& filters)
{
FiltersMap::const_iterator it = filters.begin();
for (; it != filters.end(); ++it) {
if (it->first == FF_NAME) {
- if (!pcrecpp::RE(it->second).PartialMatch(name)) { return false; }
+ if (!pcrecpp::RE(it->second).PartialMatch(name)) {
+ return false;
+ }
} else if (it->first == FF_SIZE) {
std::size_t size;
std::stringstream ss(it->second);
ss >> size;
if (!ss ||
- (size != static_cast<size_t>(info.st_size))) { return false; }
+ (size != static_cast<size_t>(info.st_size)))
+ {
+ return false;
+ }
} else if (it->first == FF_CREATED) {
std::time_t created;
std::stringstream ss(it->second);
ss >> created;
- if (!ss || (created != info.st_ctime)) { return false; }
+ if (!ss || (created != info.st_ctime)) {
+ return false;
+ }
} else if (it->first == FF_MODIFIED) {
std::time_t modified;
std::stringstream ss(it->second);
ss >> modified;
- if (!ss || (modified != info.st_mtime)) { return false; }
+ if (!ss || (modified != info.st_mtime)) {
+ return false;
+ }
}
}
return true;
{
try {
event->setResult(Node::resolve(event->getPath()));
- }
- catch (const Commons::PlatformException& ex) {
+ } catch (const Commons::PlatformException& ex) {
LogError("Exception: " << ex.GetMessage());
event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
}
}
void Manager::checkPaths(
- Api::IPathPtr &src,
- Api::IPathPtr &dest) {
+ Api::IPathPtr &src,
+ Api::IPathPtr &dest)
+{
Assert(dest);
Assert(src);
if (!dest->isAbsolute()) {
if (!access(parent->getPath(), AT_WRITE)) {
ThrowMsg(Commons::SecurityException,
- "Not enough permissions to write to destination.");
+ "Not enough permissions to write to destination.");
}
}
-bool Manager::pathExists(const std::string &path) {
+bool Manager::pathExists(const std::string &path)
+{
errno = 0;
struct stat info;
memset(&info, 0, sizeof(struct stat));
INodePtr srcNode = Node::resolve(event->getSource());
int requiredAccess;
switch (srcNode->getType()) {
- case NT_DIRECTORY:
- requiredAccess = AT_EXEC;
- break;
- case NT_FILE:
- requiredAccess = AT_READ;
- break;
+ case NT_DIRECTORY:
+ requiredAccess = AT_EXEC;
+ break;
+ case NT_FILE:
+ requiredAccess = AT_READ;
+ break;
}
if (!access(srcNode->getPath(), requiredAccess)) {
ThrowMsg(Commons::SecurityException,
- "Not enough permissions to copy source node.");
+ "Not enough permissions to copy source node.");
}
IPathPtr src = event->getSource();
copyElement(realSrc, realDest);
event->setResult(Node::resolve(dest));
- }
- catch (const Commons::PlatformException& ex) {
+ } catch (const Commons::PlatformException& ex) {
LogError("Exception: " << ex.GetMessage());
event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
- }
- catch (const Commons::SecurityException& ex) {
+ } catch (const Commons::SecurityException& ex) {
LogError("Exception: " << ex.GetMessage());
event->setExceptionCode(Commons::ExceptionCodes::SecurityException);
}
INodePtr srcNode = Node::resolve(src);
if (!access(srcNode->getParent()->getPath(), AT_WRITE)) {
ThrowMsg(Commons::SecurityException,
- "Not enough permissions to move source node.");
+ "Not enough permissions to move source node.");
}
checkPaths(src, dest);
dest->getFullPath().c_str()))
{
int error = errno;
- switch (error)
- {
+ switch (error) {
case EXDEV:
- {
- if (destExists) {
- //destination exist. Need to be removed
- Try {
- INodePtr node = Node::resolve(
- event->getDestination());
- node->remove(event->getOptions());
- }
- Catch(Commons::PlatformException) {
- LogError("Exception while removing dest directory");
- event->setExceptionCode(
- Commons::ExceptionCodes::PlatformException);
- }
- Catch(Commons::SecurityException) {
- event->setExceptionCode(
- Commons::ExceptionCodes::SecurityException);
- }
- }
-
- copyElement(src->getFullPath(),
- dest->getFullPath());
- //remove source files
+ {
+ if (destExists) {
+ //destination exist. Need to be removed
Try {
- INodePtr node = Node::resolve(event->getSource());
+ INodePtr node = Node::resolve(
+ event->getDestination());
node->remove(event->getOptions());
}
- Catch(Commons::Exception) {
- LogError("Exception: "
- << _rethrown_exception.GetMessage());
+ Catch(Commons::PlatformException) {
+ LogError("Exception while removing dest directory");
+ event->setExceptionCode(
+ Commons::ExceptionCodes::PlatformException);
+ }
+ Catch(Commons::SecurityException) {
+ event->setExceptionCode(
+ Commons::ExceptionCodes::SecurityException);
}
- break;
}
+
+ copyElement(src->getFullPath(),
+ dest->getFullPath());
+ //remove source files
+ Try {
+ INodePtr node = Node::resolve(event->getSource());
+ node->remove(event->getOptions());
+ }
+ Catch(Commons::Exception) {
+ LogError("Exception: "
+ << _rethrown_exception.GetMessage());
+ }
+ break;
+ }
default:
ThrowMsg(Commons::PlatformException,
"Error on rename: " << DPL::GetErrnoString(error));
}
event->setResult(Node::resolve(dest));
- }
- catch (const Commons::PlatformException& ex) {
+ } catch (const Commons::PlatformException& ex) {
LogError("Exception: " << ex.GetMessage());
event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
- }
- catch (const Commons::SecurityException& ex) {
+ } catch (const Commons::SecurityException& ex) {
LogError("Exception: " << ex.GetMessage());
event->setExceptionCode(Commons::ExceptionCodes::SecurityException);
}
NodeList result;
find(event->getPath(), event->getFilters(), result, event);
event->setResult(result);
- }
- catch (const Commons::Exception& ex) {
+ } catch (const Commons::Exception& ex) {
LogError("Exception: " << ex.GetMessage());
event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
}
}
void Manager::setupLocation(LocationType location,
- const char* path)
+ const char* path)
{
if (!nodeExists(path)) {
try {
makePath(path, 0755);
- }
- catch (const Commons::PlatformException& ex) {
+ } catch (const Commons::PlatformException& ex) {
LogError("Exception: " << ex.DumpToString());
return;
}
}
m_locations[location] = IPath::create(path);
}
-
} // Filesystem
} // WrtDeviceApis