summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2017-10-27 01:28:33 +0100
committerDan Goodliffe <dan@randomdan.homeip.net>2017-12-16 20:51:55 +0000
commitf0caf372067d4c9b235deab1894a5940d8aa10e1 (patch)
tree8a5eb66359d7ef8ebe23450ed3bc3a91f27d3fd8
parentUse namespace NetFS { ... } in files and dirs (diff)
downloadnetfs-f0caf372067d4c9b235deab1894a5940d8aa10e1.tar.bz2
netfs-f0caf372067d4c9b235deab1894a5940d8aa10e1.tar.xz
netfs-f0caf372067d4c9b235deab1894a5940d8aa10e1.zip
Single [template] implementation of proxy wrapper maps for open files and directories
-rw-r--r--netfs/fuse/fuseApp.cpp3
-rw-r--r--netfs/fuse/fuseApp.h18
-rw-r--r--netfs/fuse/fuseApp.impl.h43
-rw-r--r--netfs/fuse/fuseDirs.cpp38
-rw-r--r--netfs/fuse/fuseFiles.cpp49
5 files changed, 78 insertions, 73 deletions
diff --git a/netfs/fuse/fuseApp.cpp b/netfs/fuse/fuseApp.cpp
index 17319ec..436df25 100644
--- a/netfs/fuse/fuseApp.cpp
+++ b/netfs/fuse/fuseApp.cpp
@@ -17,8 +17,7 @@ namespace AdHoc {
NetFS::FuseApp::FuseApp(const Ice::StringSeq & a) :
args(a),
sessionOpened(false),
- openDirID(0),
- openFileID(0),
+ openHandleId(0),
converter(userLookup, groupLookup)
{
}
diff --git a/netfs/fuse/fuseApp.h b/netfs/fuse/fuseApp.h
index 2169e85..1e43166 100644
--- a/netfs/fuse/fuseApp.h
+++ b/netfs/fuse/fuseApp.h
@@ -104,13 +104,14 @@ namespace NetFS {
virtual NetFS::Client::ResourcePtr configureFromUri(const std::string &) const;
private:
- void setProxy(OpenFilePtr, uint64_t & fh);
- OpenFilePtr getFileProxy(uint64_t localID) const;
- void clearFileProxy(uint64_t localID);
-
- void setProxy(OpenDirPtr, uint64_t & fh);
- OpenDirPtr getDirProxy(uint64_t localID) const;
- void clearDirProxy(uint64_t localID);
+ template<typename Handle>
+ void setProxy(const Handle &, uint64_t & fh);
+ template<typename Handle>
+ Handle getProxy(uint64_t localID);
+ template<typename Handle>
+ void clearProxy(uint64_t localID);
+ template<typename Handle>
+ std::map<int, Handle> & getMap();
ReqEnv reqEnv();
@@ -127,9 +128,8 @@ namespace NetFS {
std::string mountPoint;
OpenDirs openDirs;
- int openDirID;
OpenFiles openFiles;
- int openFileID;
+ int openHandleId;
EntCache<User> userLookup;
EntCache<Group> groupLookup;
diff --git a/netfs/fuse/fuseApp.impl.h b/netfs/fuse/fuseApp.impl.h
new file mode 100644
index 0000000..66fe3b2
--- /dev/null
+++ b/netfs/fuse/fuseApp.impl.h
@@ -0,0 +1,43 @@
+#ifndef NETFS_FUSE_COMMON_H
+#define NETFS_FUSE_COMMON_H
+
+#include "fuseApp.h"
+#include <lockHelpers.h>
+#include <safeMapFind.h>
+
+namespace NetFS {
+ template<typename Handle>
+ void
+ FuseApp::setProxy(const Handle & od, uint64_t & fh)
+ {
+ auto & map = getMap<Handle>();
+ Lock(_lock);
+ while (map.find(fh = ++openHandleId) != map.end()) ;
+ map.emplace(fh, od);
+ }
+
+ template<typename Handle>
+ Handle
+ FuseApp::getProxy(uint64_t localID)
+ {
+ const auto & map = getMap<Handle>();
+ SharedLock(_lock);
+ auto i = map.find(localID);
+ if (i != map.end()) {
+ return i->second;
+ }
+ throw NetFS::SystemError(EBADF);
+ }
+
+ template<typename Handle>
+ void
+ FuseApp::clearProxy(uint64_t localID)
+ {
+ auto & map = getMap<Handle>();
+ Lock(_lock);
+ map.erase(localID);
+ }
+}
+
+#endif
+
diff --git a/netfs/fuse/fuseDirs.cpp b/netfs/fuse/fuseDirs.cpp
index f872b79..3325375 100644
--- a/netfs/fuse/fuseDirs.cpp
+++ b/netfs/fuse/fuseDirs.cpp
@@ -1,4 +1,4 @@
-#include "fuseApp.h"
+#include "fuseApp.impl.h"
#include <lockHelpers.h>
#include <entCache.h>
@@ -10,30 +10,11 @@ FuseApp::OpenDir::OpenDir(DirectoryPrx r, const std::string & p) :
{
}
-void
-NetFS::FuseApp::setProxy(OpenDirPtr od, uint64_t & fh)
+template<>
+std::map<int, FuseApp::OpenDirPtr> &
+FuseApp::getMap<FuseApp::OpenDirPtr>()
{
- Lock(_lock);
- while (openDirs.find(fh = ++openDirID) != openDirs.end()) ;
- openDirs.insert({ fh, od });
-}
-
-NetFS::FuseApp::OpenDirPtr
-NetFS::FuseApp::getDirProxy(uint64_t localID) const
-{
- SharedLock(_lock);
- OpenDirs::const_iterator i = openDirs.find(localID);
- if (i != openDirs.end()) {
- return i->second;
- }
- throw NetFS::SystemError(EBADF);
-}
-
-void
-NetFS::FuseApp::clearDirProxy(uint64_t localID)
-{
- Lock(_lock);
- openDirs.erase(localID);
+ return openDirs;
}
int
@@ -41,7 +22,7 @@ FuseApp::opendir(const char * p, struct fuse_file_info * fi)
{
try {
auto remote = volume->opendir(reqEnv(), p);
- setProxy(new OpenDir(remote, p), fi->fh);
+ setProxy<OpenDirPtr>(new OpenDir(remote, p), fi->fh);
return 0;
}
catch (SystemError & e) {
@@ -53,12 +34,13 @@ int
FuseApp::releasedir(const char *, struct fuse_file_info * fi)
{
try {
- auto remote = getDirProxy(fi->fh)->remote;
+ auto remote = getProxy<OpenDirPtr>(fi->fh)->remote;
remote->close();
- clearDirProxy(fi->fh);
+ clearProxy<OpenDirPtr>(fi->fh);
return 0;
}
catch (SystemError & e) {
+ clearProxy<OpenDirPtr>(fi->fh);
return -e.syserrno;
}
}
@@ -67,7 +49,7 @@ int
FuseApp::readdir(const char * p, void * buf, fuse_fill_dir_t filler, off_t, struct fuse_file_info * fi)
{
try {
- auto od = getDirProxy(fi->fh);
+ auto od = getProxy<OpenDirPtr>(fi->fh);
std::string path(p);
path += "/";
auto expiry = time(NULL) + 2;
diff --git a/netfs/fuse/fuseFiles.cpp b/netfs/fuse/fuseFiles.cpp
index 2fc6b26..350e74f 100644
--- a/netfs/fuse/fuseFiles.cpp
+++ b/netfs/fuse/fuseFiles.cpp
@@ -1,5 +1,5 @@
#include <string.h>
-#include "fuseApp.h"
+#include "fuseApp.impl.h"
#include "lockHelpers.h"
#include <entCache.h>
@@ -11,30 +11,11 @@ FuseApp::OpenFile::OpenFile(FilePrx r, const std::string & p, int f) :
{
}
-void
-NetFS::FuseApp::setProxy(OpenFilePtr of, uint64_t & fh)
-{
- Lock(_lock);
- while (openFiles.find(fh = ++openFileID) != openFiles.end()) ;
- openFiles.insert({ fh, of });
-}
-
-NetFS::FuseApp::OpenFilePtr
-NetFS::FuseApp::getFileProxy(uint64_t localID) const
-{
- SharedLock(_lock);
- OpenFiles::const_iterator i = openFiles.find(localID);
- if (i != openFiles.end()) {
- return i->second;
- }
- throw NetFS::SystemError(EBADF);
-}
-
-void
-FuseApp::clearFileProxy(uint64_t localID)
+template<>
+std::map<int, FuseApp::OpenFilePtr> &
+FuseApp::getMap<FuseApp::OpenFilePtr>()
{
- Lock(_lock);
- openFiles.erase(localID);
+ return openFiles;
}
void
@@ -81,7 +62,7 @@ FuseApp::open(const char * p, struct fuse_file_info * fi)
{
try {
auto remote = volume->open(reqEnv(), p, fi->flags);
- setProxy(new OpenFile(remote, p, fi->flags), fi->fh);
+ setProxy<OpenFilePtr>(new OpenFile(remote, p, fi->flags), fi->fh);
return 0;
}
catch (SystemError & e) {
@@ -94,7 +75,7 @@ FuseApp::create(const char * p, mode_t m, struct fuse_file_info * fi)
{
try {
auto remote = volume->create(reqEnv(), p, fi->flags, m);
- setProxy(new OpenFile(remote, p, fi->flags), fi->fh);
+ setProxy<OpenFilePtr>(new OpenFile(remote, p, fi->flags), fi->fh);
return 0;
}
catch (SystemError & e) {
@@ -106,7 +87,7 @@ int
FuseApp::release(const char *, struct fuse_file_info * fi)
{
try {
- auto of = getFileProxy(fi->fh);
+ auto of = getProxy<OpenFilePtr>(fi->fh);
auto remote = of->remote;
try {
of->flush();
@@ -114,11 +95,11 @@ FuseApp::release(const char *, struct fuse_file_info * fi)
catch (SystemError & e) {
}
remote->close();
- clearFileProxy(fi->fh);
+ clearProxy<OpenFilePtr>(fi->fh);
return 0;
}
catch (SystemError & e) {
- clearFileProxy(fi->fh);
+ clearProxy<OpenFilePtr>(fi->fh);
return -e.syserrno;
}
}
@@ -127,7 +108,7 @@ int
FuseApp::flush(const char *, struct fuse_file_info * fi)
{
try {
- getFileProxy(fi->fh)->flush();
+ getProxy<OpenFilePtr>(fi->fh)->flush();
return 0;
}
catch (SystemError & e) {
@@ -139,7 +120,7 @@ int
FuseApp::read(const char *, char * buf, size_t s, off_t o, struct fuse_file_info * fi)
{
try {
- auto of = getFileProxy(fi->fh);
+ auto of = getProxy<OpenFilePtr>(fi->fh);
of->wait();
auto remote = of->remote;
Buffer data = remote->read(o, s);
@@ -155,7 +136,7 @@ int
FuseApp::write(const char *, const char * buf, size_t s, off_t o, struct fuse_file_info * fi)
{
try {
- auto of = getFileProxy(fi->fh);
+ auto of = getProxy<OpenFilePtr>(fi->fh);
auto remote = of->remote;
auto r = remote->begin_write(o, s, Buffer(buf, buf + s));
if (fcr->Async) {
@@ -190,7 +171,7 @@ int
FuseApp::ftruncate(const char *, off_t o, fuse_file_info * fi)
{
try {
- auto of = getFileProxy(fi->fh);
+ auto of = getProxy<OpenFilePtr>(fi->fh);
of->wait();
auto remote = of->remote;
remote->ftruncate(reqEnv(), o);
@@ -205,7 +186,7 @@ int
FuseApp::fgetattr(const char *, struct stat * s, fuse_file_info * fi)
{
try {
- auto of = getFileProxy(fi->fh);
+ auto of = getProxy<OpenFilePtr>(fi->fh);
of->wait();
auto remote = of->remote;
*s = converter.convert(remote->fgetattr(reqEnv()));