Browse Source

initial code commit

pull/36/head 1.0.0
Antonio SJ Musumeci 11 years ago
parent
commit
bb7a966296
  1. 12
      LICENSE
  2. 60
      Makefile
  3. 99
      README.md
  4. 10
      TODO
  5. 85
      src/access.cpp
  6. 33
      src/access.hpp
  7. 36
      src/assert.hpp
  8. 88
      src/chmod.cpp
  9. 33
      src/chmod.hpp
  10. 92
      src/chown.cpp
  11. 34
      src/chown.hpp
  12. 91
      src/config.cpp
  13. 64
      src/config.hpp
  14. 115
      src/create.cpp
  15. 37
      src/create.hpp
  16. 93
      src/fallocate.cpp
  17. 36
      src/fallocate.hpp
  18. 53
      src/fileinfo.hpp
  19. 704
      src/fs.cpp
  20. 159
      src/fs.hpp
  21. 75
      src/fsync.cpp
  22. 36
      src/fsync.hpp
  23. 67
      src/ftruncate.cpp
  24. 39
      src/ftruncate.hpp
  25. 83
      src/getattr.cpp
  26. 37
      src/getattr.hpp
  27. 90
      src/getxattr.cpp
  28. 35
      src/getxattr.hpp
  29. 89
      src/ioctl.cpp
  30. 37
      src/ioctl.hpp
  31. 91
      src/link.cpp
  32. 33
      src/link.hpp
  33. 87
      src/listxattr.cpp
  34. 34
      src/listxattr.hpp
  35. 163
      src/mergerfs.cpp
  36. 28
      src/mergerfs.hpp
  37. 98
      src/mkdir.cpp
  38. 33
      src/mkdir.hpp
  39. 102
      src/mknod.cpp
  40. 34
      src/mknod.hpp
  41. 99
      src/open.cpp
  42. 35
      src/open.hpp
  43. 145
      src/option_parser.cpp
  44. 37
      src/option_parser.hpp
  45. 306
      src/policy.cpp
  46. 207
      src/policy.hpp
  47. 94
      src/read.cpp
  48. 36
      src/read.hpp
  49. 147
      src/readdir.cpp
  50. 67
      src/readdir.hpp
  51. 88
      src/readlink.cpp
  52. 34
      src/readlink.hpp
  53. 83
      src/release.cpp
  54. 33
      src/release.hpp
  55. 93
      src/removexattr.cpp
  56. 33
      src/removexattr.hpp
  57. 82
      src/rename.cpp
  58. 33
      src/rename.hpp
  59. 78
      src/resources.cpp
  60. 34
      src/resources.hpp
  61. 84
      src/rmdir.cpp
  62. 32
      src/rmdir.hpp
  63. 101
      src/setxattr.cpp
  64. 36
      src/setxattr.hpp
  65. 160
      src/statfs.cpp
  66. 33
      src/statfs.hpp
  67. 79
      src/symlink.cpp
  68. 33
      src/symlink.hpp
  69. 53
      src/test.cpp
  70. 30
      src/test.hpp
  71. 91
      src/truncate.cpp
  72. 35
      src/truncate.hpp
  73. 94
      src/ugid.hpp
  74. 85
      src/unlink.cpp
  75. 32
      src/unlink.hpp
  76. 88
      src/utimens.cpp
  77. 33
      src/utimens.hpp
  78. 163
      src/write.cpp
  79. 36
      src/write.hpp

12
LICENSE

