From bb7a96629692e4c734c491f9a3a858732f8da8cc Mon Sep 17 00:00:00 2001 From: Antonio SJ Musumeci Date: Mon, 12 May 2014 12:41:46 -0400 Subject: [PATCH] initial code commit --- LICENSE | 36 ++- Makefile | 60 ++++ README.md | 99 +++++- TODO | 10 + src/access.cpp | 85 +++++ src/access.hpp | 33 ++ src/assert.hpp | 36 +++ src/chmod.cpp | 88 ++++++ src/chmod.hpp | 33 ++ src/chown.cpp | 92 ++++++ src/chown.hpp | 34 ++ src/config.cpp | 91 ++++++ src/config.hpp | 64 ++++ src/create.cpp | 115 +++++++ src/create.hpp | 37 +++ src/fallocate.cpp | 93 ++++++ src/fallocate.hpp | 36 +++ src/fileinfo.hpp | 53 ++++ src/fs.cpp | 704 ++++++++++++++++++++++++++++++++++++++++++ src/fs.hpp | 159 ++++++++++ src/fsync.cpp | 75 +++++ src/fsync.hpp | 36 +++ src/ftruncate.cpp | 67 ++++ src/ftruncate.hpp | 39 +++ src/getattr.cpp | 83 +++++ src/getattr.hpp | 37 +++ src/getxattr.cpp | 90 ++++++ src/getxattr.hpp | 35 +++ src/ioctl.cpp | 89 ++++++ src/ioctl.hpp | 37 +++ src/link.cpp | 91 ++++++ src/link.hpp | 33 ++ src/listxattr.cpp | 87 ++++++ src/listxattr.hpp | 34 ++ src/mergerfs.cpp | 163 ++++++++++ src/mergerfs.hpp | 28 ++ src/mkdir.cpp | 98 ++++++ src/mkdir.hpp | 33 ++ src/mknod.cpp | 102 ++++++ src/mknod.hpp | 34 ++ src/open.cpp | 99 ++++++ src/open.hpp | 35 +++ src/option_parser.cpp | 145 +++++++++ src/option_parser.hpp | 37 +++ src/policy.cpp | 306 ++++++++++++++++++ src/policy.hpp | 207 +++++++++++++ src/read.cpp | 94 ++++++ src/read.hpp | 36 +++ src/readdir.cpp | 147 +++++++++ src/readdir.hpp | 67 ++++ src/readlink.cpp | 88 ++++++ src/readlink.hpp | 34 ++ src/release.cpp | 83 +++++ src/release.hpp | 33 ++ src/removexattr.cpp | 93 ++++++ src/removexattr.hpp | 33 ++ src/rename.cpp | 82 +++++ src/rename.hpp | 33 ++ src/resources.cpp | 78 +++++ src/resources.hpp | 34 ++ src/rmdir.cpp | 84 +++++ src/rmdir.hpp | 32 ++ src/setxattr.cpp | 101 ++++++ src/setxattr.hpp | 36 +++ src/statfs.cpp | 160 ++++++++++ src/statfs.hpp | 33 ++ src/symlink.cpp | 79 +++++ src/symlink.hpp | 33 ++ src/test.cpp | 53 ++++ src/test.hpp | 30 ++ src/truncate.cpp | 91 ++++++ src/truncate.hpp | 35 +++ src/ugid.hpp | 94 ++++++ src/unlink.cpp | 85 +++++ src/unlink.hpp | 32 ++ src/utimens.cpp | 88 ++++++ src/utimens.hpp | 33 ++ src/write.cpp | 163 ++++++++++ src/write.hpp | 36 +++ 79 files changed, 6293 insertions(+), 18 deletions(-) create mode 100644 Makefile create mode 100644 TODO create mode 100644 src/access.cpp create mode 100644 src/access.hpp create mode 100644 src/assert.hpp create mode 100644 src/chmod.cpp create mode 100644 src/chmod.hpp create mode 100644 src/chown.cpp create mode 100644 src/chown.hpp create mode 100644 src/config.cpp create mode 100644 src/config.hpp create mode 100644 src/create.cpp create mode 100644 src/create.hpp create mode 100644 src/fallocate.cpp create mode 100644 src/fallocate.hpp create mode 100644 src/fileinfo.hpp create mode 100644 src/fs.cpp create mode 100644 src/fs.hpp create mode 100644 src/fsync.cpp create mode 100644 src/fsync.hpp create mode 100644 src/ftruncate.cpp create mode 100644 src/ftruncate.hpp create mode 100644 src/getattr.cpp create mode 100644 src/getattr.hpp create mode 100644 src/getxattr.cpp create mode 100644 src/getxattr.hpp create mode 100644 src/ioctl.cpp create mode 100644 src/ioctl.hpp create mode 100644 src/link.cpp create mode 100644 src/link.hpp create mode 100644 src/listxattr.cpp create mode 100644 src/listxattr.hpp create mode 100644 src/mergerfs.cpp create mode 100644 src/mergerfs.hpp create mode 100644 src/mkdir.cpp create mode 100644 src/mkdir.hpp create mode 100644 src/mknod.cpp create mode 100644 src/mknod.hpp create mode 100644 src/open.cpp create mode 100644 src/open.hpp create mode 100644 src/option_parser.cpp create mode 100644 src/option_parser.hpp create mode 100644 src/policy.cpp create mode 100644 src/policy.hpp create mode 100644 src/read.cpp create mode 100644 src/read.hpp create mode 100644 src/readdir.cpp create mode 100644 src/readdir.hpp create mode 100644 src/readlink.cpp create mode 100644 src/readlink.hpp create mode 100644 src/release.cpp create mode 100644 src/release.hpp create mode 100644 src/removexattr.cpp create mode 100644 src/removexattr.hpp create mode 100644 src/rename.cpp create mode 100644 src/rename.hpp create mode 100644 src/resources.cpp create mode 100644 src/resources.hpp create mode 100644 src/rmdir.cpp create mode 100644 src/rmdir.hpp create mode 100644 src/setxattr.cpp create mode 100644 src/setxattr.hpp create mode 100644 src/statfs.cpp create mode 100644 src/statfs.hpp create mode 100644 src/symlink.cpp create mode 100644 src/symlink.hpp create mode 100644 src/test.cpp create mode 100644 src/test.hpp create mode 100644 src/truncate.cpp create mode 100644 src/truncate.hpp create mode 100644 src/ugid.hpp create mode 100644 src/unlink.cpp create mode 100644 src/unlink.hpp create mode 100644 src/utimens.cpp create mode 100644 src/utimens.hpp create mode 100644 src/write.cpp create mode 100644 src/write.hpp diff --git a/LICENSE b/LICENSE index 05d77dbf..59e5e83b 100644 --- a/LICENSE +++ b/LICENSE @@ -1,21 +1,23 @@ -The MIT License (MIT) +/* + The MIT License (MIT) -Copyright (c) 2014 trapexit + Copyright (c) 2014 Antonio SJ Musumeci -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. \ No newline at end of file + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ diff --git a/Makefile b/Makefile new file mode 100644 index 00000000..1e32b567 --- /dev/null +++ b/Makefile @@ -0,0 +1,60 @@ +# The MIT License (MIT) +# +# Copyright (c) 2014 Antonio SJ Musumeci +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +# THE SOFTWARE. + +SRC = $(wildcard src/*.cpp) +OBJ = $(SRC:src/%.cpp=obj/%.o) +DEPS = $(OBJ:obj/%.o=obj/%.d) +TARGET = mergerfs +CFLAGS = -g -Wall \ + $(shell pkg-config fuse --cflags) \ + -DFUSE_USE_VERSION=26 \ + -MMD + +ifdef WITHOUT_XATTR +CFLAGS += -DWITHOUT_XATTR +endif + +LDFLAGS = $(shell pkg-config fuse --libs) + +all: $(TARGET) + +help: + @echo usage: make + @echo make WITHOUT_XATTR=1 - to build program without xattrs functionality + +$(TARGET): obj/obj-stamp $(OBJ) + g++ $(CFLAGS) $(OBJ) -o $@ $(LDFLAGS) + +obj/obj-stamp: + mkdir -p obj + touch $@ + +obj/%.o: src/%.cpp + g++ $(CFLAGS) -c $< -o $@ + +clean: + rm -rf obj "$(TARGET)" + find -name "*~" -delete + +.PHONY: all clean help + +include $(wildcard obj/*.d) diff --git a/README.md b/README.md index 4042c4ac..83cbdb10 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,101 @@ mergerfs ======== -a FUSE union filesystem +another FUSE union filesystem + +mergerfs is similar to mhddfs, unionfs, and aufs. Like mhddfs in that it too uses FUSE. Like aufs in that it provides multiple policies for how to handle behavior. + +Why create mergerfs when those exist? mhddfs isn't really maintained or flexible. There are also issues with running as root. aufs is more flexible but contains some hard to debug inconsistencies in behavior. Neither support file attributes ([chattr](http://linux.die.net/man/1/chattr)). + +Policies +======== + +Filesystem calls are broken up into 5 classes of policies: search, action, create, statfs, and none. + +Below shows each policy class, the FUSE calls they impact, and the policy names. + +####Policy classifications#### +| Class | FUSE calls | Policies | +|-------|------------|----------| +| search | access, getattr, getxattr, listxattr, open, readlink | First Found (ff), First Found w/ Permission (ffwp), Newest (newest) | +| action | chmod, link, removexattr, rmdir, setxattr, truncate, unlink, utimens | First Found (ff), First Found w/ Permission (ffwp), Newest (newest), All Found (all) | +| create | create, mkdir, mknod | Existing Path (ep), Most Free Space (mfs), Existing Path Most Free Space (epmfs), Random (rand) | +| statfs | statfs | Sum Used Max Free (sumf), Sum Used Sum Free (susf) | +| none | fallocate, fsync, ftruncate, ioctl, read, readdir, rename, symlink, write, release | | + +####Descriptions#### +| Class/Policy | Description | +|--------------|-------------| +| search/ff | Given the order the paths were provided at mount time act on the first one found (regardless if stat would return EACCES). | +| search/ffwp | Given the order the paths were provided at mount time act on the first one found which you have access (stat does not error with EACCES). | +| search/newest | If multiple files exist return the one with the most recent mtime. | +| action/ff | (same as search/ff) | +| action/ffwp | (same as search/ffwp) | +| action/newest | (same as search/newest) | +| action/all | Attempt to apply the call to each file found. If any sub call succeeds the entire operation succeeds and other errors ignored. If all fail then the last error is reported. | +| create/ep | Choose the first path which is found. | +| create/mfs | Assuming the path is found to exist (ENOENT would not be returned) use the drive with the most free space available. | +| create/epmfs | If the path exists in multiple locations use the one with the most free space. Otherwise fall back to mfs. | +| create/rand | Pick a destination at random. Again the dirname of the full path must exist somewhere. | +| statfs/sumf | When reporting the size of the filesystem it will show the sum of all used but the available space will be reported as the max available across the filesystems mounted. | +| statfs/susf | As above but will report the sum of available space. Since the largest usable space is that of the filesystem with the most usable space this option is deceptive. | + +**NOTE:** create is really a search for existence and then create. The 'search' policy applies to the first part. If the [dirname](http://linux.die.net/man/3/dirname) of the full path is not found to exist [ENOENT](http://linux.die.net/man/3/errno) is returned. + +Usage +===== + +``` +$ mergerfs -o create=epmfs,search=ff,action=ff,statfs=sumf :: +``` + +| Option | Values | Default | +|--------|--------|---------| +| search | ff, ffwp, newest | ff | +| action | ff, ffwp, newest, all | ff | +| create | ep, mfs, epmfs, rand | epmfs | +| statfs | sumf, susf | sumf | + +Building +======== + +Need to install FUSE development libraries. + + +``` +[trapexit:~/dev/mergerfs] $ make help +usage: make +make WITHOUT_XATTR=1 - to build program without xattrs functionality +``` + +Runtime Settings +================ + +``` +/.mergerfs +``` + +There is a pseudo file available at the mountpoint which currently allows for the runtime modification of policies. The file will not show up in readdirs but can be stat'ed, read, and writen. Most other calls will fail with EPERM, EINVAL, or whatever may be appropriate for that call. Anything not understood while writing will result in EINVAL otherwise the number of bytes written will be returned. + +Reading the file will result in a newline delimited list of current settings as followed: + +``` +[trapexit:/tmp/mount] $ cat .mergerfs +action=ff +create=epmfs +search=ff +statfs=sumf +``` + +Writing to the file is buffered and waits till a newline to process. Meaning echo works well. + +``` +[trapexit:/tmp/mount] $ echo "search=newest" >> .mergerfs +[trapexit:/tmp/mount] $ cat .mergerfs +action=ff +create=epmfs +search=newest +statfs=sumf +``` + +*NOTE:* offset is not supported and ignored in both read and write. There is also a safety check which limits buffered + incoming length to a max of 1024 bytes. diff --git a/TODO b/TODO new file mode 100644 index 00000000..af8c69cc --- /dev/null +++ b/TODO @@ -0,0 +1,10 @@ +* logging +* dynamic changing of source mounts +* policies for readdir + * first found + * all found + * all found w/ permission + * newest +* unit tests +* add more to ioctl +* use {list,set}xattr for setting runtime values via .mergerfs pseudofile diff --git a/src/access.cpp b/src/access.cpp new file mode 100644 index 00000000..b243302b --- /dev/null +++ b/src/access.cpp @@ -0,0 +1,85 @@ +/* + The MIT License (MIT) + + Copyright (c) 2014 Antonio SJ Musumeci + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +#ifndef _GNU_SOURCE +#define _GNU_SOURCE +#endif + +#include + +#include +#include + +#include +#include + +#include "ugid.hpp" +#include "fs.hpp" +#include "config.hpp" +#include "assert.hpp" + +using std::string; +using std::vector; +using mergerfs::Policy; + +static +int +_access(const Policy::Search::Func searchFunc, + const vector &srcmounts, + const string fusepath, + const int mask) +{ + int rv; + string path; + + path = searchFunc(srcmounts,fusepath).full; + if(path.empty()) + return -ENOENT; + + rv = ::eaccess(path.c_str(),mask); + + return ((rv == -1) ? -errno : 0); +} + +namespace mergerfs +{ + namespace access + { + int + access(const char *fusepath, + int mask) + { + const ugid::SetResetGuard ugid; + const config::Config &config = config::get(); + + if(fusepath == config.controlfile) + return 0; + + return _access(config.policy.search, + config.srcmounts, + fusepath, + mask); + } + } +} diff --git a/src/access.hpp b/src/access.hpp new file mode 100644 index 00000000..acaccc46 --- /dev/null +++ b/src/access.hpp @@ -0,0 +1,33 @@ +/* + The MIT License (MIT) + + Copyright (c) 2014 Antonio SJ Musumeci + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +namespace mergerfs +{ + namespace access + { + int + access(const char *fusepath, + int mask); + } +} diff --git a/src/assert.hpp b/src/assert.hpp new file mode 100644 index 00000000..91ad8774 --- /dev/null +++ b/src/assert.hpp @@ -0,0 +1,36 @@ +/* + The MIT License (MIT) + + Copyright (c) 2014 Antonio SJ Musumeci + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +#define STATIC_ASSERT(cond) assert::StaticAssert< (cond) >() +#define STATIC_ARRAYLENGTH_ASSERT(array,size) STATIC_ASSERT(((sizeof(array)/sizeof(array[0]))==(size))) + +namespace assert +{ + template + struct StaticAssert; + + template<> + struct StaticAssert + {}; +} diff --git a/src/chmod.cpp b/src/chmod.cpp new file mode 100644 index 00000000..df9d6f8f --- /dev/null +++ b/src/chmod.cpp @@ -0,0 +1,88 @@ +/* + The MIT License (MIT) + + Copyright (c) 2014 Antonio SJ Musumeci + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +#include + +#include +#include +#include + +#include "ugid.hpp" +#include "fs.hpp" +#include "config.hpp" +#include "assert.hpp" + +using std::string; +using std::vector; +using mergerfs::Policy; + +static +int +_chmod(const Policy::Action::Func searchFunc, + const vector &srcmounts, + const string fusepath, + const mode_t mode) +{ + int rv; + int error; + vector paths; + + searchFunc(srcmounts,fusepath,paths); + if(paths.empty()) + return -ENOENT; + + rv = -1; + error = 0; + for(vector::const_iterator + i = paths.begin(), ei = paths.end(); i != ei; ++i) + { + rv &= ::chmod(i->full.c_str(),mode); + if(rv == -1) + error = errno; + } + + return ((rv == -1) ? -error : 0); +} + +namespace mergerfs +{ + namespace chmod + { + int + chmod(const char *fusepath, + mode_t mode) + { + ugid::SetResetGuard ugid; + const config::Config &config = config::get(); + + if(fusepath == config.controlfile) + return -EPERM; + + return _chmod(config.policy.action, + config.srcmounts, + fusepath, + mode); + } + } +} diff --git a/src/chmod.hpp b/src/chmod.hpp new file mode 100644 index 00000000..ed552a91 --- /dev/null +++ b/src/chmod.hpp @@ -0,0 +1,33 @@ +/* + The MIT License (MIT) + + Copyright (c) 2014 Antonio SJ Musumeci + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +namespace mergerfs +{ + namespace chmod + { + int + chmod(const char *fusepath, + mode_t mode); + } +} diff --git a/src/chown.cpp b/src/chown.cpp new file mode 100644 index 00000000..41c910d8 --- /dev/null +++ b/src/chown.cpp @@ -0,0 +1,92 @@ +/* + The MIT License (MIT) + + Copyright (c) 2014 Antonio SJ Musumeci + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +#include + +#include + +#include +#include + +#include "ugid.hpp" +#include "fs.hpp" +#include "config.hpp" +#include "assert.hpp" + +using std::string; +using std::vector; +using mergerfs::Policy; + +static +int +_chown(const Policy::Action::Func searchFunc, + const vector &srcmounts, + const string fusepath, + const uid_t uid, + const gid_t gid) +{ + int rv; + int error; + vector paths; + + searchFunc(srcmounts,fusepath,paths); + if(paths.empty()) + return -ENOENT; + + rv = -1; + error = 0; + for(vector::const_iterator + i = paths.begin(), ei = paths.end(); i != ei; ++i) + { + rv &= ::lchown(i->full.c_str(),uid,gid); + if(rv == -1) + error = errno; + } + + return ((rv == -1) ? -error : 0); +} + +namespace mergerfs +{ + namespace chown + { + int + chown(const char *fusepath, + uid_t uid, + gid_t gid) + { + const ugid::SetResetGuard ugid; + const config::Config &config = config::get(); + + if(fusepath == config.controlfile) + return -EPERM; + + return _chown(config.policy.action, + config.srcmounts, + fusepath, + uid, + gid); + } + } +} diff --git a/src/chown.hpp b/src/chown.hpp new file mode 100644 index 00000000..24206cb9 --- /dev/null +++ b/src/chown.hpp @@ -0,0 +1,34 @@ +/* + The MIT License (MIT) + + Copyright (c) 2014 Antonio SJ Musumeci + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +namespace mergerfs +{ + namespace chown + { + int + chown(const char *fusepath, + uid_t uid, + gid_t gid); + } +} diff --git a/src/config.cpp b/src/config.cpp new file mode 100644 index 00000000..0815a045 --- /dev/null +++ b/src/config.cpp @@ -0,0 +1,91 @@ +/* + The MIT License (MIT) + + Copyright (c) 2014 Antonio SJ Musumeci + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +#include + +#include + +#include +#include + +#include "config.hpp" + +namespace mergerfs +{ + namespace config + { + Config::Config() + : controlfile("/.mergerfs"), + testmode(false) + { + time_t now = time(NULL); + + controlfilestat.st_dev = 0; + controlfilestat.st_ino = 0; + controlfilestat.st_mode = (S_IFREG|S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH); + controlfilestat.st_nlink = 1; + controlfilestat.st_uid = ::getuid(); + controlfilestat.st_gid = ::getgid(); + controlfilestat.st_rdev = 0; + controlfilestat.st_size = 0; + controlfilestat.st_blksize = 1024; + controlfilestat.st_blocks = 0; + controlfilestat.st_atime = now; + controlfilestat.st_mtime = now; + controlfilestat.st_ctime = now; + } + + std::string + Config::generateReadStr() const + { + std::stringstream ss; + + ss << "action=" << policy.action.str() << std::endl + << "create=" << policy.create.str() << std::endl + << "search=" << policy.search.str() << std::endl + << "statfs=" << policy.statfs.str() << std::endl; + + return ss.str(); + } + + void + Config::updateReadStr() + { + readstr = generateReadStr(); + controlfilestat.st_size = readstr.size(); + } + + const Config& + get(void) + { + return (*((Config*)fuse_get_context()->private_data)); + } + + Config& + get_writable(void) + { + return (*((Config*)fuse_get_context()->private_data)); + } + } +} diff --git a/src/config.hpp b/src/config.hpp new file mode 100644 index 00000000..29a200da --- /dev/null +++ b/src/config.hpp @@ -0,0 +1,64 @@ +/* + The MIT License (MIT) + + Copyright (c) 2014 Antonio SJ Musumeci + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +#ifndef __CONFIG_HPP__ +#define __CONFIG_HPP__ + +#include + +#include +#include + +#include "policy.hpp" + +namespace mergerfs +{ + namespace config + { + class Config + { + public: + Config(); + + std::string generateReadStr() const; + void updateReadStr(); + + public: + std::string destmount; + std::vector srcmounts; + Policy policy; + + const std::string controlfile; + struct stat controlfilestat; + std::string readstr; + + bool testmode; + }; + + const Config &get(void); + Config &get_writable(void); + } +} + +#endif diff --git a/src/create.cpp b/src/create.cpp new file mode 100644 index 00000000..65720162 --- /dev/null +++ b/src/create.cpp @@ -0,0 +1,115 @@ +/* + The MIT License (MIT) + + Copyright (c) 2014 Antonio SJ Musumeci + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +#include + +#include +#include +#include +#include + +#include +#include + +#include "config.hpp" +#include "ugid.hpp" +#include "fileinfo.hpp" +#include "fs.hpp" +#include "assert.hpp" + +using std::string; +using std::vector; +using mergerfs::FileInfo; +using mergerfs::Policy; + +static +int +_create_controlfile(uint64_t &fh) +{ + fh = (uint64_t)new string; + + return 0; +} + +static +int +_create(const Policy::Search::Func searchFunc, + const Policy::Create::Func createPathFunc, + const vector &srcmounts, + const string fusepath, + const mode_t mode, + const int flags, + uint64_t &fh) +{ + int fd; + string path; + string dirname; + fs::Path createpath; + fs::Path existingpath; + + dirname = fs::dirname(fusepath); + existingpath = searchFunc(srcmounts,dirname); + if(existingpath.base.empty()) + return -ENOENT; + + createpath = createPathFunc(srcmounts,dirname); + if(createpath.base != existingpath.base) + fs::clonepath(existingpath.base,createpath.base,dirname); + + path = fs::make_path(createpath.base,fusepath); + + fd = ::open(path.c_str(),flags,mode); + if(fd == -1) + return -errno; + + fh = (uint64_t)new FileInfo(fd,flags,path); + + return 0; +} + +namespace mergerfs +{ + namespace create + { + int + create(const char *fusepath, + mode_t mode, + struct fuse_file_info *fileinfo) + { + ugid::SetResetGuard ugid; + const config::Config &config = config::get(); + + if(fusepath == config.controlfile) + return _create_controlfile(fileinfo->fh); + + return _create(config.policy.search, + config.policy.create, + config.srcmounts, + fusepath, + mode, + fileinfo->flags, + fileinfo->fh); + } + } +} diff --git a/src/create.hpp b/src/create.hpp new file mode 100644 index 00000000..cd5d3e00 --- /dev/null +++ b/src/create.hpp @@ -0,0 +1,37 @@ +/* + The MIT License (MIT) + + Copyright (c) 2014 Antonio SJ Musumeci + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +#include +#include + +namespace mergerfs +{ + namespace create + { + int + create(const char *fusepath, + mode_t mode, + struct fuse_file_info *fileinfo); + } +} diff --git a/src/fallocate.cpp b/src/fallocate.cpp new file mode 100644 index 00000000..a99a2049 --- /dev/null +++ b/src/fallocate.cpp @@ -0,0 +1,93 @@ +/* + The MIT License (MIT) + + Copyright (c) 2014 Antonio SJ Musumeci + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +#ifndef _GNU_SOURCE +#define _GNU_SOURCE +#endif + +#include + +#include +#include + +#include "ugid.hpp" +#include "config.hpp" +#include "fileinfo.hpp" + +using namespace mergerfs; + +static +int +_fallocate(const int fd, + const int mode, + const off_t offset, + const off_t len) +{ + int rv; + +#ifdef _GNU_SOURCE + rv = ::fallocate(fd,mode,offset,len); +#elif defined _XOPEN_SOURCE >= 600 || _POSIX_C_SOURCE >= 200112L + if(mode) + { + rv = -1; + errno = EOPNOTSUPP; + } + else + { + rv = ::posix_fallocate(fd,offset,len); + } +#else + rv = -1; + errno = EOPNOTSUPP; +#endif + + return ((rv == -1) ? -errno : 0); +} + +namespace mergerfs +{ + namespace fallocate + { + int + fallocate(const char *fusepath, + int mode, + off_t offset, + off_t len, + struct fuse_file_info *fi) + { + const ugid::SetResetGuard ugid; + const config::Config &config = config::get(); + const FileInfo *fileinfo = (FileInfo*)fi->fh; + + if(fusepath == config.controlfile) + return -EINVAL; + + return _fallocate(fileinfo->fd, + mode, + offset, + len); + } + } +} diff --git a/src/fallocate.hpp b/src/fallocate.hpp new file mode 100644 index 00000000..b23db070 --- /dev/null +++ b/src/fallocate.hpp @@ -0,0 +1,36 @@ +/* + The MIT License (MIT) + + Copyright (c) 2014 Antonio SJ Musumeci + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +namespace mergerfs +{ + namespace fallocate + { + int + fallocate(const char *fusepath, + int mode, + off_t offset, + off_t len, + struct fuse_file_info *fi); + } +} diff --git a/src/fileinfo.hpp b/src/fileinfo.hpp new file mode 100644 index 00000000..950f300e --- /dev/null +++ b/src/fileinfo.hpp @@ -0,0 +1,53 @@ +/* + The MIT License (MIT) + + Copyright (c) 2014 Antonio SJ Musumeci + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +#ifndef __FILEINFO_H__ +#define __FILEINFO_H__ + +#include + +namespace mergerfs +{ + struct FileInfo + { + public: + FileInfo(int fd_, + int flags_, + std::string realpath_) : + fd(fd_), + flags(flags_), + realpath(realpath_) + { + } + + int fd; + int flags; + std::string realpath; + + private: + FileInfo() {} + }; +} + +#endif /* __FILEINFO_H__ */ diff --git a/src/fs.cpp b/src/fs.cpp new file mode 100644 index 00000000..a0a84151 --- /dev/null +++ b/src/fs.cpp @@ -0,0 +1,704 @@ +/* + The MIT License (MIT) + + Copyright (c) 2014 Antonio SJ Musumeci + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "fs.hpp" + +using std::string; +using std::vector; +using std::map; +using std::istringstream; + +template +Container& +split(Container &result, + const typename Container::value_type &s, + typename Container::value_type::value_type delimiter) +{ + string str; + istringstream ss(s); + + while(std::getline(ss,str,delimiter)) + result.push_back(str); + + return result; +} + +template +Iter +random_element(Iter begin, + Iter end) +{ + const unsigned long n = std::distance(begin, end); + + std::advance(begin, (std::rand() % n)); + + return begin; +} + +namespace fs +{ + string + dirname(const string path) + { + string parent = path; + string::reverse_iterator i; + string::reverse_iterator bi; + + bi = parent.rend(); + i = parent.rbegin(); + while(*i == '/' && i != bi) + i++; + + while(*i != '/' && i != bi) + i++; + + while(*i == '/' && i != bi) + i++; + + parent.erase(i.base(),parent.end()); + + return parent; + } + + string + basename(const string path) + { + return path.substr(path.find_last_of('/')+1); + } + + bool + dir_is_empty(const string path) + { + DIR *dir; + struct dirent *de; + + dir = ::opendir(path.c_str()); + if(!dir) + return false; + + while((de = ::readdir(dir))) + { + const char *d_name = de->d_name; + + if(d_name[0] == '.' && + ((d_name[1] == '\0') || + (d_name[1] == '.' && d_name[2] == '\0'))) + continue; + + ::closedir(dir); + + return false; + } + + ::closedir(dir); + + return true; + } + + string + make_path(const string base, + const string suffix) + { + return base + suffix; + } + + bool + path_exists(vector::const_iterator begin, + vector::const_iterator end, + const string fusepath) + { + for(vector::const_iterator + iter = begin; iter != end; ++iter) + { + int rv; + struct stat st; + string path; + + path = make_path(*iter,fusepath); + rv = ::lstat(path.c_str(),&st); + if(rv == 0) + return true; + } + + return false; + } + + bool + path_exists(const vector &srcmounts, + const string fusepath) + { + return path_exists(srcmounts.begin(), + srcmounts.end(), + fusepath); + } + + Path + find_first(vector::const_iterator beginiter, + vector::const_iterator enditer, + const string suffix) + { + for(vector::const_iterator + iter = beginiter; iter != enditer; ++iter) + { + int rv; + struct stat st; + string path; + + path = fs::make_path(*iter,suffix); + rv = ::lstat(path.c_str(),&st); + if(rv == 0 || (rv == -1 && errno == EACCES)) + return Path(*iter,path); + } + + return Path(); + } + + Path + find_first(const vector &basepaths, + const string suffix) + { + return find_first(basepaths.begin(), + basepaths.end(), + suffix); + } + + void + find_first(const vector &basepaths, + const string suffix, + vector &paths) + { + paths.push_back(find_first(basepaths,suffix)); + } + + Path + find_first_with_permission(vector::const_iterator beginiter, + vector::const_iterator enditer, + const string suffix) + { + for(vector::const_iterator + iter = beginiter; iter != enditer; ++iter) + { + int rv; + struct stat st; + string path; + + path = make_path(*iter,suffix); + rv = ::lstat(path.c_str(),&st); + if(rv == 0) + return Path(*iter,path); + } + + return Path(); + } + + Path + find_first_with_permission(const vector &basepaths, + const string suffix) + { + return find_first_with_permission(basepaths.begin(), + basepaths.end(), + suffix); + } + + void + find_first_with_permission(const vector &basepaths, + const string suffix, + vector &paths) + { + paths.push_back(find_first_with_permission(basepaths,suffix)); + } + + Path + find_newest(vector::const_iterator beginiter, + vector::const_iterator enditer, + const string suffix) + { + time_t newest; + string npath; + vector::const_iterator niter; + + newest = 0; + for(vector::const_iterator + iter = beginiter; iter != enditer; ++iter) + { + int rv; + struct stat st; + const string path = make_path(*iter,suffix); + + rv = ::lstat(path.c_str(),&st); + if(rv == 0 && st.st_mtime > newest) + { + newest = st.st_mtime; + niter = iter; + npath = path; + } + } + + if(newest) + return Path(*niter,npath); + + return Path(); + } + + Path + find_newest(const vector &basepaths, + const string suffix) + { + return find_newest(basepaths.begin(), + basepaths.end(), + suffix); + } + + void + find_newest(const vector &basepaths, + const string suffix, + vector &paths) + { + paths.push_back(find_newest(basepaths,suffix)); + } + + void + find_all(const vector &basepaths, + const string suffix, + vector &paths) + { + for(vector::const_iterator + i = basepaths.begin(), ei = basepaths.end(); i != ei; ++i) + { + int rv; + struct stat st; + string path; + + path = fs::make_path(*i,suffix); + rv = ::lstat(path.c_str(),&st); + if(rv == 0 || (rv == -1 && errno == EACCES)) + paths.push_back(Path(*i,path)); + } + } + + Path + find_mfs(vector::const_iterator iter, + vector::const_iterator enditer, + const string fusepath) + { + fsblkcnt_t mfs = 0; + string mfspath; + string fullmfspath; + + for(;iter != enditer; ++iter) + { + int rv; + struct statvfs fsstats; + const string mountpoint = *iter; + + rv = ::statvfs(mountpoint.c_str(),&fsstats); + if(rv == 0) + { + fsblkcnt_t spaceavail; + + spaceavail = (fsstats.f_bsize * fsstats.f_bavail); + if(spaceavail > mfs) + { + mfs = spaceavail; + mfspath = mountpoint; + } + } + } + + fullmfspath = make_path(mfspath,fusepath); + + return Path(mfspath,fullmfspath); + } + + Path + find_mfs(const vector &paths, + const string fusepath) + { + return find_mfs(paths.begin(), + paths.end(), + fusepath); + } + + Path + find_mfs_existing(vector::const_iterator iter, + vector::const_iterator enditer, + const string fusepath) + { + fsblkcnt_t existingmfs = 0; + fsblkcnt_t generalmfs = 0; + string path; + string generalmfspath; + string existingmfspath; + + do + { + int rv; + struct statvfs fsstats; + const string mountpoint = *iter; + + rv = ::statvfs(mountpoint.c_str(),&fsstats); + if(rv == 0) + { + struct stat filestats; + fsblkcnt_t spaceavail; + + spaceavail = (fsstats.f_bsize * fsstats.f_bavail); + if(spaceavail > generalmfs) + { + generalmfs = spaceavail; + generalmfspath = mountpoint; + } + + path = make_path(mountpoint,fusepath); + rv = ::lstat(path.c_str(),&filestats); + if(rv == 0) + { + if(spaceavail > existingmfs) + { + existingmfs = spaceavail; + existingmfspath = mountpoint; + } + } + } + + ++iter; + } + while(iter != enditer); + + if(existingmfspath.empty()) + existingmfspath = generalmfspath; + + return Path(existingmfspath,path); + } + + Path + find_mfs_existing(const vector &paths, + const string fusepath) + { + return find_mfs_existing(paths.begin(), + paths.end(), + fusepath); + } + + Path + find_random(vector::const_iterator iter, + vector::const_iterator enditer, + string fusepath) + { + string randombasepath; + string randomfullpath; + + randombasepath = *random_element(iter,enditer); + randomfullpath = make_path(randombasepath,fusepath); + + return Path(randombasepath,randomfullpath); + } + + Path + find_random(const vector &paths, + const string fusepath) + { + return find_random(paths.begin(), + paths.end(), + fusepath); + } + + int + listxattr(const string path, + vector &attrs) + { + int rv; + int size; + + rv = -1; + errno = ERANGE; + while(rv == -1 && errno == ERANGE) + { + size = ::listxattr(path.c_str(),NULL,0); + attrs.resize(size); + rv = ::listxattr(path.c_str(),&attrs[0],size); + } + + return rv; + } + + int + listxattr(const string path, + vector &attrvector) + { + int rv; + vector attrs; + + rv = listxattr(path,attrs); + if(rv != -1) + { + string tmp(attrs.begin(),attrs.end()); + split(attrvector,tmp,'\0'); + } + + return rv; + } + + int + listxattr(const string path, + string &attrstr) + { + int rv; + vector attrs; + + rv = listxattr(path,attrs); + if(rv != -1) + attrstr = string(attrs.begin(),attrs.end()); + + return rv; + } + + int + getxattr(const string path, + const string attr, + vector &value) + { + int rv; + int size; + + rv = -1; + errno = ERANGE; + while(rv == -1 && errno == ERANGE) + { + size = ::getxattr(path.c_str(),attr.c_str(),NULL,0); + value.resize(size); + rv = ::getxattr(path.c_str(),attr.c_str(),&value[0],size); + } + + return rv; + } + + int + getxattr(const string path, + const string attr, + string &value) + { + int rv; + vector tmpvalue; + + rv = getxattr(path,attr,tmpvalue); + if(rv != -1) + value = string(tmpvalue.begin(),tmpvalue.end()); + + return rv; + } + + + int + getxattrs(const string path, + map &attrs) + { + int rv; + string attrstr; + + rv = listxattr(path,attrstr); + if(rv == -1) + return -1; + + { + string key; + istringstream ss(attrstr); + + while(getline(ss,key,'\0')) + { + string value; + + rv = getxattr(path,key,value); + if(rv != -1) + attrs[key] = value; + } + } + + return 0; + } + + int + setxattr(const string path, + const string key, + const string value, + const int flags) + { + return ::setxattr(path.c_str(), + key.c_str(), + value.data(), + value.size(), + flags); + } + + int + setxattr(const int fd, + const string key, + const string value, + const int flags) + { + return ::fsetxattr(fd, + key.c_str(), + value.data(), + value.size(), + flags); + } + + int + setxattrs(const string path, + const map &attrs) + { + int fd; + + fd = ::open(path.c_str(),O_RDONLY|O_NONBLOCK); + if(fd == -1) + return -1; + + for(map::const_iterator + i = attrs.begin(), ei = attrs.end(); i != ei; ++i) + { + setxattr(fd,i->first,i->second,0); + } + + return ::close(fd); + } + + int + copyxattrs(const string from, + const string to) + { + int rv; + map attrs; + + rv = getxattrs(from,attrs); + if(rv == -1) + return -1; + + return setxattrs(to,attrs); + } + + int + copyattr(const string from, + const string to) + { + int fd; + int rv; + int flags; + int error; + const int openflags = O_RDONLY|O_NONBLOCK; + + fd = ::open(from.c_str(),openflags); + if(fd == -1) + return -1; + + rv = ::ioctl(fd,FS_IOC_GETFLAGS,&flags); + if(rv == -1) + { + error = errno; + ::close(fd); + errno = error; + return -1; + } + + fd = ::open(to.c_str(),openflags); + if(fd == -1) + return -1; + + rv = ::ioctl(fd,FS_IOC_SETFLAGS,&flags); + if(rv == -1) + { + error = errno; + ::close(fd); + errno = error; + return -1; + } + + return ::close(fd); + } + + int + clonepath(const string fromsrc, + const string tosrc, + const string relative) + { + int rv; + struct stat st; + string topath; + string frompath; + string dirname; + + dirname = fs::dirname(relative); + if(!dirname.empty()) + { + rv = clonepath(fromsrc,tosrc,dirname); + if(rv == -1) + return -1; + } + + frompath = make_path(fromsrc,relative); + + rv = ::stat(frompath.c_str(),&st); + if(rv == -1) + return -1; + else if(!S_ISDIR(st.st_mode)) + return (errno = ENOTDIR,-1); + + topath = make_path(tosrc,relative); + rv = ::mkdir(topath.c_str(),st.st_mode); + if(rv == -1) + return -1; + + rv = ::chown(topath.c_str(),st.st_uid,st.st_gid); + if(rv == -1) + return -1; + + // It may not support it... it's fine... + rv = copyattr(frompath,topath); + if(rv == -1 && errno != ENOTTY) + return -1; + + rv = copyxattrs(frompath,topath); + if(rv == -1 && errno != ENOTTY) + return -1; + + return (errno = 0); + } +}; diff --git a/src/fs.hpp b/src/fs.hpp new file mode 100644 index 00000000..5d51fe31 --- /dev/null +++ b/src/fs.hpp @@ -0,0 +1,159 @@ +/* + The MIT License (MIT) + + Copyright (c) 2014 Antonio SJ Musumeci + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +#ifndef __FS_HPP__ +#define __FS_HPP__ + +#include +#include +#include + +namespace fs +{ + using std::string; + using std::vector; + using std::map; + + struct Path + { + Path() {} + Path(const string b, + const string f) + : base(b), + full(f) + {} + + string base; + string full; + }; + + typedef fs::Path (*SearchFunc)(const vector&,const string); + typedef void (*VecSearchFunc)(const vector&,const string,vector&); + + string dirname(const string path); + string basename(const string path); + + bool dir_is_empty(const string path); + + string make_path(const string base, + const string suffix); + + bool path_exists(vector::const_iterator begin, + vector::const_iterator end, + const string fusepath); + bool path_exists(const vector &srcmounts, + const string fusepath); + + Path find_first(vector::const_iterator begin, + vector::const_iterator end, + const string fusepath); + Path find_first(const vector &paths, + const string fusepath); + void find_first(const vector &paths, + const string fusepath, + vector &rv); + + Path find_first_with_permission(vector::const_iterator begin, + vector::const_iterator end, + const string fusepath); + Path find_first_with_permission(const vector &paths, + const string fusepath); + void find_first_with_permission(const vector &paths, + const string fusepath, + vector &rv); + + Path find_newest(vector::const_iterator begin, + vector::const_iterator end, + const string fusepath); + Path find_newest(const vector &paths, + const string fusepath); + void find_newest(const vector &paths, + const string fusepath, + vector &rv); + + void find_all(const vector &paths, + const string fusepath, + vector &rv); + + Path find_mfs(vector::const_iterator iter, + vector::const_iterator enditer, + const string fusepath); + Path find_mfs(const vector &paths, + const string fusepath); + + Path find_mfs_existing(vector::const_iterator iter, + vector::const_iterator enditer, + const string fusepath); + Path find_mfs_existing(const vector &paths, + const string fusepath); + + Path find_random(vector::const_iterator iter, + vector::const_iterator enditer, + const string fusepath); + Path find_random(const vector &paths, + const string fusepath); + + + + int clonepath(const string srcfrom, + const string srcto, + const string relative); + + int listxattr(const string path, + vector &attrs); + int listxattr(const string path, + string &attrs); + int listxattr(const string path, + vector &attrs); + + int getxattr(const string path, + const string attr, + vector &value); + int getxattr(const string path, + const string attr, + string &value); + + int getxattrs(const string path, + map &attrs); + + int setxattr(const string path, + const string key, + const string value, + const int flags); + int setxattr(const int fd, + const string key, + const string value, + const int flags); + + int setxattrs(const string path, + const map &attrs); + + int copyxattrs(const string from, + const string to); + + int copyattr(const string from, + const string to); +}; + +#endif // __FS_HPP__ diff --git a/src/fsync.cpp b/src/fsync.cpp new file mode 100644 index 00000000..d63e9a8c --- /dev/null +++ b/src/fsync.cpp @@ -0,0 +1,75 @@ +/* + The MIT License (MIT) + + Copyright (c) 2014 Antonio SJ Musumeci + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +#ifndef _GNU_SOURCE +#define _GNU_SOURCE +#endif + +#include + +#include +#include + +#include +#include + +#include "config.hpp" +#include "ugid.hpp" +#include "fileinfo.hpp" + +static +int +_fsync(const int fd, + const int isdatasync) +{ + int rv; + + rv = (isdatasync ? + ::fdatasync(fd) : + ::fsync(fd)); + + return ((rv == -1) ? -errno : 0); +} + +namespace mergerfs +{ + namespace fsync + { + int + fsync(const char *fusepath, + int isdatasync, + struct fuse_file_info *fi) + { + const ugid::SetResetGuard ugid; + const config::Config &config = config::get(); + const FileInfo *fileinfo = (FileInfo*)fi->fh; + + if(fusepath == config.controlfile) + return 0; + + return _fsync(fileinfo->fd, + isdatasync); + } + } +} diff --git a/src/fsync.hpp b/src/fsync.hpp new file mode 100644 index 00000000..e5062bcd --- /dev/null +++ b/src/fsync.hpp @@ -0,0 +1,36 @@ +/* + The MIT License (MIT) + + Copyright (c) 2014 Antonio SJ Musumeci + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +#include + +namespace mergerfs +{ + namespace fsync + { + int + fsync(const char *fusepath, + int isdatasync, + struct fuse_file_info *fi); + } +} diff --git a/src/ftruncate.cpp b/src/ftruncate.cpp new file mode 100644 index 00000000..b7172908 --- /dev/null +++ b/src/ftruncate.cpp @@ -0,0 +1,67 @@ +/* + The MIT License (MIT) + + Copyright (c) 2014 Antonio SJ Musumeci + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +#include + +#include +#include +#include + +#include "config.hpp" +#include "ugid.hpp" +#include "fileinfo.hpp" + +static +int +_ftruncate(const int fd, + const off_t size) +{ + int rv; + + rv = ::ftruncate(fd,size); + + return ((rv == -1) ? -errno : 0); +} + +namespace mergerfs +{ + namespace ftruncate + { + int + ftruncate(const char *fusepath, + off_t size, + struct fuse_file_info *fi) + { + const ugid::SetResetGuard ugid; + const config::Config &config = config::get(); + const FileInfo *fileinfo = (FileInfo*)fi->fh; + + if(fusepath == config.controlfile) + return -EPERM; + + return _ftruncate(fileinfo->fd, + size); + } + } +} diff --git a/src/ftruncate.hpp b/src/ftruncate.hpp new file mode 100644 index 00000000..b8bad8e2 --- /dev/null +++ b/src/ftruncate.hpp @@ -0,0 +1,39 @@ +/* + The MIT License (MIT) + + Copyright (c) 2014 Antonio SJ Musumeci + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +#include + +#include +#include + +namespace mergerfs +{ + namespace ftruncate + { + int + ftruncate(const char *fusepath, + off_t size, + struct fuse_file_info *fi); + } +} diff --git a/src/getattr.cpp b/src/getattr.cpp new file mode 100644 index 00000000..5d34691a --- /dev/null +++ b/src/getattr.cpp @@ -0,0 +1,83 @@ +/* + The MIT License (MIT) + + Copyright (c) 2014 Antonio SJ Musumeci + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +#include + +#include +#include + +#include +#include +#include +#include + +#include "config.hpp" +#include "fs.hpp" +#include "ugid.hpp" +#include "assert.hpp" + +using std::string; +using std::vector; +using mergerfs::Policy; + +static +int +_getattr(const Policy::Search::Func searchFunc, + const vector &srcmounts, + const string fusepath, + struct stat &buf) +{ + int rv; + string path; + + path = searchFunc(srcmounts,fusepath).full; + if(path.empty()) + return -ENOENT; + + rv = ::lstat(path.c_str(),&buf); + + return ((rv == -1) ? -errno : 0); +} + +namespace mergerfs +{ + namespace getattr + { + int + getattr(const char *fusepath, + struct stat *buf) + { + const ugid::SetResetGuard ugid; + const config::Config &config = config::get(); + + if(fusepath == config.controlfile) + return (*buf = config.controlfilestat,0); + + return _getattr(config.policy.search, + config.srcmounts, + fusepath, + *buf); + } + } +} diff --git a/src/getattr.hpp b/src/getattr.hpp new file mode 100644 index 00000000..e062da48 --- /dev/null +++ b/src/getattr.hpp @@ -0,0 +1,37 @@ +/* + The MIT License (MIT) + + Copyright (c) 2014 Antonio SJ Musumeci + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +#include +#include +#include + +namespace mergerfs +{ + namespace getattr + { + int + getattr(const char *fusepath, + struct stat *buf); + } +} diff --git a/src/getxattr.cpp b/src/getxattr.cpp new file mode 100644 index 00000000..d51591ab --- /dev/null +++ b/src/getxattr.cpp @@ -0,0 +1,90 @@ +/* + The MIT License (MIT) + + Copyright (c) 2014 Antonio SJ Musumeci + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +#include +#include + +#include +#include +#include + +#include "config.hpp" +#include "fs.hpp" +#include "ugid.hpp" +#include "assert.hpp" + +using std::string; +using std::vector; +using mergerfs::Policy; + +static +int +_getxattr(const Policy::Search::Func searchFunc, + const vector &srcmounts, + const string fusepath, + const char *attrname, + char *buf, + const size_t count) +{ +#ifndef WITHOUT_XATTR + int rv; + string path; + + path = searchFunc(srcmounts,fusepath).full; + if(path.empty()) + return -ENOENT; + + rv = ::lgetxattr(path.c_str(),attrname,buf,count); + + return ((rv == -1) ? -errno : rv); +#else + return -ENOTSUP; +#endif +} + +namespace mergerfs +{ + namespace getxattr + { + int + getxattr(const char *fusepath, + const char *attrname, + char *buf, + size_t count) + { + const ugid::SetResetGuard ugid; + const config::Config &config = config::get(); + + if(fusepath == config.controlfile) + return -ENOTSUP; + + return _getxattr(config.policy.search, + config.srcmounts, + fusepath, + attrname, + buf, + count); + } + } +} diff --git a/src/getxattr.hpp b/src/getxattr.hpp new file mode 100644 index 00000000..9c825edb --- /dev/null +++ b/src/getxattr.hpp @@ -0,0 +1,35 @@ +/* + The MIT License (MIT) + + Copyright (c) 2014 Antonio SJ Musumeci + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +namespace mergerfs +{ + namespace getxattr + { + int + getxattr(const char *fusepath, + const char *attrname, + char *buf, + size_t count); + } +} diff --git a/src/ioctl.cpp b/src/ioctl.cpp new file mode 100644 index 00000000..7ba7b9bd --- /dev/null +++ b/src/ioctl.cpp @@ -0,0 +1,89 @@ +/* + The MIT License (MIT) + + Copyright (c) 2014 Antonio SJ Musumeci + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +#include + +#include + +#include +#include +#include + +#include "config.hpp" +#include "fileinfo.hpp" +#include "ugid.hpp" + +static +int +_ioctl(const int fd, + const int cmd, + void *arg, + const unsigned int flags, + void *data) +{ + int rv; + + switch(cmd) + { + case FS_IOC_GETFLAGS: + case FS_IOC_SETFLAGS: + rv = ::ioctl(fd,cmd,data); + break; + + default: + rv = -1; + errno = ENOTTY; + break; + } + + return ((rv == -1) ? -errno : rv); +} + +namespace mergerfs +{ + namespace ioctl + { + int + ioctl(const char *fusepath, + int cmd, + void *arg, + struct fuse_file_info *fi, + unsigned int flags, + void *data) + { + const ugid::SetResetGuard ugid; + const config::Config &config = config::get(); + const FileInfo *fileinfo = (FileInfo*)fi->fh; + + if(fusepath == config.controlfile) + return -EINVAL; + + return _ioctl(fileinfo->fd, + cmd, + arg, + flags, + data); + } + } +} diff --git a/src/ioctl.hpp b/src/ioctl.hpp new file mode 100644 index 00000000..b53eb131 --- /dev/null +++ b/src/ioctl.hpp @@ -0,0 +1,37 @@ +/* + The MIT License (MIT) + + Copyright (c) 2014 Antonio SJ Musumeci + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +namespace mergerfs +{ + namespace ioctl + { + int + ioctl(const char *fusepath, + int cmd, + void *arg, + struct fuse_file_info *fi, + unsigned int flags, + void *data); + } +} diff --git a/src/link.cpp b/src/link.cpp new file mode 100644 index 00000000..a0b4a63c --- /dev/null +++ b/src/link.cpp @@ -0,0 +1,91 @@ +/* + The MIT License (MIT) + + Copyright (c) 2014 Antonio SJ Musumeci + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +#include +#include + +#include +#include + +#include "config.hpp" +#include "fs.hpp" +#include "ugid.hpp" +#include "assert.hpp" + +using std::string; +using std::vector; +using mergerfs::Policy; + +static +int +_link(const Policy::Action::Func searchFunc, + const vector &srcmounts, + const string from, + const string to) +{ + int rv; + int error; + vector paths; + + searchFunc(srcmounts,from,paths); + if(paths.empty()) + return -ENOENT; + + rv = -1; + error = 0; + for(vector::const_iterator + i = paths.begin(), ei = paths.end(); i != ei; ++i) + { + const string pathfrom = fs::make_path(i->base,from); + const string pathto = fs::make_path(i->base,to); + + rv &= ::link(pathfrom.c_str(),pathto.c_str()); + if(rv == -1) + error = errno; + } + + return ((rv == -1) ? -error : 0); +} + +namespace mergerfs +{ + namespace link + { + int + link(const char *from, + const char *to) + { + const ugid::SetResetGuard ugid; + const config::Config &config = config::get(); + + if(from == config.controlfile) + return -EPERM; + + return _link(config.policy.action, + config.srcmounts, + from, + to); + } + } +} diff --git a/src/link.hpp b/src/link.hpp new file mode 100644 index 00000000..c3673abf --- /dev/null +++ b/src/link.hpp @@ -0,0 +1,33 @@ +/* + The MIT License (MIT) + + Copyright (c) 2014 Antonio SJ Musumeci + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +namespace mergerfs +{ + namespace link + { + int + link(const char *from, + const char *to); + } +} diff --git a/src/listxattr.cpp b/src/listxattr.cpp new file mode 100644 index 00000000..1a42755f --- /dev/null +++ b/src/listxattr.cpp @@ -0,0 +1,87 @@ +/* + The MIT License (MIT) + + Copyright (c) 2014 Antonio SJ Musumeci + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +#include +#include + +#include +#include +#include + +#include "config.hpp" +#include "fs.hpp" +#include "ugid.hpp" +#include "assert.hpp" + +using std::string; +using std::vector; +using mergerfs::Policy; + +static +int +_listxattr(const Policy::Search::Func searchFunc, + const vector &srcmounts, + const string fusepath, + char *list, + const size_t size) +{ +#ifndef WITHOUT_XATTR + int rv; + string path; + + path = searchFunc(srcmounts,fusepath).full; + if(path.empty()) + return -ENOENT; + + rv = ::llistxattr(path.c_str(),list,size); + + return ((rv == -1) ? -errno : rv); +#else + return -ENOTSUP; +#endif +} + +namespace mergerfs +{ + namespace listxattr + { + int + listxattr(const char *fusepath, + char *list, + size_t size) + { + const ugid::SetResetGuard ugid; + const config::Config &config = config::get(); + + if(fusepath == config.controlfile) + return -ENOTSUP; + + return _listxattr(config.policy.search, + config.srcmounts, + fusepath, + list, + size); + } + } +} diff --git a/src/listxattr.hpp b/src/listxattr.hpp new file mode 100644 index 00000000..123eead3 --- /dev/null +++ b/src/listxattr.hpp @@ -0,0 +1,34 @@ +/* + The MIT License (MIT) + + Copyright (c) 2014 Antonio SJ Musumeci + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +namespace mergerfs +{ + namespace listxattr + { + int + listxattr(const char *fusepath, + char *buf, + size_t count); + } +} diff --git a/src/mergerfs.cpp b/src/mergerfs.cpp new file mode 100644 index 00000000..0502f89d --- /dev/null +++ b/src/mergerfs.cpp @@ -0,0 +1,163 @@ +/* + The MIT License (MIT) + + Copyright (c) 2014 Antonio SJ Musumeci + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + */ + +#include +#include + +#include +#include + +#include "mergerfs.hpp" +#include "option_parser.hpp" +#include "resources.hpp" +#include "fs.hpp" +#include "test.hpp" + +#include "access.hpp" +#include "chmod.hpp" +#include "chown.hpp" +#include "create.hpp" +#include "fallocate.hpp" +#include "fsync.hpp" +#include "ftruncate.hpp" +#include "getattr.hpp" +#include "getxattr.hpp" +#include "ioctl.hpp" +#include "link.hpp" +#include "listxattr.hpp" +#include "mkdir.hpp" +#include "mknod.hpp" +#include "open.hpp" +#include "read.hpp" +#include "readdir.hpp" +#include "readlink.hpp" +#include "release.hpp" +#include "removexattr.hpp" +#include "rename.hpp" +#include "rmdir.hpp" +#include "setxattr.hpp" +#include "statfs.hpp" +#include "symlink.hpp" +#include "truncate.hpp" +#include "unlink.hpp" +#include "utimens.hpp" +#include "write.hpp" + +static +struct fuse_operations +get_fuse_operations() +{ + struct fuse_operations ops = {}; + + ops.flag_nopath = false; + ops.flag_nullpath_ok = false; + + ops.access = mergerfs::access::access; + ops.bmap = NULL; + ops.chmod = mergerfs::chmod::chmod; + ops.chown = mergerfs::chown::chown; + ops.create = mergerfs::create::create; + ops.destroy = NULL; + ops.fallocate = mergerfs::fallocate::fallocate; + ops.fgetattr = NULL; + ops.flock = NULL; + ops.flush = NULL; /* called on close() of fd */ + ops.fsync = mergerfs::fsync::fsync; + ops.fsyncdir = NULL; + ops.ftruncate = mergerfs::ftruncate::ftruncate; + ops.getattr = mergerfs::getattr::getattr; + ops.getdir = NULL; /* deprecated; use readdir */ + ops.getxattr = mergerfs::getxattr::getxattr; + ops.init = NULL; + ops.ioctl = NULL; + ops.ioctl = mergerfs::ioctl::ioctl; + ops.link = mergerfs::link::link; + ops.listxattr = mergerfs::listxattr::listxattr; + ops.lock = NULL; + ops.mkdir = mergerfs::mkdir::mkdir; + ops.mknod = mergerfs::mknod::mknod; + ops.open = mergerfs::open::open; + ops.opendir = NULL; + ops.poll = NULL; + ops.read = mergerfs::read::read; + ops.read_buf = NULL; + ops.readdir = mergerfs::readdir::readdir; + ops.readlink = mergerfs::readlink::readlink; + ops.release = mergerfs::release::release; + ops.releasedir = NULL; + ops.removexattr = mergerfs::removexattr::removexattr; + ops.rename = mergerfs::rename::rename; + ops.rmdir = mergerfs::rmdir::rmdir; + ops.setxattr = mergerfs::setxattr::setxattr; + ops.statfs = mergerfs::statfs::statfs; + ops.symlink = mergerfs::symlink::symlink; + ops.truncate = mergerfs::truncate::truncate; + ops.unlink = mergerfs::unlink::unlink; + ops.utime = NULL; /* deprecated; use utimens() */ + ops.utimens = mergerfs::utimens::utimens; + ops.write = mergerfs::write::write; + ops.write_buf = NULL; + + return ops; +} + +namespace mergerfs +{ + int + main(const struct fuse_args &args, + config::Config &config) + { + struct fuse_operations ops; + + ops = get_fuse_operations(); + + std::srand(time(NULL)); + resources::reset_umask(); + resources::maxout_rlimit_nofile(); + resources::maxout_rlimit_fsize(); + + return fuse_main(args.argc, + args.argv, + &ops, + &config); + } +} + +int +main(int argc, + char *argv[]) +{ + int rv; + mergerfs::config::Config config; + struct fuse_args args = FUSE_ARGS_INIT(argc,argv); + + mergerfs::options::parse(args,config); + + if(config.testmode == false) + rv = mergerfs::main(args,config); + else + rv = mergerfs::test(args,config); + + return rv; +} diff --git a/src/mergerfs.hpp b/src/mergerfs.hpp new file mode 100644 index 00000000..c0963717 --- /dev/null +++ b/src/mergerfs.hpp @@ -0,0 +1,28 @@ +/* + The MIT License (MIT) + + Copyright (c) 2014 Antonio SJ Musumeci + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +#ifndef __MERGER_HPP__ +#define __MERGER_HPP__ + +#endif diff --git a/src/mkdir.cpp b/src/mkdir.cpp new file mode 100644 index 00000000..b42479da --- /dev/null +++ b/src/mkdir.cpp @@ -0,0 +1,98 @@ +/* + The MIT License (MIT) + + Copyright (c) 2014 Antonio SJ Musumeci + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +#include + +#include +#include +#include + +#include +#include + +#include "ugid.hpp" +#include "fileinfo.hpp" +#include "fs.hpp" +#include "config.hpp" +#include "assert.hpp" + +using std::string; +using std::vector; +using mergerfs::Policy; + +static +int +_mkdir(const Policy::Search::Func searchFunc, + const Policy::Create::Func createPathFunc, + const vector &srcmounts, + const string fusepath, + const mode_t mode) +{ + int rv; + string path; + string dirname; + fs::Path createpath; + fs::Path existingpath; + + if(fs::path_exists(srcmounts,fusepath)) + return -EEXIST; + + dirname = fs::dirname(fusepath); + existingpath = searchFunc(srcmounts,dirname); + if(existingpath.base.empty()) + return -ENOENT; + + createpath = createPathFunc(srcmounts,dirname); + if(createpath.base != existingpath.base) + fs::clonepath(existingpath.base,createpath.base,dirname); + + path = fs::make_path(createpath.base,fusepath); + + rv = ::mkdir(path.c_str(),mode); + + return ((rv == -1) ? -errno : 0); +} + +namespace mergerfs +{ + namespace mkdir + { + int + mkdir(const char *fusepath, + mode_t mode) + { + const ugid::SetResetGuard ugid; + const config::Config &config = config::get(); + + if(fusepath == config.controlfile) + return -EEXIST; + + return _mkdir(config.policy.search, + config.policy.create, + config.srcmounts, + fusepath, + mode); + } + } +} diff --git a/src/mkdir.hpp b/src/mkdir.hpp new file mode 100644 index 00000000..2ddc5644 --- /dev/null +++ b/src/mkdir.hpp @@ -0,0 +1,33 @@ +/* + The MIT License (MIT) + + Copyright (c) 2014 Antonio SJ Musumeci + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +namespace mergerfs +{ + namespace mkdir + { + int + mkdir(const char *fusepath, + mode_t mode); + } +} diff --git a/src/mknod.cpp b/src/mknod.cpp new file mode 100644 index 00000000..8a8210b2 --- /dev/null +++ b/src/mknod.cpp @@ -0,0 +1,102 @@ +/* + The MIT License (MIT) + + Copyright (c) 2014 Antonio SJ Musumeci + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +#include + +#include +#include + +#include +#include +#include +#include +#include + +#include "ugid.hpp" +#include "fs.hpp" +#include "config.hpp" +#include "assert.hpp" + +using std::string; +using std::vector; +using mergerfs::Policy; + +static +int +_mknod(const Policy::Search::Func searchFunc, + const Policy::Create::Func createPathFunc, + const vector &srcmounts, + const string fusepath, + const mode_t mode, + const dev_t dev) +{ + int rv; + string path; + string dirname; + fs::Path createpath; + fs::Path existingpath; + + if(fs::path_exists(srcmounts,fusepath)) + return -EEXIST; + + dirname = fs::dirname(fusepath); + existingpath = searchFunc(srcmounts,dirname); + if(existingpath.base.empty()) + return -ENOENT; + + createpath = createPathFunc(srcmounts,dirname); + if(existingpath.base != createpath.base) + fs::clonepath(existingpath.base,createpath.base,dirname); + + path = fs::make_path(createpath.base,fusepath); + + rv = ::mknod(path.c_str(),mode,dev); + + return ((rv == -1) ? -errno : 0); +} + +namespace mergerfs +{ + namespace mknod + { + int + mknod(const char *fusepath, + mode_t mode, + dev_t rdev) + { + const ugid::SetResetGuard ugid; + const config::Config &config = config::get(); + + if(fusepath == config.controlfile) + return -EEXIST; + + return _mknod(config.policy.search, + config.policy.create, + config.srcmounts, + fusepath, + mode, + rdev); + } + } +} diff --git a/src/mknod.hpp b/src/mknod.hpp new file mode 100644 index 00000000..e891d9b2 --- /dev/null +++ b/src/mknod.hpp @@ -0,0 +1,34 @@ +/* + The MIT License (MIT) + + Copyright (c) 2014 Antonio SJ Musumeci + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +namespace mergerfs +{ + namespace mknod + { + int + mknod(const char *fusepath, + mode_t mode, + dev_t rdev); + } +} diff --git a/src/open.cpp b/src/open.cpp new file mode 100644 index 00000000..b2ffcc12 --- /dev/null +++ b/src/open.cpp @@ -0,0 +1,99 @@ +/* + The MIT License (MIT) + + Copyright (c) 2014 Antonio SJ Musumeci + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +#include + +#include +#include +#include +#include +#include +#include + +#include "ugid.hpp" +#include "fileinfo.hpp" +#include "fs.hpp" +#include "config.hpp" +#include "assert.hpp" + +using std::string; +using std::vector; +using mergerfs::FileInfo; +using mergerfs::Policy; + +static +int +_open_controlfile(uint64_t &fh) +{ + fh = (uint64_t)new string; + + return 0; +} + +static +int +_open(const Policy::Search::Func searchFunc, + const vector &srcmounts, + const string fusepath, + const int flags, + uint64_t &fh) +{ + int fd; + fs::Path path; + + path = searchFunc(srcmounts,fusepath); + if(path.full.empty()) + return -ENOENT; + + fd = ::open(path.full.c_str(),flags); + if(fd == -1) + return -errno; + + fh = (uint64_t)new FileInfo(fd,flags,path.full); + + return 0; +} + +namespace mergerfs +{ + namespace open + { + int + open(const char *fusepath, + struct fuse_file_info *fileinfo) + { + const ugid::SetResetGuard ugid; + const config::Config &config = config::get(); + + if(fusepath == config.controlfile) + return _open_controlfile(fileinfo->fh); + + return _open(config.policy.search, + config.srcmounts, + fusepath, + fileinfo->flags, + fileinfo->fh); + } + } +} diff --git a/src/open.hpp b/src/open.hpp new file mode 100644 index 00000000..7eaefd7b --- /dev/null +++ b/src/open.hpp @@ -0,0 +1,35 @@ +/* + The MIT License (MIT) + + Copyright (c) 2014 Antonio SJ Musumeci + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +#include + +namespace mergerfs +{ + namespace open + { + int + open(const char *fusepath, + struct fuse_file_info *fileinfo); + } +} diff --git a/src/option_parser.cpp b/src/option_parser.cpp new file mode 100644 index 00000000..8c3b0a1b --- /dev/null +++ b/src/option_parser.cpp @@ -0,0 +1,145 @@ +/* + The MIT License (MIT) + + Copyright (c) 2014 Antonio SJ Musumeci + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +#include + +#include +#include + +#include +#include +#include + +#include "config.hpp" +#include "policy.hpp" + +using namespace mergerfs; + +template +Container& +split(Container &result, + const typename Container::value_type &s, + typename Container::value_type::value_type delimiter) +{ + std::istringstream ss(s); + while(!ss.eof()) + { + typename Container::value_type field; + std::getline(ss,field,delimiter); + if(field.empty()) + continue; + result.push_back(field); + } + + return result; +} + +static +int +process_opt(config::Config &config, + const std::string arg) +{ + int rv = 0; + std::vector argvalue; + Policy &policy = config.policy; + + split(argvalue,arg,'='); + switch(argvalue.size()) + { + case 2: + if(argvalue[0] == "statfs") + policy.statfs.fromString(argvalue[1]); + else if(argvalue[0] == "create") + policy.create.fromString(argvalue[1]); + else if(argvalue[0] == "search") + policy.search.fromString(argvalue[1]); + else if(argvalue[0] == "action") + policy.action.fromString(argvalue[1]); + else + rv = 1; + break; + + default: + case 1: + if(argvalue[0] == "test") + config.testmode = true; + else + rv = 1; + break; + }; + + return rv; +} + +static +int +option_processor(void *data, + const char *arg, + int key, + struct fuse_args *outargs) +{ + int rv = 0; + config::Config &config = *(config::Config*)data; + + switch(key) + { + case FUSE_OPT_KEY_OPT: + rv = process_opt(config,arg); + break; + + case FUSE_OPT_KEY_NONOPT: + if(config.destmount.empty()) + { + rv = 1; + config.destmount = arg; + } + else + { + split(config.srcmounts,arg,':'); + } + break; + + default: + break; + } + + return rv; +} + +namespace mergerfs +{ + namespace options + { + void + parse(struct fuse_args &args, + config::Config &config) + { + fuse_opt_parse(&args, + &config, + NULL, + ::option_processor); + config.updateReadStr(); + } + } +} diff --git a/src/option_parser.hpp b/src/option_parser.hpp new file mode 100644 index 00000000..2dc1928b --- /dev/null +++ b/src/option_parser.hpp @@ -0,0 +1,37 @@ +/* + The MIT License (MIT) + + Copyright (c) 2014 Antonio SJ Musumeci + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +#include + +#include "config.hpp" + +namespace mergerfs +{ + namespace options + { + void + parse(struct fuse_args &args, + mergerfs::config::Config &config); + } +} diff --git a/src/policy.cpp b/src/policy.cpp new file mode 100644 index 00000000..51d509dc --- /dev/null +++ b/src/policy.cpp @@ -0,0 +1,306 @@ +/* + The MIT License (MIT) + + Copyright (c) 2014 Antonio SJ Musumeci + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +#include "policy.hpp" +#include "fs.hpp" + +namespace mergerfs +{ + Policy::Create::Create(Type value) + { + *this = value; + } + + Policy::Create& + Policy::Create::operator=(const Policy::Create::Type value) + { + _str = toString(value); + _value = fromString(_str); + _func = findFunc(_value); + + return *this; + } + + Policy::Create& + Policy::Create::operator=(const std::string str) + { + _value = fromString(str); + _str = toString(_value); + _func = findFunc(_value); + + return *this; + } + + Policy::Create::Type + Policy::Create::fromString(const std::string str) + { + if(str == "ep") + return Policy::Create::ExistingPath; + else if(str == "mfs") + return Policy::Create::MostFreeSpace; + else if(str == "epmfs") + return Policy::Create::ExistingPathMostFreeSpace; + else if(str == "rand") + return Policy::Create::Random; + + return Policy::Create::Invalid; + } + + std::string + Policy::Create::toString(const Type value) + { + switch(value) + { + case Policy::Create::ExistingPath: + return "ep"; + case Policy::Create::ExistingPathMostFreeSpace: + return "epmfs"; + case Policy::Create::MostFreeSpace: + return "mfs"; + case Policy::Create::Random: + return "rand"; + case Policy::Create::Invalid: + default: + return "invalid"; + } + } + + Policy::Create::Func + Policy::Create::findFunc(Policy::Create::Type value) + { + switch(value) + { + case Policy::Create::ExistingPath: + return fs::find_first; + case Policy::Create::ExistingPathMostFreeSpace: + return fs::find_mfs_existing; + case Policy::Create::MostFreeSpace: + return fs::find_mfs; + case Policy::Create::Random: + return fs::find_random; + case Policy::Create::Invalid: + default: + return NULL; + } + } + + Policy::Search::Search(Type value) + { + *this = value; + } + + Policy::Search& + Policy::Search::operator=(const Policy::Search::Type value) + { + _str = toString(value); + _value = fromString(_str); + _func = findFunc(_value); + + return *this; + } + + Policy::Search& + Policy::Search::operator=(const std::string str) + { + _value = fromString(str); + _str = toString(_value); + _func = findFunc(_value); + + return *this; + } + + Policy::Search::Type + Policy::Search::fromString(const std::string str) + { + if(str == "ff") + return Policy::Search::FirstFound; + else if(str == "ffwp") + return Policy::Search::FirstFoundWithPermissions; + else if(str == "newest") + return Policy::Search::Newest; + + return Policy::Search::Invalid; + } + + std::string + Policy::Search::toString(Policy::Search::Type value) + { + switch(value) + { + case Policy::Search::FirstFound: + return "ff"; + case Policy::Search::FirstFoundWithPermissions: + return "ffwp"; + case Policy::Search::Newest: + return "newest"; + case Policy::Search::Invalid: + default: + return "invalid"; + } + } + + Policy::Search::Func + Policy::Search::findFunc(Policy::Search::Type value) + { + switch(value) + { + case Policy::Search::FirstFound: + return fs::find_first; + case Policy::Search::FirstFoundWithPermissions: + return fs::find_first_with_permission; + case Policy::Search::Newest: + return fs::find_newest; + case Policy::Search::Invalid: + default: + return NULL; + } + } + + Policy::Action::Action(const Policy::Action::Type value) + { + *this = value; + } + + Policy::Action& + Policy::Action::operator=(const Policy::Action::Type value) + { + _str = toString(value); + _value = fromString(_str); + _func = findFunc(_value); + + return *this; + } + + Policy::Action& + Policy::Action::operator=(const std::string str) + { + _value = fromString(str); + _str = toString(_value); + _func = findFunc(_value); + + return *this; + } + + Policy::Action::Type + Policy::Action::fromString(const std::string str) + { + if(str == "ff") + return Policy::Action::FirstFound; + else if(str == "ffwp") + return Policy::Action::FirstFoundWithPermissions; + else if(str == "newest") + return Policy::Action::Newest; + else if(str == "all") + return Policy::Action::All; + + return Policy::Action::Invalid; + } + + std::string + Policy::Action::toString(Policy::Action::Type value) + { + switch(value) + { + case Policy::Action::FirstFound: + return "ff"; + case Policy::Action::FirstFoundWithPermissions: + return "ffwp"; + case Policy::Action::Newest: + return "newest"; + case Policy::Action::All: + return "all"; + case Policy::Action::Invalid: + default: + return "invalid"; + } + } + + Policy::Action::Func + Policy::Action::findFunc(const Policy::Action::Type value) + { + switch(value) + { + case Policy::Action::FirstFound: + return fs::find_first; + case Policy::Action::FirstFoundWithPermissions: + return fs::find_first_with_permission; + case Policy::Action::Newest: + return fs::find_newest; + case Policy::Action::All: + return fs::find_all; + case Policy::Action::Invalid: + default: + return NULL; + } + } + + Policy::StatFS::StatFS(const Policy::StatFS::Type value) + { + *this = value; + } + + Policy::StatFS& + Policy::StatFS::operator=(Policy::StatFS::Type value) + { + _str = toString(value); + _value = fromString(_str); + + return *this; + } + + Policy::StatFS& + Policy::StatFS::operator=(const std::string str) + { + _value = fromString(str); + _str = toString(_value); + + return *this; + } + + Policy::StatFS::Type + Policy::StatFS::fromString(const std::string str) + { + if(str == "susf") + return Policy::StatFS::SumUsedSumFree; + else if(str == "sumf") + return Policy::StatFS::SumUsedMaxFree; + + return Policy::StatFS::Invalid; + } + + std::string + Policy::StatFS::toString(const Policy::StatFS::Type value) + { + switch(value) + { + case Policy::StatFS::SumUsedSumFree: + return "susf"; + case Policy::StatFS::SumUsedMaxFree: + return "sumf"; + case Policy::StatFS::Invalid: + default: + return "invalid"; + } + } +} diff --git a/src/policy.hpp b/src/policy.hpp new file mode 100644 index 00000000..bb84317d --- /dev/null +++ b/src/policy.hpp @@ -0,0 +1,207 @@ +/* + The MIT License (MIT) + + Copyright (c) 2014 Antonio SJ Musumeci + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +#ifndef __POLICY_HPP__ +#define __POLICY_HPP__ + +#include + +#include "fs.hpp" + +namespace mergerfs +{ + class Policy + { + public: + enum Type + { + STATFS, + ACTION, + CREATE, + SEARCH + }; + + public: + class StatFS + { + public: + enum Type + { + Invalid = -1, + SumUsedSumFree, + SumUsedMaxFree, + Max + }; + + StatFS(Type _value); + + public: + operator Type() const { return _value; } + operator std::string() const { return _str; } + std::string str() const { return _str; } + + StatFS& operator=(const Type); + StatFS& operator=(const std::string); + + static Type fromString(const std::string); + static std::string toString(const Type); + + private: + StatFS(); + + private: + Type _value; + std::string _str; + }; + + class Create + { + public: + typedef fs::SearchFunc Func; + enum Type + { + Invalid = -1, + ExistingPath, + MostFreeSpace, + ExistingPathMostFreeSpace, + Random, + Max + }; + + public: + Create(Type); + + operator Type() const { return _value; } + operator Func() const { return _func; } + operator std::string() const { return _str; } + std::string str() const { return _str; } + + Create& operator=(const Type); + Create& operator=(const std::string); + + static Type fromString(const std::string); + static std::string toString(const Type); + static Func findFunc(const Type); + + private: + Create(); + + private: + Type _value; + std::string _str; + Func _func; + }; + + class Search + { + public: + typedef fs::SearchFunc Func; + enum Type + { + Invalid = -1, + FirstFound, + FirstFoundWithPermissions, + Newest, + Max + }; + + public: + Search(Type); + + operator Type() const { return _value; } + operator Func() const { return _func; } + operator std::string() const { return _str; } + std::string str() const { return _str; } + + Search& operator=(const Type); + Search& operator=(const std::string); + + static Type fromString(const std::string); + static std::string toString(const Type); + static Func findFunc(const Type); + + private: + Search(); + + private: + Type _value; + std::string _str; + Func _func; + }; + + class Action + { + public: + typedef fs::VecSearchFunc Func; + enum Type + { + Invalid = -1, + FirstFound, + FirstFoundWithPermissions, + Newest, + All, + Max + }; + + public: + Action(Type); + + operator Type() const { return _value; } + operator Func() const { return _func; } + operator std::string() const { return _str; } + std::string str() const { return _str; } + + Action& operator=(const Type); + Action& operator=(const std::string); + + static Type fromString(const std::string); + static std::string toString(const Type); + static Func findFunc(const Type); + + private: + Action(); + + private: + Type _value; + std::string _str; + Func _func; + }; + + public: + Policy() : + statfs(StatFS::SumUsedMaxFree), + create(Create::ExistingPathMostFreeSpace), + search(Search::FirstFound), + action(Action::FirstFound) + {} + + public: + StatFS statfs; + Create create; + Search search; + Action action; + }; +} + +#endif diff --git a/src/read.cpp b/src/read.cpp new file mode 100644 index 00000000..56f8c204 --- /dev/null +++ b/src/read.cpp @@ -0,0 +1,94 @@ +/* + The MIT License (MIT) + + Copyright (c) 2014 Antonio SJ Musumeci + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +#include + +#include +#include +#include + +#include +#include + +#include "config.hpp" +#include "ugid.hpp" +#include "fileinfo.hpp" + +using std::string; + +static +int +_read_controlfile(const string readstr, + char *buf, + const size_t count) +{ + size_t n; + + n = std::min(count,readstr.length()); + + memcpy(buf,readstr.data(),n); + + return (int)n; +} + +static +int +_read(const int fd, + void *buf, + const size_t count, + const off_t offset) +{ + int rv; + + rv = ::pread(fd,buf,count,offset); + + return ((rv == -1) ? -errno : rv); +} + +namespace mergerfs +{ + namespace read + { + int + read(const char *fusepath, + char *buf, + size_t count, + off_t offset, + struct fuse_file_info *fi) + { + const ugid::SetResetGuard ugid; + const config::Config &config = config::get(); + + if(fusepath == config.controlfile) + return _read_controlfile(config.readstr, + buf, + count); + + return _read(((FileInfo*)fi->fh)->fd, + buf, + count, + offset); + } + } +} diff --git a/src/read.hpp b/src/read.hpp new file mode 100644 index 00000000..b3cf9dcc --- /dev/null +++ b/src/read.hpp @@ -0,0 +1,36 @@ +/* + The MIT License (MIT) + + Copyright (c) 2014 Antonio SJ Musumeci + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +namespace mergerfs +{ + namespace read + { + int + read(const char *fusepath, + char *buf, + size_t count, + off_t offset, + struct fuse_file_info *fi); + } +} diff --git a/src/readdir.cpp b/src/readdir.cpp new file mode 100644 index 00000000..19c61738 --- /dev/null +++ b/src/readdir.cpp @@ -0,0 +1,147 @@ +/* + The MIT License (MIT) + + Copyright (c) 2014 Antonio SJ Musumeci + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +#include + +#include +#include +#include + +#include +#include + +#include "readdir.hpp" +#include "config.hpp" +#include "ugid.hpp" +#include "fs.hpp" + +using std::string; +using std::vector; +using std::set; +using std::pair; +using mergerfs::readdir::FileData; + +#define NO_OFFSET 0 + +static +int +_readdir(const vector &srcmounts, + const string dirname, + void *buf, + const fuse_fill_dir_t filler) +{ + set found; + + for(vector::const_iterator + iter = srcmounts.begin(), enditer = srcmounts.end(); + iter != enditer; + ++iter) + { + DIR *dh; + string basepath; + + basepath = fs::make_path(*iter,dirname); + dh = ::opendir(basepath.c_str()); + if(!dh) + continue; + + for(struct dirent *de = ::readdir(dh); de != NULL; de = ::readdir(dh)) + { + string d_name(de->d_name); + pair::iterator,bool> ret; + + ret = found.insert(d_name); + if(ret.second == false) + continue; + + { + struct stat st; + string path; + + path = fs::make_path(basepath,d_name); + if(::lstat(path.c_str(),&st) == -1) + { + found.erase(ret.first); + continue; + } + + filler(buf,de->d_name,&st,NO_OFFSET); + } + } + + ::closedir(dh); + } + + if(found.empty()) + return -ENOENT; + + return 0; +} + +static +int +stat_vector_filler(void *buf, + const char *name, + const struct stat *stbuf, + off_t off) +{ + vector *stats = (vector*)buf; + + stats->push_back(FileData(name,*stbuf)); + + return 0; +} + +namespace mergerfs +{ + namespace readdir + { + int + readdir(const vector &srcmounts, + const string dirname, + vector &stats) + { + return _readdir(srcmounts, + dirname, + &stats, + stat_vector_filler); + } + + int + readdir(const char *fusepath, + void *buf, + fuse_fill_dir_t filler, + off_t offset, + struct fuse_file_info *fi) + { + const ugid::SetResetGuard ugid; + const config::Config &config = config::get(); + + return _readdir(config.srcmounts, + fusepath, + buf, + filler); + } + } +} diff --git a/src/readdir.hpp b/src/readdir.hpp new file mode 100644 index 00000000..26dc03f9 --- /dev/null +++ b/src/readdir.hpp @@ -0,0 +1,67 @@ +/* + The MIT License (MIT) + + Copyright (c) 2014 Antonio SJ Musumeci + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +#ifndef __MERGERFS_READDIR_HPP__ +#define __MERGERFS_READDIR_HPP__ + +#include + +#include +#include + +#include +#include +#include + +namespace mergerfs +{ + namespace readdir + { + struct FileData + { + FileData(std::string filename_, + struct stat stats_) + : filename(filename_), + stats(stats_) + {} + + std::string filename; + struct stat stats; + }; + + int + readdir(const char *fusepath, + void *buf, + fuse_fill_dir_t filler, + off_t offset, + struct fuse_file_info *fi); + + int + readdir(const std::vector &srcmounts, + const std::string dirname, + std::vector &stats); + } +} + +#endif diff --git a/src/readlink.cpp b/src/readlink.cpp new file mode 100644 index 00000000..05887485 --- /dev/null +++ b/src/readlink.cpp @@ -0,0 +1,88 @@ +/* + The MIT License (MIT) + + Copyright (c) 2014 Antonio SJ Musumeci + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +#include + +#include +#include +#include +#include +#include + +#include "config.hpp" +#include "ugid.hpp" +#include "fs.hpp" +#include "assert.hpp" + +using std::string; +using std::vector; +using mergerfs::Policy; + +static +int +_readlink(const Policy::Search::Func searchFunc, + const vector& srcmounts, + const string fusepath, + char *buf, + const size_t size) +{ + int rv; + string path; + + path = searchFunc(srcmounts,fusepath).full; + if(path.empty()) + return -ENOENT; + + rv = ::readlink(path.c_str(),buf,size); + if(rv == -1) + return -errno; + + buf[rv] = '\0'; + + return 0; +} + +namespace mergerfs +{ + namespace readlink + { + int + readlink(const char *fusepath, + char *buf, + size_t size) + { + const ugid::SetResetGuard ugid; + const config::Config &config = config::get(); + + if(fusepath == config.controlfile) + return -EINVAL; + + return _readlink(config.policy.search, + config.srcmounts, + fusepath, + buf, + size); + } + } +} diff --git a/src/readlink.hpp b/src/readlink.hpp new file mode 100644 index 00000000..82740545 --- /dev/null +++ b/src/readlink.hpp @@ -0,0 +1,34 @@ +/* + The MIT License (MIT) + + Copyright (c) 2014 Antonio SJ Musumeci + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +namespace mergerfs +{ + namespace readlink + { + int + readlink(const char *fusepath, + char *buf, + size_t size); + } +} diff --git a/src/release.cpp b/src/release.cpp new file mode 100644 index 00000000..7d9d8ede --- /dev/null +++ b/src/release.cpp @@ -0,0 +1,83 @@ +/* + The MIT License (MIT) + + Copyright (c) 2014 Antonio SJ Musumeci + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +#include + +#include + +#include + +#include "config.hpp" +#include "ugid.hpp" +#include "fileinfo.hpp" + +using std::string; +using mergerfs::FileInfo; + +static +int +_release_controlfile(uint64_t &fh) +{ + string *strbuf = (string*)fh; + + delete strbuf; + + fh = 0; + + return 0; +} + +static +int +_release(uint64_t &fh) +{ + const FileInfo *fileinfo = (FileInfo*)fh; + + ::close(fileinfo->fd); + + delete fileinfo; + + fh = 0; + + return 0; +} + +namespace mergerfs +{ + namespace release + { + int + release(const char *fusepath, + struct fuse_file_info *fi) + { + const ugid::SetResetGuard ugid; + const config::Config &config = config::get(); + + if(fusepath == config.controlfile) + return _release_controlfile(fi->fh); + + return _release(fi->fh); + } + } +} diff --git a/src/release.hpp b/src/release.hpp new file mode 100644 index 00000000..95db9865 --- /dev/null +++ b/src/release.hpp @@ -0,0 +1,33 @@ +/* + The MIT License (MIT) + + Copyright (c) 2014 Antonio SJ Musumeci + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +namespace mergerfs +{ + namespace release + { + int + release(const char *fusepath, + struct fuse_file_info *fi); + } +} diff --git a/src/removexattr.cpp b/src/removexattr.cpp new file mode 100644 index 00000000..ed5a9d03 --- /dev/null +++ b/src/removexattr.cpp @@ -0,0 +1,93 @@ +/* + The MIT License (MIT) + + Copyright (c) 2014 Antonio SJ Musumeci + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +#include +#include + +#include +#include +#include + +#include "config.hpp" +#include "ugid.hpp" +#include "fs.hpp" +#include "assert.hpp" + +using std::string; +using std::vector; +using mergerfs::Policy; + +static +int +_removexattr(const Policy::Action::Func searchFunc, + const vector &srcmounts, + const string fusepath, + const char *attrname) +{ +#ifndef WITHOUT_XATTR + int rv; + int error; + vector paths; + + searchFunc(srcmounts,fusepath,paths); + if(paths.empty()) + return -ENOENT; + + rv = -1; + error = 0; + for(vector::const_iterator + i = paths.begin(), ei = paths.end(); i != ei; ++i) + { + rv &= ::lremovexattr(i->full.c_str(),attrname); + if(rv == -1) + error = errno; + } + + return ((rv == -1) ? -error : 0); +#else + return -ENOTSUP; +#endif +} + +namespace mergerfs +{ + namespace removexattr + { + int + removexattr(const char *fusepath, + const char *attrname) + { + const ugid::SetResetGuard uid; + const config::Config &config = config::get(); + + if(fusepath == config.controlfile) + return -ENOTSUP; + + return _removexattr(config.policy.action, + config.srcmounts, + fusepath, + attrname); + } + } +} diff --git a/src/removexattr.hpp b/src/removexattr.hpp new file mode 100644 index 00000000..93aacae5 --- /dev/null +++ b/src/removexattr.hpp @@ -0,0 +1,33 @@ +/* + The MIT License (MIT) + + Copyright (c) 2014 Antonio SJ Musumeci + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +namespace mergerfs +{ + namespace removexattr + { + int + removexattr(const char *fusepath, + const char *attrname); + } +} diff --git a/src/rename.cpp b/src/rename.cpp new file mode 100644 index 00000000..49a277a2 --- /dev/null +++ b/src/rename.cpp @@ -0,0 +1,82 @@ +/* + The MIT License (MIT) + + Copyright (c) 2014 Antonio SJ Musumeci + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +#include +#include +#include + +#include +#include + +#include "ugid.hpp" +#include "fs.hpp" +#include "config.hpp" + +using std::string; +using std::vector; +using mergerfs::Policy; + +static +int +_rename(const Policy::Search::Func searchFunc, + const vector &srcmounts, + const string from, + const string to) +{ + int rv; + string pathto; + fs::Path pathfrom; + + pathfrom = searchFunc(srcmounts,from); + if(pathfrom.base.empty()) + return -ENOENT; + + pathto = fs::make_path(pathfrom.base,to); + + rv = ::rename(pathfrom.full.c_str(),pathto.c_str()); + + return ((rv == -1) ? -errno : 0); +} + +namespace mergerfs +{ + namespace rename + { + int + rename(const char *from, + const char *to) + { + const ugid::SetResetGuard uid; + const config::Config &config = config::get(); + + if(from == config.controlfile) + return -ENOENT; + + return _rename(config.policy.search, + config.srcmounts, + from, + to); + } + } +} diff --git a/src/rename.hpp b/src/rename.hpp new file mode 100644 index 00000000..9749e8ad --- /dev/null +++ b/src/rename.hpp @@ -0,0 +1,33 @@ +/* + The MIT License (MIT) + + Copyright (c) 2014 Antonio SJ Musumeci + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +namespace mergerfs +{ + namespace rename + { + int + rename(const char *from, + const char *to); + } +} diff --git a/src/resources.cpp b/src/resources.cpp new file mode 100644 index 00000000..5759c2d2 --- /dev/null +++ b/src/resources.cpp @@ -0,0 +1,78 @@ +/* + The MIT License (MIT) + + Copyright (c) 2014 Antonio SJ Musumeci + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +#include +#include +#include +#include +#include + +namespace mergerfs +{ + namespace resources + { + int + reset_umask(void) + { + umask(0); + return 0; + } + + int + maxout_rlimit(int resource) + { + struct rlimit rlim; + + if(geteuid() == 0) + { + rlim.rlim_cur = RLIM_INFINITY; + rlim.rlim_max = RLIM_INFINITY; + } + else + { + int rv; + + rv = ::getrlimit(resource,&rlim); + if(rv == -1) + return -1; + + rlim.rlim_cur = rlim.rlim_max; + } + + return ::setrlimit(resource,&rlim); + } + + int + maxout_rlimit_nofile(void) + { + return maxout_rlimit(RLIMIT_NOFILE); + } + + int + maxout_rlimit_fsize(void) + { + return maxout_rlimit(RLIMIT_FSIZE); + } + } +} diff --git a/src/resources.hpp b/src/resources.hpp new file mode 100644 index 00000000..ed0daef0 --- /dev/null +++ b/src/resources.hpp @@ -0,0 +1,34 @@ +/* + The MIT License (MIT) + + Copyright (c) 2014 Antonio SJ Musumeci + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +namespace mergerfs +{ + namespace resources + { + int reset_umask(void); + int maxout_rlimit(int resource); + int maxout_rlimit_nofile(void); + int maxout_rlimit_fsize(void); + } +} diff --git a/src/rmdir.cpp b/src/rmdir.cpp new file mode 100644 index 00000000..ff0df2ed --- /dev/null +++ b/src/rmdir.cpp @@ -0,0 +1,84 @@ +/* + The MIT License (MIT) + + Copyright (c) 2014 Antonio SJ Musumeci + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +#include +#include + +#include + +#include "ugid.hpp" +#include "fs.hpp" +#include "config.hpp" +#include "assert.hpp" + +using std::string; +using std::vector; +using mergerfs::Policy; + +static +int +_rmdir(const Policy::Action::Func searchFunc, + const vector &srcmounts, + const string fusepath) +{ + int rv; + int error; + vector paths; + + searchFunc(srcmounts,fusepath,paths); + if(paths.empty()) + return -ENOENT; + + rv = -1; + error = 0; + for(vector::const_iterator + i = paths.begin(), ei = paths.end(); i != ei; ++i) + { + rv &= ::rmdir(i->full.c_str()); + if(rv == -1) + error = errno; + } + + return ((rv == -1) ? -error : 0); +} + +namespace mergerfs +{ + namespace rmdir + { + int + rmdir(const char *fusepath) + { + const ugid::SetResetGuard uid; + const config::Config &config = config::get(); + + if(fusepath == config.controlfile) + return -ENOTDIR; + + return _rmdir(config.policy.action, + config.srcmounts, + fusepath); + } + } +} diff --git a/src/rmdir.hpp b/src/rmdir.hpp new file mode 100644 index 00000000..1713e0c9 --- /dev/null +++ b/src/rmdir.hpp @@ -0,0 +1,32 @@ +/* + The MIT License (MIT) + + Copyright (c) 2014 Antonio SJ Musumeci + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +namespace mergerfs +{ + namespace rmdir + { + int + rmdir(const char *fusepath); + } +} diff --git a/src/setxattr.cpp b/src/setxattr.cpp new file mode 100644 index 00000000..f3e44b35 --- /dev/null +++ b/src/setxattr.cpp @@ -0,0 +1,101 @@ +/* + The MIT License (MIT) + + Copyright (c) 2014 Antonio SJ Musumeci + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +#include +#include + +#include +#include +#include + +#include "config.hpp" +#include "fs.hpp" +#include "ugid.hpp" +#include "assert.hpp" + +using std::string; +using std::vector; +using mergerfs::Policy; + +static +int +_setxattr(const Policy::Action::Func searchFunc, + const vector &srcmounts, + const string fusepath, + const char *attrname, + const char *attrval, + const size_t attrvalsize, + const int flags) +{ +#ifndef WITHOUT_XATTR + int rv; + int error; + vector paths; + + searchFunc(srcmounts,fusepath,paths); + if(paths.empty()) + return -ENOENT; + + rv = -1; + error = 0; + for(vector::const_iterator + i = paths.begin(), ei = paths.end(); i != ei; ++i) + { + rv &= ::lsetxattr(i->full.c_str(),attrname,attrval,attrvalsize,flags); + if(rv == -1) + error = errno; + } + + return ((rv == -1) ? -error : 0); +#else + return -ENOTSUP; +#endif +} +namespace mergerfs +{ + namespace setxattr + { + int + setxattr(const char *fusepath, + const char *attrname, + const char *attrval, + size_t attrvalsize, + int flags) + { + const ugid::SetResetGuard uid; + const config::Config &config = config::get(); + + if(fusepath == config.controlfile) + return -ENOTSUP; + + return _setxattr(config.policy.action, + config.srcmounts, + fusepath, + attrname, + attrval, + attrvalsize, + flags); + } + } +} diff --git a/src/setxattr.hpp b/src/setxattr.hpp new file mode 100644 index 00000000..88d0ed04 --- /dev/null +++ b/src/setxattr.hpp @@ -0,0 +1,36 @@ +/* + The MIT License (MIT) + + Copyright (c) 2014 Antonio SJ Musumeci + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +namespace mergerfs +{ + namespace setxattr + { + int + setxattr(const char *fusepath, + const char *attrname, + const char *attrval, + size_t attrvalsize, + int flags); + } +} diff --git a/src/statfs.cpp b/src/statfs.cpp new file mode 100644 index 00000000..336193b5 --- /dev/null +++ b/src/statfs.cpp @@ -0,0 +1,160 @@ +/* + The MIT License (MIT) + + Copyright (c) 2014 Antonio SJ Musumeci + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +#include + +#include +#include + +#include +#include +#include +#include + +#include "ugid.hpp" +#include "config.hpp" + +using std::string; +using std::vector; +using std::map; +using std::pair; +using mergerfs::Policy; + +static +void +_normalize_statvfs(struct statvfs *fsstat, + const unsigned long min_bsize, + const unsigned long min_frsize, + const unsigned long min_namemax) +{ + if(fsstat->f_bsize > min_bsize) + { + fsstat->f_bfree *= fsstat->f_bsize / min_bsize; + fsstat->f_bavail *= fsstat->f_bsize / min_bsize; + fsstat->f_bsize = min_bsize; + } + + if(fsstat->f_frsize > min_frsize) + { + fsstat->f_blocks *= fsstat->f_frsize / min_frsize; + fsstat->f_frsize = min_frsize; + } + + if(fsstat->f_namemax > min_namemax) + fsstat->f_namemax = min_namemax; +} + +static +void +_merge_statvfs(struct statvfs * const out, + const struct statvfs * const in, + const Policy::StatFS policy) +{ + switch(policy) + { + case Policy::StatFS::SumUsedMaxFree: + if(out->f_bfree < in->f_bfree) + { + out->f_bfree = in->f_bfree; + out->f_bavail = in->f_bavail; + } + break; + + default: + case Policy::StatFS::SumUsedSumFree: + out->f_bfree += in->f_bfree; + out->f_bavail += in->f_bavail; + break; + } + + out->f_ffree += in->f_ffree; + out->f_favail += in->f_favail; + out->f_files += in->f_files; + out->f_blocks += in->f_blocks; +} + +static +int +_statfs(const Policy::StatFS policy, + const vector &srcmounts, + struct statvfs &fsstat) +{ + unsigned long min_bsize = ULONG_MAX; + unsigned long min_frsize = ULONG_MAX; + unsigned long min_namemax = ULONG_MAX; + map fsstats; + vector::const_iterator iter; + vector::const_iterator enditer; + + for(iter = srcmounts.begin(), enditer = srcmounts.end(); iter != enditer; ++iter) + { + int rv; + struct statvfs fsstat; + rv = ::statvfs(iter->c_str(),&fsstat); + if(rv != 0) + continue; + + if(min_bsize > fsstat.f_bsize) + min_bsize = fsstat.f_bsize; + if(min_frsize > fsstat.f_frsize) + min_frsize = fsstat.f_frsize; + if(min_namemax > fsstat.f_namemax) + min_namemax = fsstat.f_namemax; + + fsstats.insert(pair(fsstat.f_fsid,fsstat)); + } + + map::iterator fsstatiter = fsstats.begin(); + map::iterator endfsstatiter = fsstats.end(); + if(fsstatiter != endfsstatiter) + { + fsstat = (fsstatiter++)->second; + _normalize_statvfs(&fsstat,min_bsize,min_frsize,min_namemax); + for(;fsstatiter != endfsstatiter;++fsstatiter) + { + _normalize_statvfs(&fsstatiter->second,min_bsize,min_frsize,min_namemax); + _merge_statvfs(&fsstat,&fsstatiter->second,policy); + } + } + + return 0; +} + +namespace mergerfs +{ + namespace statfs + { + int + statfs(const char *fusepath, + struct statvfs *stat) + { + const ugid::SetResetGuard uid; + const config::Config &config = config::get(); + + return _statfs(config.policy.statfs, + config.srcmounts, + *stat); + } + } +} diff --git a/src/statfs.hpp b/src/statfs.hpp new file mode 100644 index 00000000..79fcff2f --- /dev/null +++ b/src/statfs.hpp @@ -0,0 +1,33 @@ +/* + The MIT License (MIT) + + Copyright (c) 2014 Antonio SJ Musumeci + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +namespace mergerfs +{ + namespace statfs + { + int + statfs(const char *fusepath, + struct statvfs *fsstat); + } +} diff --git a/src/symlink.cpp b/src/symlink.cpp new file mode 100644 index 00000000..6f5b04b2 --- /dev/null +++ b/src/symlink.cpp @@ -0,0 +1,79 @@ +/* + The MIT License (MIT) + + Copyright (c) 2014 Antonio SJ Musumeci + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +#include + +#include +#include +#include +#include + +#include "fs.hpp" +#include "config.hpp" +#include "ugid.hpp" + +using std::string; +using std::vector; + +static +int +_symlink(const vector &srcmounts, + const string from, + const string to) +{ + int rv; + fs::Path path; + + path = fs::find_first(srcmounts,fs::dirname(to)); + if(path.base.empty()) + return -ENOENT; + + path.full = fs::make_path(path.base,to); + + rv = symlink(from.c_str(),path.full.c_str()); + + return ((rv == -1) ? -errno : 0); +} + +namespace mergerfs +{ + namespace symlink + { + int + symlink(const char *oldpath, + const char *newpath) + { + const ugid::SetResetGuard uid; + const config::Config &config = config::get(); + + if(oldpath == config.controlfile || + newpath == config.controlfile) + return -EPERM; + + return _symlink(config.srcmounts, + oldpath, + newpath); + } + } +} diff --git a/src/symlink.hpp b/src/symlink.hpp new file mode 100644 index 00000000..63a601cb --- /dev/null +++ b/src/symlink.hpp @@ -0,0 +1,33 @@ +/* + The MIT License (MIT) + + Copyright (c) 2014 Antonio SJ Musumeci + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +namespace mergerfs +{ + namespace symlink + { + int + symlink(const char *oldpath, + const char *newpath); + } +} diff --git a/src/test.cpp b/src/test.cpp new file mode 100644 index 00000000..be57116b --- /dev/null +++ b/src/test.cpp @@ -0,0 +1,53 @@ +/* + The MIT License (MIT) + + Copyright (c) 2014 Antonio SJ Musumeci + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +#include +#include +#include + +#include +#include +#include + +#include +#include +#include + +#include + +#include "config.hpp" +#include "readdir.hpp" +#include "fs.hpp" + +using std::string; + +namespace mergerfs +{ + int + test(const struct fuse_args &args, + const mergerfs::config::Config &config) + { + return 0; + } +} diff --git a/src/test.hpp b/src/test.hpp new file mode 100644 index 00000000..094fb673 --- /dev/null +++ b/src/test.hpp @@ -0,0 +1,30 @@ +/* + The MIT License (MIT) + + Copyright (c) 2014 Antonio SJ Musumeci + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +namespace mergerfs +{ + int + test(const struct fuse_args &args, + const mergerfs::config::Config &config); +} diff --git a/src/truncate.cpp b/src/truncate.cpp new file mode 100644 index 00000000..f38cbd49 --- /dev/null +++ b/src/truncate.cpp @@ -0,0 +1,91 @@ +/* + The MIT License (MIT) + + Copyright (c) 2014 Antonio SJ Musumeci + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +#include + +#include +#include +#include + +#include +#include + +#include "ugid.hpp" +#include "fs.hpp" +#include "config.hpp" +#include "assert.hpp" + +using std::string; +using std::vector; +using mergerfs::Policy; + +static +int +_truncate(const Policy::Action::Func searchFunc, + const vector &srcmounts, + const string fusepath, + const off_t size) +{ + int rv; + int error; + vector paths; + + searchFunc(srcmounts,fusepath,paths); + if(paths.empty()) + return -ENOENT; + + rv = -1; + error = 0; + for(vector::const_iterator + i = paths.begin(), ei = paths.end(); i != ei; ++i) + { + rv &= ::truncate(i->full.c_str(),size); + if(rv == -1) + error = errno; + } + + return ((rv == -1) ? -error : 0); +} + +namespace mergerfs +{ + namespace truncate + { + int + truncate(const char *fusepath, + off_t size) + { + const ugid::SetResetGuard uid; + const config::Config &config = config::get(); + + if(fusepath == config.controlfile) + return -EPERM; + + return _truncate(config.policy.action, + config.srcmounts, + fusepath, + size); + } + } +} diff --git a/src/truncate.hpp b/src/truncate.hpp new file mode 100644 index 00000000..a0994f9d --- /dev/null +++ b/src/truncate.hpp @@ -0,0 +1,35 @@ +/* + The MIT License (MIT) + + Copyright (c) 2014 Antonio SJ Musumeci + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +#include + +namespace mergerfs +{ + namespace truncate + { + int + truncate(const char *fusepath, + off_t size); + } +} diff --git a/src/ugid.hpp b/src/ugid.hpp new file mode 100644 index 00000000..a72cfc0d --- /dev/null +++ b/src/ugid.hpp @@ -0,0 +1,94 @@ +/* + The MIT License (MIT) + + Copyright (c) 2014 Antonio SJ Musumeci + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +#ifndef __UGID_HPP__ +#define __UGID_HPP__ + +#include + +#include +#include +#include + +namespace mergerfs +{ + namespace ugid + { + struct SetResetGuard + { + SetResetGuard() + { + const struct fuse_context *fc = fuse_get_context(); + + olduid = ::getuid(); + oldgid = ::getgid(); + oldumask = ::umask(fc->umask); + newuid = fc->uid; + newgid = fc->gid; + newumask = fc->umask; + + if(olduid != newuid) + ::seteuid(newuid); + if(oldgid != newgid) + ::setegid(newgid); + } + + SetResetGuard(uid_t u, + gid_t g, + mode_t m) + { + olduid = ::getuid(); + oldgid = ::getgid(); + oldumask = ::umask(m); + newuid = u; + newgid = g; + newumask = m; + + if(olduid != newuid) + ::seteuid(newuid); + if(oldgid != newgid) + ::setegid(newgid); + } + + ~SetResetGuard() + { + ::umask(oldumask); + if(olduid != newuid) + ::seteuid(olduid); + if(oldgid != newgid) + ::setegid(oldgid); + } + + uid_t olduid; + gid_t oldgid; + mode_t oldumask; + uid_t newuid; + gid_t newgid; + mode_t newumask; + }; + } +} + + +#endif /* __UGID_HPP__ */ diff --git a/src/unlink.cpp b/src/unlink.cpp new file mode 100644 index 00000000..f9d046ed --- /dev/null +++ b/src/unlink.cpp @@ -0,0 +1,85 @@ +/* + The MIT License (MIT) + + Copyright (c) 2014 Antonio SJ Musumeci + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +#include +#include + +#include +#include + +#include "ugid.hpp" +#include "fs.hpp" +#include "config.hpp" +#include "assert.hpp" + +using std::string; +using std::vector; +using mergerfs::Policy; + +static +int +_unlink(const Policy::Action::Func searchFunc, + const vector &srcmounts, + const string fusepath) +{ + int rv; + int error; + vector paths; + + searchFunc(srcmounts,fusepath,paths); + if(paths.empty()) + return -ENOENT; + + rv = -1; + error = 0; + for(vector::const_iterator + i = paths.begin(), ei = paths.end(); i != ei; ++i) + { + rv &= ::unlink(i->full.c_str()); + if(rv == -1) + error = errno; + } + + return ((rv == -1) ? -error : 0); +} + +namespace mergerfs +{ + namespace unlink + { + int + unlink(const char *fusepath) + { + const ugid::SetResetGuard ugid; + const config::Config &config = config::get(); + + if(fusepath == config.controlfile) + return -EPERM; + + return _unlink(config.policy.action, + config.srcmounts, + fusepath); + } + } +} diff --git a/src/unlink.hpp b/src/unlink.hpp new file mode 100644 index 00000000..5f6bf2c7 --- /dev/null +++ b/src/unlink.hpp @@ -0,0 +1,32 @@ +/* + The MIT License (MIT) + + Copyright (c) 2014 Antonio SJ Musumeci + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +namespace mergerfs +{ + namespace unlink + { + int + unlink(const char *fusepath); + } +} diff --git a/src/utimens.cpp b/src/utimens.cpp new file mode 100644 index 00000000..b68fb0cf --- /dev/null +++ b/src/utimens.cpp @@ -0,0 +1,88 @@ +/* + The MIT License (MIT) + + Copyright (c) 2014 Antonio SJ Musumeci + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +#include +#include + +#include +#include + +#include "ugid.hpp" +#include "fs.hpp" +#include "config.hpp" +#include "assert.hpp" + +using std::string; +using std::vector; +using mergerfs::Policy; + +static +int +_utimens(const Policy::Action::Func searchFunc, + const vector &srcmounts, + const string fusepath, + const struct timespec ts[2]) +{ + int rv; + int error; + vector paths; + + searchFunc(srcmounts,fusepath,paths); + if(paths.empty()) + return -ENOENT; + + rv = -1; + error = 0; + for(vector::const_iterator + i = paths.begin(), ei = paths.end(); i != ei; ++i) + { + rv &= ::utimensat(0,i->full.c_str(),ts,AT_SYMLINK_NOFOLLOW); + if(rv == -1) + error = errno; + } + + return ((rv == -1) ? -error : 0); +} + +namespace mergerfs +{ + namespace utimens + { + int + utimens(const char *fusepath, + const struct timespec ts[2]) + { + ugid::SetResetGuard ugid; + const config::Config &config = config::get(); + + if(fusepath == config.controlfile) + return -EPERM; + + return _utimens(config.policy.action, + config.srcmounts, + fusepath, + ts); + } + } +} diff --git a/src/utimens.hpp b/src/utimens.hpp new file mode 100644 index 00000000..ff45ffd9 --- /dev/null +++ b/src/utimens.hpp @@ -0,0 +1,33 @@ +/* + The MIT License (MIT) + + Copyright (c) 2014 Antonio SJ Musumeci + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +namespace mergerfs +{ + namespace utimens + { + int + utimens(const char *fusepath, + const struct timespec ts[2]); + } +} diff --git a/src/write.cpp b/src/write.cpp new file mode 100644 index 00000000..d0b27cb3 --- /dev/null +++ b/src/write.cpp @@ -0,0 +1,163 @@ +/* + The MIT License (MIT) + + Copyright (c) 2014 Antonio SJ Musumeci + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +#include + +#include +#include + +#include +#include +#include + +#include "config.hpp" +#include "policy.hpp" +#include "ugid.hpp" +#include "fileinfo.hpp" + +using mergerfs::config::Config; +using mergerfs::Policy; +using std::string; +using std::vector; +using std::stringstream; + +static +int +_process_kv(Config &config, + const string key, + const string value) +{ + int rv = 0; + + if(key == "search") + { + rv = (Policy::Search::fromString(value) != -1) ? 0 : -EINVAL; + if(rv == 0) + config.policy.search = value; + } + else if(key == "action") + { + rv = (Policy::Action::fromString(value) != -1) ? 0 : -EINVAL; + if(rv == 0) + config.policy.action = value; + } + else if(key == "create") + { + rv = (Policy::Create::fromString(value) != -1) ? 0 : -EINVAL; + if(rv == 0) + config.policy.create = value; + } + else if(key == "statfs") + { + rv = (Policy::StatFS::fromString(value) != -1) ? 0 : -EINVAL; + if(rv == 0) + config.policy.statfs = value; + } + else + { + rv = -EINVAL; + } + + if(rv == 0) + config.updateReadStr(); + + return rv; +} + +static +int +_write_controlfile(Config &config, + string &existing, + const string buf, + const off_t _offset) +{ + size_t bufsize = buf.size(); + size_t existingsize = existing.size(); + + if((existingsize + buf.size()) > 1024) + return (existing.clear(),-EINVAL); + + existing += buf; + + size_t nlpos = existing.find_first_of('\n',existingsize); + if(nlpos == string::npos) + return 0; + + string line = existing.substr(0,nlpos); + existing = existing.substr(nlpos+1); + + size_t equalsoffset = line.find_first_of('='); + if(equalsoffset == string::npos) + return -EINVAL; + + int rv; + string key = line.substr(0,equalsoffset); + string value = line.substr(equalsoffset+1); + + rv = _process_kv(config,key,value); + + return ((rv < 0) ? rv : bufsize); +} + +static +int +_write(const int fd, + const void *buf, + const size_t count, + const off_t offset) +{ + int rv; + + rv = ::pwrite(fd,buf,count,offset); + + return ((rv == -1) ? -errno : rv); +} + +namespace mergerfs +{ + namespace write + { + int + write(const char *fusepath, + const char *buf, + size_t count, + off_t offset, + struct fuse_file_info *fi) + { + const ugid::SetResetGuard ugid; + const config::Config &config = config::get(); + + if(fusepath == config.controlfile) + return _write_controlfile(config::get_writable(), + *(string*)fi->fh, + string(buf,count), + offset); + + return _write(((FileInfo*)fi->fh)->fd, + buf, + count, + offset); + } + } +} diff --git a/src/write.hpp b/src/write.hpp new file mode 100644 index 00000000..05abb0fb --- /dev/null +++ b/src/write.hpp @@ -0,0 +1,36 @@ +/* + The MIT License (MIT) + + Copyright (c) 2014 Antonio SJ Musumeci + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +namespace mergerfs +{ + namespace write + { + int + write(const char *fusepath, + const char *buf, + size_t count, + off_t offset, + struct fuse_file_info *fi); + } +}