#include #include #include #include #include #include #include #include #include #include "daemonVolume.h" #include "daemonFile.h" #include "daemonDirectory.h" #include "lockHelpers.h" #include "ioHelpers.h" #include #include #include "daemon.h" extern std::map files; VolumeServer::VolumeServer(const boost::filesystem::path & r) : root(r) { } VolumeServer::~VolumeServer() { } void VolumeServer::disconnect(const Ice::Current & ice) { ice.adapter->remove(ice.id); } Ice::Int VolumeServer::access(const NetFS::ReqEnv & re, const std::string & path, Ice::Int mode, const Ice::Current &) { TempPrivs tp(re, root); struct stat s; boost::filesystem::path p(resolvePath(path)); if (::stat(p.string().c_str(), &s) != 0) { return errno; } if (mode == F_OK) { // stat succeeded, file must exist return 0; } tp.AssertRead(p.parent_path()); if (mode & R_OK && !ReadableBy(s, tp.myu, tp.myg)) { return EACCES; } if (mode & W_OK && !WritableBy(s, tp.myu, tp.myg)) { return EACCES; } if (mode & X_OK && !ExecutableBy(s, tp.myu, tp.myg)) { return EACCES; } return 0; } NetFS::Attr VolumeServer::getattr(const NetFS::ReqEnv & re, const std::string & path, const Ice::Current &) { TempPrivs tp(re, root); struct stat s; boost::filesystem::path p(resolvePath(path)); tp.AssertRead(p.parent_path()); if (::lstat(p.string().c_str(), &s) != 0) { throw NetFS::SystemError(errno); } NetFS::Attr a; a << StatSource { s, boost::bind(&UserEntCache::getName, &UserEntCache::instance, _1, _2), boost::bind(&GroupEntCache::getName, &GroupEntCache::instance, _1, _2) }; return a; } void VolumeServer::mknod(const NetFS::ReqEnv & re, const std::string & path, Ice::Int mode, Ice::Int dev, const Ice::Current&) { TempPrivs tp(re, root); errno = 0; boost::filesystem::path p(resolvePath(path)); tp.AssertWrite(p.parent_path()); if (::mknod(p.string().c_str(), mode, dev) != 0) { throw NetFS::SystemError(errno); } } void VolumeServer::symlink(const NetFS::ReqEnv & re, const std::string & path1, const std::string & path2, const Ice::Current &) { TempPrivs tp(re, root); errno = 0; boost::filesystem::path p(resolvePath(path2)); tp.AssertWrite(p.parent_path()); if (::symlink(path1.c_str(), p.string().c_str()) != 0) { throw NetFS::SystemError(errno); } if (::lchown(p.string().c_str(), tp.myu, tp.myg) != 0) { ::unlink(p.string().c_str()); throw NetFS::SystemError(errno); } } void VolumeServer::link(const NetFS::ReqEnv & re, const std::string & path1, const std::string & path2, const Ice::Current &) { TempPrivs tp(re, root); errno = 0; boost::filesystem::path p1(resolvePath(path1)); boost::filesystem::path p2(resolvePath(path2)); if (::link(p1.string().c_str(), p2.string().c_str()) != 0) { throw NetFS::SystemError(errno); } if (::chown(p2.string().c_str(), tp.myu, tp.myg) != 0) { ::unlink(p2.string().c_str()); throw NetFS::SystemError(errno); } } void VolumeServer::rename(const NetFS::ReqEnv & re, const std::string & from, const std::string & to, const Ice::Current &) { TempPrivs tp(re, root); errno = 0; boost::filesystem::path f(resolvePath(from)); boost::filesystem::path t(resolvePath(to)); tp.AssertWrite(f.parent_path()); tp.AssertWrite(f); if (boost::filesystem::is_directory(t)) { tp.AssertWrite(t); } else { tp.AssertWrite(t.parent_path()); } if (::rename(f.string().c_str(), t.string().c_str()) != 0) { throw NetFS::SystemError(errno); } } std::string VolumeServer::readlink(const NetFS::ReqEnv & re, const std::string & path, const Ice::Current &) { TempPrivs tp(re, root); errno = 0; char buf[PATH_MAX]; boost::filesystem::path p(resolvePath(path)); tp.AssertRead(p); ssize_t rc = ::readlink(p.string().c_str(), buf, PATH_MAX); if (rc == -1) { throw NetFS::SystemError(errno); } return std::string(buf, rc); } void VolumeServer::chmod(const NetFS::ReqEnv & re, const std::string & path, Ice::Int mode, const Ice::Current &) { TempPrivs tp(re, root); errno = 0; boost::filesystem::path p(resolvePath(path)); tp.AssertWrite(p); if (::chmod(p.string().c_str(), mode) != 0) { throw NetFS::SystemError(errno); } } void VolumeServer::chown(const NetFS::ReqEnv & re, const std::string & path, Ice::Int uid, Ice::Int gid, const Ice::Current &) { TempPrivs tp(re, root); errno = 0; boost::filesystem::path p(resolvePath(path)); tp.AssertWrite(p); if (::lchown(p.string().c_str(), uid, gid) != 0) { throw NetFS::SystemError(errno); } } void VolumeServer::utimens(const NetFS::ReqEnv & re, const std::string & path, Ice::Long s0, Ice::Long ns0, Ice::Long s1, Ice::Long ns1, const Ice::Current&) { TempPrivs tp(re, root); errno = 0; struct timespec times[2]; times[0].tv_sec = s0; times[0].tv_nsec = ns0; times[1].tv_sec = s1; times[1].tv_nsec = ns1; boost::filesystem::path p(resolvePath(path)); tp.AssertWrite(p); if (::utimensat(0, p.string().c_str(), times, AT_SYMLINK_NOFOLLOW) != 0) { throw NetFS::SystemError(errno); } } NetFS::VFS VolumeServer::statfs(const NetFS::ReqEnv & re, const std::string & path, const Ice::Current&) { TempPrivs tp(re, root); errno = 0; struct statvfs s; boost::filesystem::path p(resolvePath(path)); tp.AssertRead(p); if (::statvfs(p.string().c_str(), &s) != 0) { throw NetFS::SystemError(errno); } NetFS::VFS t; t << s; return t; } void VolumeServer::truncate(const NetFS::ReqEnv & re, const std::string & path, Ice::Long size, const Ice::Current&) { TempPrivs tp(re, root); errno = 0; boost::filesystem::path p(resolvePath(path)); tp.AssertWrite(p); if (::truncate(p.string().c_str(), size) != 0) { throw NetFS::SystemError(errno); } } void VolumeServer::unlink(const NetFS::ReqEnv & re, const std::string & path, const Ice::Current&) { TempPrivs tp(re, root); errno = 0; boost::filesystem::path p(resolvePath(path)); tp.AssertWrite(p); tp.AssertWrite(p.parent_path()); if (::unlink(p.string().c_str()) != 0) { throw NetFS::SystemError(errno); } } NetFS::ReadOnlyFilePrx VolumeServer::openReadOnly(const NetFS::ReqEnv & re, const std::string & path, Ice::Int flags, const Ice::Current & ice) { return open(re, path, flags, ice); } NetFS::FilePrx VolumeServer::open(const NetFS::ReqEnv & re, const std::string & path, Ice::Int flags, const Ice::Current & ice) { TempPrivs tp(re, root); errno = 0; boost::filesystem::path p(resolvePath(path)); tp.AssertRead(p); int fd = ::open(p.string().c_str(), flags); if (fd == -1) { throw NetFS::SystemError(errno); } return NetFS::FilePrx::checkedCast(ice.adapter->addWithUUID(new FileServer(fd))); } NetFS::FilePrx VolumeServer::create(const NetFS::ReqEnv & re, const std::string & path, Ice::Int flags, Ice::Int mode, const Ice::Current & ice) { TempPrivs tp(re, root); errno = 0; boost::filesystem::path p(resolvePath(path)); tp.AssertWrite(p.parent_path()); int fd = ::open(p.string().c_str(), O_CREAT | flags, mode); if (fd == -1) { throw NetFS::SystemError(errno); } if (fchown(fd, tp.myu, tp.myg) != 0) { ::close(fd); ::unlink(p.string().c_str()); throw NetFS::SystemError(errno); } return NetFS::FilePrx::checkedCast(ice.adapter->addWithUUID(new FileServer(fd))); } NetFS::DirectoryPrx VolumeServer::opendir(const NetFS::ReqEnv & re, const std::string & path, const Ice::Current & ice) { TempPrivs tp(re, root); errno = 0; boost::filesystem::path p(resolvePath(path)); tp.AssertRead(p); DIR * od = ::opendir(p.string().c_str()); if (!od) { throw NetFS::SystemError(errno); } return NetFS::DirectoryPrx::checkedCast(ice.adapter->addWithUUID(new DirectoryServer(od))); } void VolumeServer::mkdir(const NetFS::ReqEnv & re, const std::string & path, Ice::Int mode, const Ice::Current&) { TempPrivs tp(re, root); errno = 0; boost::filesystem::path p(resolvePath(path)); tp.AssertWrite(p.parent_path()); if (::mkdir(p.string().c_str(), mode) != 0) { throw NetFS::SystemError(errno); } if (::chown(p.string().c_str(), tp.myu, tp.myg) != 0) { ::rmdir(p.string().c_str()); throw NetFS::SystemError(errno); } } void VolumeServer::rmdir(const NetFS::ReqEnv & re, const std::string & path, const Ice::Current&) { TempPrivs tp(re, root); errno = 0; boost::filesystem::path p(resolvePath(path)); tp.AssertWrite(p); tp.AssertWrite(p.parent_path()); if (::rmdir(p.string().c_str()) != 0) { throw NetFS::SystemError(errno); } } boost::filesystem::path VolumeServer::resolvePath(const std::string & path) const { Lock(lock); return root / path; }