@ -1,6 +1,7 @@
/*
The MIT License (MIT) The MIT License (MIT)
Copyright (c) 2014 trapexit
Copyright (c) 2014 Antonio SJ Musumeci <trapexit@spawn.link>
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal of this software and associated documentation files (the "Software"), to deal
@ -9,13 +10,14 @@ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions: 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 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 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.
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

60
Makefile

@ -0,0 +1,60 @@
# The MIT License (MIT)
#
# Copyright (c) 2014 Antonio SJ Musumeci <trapexit@spawn.link>
#
# 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)

99
README.md

@ -1,4 +1,101 @@
mergerfs 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 <mountpoint> <dir0>:<dir1>:<dir2>
```
| 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
================
```
<mountpoint>/.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.

10
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

85
src/access.cpp

@ -0,0 +1,85 @@
/*
The MIT License (MIT)
Copyright (c) 2014 Antonio SJ Musumeci <trapexit@spawn.link>
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 <fuse.h>
#include <string>
#include <vector>
#include <unistd.h>
#include <errno.h>
#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<string> &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);
}
}
}

33
src/access.hpp

@ -0,0 +1,33 @@
/*
The MIT License (MIT)
Copyright (c) 2014 Antonio SJ Musumeci <trapexit@spawn.link>
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);
}
}

36
src/assert.hpp

@ -0,0 +1,36 @@
/*
The MIT License (MIT)
Copyright (c) 2014 Antonio SJ Musumeci <trapexit@spawn.link>
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<bool>
struct StaticAssert;
template<>
struct StaticAssert<true>
{};
}

88
src/chmod.cpp

@ -0,0 +1,88 @@
/*
The MIT License (MIT)
Copyright (c) 2014 Antonio SJ Musumeci <trapexit@spawn.link>
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 <fuse.h>
#include <errno.h>
#include <string>
#include <vector>
#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<string> &srcmounts,
const string fusepath,
const mode_t mode)
{
int rv;
int error;
vector<fs::Path> paths;
searchFunc(srcmounts,fusepath,paths);
if(paths.empty())
return -ENOENT;
rv = -1;
error = 0;
for(vector<fs::Path>::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);
}
}
}

33
src/chmod.hpp

@ -0,0 +1,33 @@
/*
The MIT License (MIT)
Copyright (c) 2014 Antonio SJ Musumeci <trapexit@spawn.link>
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);
}
}

92
src/chown.cpp

@ -0,0 +1,92 @@
/*
The MIT License (MIT)
Copyright (c) 2014 Antonio SJ Musumeci <trapexit@spawn.link>
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 <fuse.h>
#include <errno.h>
#include <string>
#include <vector>
#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<string> &srcmounts,
const string fusepath,
const uid_t uid,
const gid_t gid)
{
int rv;
int error;
vector<fs::Path> paths;
searchFunc(srcmounts,fusepath,paths);
if(paths.empty())
return -ENOENT;
rv = -1;
error = 0;
for(vector<fs::Path>::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);
}
}
}

34
src/chown.hpp

@ -0,0 +1,34 @@
/*
The MIT License (MIT)
Copyright (c) 2014 Antonio SJ Musumeci <trapexit@spawn.link>
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);
}
}

91
src/config.cpp

@ -0,0 +1,91 @@
/*
The MIT License (MIT)
Copyright (c) 2014 Antonio SJ Musumeci <trapexit@spawn.link>
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 <fuse.h>
#include <sstream>
#include <unistd.h>
#include <sys/stat.h>
#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));
}
}
}

64
src/config.hpp

@ -0,0 +1,64 @@
/*
The MIT License (MIT)
Copyright (c) 2014 Antonio SJ Musumeci <trapexit@spawn.link>
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 <sys/stat.h>
#include <string>
#include <vector>
#include "policy.hpp"
namespace mergerfs
{
namespace config
{
class Config
{
public:
Config();
std::string generateReadStr() const;
void updateReadStr();
public:
std::string destmount;
std::vector<std::string> srcmounts;
Policy policy;
const std::string controlfile;
struct stat controlfilestat;
std::string readstr;
bool testmode;
};
const Config &get(void);
Config &get_writable(void);
}
}
#endif

115
src/create.cpp

@ -0,0 +1,115 @@
/*
The MIT License (MIT)
Copyright (c) 2014 Antonio SJ Musumeci <trapexit@spawn.link>
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 <fuse.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <string>
#include <vector>
#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<string> &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);
}
}
}

37
src/create.hpp

@ -0,0 +1,37 @@
/*
The MIT License (MIT)
Copyright (c) 2014 Antonio SJ Musumeci <trapexit@spawn.link>
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 <sys/types.h>
#include <fuse.h>
namespace mergerfs
{
namespace create
{
int
create(const char *fusepath,
mode_t mode,
struct fuse_file_info *fileinfo);
}
}

93
src/fallocate.cpp

@ -0,0 +1,93 @@
/*
The MIT License (MIT)
Copyright (c) 2014 Antonio SJ Musumeci <trapexit@spawn.link>
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 <fuse.h>
#include <errno.h>
#include <fcntl.h>
#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);
}
}
}

36
src/fallocate.hpp

@ -0,0 +1,36 @@
/*
The MIT License (MIT)
Copyright (c) 2014 Antonio SJ Musumeci <trapexit@spawn.link>
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);
}
}

53
src/fileinfo.hpp

@ -0,0 +1,53 @@
/*
The MIT License (MIT)
Copyright (c) 2014 Antonio SJ Musumeci <trapexit@spawn.link>
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 <string>
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__ */

