event->setCancelAllowed(true);
}
+void Manager::checkPaths(
+ Api::IPathPtr &src,
+ Api::IPathPtr &dest) {
+ Assert(dest);
+ Assert(src);
+ if (!dest->isAbsolute()) {
+ dest = src->getPath() + *dest;
+ }
+
+ if (src == dest) {
+ ThrowMsg(Commons::PlatformException,
+ "Destination is same as source: " << src->getFullPath());
+ }
+
+ INodePtr parent;
+ Try {
+ parent = Node::resolve(IPath::create(dest->getPath()));
+ }
+ Catch(Commons::PlatformException) {
+ ReThrowMsg(Commons::PlatformException,
+ "Could not get destination's parent node.");
+ }
+
+ if (parent->getType() != NT_DIRECTORY) {
+ ThrowMsg(Commons::PlatformException,
+ "Destination's parent node is not directory.");
+ }
+
+ if (!access(parent->getPath(), AT_WRITE)) {
+ ThrowMsg(Commons::SecurityException,
+ "Not enough permissions to write to destination.");
+ }
+}
+
+bool Manager::pathExists(const std::string &path) {
+ errno = 0;
+ struct stat info;
+ memset(&info, 0, sizeof(struct stat));
+ int status = lstat(path.c_str(), &info);
+ if ((status != 0) && (errno != ENOENT)) {
+ ThrowMsg(Commons::PlatformException,
+ "No access to platform destination node.");
+ }
+ return 0 == status;
+}
+
void Manager::OnRequestReceived(const EventCopyPtr& event)
{
Try {
IPathPtr src = event->getSource();
IPathPtr dest = event->getDestination();
- Assert(dest);
- if (!dest->isAbsolute()) {
- dest = src->getPath() + *dest;
- }
-
- if (src == dest) {
- ThrowMsg(Commons::PlatformException,
- "Destination is same as source: " <<
- srcNode->getPath()->getFullPath());
- }
-
- INodePtr parent;
- Try {
- parent = Node::resolve(IPath::create(dest->getPath()));
- }
- Catch(Commons::PlatformException) {
- ReThrowMsg(Commons::PlatformException,
- "Could not get destination's parent node.");
- }
-
- if (parent->getType() != NT_DIRECTORY) {
- ThrowMsg(Commons::PlatformException,
- "Destination's parent node is not directory.");
- }
-
- if (!access(parent->getPath(), AT_WRITE)) {
- ThrowMsg(Commons::SecurityException,
- "Not enough permissions to copy source node to destination.");
- }
+ checkPaths(src, dest);
std::string realSrc = src->getFullPath();
std::string realDest = dest->getFullPath();
- errno = 0;
- struct stat info;
- memset(&info, 0, sizeof(struct stat));
- int status = lstat(realDest.c_str(), &info);
- if ((status != 0) && (errno != ENOENT)) {
- ThrowMsg(Commons::PlatformException,
- "No access to platform destination node.");
- }
-
- if (0 == status) {
+ if (pathExists(realDest)) {
//no owerwrite flag setted -> exception
if ((event->getOptions() & OPT_OVERWRITE) == 0) {
ThrowMsg(Commons::PlatformException, "Overwrite is not set.");
"Not enough permissions to move source node.");
}
- Assert(dest);
- if (!dest->isAbsolute()) {
- dest = src->getPath() + *dest;
- }
-
- if (src == dest) {
- ThrowMsg(Commons::PlatformException,
- "Destination is same as source: " <<
- srcNode->getPath()->getFullPath());
- }
-
- INodePtr parent;
- Try {
- parent = Node::resolve(IPath::create(dest->getPath()));
- }
- Catch(Commons::PlatformException) {
- ReThrowMsg(Commons::PlatformException,
- "Could not get destination's parent node.");
- }
-
- if (parent->getType() != NT_DIRECTORY) {
- ThrowMsg(Commons::PlatformException,
- "Destination's parent node is not directory.");
- }
-
- if (!access(parent->getPath(), AT_WRITE)) {
- ThrowMsg(Commons::SecurityException,
- "Not enough permissions to move source node to destination.");
- }
+ checkPaths(src, dest);
- errno = 0;
- struct stat info;
- memset(&info, 0, sizeof(info));
- int status = lstat(dest->getFullPath().c_str(), &info);
- if ((status != 0) && (errno != ENOENT)) {
- ThrowMsg(Commons::PlatformException,
- "No access to platform destination node.");
- }
+ bool destExists = pathExists(dest->getFullPath());
- if ((0 == (event->getOptions() & OPT_OVERWRITE)) && (0 == status)) {
+ if (destExists && (0 == (event->getOptions() & OPT_OVERWRITE))) {
ThrowMsg(Commons::PlatformException, "Overwrite is not set.");
}
{
case EXDEV:
{
- if (0 == status) {
+ if (destExists) {
//destination exist. Need to be removed
Try {
INodePtr node = Node::resolve(