704
src/fs.cpp

@ -0,0 +1,704 @@
/*
The MIT License (MIT)
Copyright (c) 2014 Antonio SJ Musumeci <trapexit@spawn.link>
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 <string>
#include <vector>
#include <map>
#include <sstream>
#include <cstdlib>
#include <iterator>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <linux/fs.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/statvfs.h>
#include <sys/types.h>
#include <attr/xattr.h>
#include <unistd.h>
#include "fs.hpp"
using std::string;
using std::vector;
using std::map;
using std::istringstream;
template<typename Container>
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 <typename Iter>
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<string>::const_iterator begin,
vector<string>::const_iterator end,
const string fusepath)
{
for(vector<string>::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<string> &srcmounts,
const string fusepath)
{
return path_exists(srcmounts.begin(),
srcmounts.end(),
fusepath);
}
Path
find_first(vector<string>::const_iterator beginiter,
vector<string>::const_iterator enditer,
const string suffix)
{
for(vector<string>::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<string> &basepaths,
const string suffix)
{
return find_first(basepaths.begin(),
basepaths.end(),
suffix);
}
void
find_first(const vector<string> &basepaths,
const string suffix,
vector<Path> &paths)
{
paths.push_back(find_first(basepaths,suffix));
}
Path
find_first_with_permission(vector<string>::const_iterator beginiter,
vector<string>::const_iterator enditer,
const string suffix)
{
for(vector<string>::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<string> &basepaths,
const string suffix)
{
return find_first_with_permission(basepaths.begin(),
basepaths.end(),
suffix);
}
void
find_first_with_permission(const vector<string> &basepaths,
const string suffix,
vector<Path> &paths)
{
paths.push_back(find_first_with_permission(basepaths,suffix));
}
Path
find_newest(vector<string>::const_iterator beginiter,
vector<string>::const_iterator enditer,
const string suffix)
{
time_t newest;
string npath;
vector<string>::const_iterator niter;
newest = 0;
for(vector<string>::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<string> &basepaths,
const string suffix)
{
return find_newest(basepaths.begin(),
basepaths.end(),
suffix);
}
void
find_newest(const vector<string> &basepaths,
const string suffix,
vector<Path> &paths)
{
paths.push_back(find_newest(basepaths,suffix));
}
void
find_all(const vector<string> &basepaths,
const string suffix,
vector<Path> &paths)
{
for(vector<string>::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<string>::const_iterator iter,
vector<string>::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<string> &paths,
const string fusepath)
{
return find_mfs(paths.begin(),
paths.end(),
fusepath);
}
Path
find_mfs_existing(vector<string>::const_iterator iter,
vector<string>::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<string> &paths,
const string fusepath)
{
return find_mfs_existing(paths.begin(),
paths.end(),
fusepath);
}
Path
find_random(vector<string>::const_iterator iter,
vector<string>::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<string> &paths,
const string fusepath)
{
return find_random(paths.begin(),
paths.end(),
fusepath);
}
int
listxattr(const string path,
vector<char> &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<string> &attrvector)
{
int rv;
vector<char> 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<char> 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<char> &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<char> tmpvalue;
rv = getxattr(path,attr,tmpvalue);
if(rv != -1)
value = string(tmpvalue.begin(),tmpvalue.end());
return rv;
}
int
getxattrs(const string path,
map<string,string> &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<string,string> &attrs)
{
int fd;
fd = ::open(path.c_str(),O_RDONLY|O_NONBLOCK);
if(fd == -1)
return -1;
for(map<string,string>::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<string,string> 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);
}
};

159
src/fs.hpp

@ -0,0 +1,159 @@
/*
The MIT License (MIT)
Copyright (c) 2014 Antonio SJ Musumeci <trapexit@spawn.link>
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 <string>
#include <vector>
#include <map>
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<string>&,const string);
typedef void (*VecSearchFunc)(const vector<string>&,const string,vector<Path>&);
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<string>::const_iterator begin,
vector<string>::const_iterator end,
const string fusepath);
bool path_exists(const vector<string> &srcmounts,
const string fusepath);
Path find_first(vector<string>::const_iterator begin,
vector<string>::const_iterator end,
const string fusepath);
Path find_first(const vector<string> &paths,
const string fusepath);
void find_first(const vector<string> &paths,
const string fusepath,
vector<Path> &rv);
Path find_first_with_permission(vector<string>::const_iterator begin,
vector<string>::const_iterator end,
const string fusepath);
Path find_first_with_permission(const vector<string> &paths,
const string fusepath);
void find_first_with_permission(const vector<string> &paths,
const string fusepath,
vector<Path> &rv);
Path find_newest(vector<string>::const_iterator begin,
vector<string>::const_iterator end,
const string fusepath);
Path find_newest(const vector<string> &paths,
const string fusepath);
void find_newest(const vector<string> &paths,
const string fusepath,
vector<Path> &rv);
void find_all(const vector<string> &paths,
const string fusepath,
vector<Path> &rv);
Path find_mfs(vector<string>::const_iterator iter,
vector<string>::const_iterator enditer,
const string fusepath);
Path find_mfs(const vector<string> &paths,
const string fusepath);
Path find_mfs_existing(vector<string>::const_iterator iter,
vector<string>::const_iterator enditer,
const string fusepath);
Path find_mfs_existing(const vector<string> &paths,
const string fusepath);
Path find_random(vector<string>::const_iterator iter,
vector<string>::const_iterator enditer,
const string fusepath);
Path find_random(const vector<string> &paths,
const string fusepath);
int clonepath(const string srcfrom,
const string srcto,
const string relative);
int listxattr(const string path,
vector<char> &attrs);
int listxattr(const string path,
string &attrs);
int listxattr(const string path,
vector<string> &attrs);
int getxattr(const string path,
const string attr,
vector<char> &value);
int getxattr(const string path,
const string attr,
string &value);
int getxattrs(const string path,
map<string,string> &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<string,string> &attrs);
int copyxattrs(const string from,
const string to);
int copyattr(const string from,
const string to);
};
#endif // __FS_HPP__

75
src/fsync.cpp

@ -0,0 +1,75 @@
/*
The MIT License (MIT)
Copyright (c) 2014 Antonio SJ Musumeci <trapexit@spawn.link>
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 <fuse.h>
#include <string>
#include <vector>
#include <unistd.h>
#include <errno.h>
#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);
}
}
}

36
src/fsync.hpp

@ -0,0 +1,36 @@
/*
The MIT License (MIT)
Copyright (c) 2014 Antonio SJ Musumeci <trapexit@spawn.link>
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 <fuse.h>
namespace mergerfs
{
namespace fsync
{
int
fsync(const char *fusepath,
int isdatasync,
struct fuse_file_info *fi);
}
}

67
src/ftruncate.cpp

@ -0,0 +1,67 @@
/*
The MIT License (MIT)
Copyright (c) 2014 Antonio SJ Musumeci <trapexit@spawn.link>
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 <fuse.h>
#include <unistd.h>
#include <sys/types.h>
#include <errno.h>
#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);
}
}
}

39
src/ftruncate.hpp

@ -0,0 +1,39 @@
/*
The MIT License (MIT)
Copyright (c) 2014 Antonio SJ Musumeci <trapexit@spawn.link>
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 <fuse.h>
#include <unistd.h>
#include <sys/types.h>
namespace mergerfs
{
namespace ftruncate
{
int
ftruncate(const char *fusepath,
off_t size,
struct fuse_file_info *fi);
}
}

83
src/getattr.cpp

@ -0,0 +1,83 @@
/*
The MIT License (MIT)
Copyright (c) 2014 Antonio SJ Musumeci <trapexit@spawn.link>
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 <fuse.h>
#include <string>
#include <vector>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#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<string> &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);
}
}
}

37
src/getattr.hpp

@ -0,0 +1,37 @@
/*
The MIT License (MIT)
Copyright (c) 2014 Antonio SJ Musumeci <trapexit@spawn.link>
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 <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
namespace mergerfs
{
namespace getattr
{
int
getattr(const char *fusepath,
struct stat *buf);
}
}

90
src/getxattr.cpp

@ -0,0 +1,90 @@
/*
The MIT License (MIT)
Copyright (c) 2014 Antonio SJ Musumeci <trapexit@spawn.link>
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 <string>
#include <vector>
#include <errno.h>
#include <sys/types.h>
#include <attr/xattr.h>
#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<string> &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);
}
}
}

35
src/getxattr.hpp

@ -0,0 +1,35 @@
/*
The MIT License (MIT)
Copyright (c) 2014 Antonio SJ Musumeci <trapexit@spawn.link>
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);
}
}

89
src/ioctl.cpp

@ -0,0 +1,89 @@
/*
The MIT License (MIT)
Copyright (c) 2014 Antonio SJ Musumeci <trapexit@spawn.link>
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 <fuse.h>
#include <string>
#include <errno.h>
#include <sys/ioctl.h>
#include <linux/fs.h>
#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);
}
}
}

37
src/ioctl.hpp

@ -0,0 +1,37 @@
/*
The MIT License (MIT)
Copyright (c) 2014 Antonio SJ Musumeci <trapexit@spawn.link>
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);
}
}

91
src/link.cpp

@ -0,0 +1,91 @@
/*
The MIT License (MIT)
Copyright (c) 2014 Antonio SJ Musumeci <trapexit@spawn.link>
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 <string>
#include <vector>
#include <errno.h>
#include <unistd.h>
#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<string> &srcmounts,
const string from,
const string to)
{
int rv;
int error;
vector<fs::Path> paths;
searchFunc(srcmounts,from,paths);
if(paths.empty())
return -ENOENT;
rv = -1;
error = 0;
for(vector<fs::Path>::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);
}
}
}

33
src/link.hpp

@ -0,0 +1,33 @@
/*
The MIT License (MIT)
Copyright (c) 2014 Antonio SJ Musumeci <trapexit@spawn.link>
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);
}
}

87
src/listxattr.cpp

@ -0,0 +1,87 @@
/*
The MIT License (MIT)
Copyright (c) 2014 Antonio SJ Musumeci <trapexit@spawn.link>
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 <string>
#include <vector>
#include <sys/types.h>
#include <attr/xattr.h>
#include <errno.h>
#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<string> &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);
}
}
}

34
src/listxattr.hpp

@ -0,0 +1,34 @@
/*
The MIT License (MIT)
Copyright (c) 2014 Antonio SJ Musumeci <trapexit@spawn.link>
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);
}
}

163
src/mergerfs.cpp

@ -0,0 +1,163 @@
/*
The MIT License (MIT)
Copyright (c) 2014 Antonio SJ Musumeci <trapexit@spawn.link>
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 <fuse.h>
#include <string.h>
#include <cstdlib>
#include <iostream>
#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;
}

28
src/mergerfs.hpp

@ -0,0 +1,28 @@
/*
The MIT License (MIT)
Copyright (c) 2014 Antonio SJ Musumeci <trapexit@spawn.link>
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

98
src/mkdir.cpp

@ -0,0 +1,98 @@
/*
The MIT License (MIT)
Copyright (c) 2014 Antonio SJ Musumeci <trapexit@spawn.link>
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 <fuse.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <string>
#include <vector>
#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<string> &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);
}
}
}

33
src/mkdir.hpp

@ -0,0 +1,33 @@
/*
The MIT License (MIT)
Copyright (c) 2014 Antonio SJ Musumeci <trapexit@spawn.link>
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);
}
}

102
src/mknod.cpp

@ -0,0 +1,102 @@
/*
The MIT License (MIT)
Copyright (c) 2014 Antonio SJ Musumeci <trapexit@spawn.link>
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 <fuse.h>
#include <string>
#include <vector>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#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<string> &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);
}
}
}

34
src/mknod.hpp

@ -0,0 +1,34 @@
/*
The MIT License (MIT)
Copyright (c) 2014 Antonio SJ Musumeci <trapexit@spawn.link>
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);
}
}

99
src/open.cpp

@ -0,0 +1,99 @@
/*
The MIT License (MIT)
Copyright (c) 2014 Antonio SJ Musumeci <trapexit@spawn.link>
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 <fuse.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <string>
#include <vector>
#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<string> &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);
}
}
}

35
src/open.hpp

@ -0,0 +1,35 @@
/*
The MIT License (MIT)
Copyright (c) 2014 Antonio SJ Musumeci <trapexit@spawn.link>
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 <fuse.h>
namespace mergerfs
{
namespace open
{
int
open(const char *fusepath,
struct fuse_file_info *fileinfo);
}
}

145
src/option_parser.cpp

@ -0,0 +1,145 @@
/*
The MIT License (MIT)
Copyright (c) 2014 Antonio SJ Musumeci <trapexit@spawn.link>
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 <fuse.h>
#include <stddef.h>
#include <string.h>
#include <iostream>
#include <string>
#include <sstream>
#include "config.hpp"
#include "policy.hpp"
using namespace mergerfs;
template<typename Container>
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<std::string> 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();
}
}
}

37
src/option_parser.hpp

@ -0,0 +1,37 @@
/*
The MIT License (MIT)
Copyright (c) 2014 Antonio SJ Musumeci <trapexit@spawn.link>
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 <fuse.h>
#include "config.hpp"
namespace mergerfs
{
namespace options
{
void
parse(struct fuse_args &args,
mergerfs::config::Config &config);
}
}

306
src/policy.cpp

@ -0,0 +1,306 @@
/*
The MIT License (MIT)
Copyright (c) 2014 Antonio SJ Musumeci <trapexit@spawn.link>
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";
}
}
}

207
src/policy.hpp

@ -0,0 +1,207 @@
/*
The MIT License (MIT)
Copyright (c) 2014 Antonio SJ Musumeci <trapexit@spawn.link>
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 <string>
#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

94
src/read.cpp

@ -0,0 +1,94 @@
/*
The MIT License (MIT)
Copyright (c) 2014 Antonio SJ Musumeci <trapexit@spawn.link>
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 <fuse.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <string>
#include <algorithm>
#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);
}
}
}

36
src/read.hpp

@ -0,0 +1,36 @@
/*
The MIT License (MIT)
Copyright (c) 2014 Antonio SJ Musumeci <trapexit@spawn.link>
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);
}
}

147
src/readdir.cpp

@ -0,0 +1,147 @@
/*
The MIT License (MIT)
Copyright (c) 2014 Antonio SJ Musumeci <trapexit@spawn.link>
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 <fuse.h>
#include <string>
#include <set>
#include <vector>
#include <errno.h>
#include <dirent.h>
#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<string> &srcmounts,
const string dirname,
void *buf,
const fuse_fill_dir_t filler)
{
set<string> found;
for(vector<string>::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<set<string>::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<FileData> *stats = (vector<FileData>*)buf;
stats->push_back(FileData(name,*stbuf));
return 0;
}
namespace mergerfs
{
namespace readdir
{
int
readdir(const vector<string> &srcmounts,
const string dirname,
vector<FileData> &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);
}
}
}

67
src/readdir.hpp

@ -0,0 +1,67 @@
/*
The MIT License (MIT)
Copyright (c) 2014 Antonio SJ Musumeci <trapexit@spawn.link>
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 <fuse.h>
#include <string>
#include <vector>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
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<std::string> &srcmounts,
const std::string dirname,
std::vector<FileData> &stats);
}
}
#endif

88
src/readlink.cpp

@ -0,0 +1,88 @@
/*
The MIT License (MIT)
Copyright (c) 2014 Antonio SJ Musumeci <trapexit@spawn.link>
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 <fuse.h>
#include <errno.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <string>
#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<string>& 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);
}
}
}

34
src/readlink.hpp

@ -0,0 +1,34 @@
/*
The MIT License (MIT)
Copyright (c) 2014 Antonio SJ Musumeci <trapexit@spawn.link>
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);
}
}

83
src/release.cpp

@ -0,0 +1,83 @@
/*
The MIT License (MIT)
Copyright (c) 2014 Antonio SJ Musumeci <trapexit@spawn.link>
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 <fuse.h>
#include <errno.h>
#include <string>
#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);
}
}
}

33
src/release.hpp

@ -0,0 +1,33 @@
/*
The MIT License (MIT)
Copyright (c) 2014 Antonio SJ Musumeci <trapexit@spawn.link>
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);
}
}

93
src/removexattr.cpp

@ -0,0 +1,93 @@
/*
The MIT License (MIT)
Copyright (c) 2014 Antonio SJ Musumeci <trapexit@spawn.link>
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 <string>
#include <vector>
#include <errno.h>
#include <sys/types.h>
#include <attr/xattr.h>
#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<string> &srcmounts,
const string fusepath,
const char *attrname)
{
#ifndef WITHOUT_XATTR
int rv;
int error;
vector<fs::Path> paths;
searchFunc(srcmounts,fusepath,paths);
if(paths.empty())
return -ENOENT;
rv = -1;
error = 0;
for(vector<fs::Path>::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);
}
}
}

33
src/removexattr.hpp

@ -0,0 +1,33 @@
/*
The MIT License (MIT)
Copyright (c) 2014 Antonio SJ Musumeci <trapexit@spawn.link>
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);
}
}

82
src/rename.cpp

@ -0,0 +1,82 @@
/*
The MIT License (MIT)
Copyright (c) 2014 Antonio SJ Musumeci <trapexit@spawn.link>
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 <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <string>
#include <vector>
#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<string> &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);
}
}
}

33
src/rename.hpp

@ -0,0 +1,33 @@
/*
The MIT License (MIT)
Copyright (c) 2014 Antonio SJ Musumeci <trapexit@spawn.link>
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);
}
}

78
src/resources.cpp

@ -0,0 +1,78 @@
/*
The MIT License (MIT)
Copyright (c) 2014 Antonio SJ Musumeci <trapexit@spawn.link>
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 <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/resource.h>
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);
}
}
}

34
src/resources.hpp

@ -0,0 +1,34 @@
/*
The MIT License (MIT)
Copyright (c) 2014 Antonio SJ Musumeci <trapexit@spawn.link>
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);
}
}

84
src/rmdir.cpp

@ -0,0 +1,84 @@
/*
The MIT License (MIT)
Copyright (c) 2014 Antonio SJ Musumeci <trapexit@spawn.link>
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 <errno.h>
#include <unistd.h>
#include <string>
#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<string> &srcmounts,
const string fusepath)
{
int rv;
int error;
vector<fs::Path> paths;
searchFunc(srcmounts,fusepath,paths);
if(paths.empty())
return -ENOENT;
rv = -1;
error = 0;
for(vector<fs::Path>::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);
}
}
}

32
src/rmdir.hpp

@ -0,0 +1,32 @@
/*
The MIT License (MIT)
Copyright (c) 2014 Antonio SJ Musumeci <trapexit@spawn.link>
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);
}
}

101
src/setxattr.cpp

@ -0,0 +1,101 @@
/*
The MIT License (MIT)
Copyright (c) 2014 Antonio SJ Musumeci <trapexit@spawn.link>
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 <string>
#include <vector>
#include <errno.h>
#include <sys/types.h>
#include <attr/xattr.h>
#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<string> &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<fs::Path> paths;
searchFunc(srcmounts,fusepath,paths);
if(paths.empty())
return -ENOENT;
rv = -1;
error = 0;
for(vector<fs::Path>::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);
}
}
}

36
src/setxattr.hpp

@ -0,0 +1,36 @@
/*
The MIT License (MIT)
Copyright (c) 2014 Antonio SJ Musumeci <trapexit@spawn.link>
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);
}
}

160
src/statfs.cpp

@ -0,0 +1,160 @@
/*
The MIT License (MIT)
Copyright (c) 2014 Antonio SJ Musumeci <trapexit@spawn.link>
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 <fuse.h>
#include <sys/statvfs.h>
#include <errno.h>
#include <climits>
#include <string>
#include <vector>
#include <map>
#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<string> &srcmounts,
struct statvfs &fsstat)
{
unsigned long min_bsize = ULONG_MAX;
unsigned long min_frsize = ULONG_MAX;
unsigned long min_namemax = ULONG_MAX;
map<unsigned long,struct statvfs> fsstats;
vector<string>::const_iterator iter;
vector<string>::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<unsigned long,struct statvfs>(fsstat.f_fsid,fsstat));
}
map<unsigned long,struct statvfs>::iterator fsstatiter = fsstats.begin();
map<unsigned long,struct statvfs>::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);
}
}
}

33
src/statfs.hpp

@ -0,0 +1,33 @@
/*
The MIT License (MIT)
Copyright (c) 2014 Antonio SJ Musumeci <trapexit@spawn.link>
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);
}
}

79
src/symlink.cpp

@ -0,0 +1,79 @@
/*
The MIT License (MIT)
Copyright (c) 2014 Antonio SJ Musumeci <trapexit@spawn.link>
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 <fuse.h>
#include <errno.h>
#include <sys/types.h>
#include <unistd.h>
#include <string>
#include "fs.hpp"
#include "config.hpp"
#include "ugid.hpp"
using std::string;
using std::vector;
static
int
_symlink(const vector<string> &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);
}
}
}

33
src/symlink.hpp

@ -0,0 +1,33 @@
/*
The MIT License (MIT)
Copyright (c) 2014 Antonio SJ Musumeci <trapexit@spawn.link>
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);
}
}

53
src/test.cpp

@ -0,0 +1,53 @@
/*
The MIT License (MIT)
Copyright (c) 2014 Antonio SJ Musumeci <trapexit@spawn.link>
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 <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <map>
#include <string.h>
#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;
}
}

30
src/test.hpp

@ -0,0 +1,30 @@
/*
The MIT License (MIT)
Copyright (c) 2014 Antonio SJ Musumeci <trapexit@spawn.link>
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);
}

91
src/truncate.cpp

@ -0,0 +1,91 @@
/*
The MIT License (MIT)
Copyright (c) 2014 Antonio SJ Musumeci <trapexit@spawn.link>
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 <fuse.h>
#include <unistd.h>
#include <sys/types.h>
#include <errno.h>
#include <string>
#include <vector>
#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<string> &srcmounts,
const string fusepath,
const off_t size)
{
int rv;
int error;
vector<fs::Path> paths;
searchFunc(srcmounts,fusepath,paths);
if(paths.empty())
return -ENOENT;
rv = -1;
error = 0;
for(vector<fs::Path>::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);
}
}
}

35
src/truncate.hpp

@ -0,0 +1,35 @@
/*
The MIT License (MIT)
Copyright (c) 2014 Antonio SJ Musumeci <trapexit@spawn.link>
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 <sys/types.h>
namespace mergerfs
{
namespace truncate
{
int
truncate(const char *fusepath,
off_t size);
}
}

94
src/ugid.hpp

@ -0,0 +1,94 @@
/*
The MIT License (MIT)
Copyright (c) 2014 Antonio SJ Musumeci <trapexit@spawn.link>
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 <fuse.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
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__ */

85
src/unlink.cpp

@ -0,0 +1,85 @@
/*
The MIT License (MIT)
Copyright (c) 2014 Antonio SJ Musumeci <trapexit@spawn.link>
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 <errno.h>
#include <unistd.h>
#include <string>
#include <vector>
#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<string> &srcmounts,
const string fusepath)
{
int rv;
int error;
vector<fs::Path> paths;
searchFunc(srcmounts,fusepath,paths);
if(paths.empty())
return -ENOENT;
rv = -1;
error = 0;
for(vector<fs::Path>::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);
}
}
}

32
src/unlink.hpp

@ -0,0 +1,32 @@
/*
The MIT License (MIT)
Copyright (c) 2014 Antonio SJ Musumeci <trapexit@spawn.link>
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);
}
}

88
src/utimens.cpp

@ -0,0 +1,88 @@
/*
The MIT License (MIT)
Copyright (c) 2014 Antonio SJ Musumeci <trapexit@spawn.link>
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 <errno.h>
#include <unistd.h>
#include <string>
#include <vector>
#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<string> &srcmounts,
const string fusepath,
const struct timespec ts[2])
{
int rv;
int error;
vector<fs::Path> paths;
searchFunc(srcmounts,fusepath,paths);
if(paths.empty())
return -ENOENT;
rv = -1;
error = 0;
for(vector<fs::Path>::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);
}
}
}

33
src/utimens.hpp

@ -0,0 +1,33 @@
/*
The MIT License (MIT)
Copyright (c) 2014 Antonio SJ Musumeci <trapexit@spawn.link>
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]);
}
}

163
src/write.cpp

@ -0,0 +1,163 @@
/*
The MIT License (MIT)
Copyright (c) 2014 Antonio SJ Musumeci <trapexit@spawn.link>
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 <fuse.h>
#include <errno.h>
#include <unistd.h>
#include <string>
#include <vector>
#include <sstream>
#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);
}
}
}

36
src/write.hpp

@ -0,0 +1,36 @@
/*
The MIT License (MIT)
Copyright (c) 2014 Antonio SJ Musumeci <trapexit@spawn.link>
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);
}
}
Loading…
Cancel
Save