Compare commits

...
34 Commits
Author SHA1 Message Date
Antonio SJ Musumeci 7e9ccd0317 support runtime setting of srcmounts. closes #12 2014-08-08 10:20:27 -04:00
Antonio SJ Musumeci 992e05e897 update docs for xattr features 2014-07-31 17:27:43 -04:00
Antonio SJ Musumeci b82db46e29 getxattr for user.mergerfs.{base,full}path returns the source paths. closes #23 2014-07-30 15:26:49 -04:00
Antonio SJ Musumeci 7b0d703f00 only allow manipulation of runtime settings via xattrs. closes #22 2014-07-29 09:26:11 -04:00
Antonio SJ Musumeci 45cec2d301 use int instead of long for ioctl. fixes #21 2014-07-22 07:07:25 -04:00
Antonio SJ Musumeci 2295714548 on ENOENT try cloning the dest path to source drive. closes #20 2014-07-07 21:54:38 -04:00
Antonio SJ Musumeci ccb0ac1463 generate the controlfile data on the fly. closes #19 2014-06-25 15:17:26 -04:00
Antonio SJ Musumeci 15a0416eed don't flush if it's the .mergerfs pseudo file. closes #18 2014-06-25 14:40:07 -04:00
Antonio SJ Musumeci ec38a9c783 fix rename'ing to local device 2014-06-24 16:13:49 -04:00
Antonio SJ Musumeci 3b6c74878c use geteuid syscall as well 2014-06-23 12:17:09 -04:00
Antonio SJ Musumeci f385f02d0f add -O2 to CFLAGS 2014-06-18 10:40:54 -04:00
Antonio SJ Musumeci 0e12d79659 platform specific code to deal with sete{u,g}id. closes #17 2014-06-17 19:12:09 -04:00
Antonio SJ Musumeci 716453596a add flush callback. closes #16 2014-06-13 11:04:26 -04:00
Antonio SJ Musumeci dd666079b6 add {read,write}_buf functions. closes #15 2014-06-13 11:02:46 -04:00
Antonio SJ Musumeci 0f8fe47016 add man file via markdown -> man conversion with pandoc. closes #14 2014-06-09 10:30:53 -04:00
Antonio SJ Musumeci 45a2e09fbe reverse source and destination mount points to match fstab requirements. closes #13 2014-06-05 07:30:04 -04:00
Antonio SJ Musumeci 645c823778 source mount paths can contain globing. closes #10 2014-05-31 17:54:18 -04:00
Antonio SJ Musumeci 18358261af fsname set to list of src mounts. closes #9 2014-05-30 23:23:45 -04:00
Antonio SJ Musumeci 4a0bc4aef7 add debian package building. closes #11 2014-05-30 22:31:44 -04:00
Antonio SJ Musumeci e2e0359eb2 fix free space calculations. closes #8 2014-05-30 18:41:42 -04:00
Antonio SJ Musumeci 655436f258 further Makefile enhancements 2014-05-30 15:46:45 -04:00
Antonio SJ Musumeci a7d9a9bf61 enhance Makefile 2014-05-28 21:39:07 -04:00
Antonio SJ Musumeci 36887e456f when readdir's filler returns non-zero return ENOMEM. closes #7 2014-05-28 18:04:38 -04:00
Antonio SJ Musumeci 652a299ac1 add fgetattr. closes #5 2014-05-28 06:35:08 -04:00
Antonio SJ Musumeci aab90b0503 rework policy code 2014-05-28 05:35:58 -04:00
Antonio SJ Musumeci 345d0bbdc4 use eaccess to determine permissions for ffwp. closes #2 2014-05-27 18:17:54 -04:00
Antonio SJ Musumeci 4c7095ccf6 remove stat'ing of files in readdir. closes #3 2014-05-27 12:41:40 -04:00
Antonio SJ Musumeci bbc4b594fe fs::make_path should check for forward slashes, add if missing 2014-05-27 07:17:32 -04:00
Antonio SJ Musumeci 16fe0cf90c remove statfs policy 2014-05-23 07:28:05 -04:00
Antonio SJ Musumeci 29ed2bca01 add FS_IOC_{GET}VERSION to ioctl 2014-05-20 13:48:46 -04:00
Antonio SJ Musumeci 243a193e66 use long instead of int to limit possibility of overflow in switch, closes #1 2014-05-20 10:40:13 -04:00
Antonio SJ Musumeci 97ce6f5f21 use {get,list,set}xattr to modify runtime 2014-05-19 23:37:18 -04:00
Antonio SJ Musumeci 428dd8c877 update build instructions in readme 2014-05-19 09:38:50 -04:00
Antonio SJ Musumeci 7f640c4d9e fix building without libattr 2014-05-19 09:34:31 -04:00
69 changed files with 2678 additions and 1617 deletions
+105 -20
View File
@@ -20,41 +20,126 @@
# 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
PKGCONFIG = $(shell which pkg-config)
GIT = $(shell which git)
TAR = $(shell which tar)
MKDIR = $(shell which mkdir)
TOUCH = $(shell which touch)
CP = $(shell which cp)
RM = $(shell which rm)
FIND = $(shell which find)
INSTALL = $(shell which install)
MKTEMP = $(shell which mktemp)
STRIP = $(shell which strip)
PANDOC = $(shell which pandoc)
ifdef WITHOUT_XATTR
CFLAGS += -DWITHOUT_XATTR
ifeq ($(PKGCONFIG),"")
$(error "pkg-config not installed"
endif
LDFLAGS = $(shell pkg-config fuse --libs)
ifeq ($(PANDOC),"")
$(warning "pandoc does not appear available: manpage won't be buildable")
endif
XATTR_AVAILABLE = $(shell test ! -e /usr/include/attr/xattr.h; echo $$?)
FUSE_AVAILABLE = $(shell ! pkg-config --exists fuse; echo $$?)
ifeq ($(FUSE_AVAILABLE),0)
FUSE_AVAILABLE = $(shell test ! -e /usr/include/fuse.h; echo $$?)
endif
ifeq ($(FUSE_AVAILABLE),0)
$(error "FUSE development package doesn't appear available")
endif
OPTS = -O2
SRC = $(wildcard src/*.cpp)
OBJ = $(SRC:src/%.cpp=obj/%.o)
DEPS = $(OBJ:obj/%.o=obj/%.d)
TARGET = mergerfs
MANPAGE = $(TARGET).1
CFLAGS = -g -Wall \
$(OPTS) \
$(shell $(PKGCONFIG) fuse --cflags) \
-DFUSE_USE_VERSION=26 \
-MMD
LDFLAGS = $(shell $(PKGCONFIG) fuse --libs)
BINDIR = $(PREFIX)/usr/bin
MANDIR = $(PREFIX)/usr/share/man/man1
INSTALLTARGET = $(DESTDIR)/$(BINDIR)/$(TARGET)
MANTARGET = $(DESTDIR)/$(MANDIR)/$(MANPAGE)
ifeq ($(XATTR_AVAILABLE),0)
$(warning "xattr not available: disabling")
CFLAGS += -DWITHOUT_XATTR
endif
KERNEL = $(shell uname -s)
ifeq ($(KERNEL),Linux)
CFLAGS += -DLINUX
endif
ifeq ($(KERNEL),Darwin)
CFLAGS += -DOSX
endif
all: $(TARGET)
help:
@echo usage: make
@echo make WITHOUT_XATTR=1 - to build program without xattrs functionality
@echo "usage: make"
@echo "make XATTR_AVAILABLE=0 - to build program without xattrs functionality (auto discovered otherwise)"
$(TARGET): obj/obj-stamp $(OBJ)
g++ $(CFLAGS) $(OBJ) -o $@ $(LDFLAGS)
$(CXX) $(CFLAGS) $(OBJ) -o $@ $(LDFLAGS)
changelog:
$(GIT) log --pretty --numstat --summary | git2cl > ChangeLog
obj/obj-stamp:
mkdir -p obj
touch $@
$(MKDIR) -p obj
$(TOUCH) $@
obj/%.o: src/%.cpp
g++ $(CFLAGS) -c $< -o $@
$(CXX) $(CFLAGS) -c $< -o $@
clean:
rm -rf obj "$(TARGET)"
find -name "*~" -delete
$(RM) -rf obj "$(TARGET)" "$(MANPAGE)"
$(FIND) -name "*~" -delete
.PHONY: all clean help
distclean: clean
$(GIT) clean -ifd
install: $(TARGET) $(MANPAGE)
$(INSTALL) -m 0755 -D "$(TARGET)" "$(INSTALLTARGET)"
$(INSTALL) -m 0644 -D "$(MANPAGE)" "$(MANTARGET)"
install-strip: install
$(STRIP) "$(INSTALLTARGET)"
uninstall:
$(RM) "$(INSTALLTARGET)"
$(RM) "$(MANTARGET)"
$(MANPAGE): README.md
$(PANDOC) -s -t man -o $(MANPAGE) README.md
man: $(MANPAGE)
tarball: clean changelog man
$(eval VERSION := $(shell $(GIT) describe --always --tags --dirty))
$(eval FILENAME := $(TARGET)-$(VERSION))
$(eval TMPDIR := $(shell $(MKTEMP) --tmpdir -d .$(FILENAME).XXXXXXXX))
$(MKDIR) $(TMPDIR)/$(FILENAME)
$(CP) -ar . $(TMPDIR)/$(FILENAME)
$(TAR) --exclude=.git -cz -C $(TMPDIR) -f ../$(FILENAME).tar.gz $(FILENAME)
$(RM) -rf $(TMPDIR)
deb:
$(eval VERSION := $(shell $(GIT) describe --always --tags --dirty))
$(GIT) dch --auto --release --new-version="$(VERSION)"
$(GIT) buildpackage --git-ignore-new
.PHONY: all clean install help
include $(wildcard obj/*.d)
+125 -66
View File
@@ -1,101 +1,160 @@
mergerfs
========
% mergerfs(1) mergerfs user manual
% Antonio SJ Musumeci <trapexit@spawn.link>
% June 9, 2014
another FUSE union filesystem
# NAME
mergerfs - another FUSE union filesystem
# SYNOPSIS
mergerfs -ocreate=epmfs,search=ff,action=ff &lt;srcpoints&gt; &lt;mountpoint&gt;
# DESCRIPTION
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
========
# OPTIONS
Filesystem calls are broken up into 5 classes of policies: search, action, create, statfs, and none.
###options###
Below shows each policy class, the FUSE calls they impact, and the policy names.
| Option | Default |
|--------|--------|
| search | ff |
| action | ff |
| create | epmfs |
####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 | |
###srcpoints###
####Descriptions####
| Class/Policy | Description |
The source points argument is a colon (':') delimited list of paths. To make it simplier to include multiple source points without having to modify your [fstab](http://linux.die.net/man/5/fstab) we also support [globbing](http://linux.die.net/man/7/glob).
```
$ mergerfs /mnt/disk*:/mnt/cdrom /media/drives
```
The above line will use all points in /mnt prefixed with *disk* and the directory *cdrom*.
In /etc/fstab it'd look like the following:
```
# <file system> <mount point> <type> <options> <dump> <pass>
/mnt/disk*:/mnt/cdrom /media/drives fuse.mergerfs allow_other 0 0
```
**NOTE:** the globbing is done at mount time. If a new directory is added matching the glob after the fact it will not be included.
# POLICIES
Filesystem calls are broken up into 4 functional categories: search, action, create, and none. These categories can be assigned a policy which dictates how [mergerfs](http://github.com/trapexit/mergerfs) behaves while when action on the filesystem. Any policy can be assigned to a category though some aren't terribly practical. For instance: rand (Random) may be useful for **create** but could lead to very odd behavior if used for **search** or **action**. Since the input for any policy is the source mounts and fusepath and the output a vector of targets the choice was made to simplify the implementation and allow a policies usage in any category. **NOTE:** In any policy which can return more than one location (currently only **all**) the first value will be used in **search** and **create** policies since they can only ever act on 1 filepath.
#### Functional classifications ####
| Class | FUSE calls |
|-------|------------|
| search | access, getattr, getxattr, listxattr, open, readlink |
| action | chmod, link, removexattr, rmdir, setxattr, truncate, unlink, utimens |
| create | create, mkdir, mknod |
| none | fallocate, fgetattr, fsync, ftruncate, ioctl, read, readdir, rename, statfs, symlink, write, release |
#### Policy descriptions ####
| 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. |
| ff (first found) | Given the order the paths were provided at mount time act on the first one found (regardless if stat would return EACCES). |
| ffwp (first found w/ permissions) | 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). |
| newest (newest file) | If multiple files exist return the one with the most recent mtime. |
| all (all files found) | 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. |
| mfs (most free space) | Assuming the path is found to exist (ENOENT would not be returned) use the drive with the most free space available. |
| epmfs (existing path, most free space) | If the path exists in multiple locations use the one with the most free space. Otherwise fall back to mfs. |
| rand (random) | Pick a destination at random. Again the dirname of the full path must exist somewhere. |
#### statvfs ####
It normalizes the source drives based on the fragment size and sums the number of adjusted blocks and inodes. This means you will see the combined space of all sources. Total, used, and free. The sources however are dedupped based on the drive so multiple points on the same drive will not result in double counting it's space.
**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
=====
# BUILDING
```
$ 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.
* Need to install FUSE development libraries (libfuse-dev).
* Optionally need libattr1 (libattr1-dev).
```
[trapexit:~/dev/mergerfs] $ make help
usage: make
make WITHOUT_XATTR=1 - to build program without xattrs functionality
make XATTR_AVAILABLE=0 - to build program without xattrs functionality (auto discovered otherwise)
```
Runtime Settings
================
# Runtime Settings
#### /.mergerfs pseudo file ####
```
<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.
There is a pseudo file available at the mountpoint which allows for the runtime modification of certain mergerfs options. The file will not show up in readdirs but can be stat'ed and manipulated via [{list,get,set}xattrs](http://linux.die.net/man/2/listxattr) calls.
Reading the file will result in a newline delimited list of current settings as followed:
Even if xattrs are disabled the [{list,get,set}xattrs](http://linux.die.net/man/2/listxattr) calls will still work.
The keys are:
* user.mergerfs.srcmounts
* user.mergerfs.action
* user.mergerfs.create
* user.mergerfs.search
```
[trapexit:/tmp/mount] $ cat .mergerfs
action=ff
create=epmfs
search=ff
statfs=sumf
[trapexit:/tmp/mount] $ xattr -l .mergerfs
user.mergerfs.srcmounts: /tmp/a:/tmp/b
user.mergerfs.action: ff
user.mergerfs.create: epmfs
user.mergerfs.search: ff
[trapexit:/tmp/mount] $ xattr -p user.mergerfs.action .mergerfs
ff
[trapexit:/tmp/mount] $ xattr -w user.mergerfs.action ffwp .mergerfs
[trapexit:/tmp/mount] $ xattr -p user.mergerfs.action .mergerfs
ffwp
[trapexit:/tmp/mount] $ xattr -w user.mergerfs.srcmounts +/tmp/c .mergerfs
[trapexit:/tmp/mount] $ xattr -p user.mergerfs.srcmounts .mergerfs
/tmp/a:/tmp/b:/tmp/c
[trapexit:/tmp/mount] $ xattr -w user.mergerfs.srcmounts =/tmp/c .mergerfs
[trapexit:/tmp/mount] $ xattr -p user.mergerfs.srcmounts .mergerfs
/tmp/c
[trapexit:/tmp/mount] $ xattr -w user.mergerfs.srcmounts '+</tmp/a:/tmp/b' .mergerfs
[trapexit:/tmp/mount] $ xattr -p user.mergerfs.srcmounts .mergerfs
/tmp/a:/tmp/b:/tmp/c
```
Writing to the file is buffered and waits till a newline to process. Meaning echo works well.
For **user.mergerfs.srcmounts** there are several instructions available for manipulating the list. The value provided is just as the value used at mount time. A colon (':') delimited list of full path globs.
| Instruction | Description |
|--------------|-------------|
| +[list] | append |
| +<[list] | prepend |
| +>[list] | append |
| -[list] | remove all values provided |
| -< | remove first in list |
| -> | remove last in list |
| =[list] | set |
| [list] | set |
#### mergerfs file xattrs ####
While they won't show up when using [listxattr](http://linux.die.net/man/2/listxattr) mergerfs offers a number of special xattrs to query information about the files served. To access the values you will need to issue a [getxattr](http://linux.die.net/man/2/getxattr) for one of the following:
* user.mergerfs.basepath : gives you the base mount point for the file given the current search policy
* user.mergerfs.fullpath : gives you the full path of the original file given the search policy
```
[trapexit:/tmp/mount] $ echo "search=newest" >> .mergerfs
[trapexit:/tmp/mount] $ cat .mergerfs
action=ff
create=epmfs
search=newest
statfs=sumf
[trapexit:/tmp/mount] $ ls
A B C
[trapexit:/tmp/mount] $ xattr -p user.mergerfs.fullpath A
/mnt/a/full/path/to/A
[trapexit:/tmp/mount] $ xattr -p user.mergerfs.basepath A
/mnt/a
```
*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
View File
@@ -1,10 +0,0 @@
* 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
+27
View File
@@ -0,0 +1,27 @@
mergerfs (1.1.0) trusty; urgency=medium
* initial code commit
* fix building without libattr
* update build instructions in readme
* use {get,list,set}xattr to modify runtime
* use long instead of int to limit possibility of overflow in switch, closes #1
* add FS_IOC_{GET}VERSION to ioctl
* remove statfs policy
* fs::make_path should check for forward slashes, add if missing
* remove stat'ing of files in readdir. closes #3
* use eaccess to determine permissions for ffwp. closes #2
* rework policy code
* add fgetattr. closes #5
* when readdir's filler returns non-zero return ENOMEM. closes #7
* enhance Makefile
* further Makefile enhancements
* fix free space calculations. closes #8
* add debian package building
-- Antonio SJ Musumeci <trapexit@spawn.link> Fri, 30 May 2014 21:32:19 -0400
mergerfs (1.0.0) trusty; urgency=medium
* Initial commit
-- Antonio SJ Musumeci <trapexit@spawn.link> Fri, 30 May 2014 21:28:09 -0400
+1
View File
@@ -0,0 +1 @@
9
+20
View File
@@ -0,0 +1,20 @@
Source: mergerfs
Section: utils
Priority: optional
Maintainer: Antonio SJ Musumeci <trapexit@spawn.link>
Build-Depends: debhelper (>= 8.0.0),
libfuse-dev,
libattr1-dev,
pkg-config
Standards-Version: 3.9.4
Homepage: http://github.com/trapexit/mergerfs
Package: mergerfs
Architecture: any
Depends: ${shlibs:Depends},
${misc:Depends},
libfuse2
Description: 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.
+31
View File
@@ -0,0 +1,31 @@
Format: http://www.debian.org/doc/packaging-manuals/copyright-format/1.0/
Upstream-Name: mergerfs-1.1.0
Source: http://github.com/trapexit/mergerfs
Files: *
Copyright: 2014 Antonio SJ Musumeci <trapexit@spawn.link>
License: MIT
Files: debian/*
Copyright: 2014 Antonio SJ Musumeci <trapexit@spawn.link>
License: MIT
License: MIT
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.
n
Vendored
+1
View File
@@ -0,0 +1 @@
README.md
+1
View File
@@ -0,0 +1 @@
mergerfs_1.1.0_amd64.deb utils optional
Vendored Executable
+8
View File
@@ -0,0 +1,8 @@
#!/usr/bin/make -f
# -*- makefile -*-
# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1
%:
dh $@
+12 -13
View File
@@ -37,7 +37,7 @@
#include "ugid.hpp"
#include "fs.hpp"
#include "config.hpp"
#include "assert.hpp"
#include "rwlock.hpp"
using std::string;
using std::vector;
@@ -45,19 +45,19 @@ using mergerfs::Policy;
static
int
_access(const Policy::Search::Func searchFunc,
const vector<string> &srcmounts,
const string fusepath,
const int mask)
_access(const fs::SearchFunc searchFunc,
const vector<string> &srcmounts,
const string fusepath,
const int mask)
{
int rv;
string path;
fs::PathVector path;
path = searchFunc(srcmounts,fusepath).full;
searchFunc(srcmounts,fusepath,path);
if(path.empty())
return -ENOENT;
rv = ::eaccess(path.c_str(),mask);
rv = ::eaccess(path[0].full.c_str(),mask);
return ((rv == -1) ? -errno : 0);
}
@@ -70,13 +70,12 @@ namespace mergerfs
access(const char *fusepath,
int mask)
{
const ugid::SetResetGuard ugid;
const struct fuse_context *fc = fuse_get_context();
const config::Config &config = config::get();
const ugid::SetResetGuard ugid(fc->uid,fc->gid);
const rwlock::ReadGuard readlock(&config.srcmountslock);
if(fusepath == config.controlfile)
return 0;
return _access(config.policy.search,
return _access(*config.search,
config.srcmounts,
fusepath,
mask);
+51
View File
@@ -0,0 +1,51 @@
/*
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 <algorithm>
template<typename K,typename V>
class buildmap
{
public:
buildmap(const K &key,
const V &val)
{
_map.insert(std::make_pair(key,val));
}
buildmap<K,V> &operator()(const K &key,
const V &val)
{
_map.insert(std::make_pair(key,val));
return *this;
}
operator std::map<K,V>()
{
return _map;
}
private:
std::map<K,V> _map;
};
+51
View File
@@ -0,0 +1,51 @@
/*
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 <algorithm>
template<typename V, bool SORT = false>
class buildvector
{
public:
buildvector(const V &val)
{
_vector.push_back(val);
}
buildvector<V,SORT> &operator()(const V &val)
{
_vector.push_back(val);
return *this;
}
operator std::vector<V>()
{
if(SORT == true)
std::sort(_vector.begin(),_vector.end());
return _vector;
}
private:
std::vector<V> _vector;
};
+70
View File
@@ -0,0 +1,70 @@
/*
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 "category.hpp"
#include "buildvector.hpp"
#define CATEGORY(X) Category(Category::Enum::X,#X)
namespace mergerfs
{
const std::vector<Category> Category::_categories_ =
buildvector<Category,true>
(CATEGORY(invalid))
(CATEGORY(action))
(CATEGORY(create))
(CATEGORY(search));
const Category * const Category::categories = &_categories_[1];
const Category &Category::invalid = Category::categories[Category::Enum::invalid];
const Category &Category::action = Category::categories[Category::Enum::action];
const Category &Category::create = Category::categories[Category::Enum::create];
const Category &Category::search = Category::categories[Category::Enum::search];
const Category&
Category::find(const std::string str)
{
for(int i = Enum::BEGIN; i != Enum::END; ++i)
{
if(categories[i] == str)
return categories[i];
}
return invalid;
}
const Category&
Category::find(const Category::Enum::Type i)
{
if(i >= Category::Enum::BEGIN &&
i < Category::Enum::END)
return categories[i];
return invalid;
}
}
+98
View File
@@ -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.
*/
#ifndef __CATEGORY_HPP__
#define __CATEGORY_HPP__
#include <string>
#include <vector>
namespace mergerfs
{
class Category
{
public:
struct Enum
{
enum Type
{
invalid = -1,
BEGIN = 0,
action = BEGIN,
create,
search,
END
};
};
private:
Enum::Type _enum;
std::string _str;
public:
Category()
: _enum(invalid),
_str(invalid)
{
}
Category(const Enum::Type enum_,
const std::string str_)
: _enum(enum_),
_str(str_)
{
}
public:
operator const Enum::Type() const { return _enum; }
operator const std::string() const { return _str; }
operator const Category*() const { return this; }
bool operator==(const std::string str_) const
{ return _str == str_; }
bool operator==(const Enum::Type enum_) const
{ return _enum == enum_; }
bool operator!=(const Category &r) const
{ return _enum != r._enum; }
bool operator<(const Category &r) const
{ return _enum < r._enum; }
public:
static const Category &find(const std::string);
static const Category &find(const Enum::Type);
public:
static const std::vector<Category> _categories_;
static const Category * const categories;
static const Category &invalid;
static const Category &action;
static const Category &create;
static const Category &search;
};
}
#endif
+12 -13
View File
@@ -31,7 +31,7 @@
#include "ugid.hpp"
#include "fs.hpp"
#include "config.hpp"
#include "assert.hpp"
#include "rwlock.hpp"
using std::string;
using std::vector;
@@ -39,14 +39,14 @@ using mergerfs::Policy;
static
int
_chmod(const Policy::Action::Func searchFunc,
const vector<string> &srcmounts,
const string fusepath,
const mode_t mode)
_chmod(const fs::SearchFunc searchFunc,
const vector<string> &srcmounts,
const string fusepath,
const mode_t mode)
{
int rv;
int error;
vector<fs::Path> paths;
fs::PathVector paths;
searchFunc(srcmounts,fusepath,paths);
if(paths.empty())
@@ -54,7 +54,7 @@ _chmod(const Policy::Action::Func searchFunc,
rv = -1;
error = 0;
for(vector<fs::Path>::const_iterator
for(fs::PathVector::const_iterator
i = paths.begin(), ei = paths.end(); i != ei; ++i)
{
rv &= ::chmod(i->full.c_str(),mode);
@@ -73,13 +73,12 @@ namespace mergerfs
chmod(const char *fusepath,
mode_t mode)
{
ugid::SetResetGuard ugid;
const config::Config &config = config::get();
const struct fuse_context *fc = fuse_get_context();
const ugid::SetResetGuard ugid(fc->uid,fc->gid);
const config::Config &config = config::get();
const rwlock::ReadGuard readlock(&config.srcmountslock);
if(fusepath == config.controlfile)
return -EPERM;
return _chmod(config.policy.action,
return _chmod(*config.action,
config.srcmounts,
fusepath,
mode);
+12 -13
View File
@@ -32,7 +32,7 @@
#include "ugid.hpp"
#include "fs.hpp"
#include "config.hpp"
#include "assert.hpp"
#include "rwlock.hpp"
using std::string;
using std::vector;
@@ -40,15 +40,15 @@ 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)
_chown(const fs::SearchFunc searchFunc,
const vector<string> &srcmounts,
const string fusepath,
const uid_t uid,
const gid_t gid)
{
int rv;
int error;
vector<fs::Path> paths;
fs::PathVector paths;
searchFunc(srcmounts,fusepath,paths);
if(paths.empty())
@@ -56,7 +56,7 @@ _chown(const Policy::Action::Func searchFunc,
rv = -1;
error = 0;
for(vector<fs::Path>::const_iterator
for(fs::PathVector::const_iterator
i = paths.begin(), ei = paths.end(); i != ei; ++i)
{
rv &= ::lchown(i->full.c_str(),uid,gid);
@@ -76,13 +76,12 @@ namespace mergerfs
uid_t uid,
gid_t gid)
{
const ugid::SetResetGuard ugid;
const struct fuse_context *fc = fuse_get_context();
const config::Config &config = config::get();
const ugid::SetResetGuard ugid(fc->uid,fc->gid);
const rwlock::ReadGuard readlock(&config.srcmountslock);
if(fusepath == config.controlfile)
return -EPERM;
return _chown(config.policy.action,
return _chown(*config.action,
config.srcmounts,
fusepath,
uid,
+18 -37
View File
@@ -24,56 +24,37 @@
#include <fuse.h>
#include <sstream>
#include <string>
#include <vector>
#include <unistd.h>
#include <sys/stat.h>
#include "config.hpp"
#include "rwlock.hpp"
#include "fs.hpp"
using std::string;
using std::vector;
namespace mergerfs
{
namespace config
{
Config::Config()
: controlfile("/.mergerfs"),
testmode(false)
: destmount(),
srcmounts(),
srcmountslock(),
action(policies[Category::Enum::action]),
create(policies[Category::Enum::create]),
search(policies[Category::Enum::search]),
controlfile("/.mergerfs")
{
time_t now = time(NULL);
pthread_rwlock_init(&srcmountslock,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();
action = &Policy::ff;
create = &Policy::epmfs;
search = &Policy::ff;
}
const Config&
+8 -8
View File
@@ -31,6 +31,7 @@
#include <vector>
#include "policy.hpp"
#include "category.hpp"
namespace mergerfs
{
@@ -41,19 +42,18 @@ namespace mergerfs
public:
Config();
std::string generateReadStr() const;
void updateReadStr();
public:
std::string destmount;
std::vector<std::string> srcmounts;
Policy policy;
mutable pthread_rwlock_t srcmountslock;
const std::string controlfile;
struct stat controlfilestat;
std::string readstr;
const Policy *policies[Category::Enum::END];
const Policy *&action;
const Policy *&create;
const Policy *&search;
bool testmode;
public:
const std::string controlfile;
};
const Config &get(void);
+27 -34
View File
@@ -36,7 +36,7 @@
#include "ugid.hpp"
#include "fileinfo.hpp"
#include "fs.hpp"
#include "assert.hpp"
#include "rwlock.hpp"
using std::string;
using std::vector;
@@ -45,39 +45,33 @@ 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)
_create(const fs::SearchFunc searchFunc,
const fs::SearchFunc 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;
fs::PathVector createpath;
fs::PathVector existingpath;
dirname = fs::dirname(fusepath);
existingpath = searchFunc(srcmounts,dirname);
if(existingpath.base.empty())
dirname = fs::dirname(fusepath);
searchFunc(srcmounts,dirname,existingpath);
if(existingpath.empty())
return -ENOENT;
createpath = createPathFunc(srcmounts,dirname);
if(createpath.base != existingpath.base)
fs::clonepath(existingpath.base,createpath.base,dirname);
createPathFunc(srcmounts,dirname,createpath);
if(createpath.empty())
return -ENOSPC;
path = fs::make_path(createpath.base,fusepath);
if(createpath[0].base != existingpath[0].base)
fs::clonepath(existingpath[0].base,createpath[0].base,dirname);
path = fs::make_path(createpath[0].base,fusepath);
fd = ::open(path.c_str(),flags,mode);
if(fd == -1)
@@ -97,17 +91,16 @@ namespace mergerfs
mode_t mode,
struct fuse_file_info *fileinfo)
{
ugid::SetResetGuard ugid;
const config::Config &config = config::get();
const struct fuse_context *fc = fuse_get_context();
const config::Config &config = config::get();
const ugid::SetResetGuard ugid(fc->uid,fc->gid);
const rwlock::ReadGuard readlock(&config.srcmountslock);
if(fusepath == config.controlfile)
return _create_controlfile(fileinfo->fh);
return _create(config.policy.search,
config.policy.create,
return _create(*config.search,
*config.create,
config.srcmounts,
fusepath,
mode,
(mode & ~fc->umask),
fileinfo->flags,
fileinfo->fh);
}
+2 -11
View File
@@ -31,12 +31,8 @@
#include <errno.h>
#include <fcntl.h>
#include "ugid.hpp"
#include "config.hpp"
#include "fileinfo.hpp"
using namespace mergerfs;
static
int
_fallocate(const int fd,
@@ -75,14 +71,9 @@ namespace mergerfs
int mode,
off_t offset,
off_t len,
struct fuse_file_info *fi)
struct fuse_file_info *ffi)
{
const ugid::SetResetGuard ugid;
const config::Config &config = config::get();
const FileInfo *fileinfo = (FileInfo*)fi->fh;
if(fusepath == config.controlfile)
return -EINVAL;
const FileInfo *fileinfo = (FileInfo*)ffi->fh;
return _fallocate(fileinfo->fd,
mode,
+61
View File
@@ -0,0 +1,61 @@
/*
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 <unistd.h>
#include <errno.h>
#include "fileinfo.hpp"
static
int
_fgetattr(const int fd,
struct stat &st)
{
int rv;
rv = ::fstat(fd,&st);
return ((rv == -1) ? -errno : 0);
}
namespace mergerfs
{
namespace fgetattr
{
int
fgetattr(const char *fusepath,
struct stat *st,
struct fuse_file_info *ffi)
{
const FileInfo *fileinfo = (FileInfo*)ffi->fh;
return _fgetattr(fileinfo->fd,
*st);
}
}
}
+38
View File
@@ -0,0 +1,38 @@
/*
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 fgetattr
{
int
fgetattr(const char *fusepath,
struct stat *st,
struct fuse_file_info *fileinfo);
}
}
+58
View File
@@ -0,0 +1,58 @@
/*
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 <errno.h>
#include "fileinfo.hpp"
static
int
_flush(const int fd)
{
int rv;
rv = dup(fd);
if(rv == -1)
errno = EIO;
else
rv = close(rv);
return ((rv == -1) ? -errno : 0);
}
namespace mergerfs
{
namespace flush
{
int
flush(const char *fusepath,
struct fuse_file_info *ffi)
{
return _flush(((FileInfo*)ffi->fh)->fd);
}
}
}
+33
View File
@@ -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 flush
{
int
flush(const char *path,
struct fuse_file_info *fi);
}
}
+321 -294
View File
@@ -37,31 +37,19 @@
#include <sys/stat.h>
#include <sys/statvfs.h>
#include <sys/types.h>
#include <attr/xattr.h>
#include <unistd.h>
#include <glob.h>
#include <fnmatch.h>
#include "fs.hpp"
#include "xattr.hpp"
#include "str.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,
@@ -138,7 +126,10 @@ namespace fs
make_path(const string base,
const string suffix)
{
return base + suffix;
if(suffix[0] == '/' ||
*base.rbegin() == '/')
return base + suffix;
return base + '/' + suffix;
}
bool
@@ -150,11 +141,10 @@ namespace fs
iter = begin; iter != end; ++iter)
{
int rv;
struct stat st;
string path;
path = make_path(*iter,fusepath);
rv = ::lstat(path.c_str(),&st);
path = fs::make_path(*iter,fusepath);
rv = ::eaccess(path.c_str(),F_OK);
if(rv == 0)
return true;
}
@@ -171,281 +161,11 @@ namespace fs
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)
{
#ifndef WITHOUT_XATTR
int rv;
int size;
@@ -459,6 +179,10 @@ namespace fs
}
return rv;
#else
errno = ENOTSUP;
return -1;
#endif
}
int
@@ -472,7 +196,7 @@ namespace fs
if(rv != -1)
{
string tmp(attrs.begin(),attrs.end());
split(attrvector,tmp,'\0');
str::split(attrvector,tmp,'\0');
}
return rv;
@@ -497,6 +221,7 @@ namespace fs
const string attr,
vector<char> &value)
{
#ifndef WITHOUT_XATTR
int rv;
int size;
@@ -510,6 +235,10 @@ namespace fs
}
return rv;
#else
errno = ENOTSUP;
return -1;
#endif
}
int
@@ -562,11 +291,16 @@ namespace fs
const string value,
const int flags)
{
#ifndef WITHOUT_XATTR
return ::setxattr(path.c_str(),
key.c_str(),
value.data(),
value.size(),
flags);
#else
errno = ENOTSUP;
return -1;
#endif
}
int
@@ -575,11 +309,16 @@ namespace fs
const string value,
const int flags)
{
#ifndef WITHOUT_XATTR
return ::fsetxattr(fd,
key.c_str(),
value.data(),
value.size(),
flags);
#else
errno = ENOTSUP;
return -1;
#endif
}
int
@@ -673,7 +412,7 @@ namespace fs
return -1;
}
frompath = make_path(fromsrc,relative);
frompath = fs::make_path(fromsrc,relative);
rv = ::stat(frompath.c_str(),&st);
if(rv == -1)
@@ -681,7 +420,7 @@ namespace fs
else if(!S_ISDIR(st.st_mode))
return (errno = ENOTDIR,-1);
topath = make_path(tosrc,relative);
topath = fs::make_path(tosrc,relative);
rv = ::mkdir(topath.c_str(),st.st_mode);
if(rv == -1)
return -1;
@@ -701,4 +440,292 @@ namespace fs
return (errno = 0);
}
void
glob(const vector<string> &patterns,
vector<string> &strs)
{
int flags;
glob_t gbuf;
flags = 0;
for(size_t i = 0; i < patterns.size(); i++)
{
glob(patterns[i].c_str(),flags,NULL,&gbuf);
flags = GLOB_APPEND;
}
for(size_t i = 0; i < gbuf.gl_pathc; ++i)
strs.push_back(gbuf.gl_pathv[i]);
globfree(&gbuf);
}
void
erase_fnmatches(const vector<string> &patterns,
vector<string> &strs)
{
vector<string>::iterator si;
vector<string>::const_iterator pi;
si = strs.begin();
while(si != strs.end())
{
int match = FNM_NOMATCH;
for(pi = patterns.begin();
pi != patterns.end() && match != 0;
++pi)
{
match = fnmatch(pi->c_str(),si->c_str(),0);
}
if(match == 0)
si = strs.erase(si);
else
++si;
}
}
namespace find
{
void
invalid(const vector<string> &basepaths,
const string fusepath,
PathVector &paths)
{
return;
}
void
ff(const vector<string> &basepaths,
const string fusepath,
PathVector &paths)
{
for(vector<string>::const_iterator
iter = basepaths.begin(), eiter = basepaths.end();
iter != eiter;
++iter)
{
int rv;
string path;
path = fs::make_path(*iter,fusepath);
rv = ::eaccess(path.c_str(),F_OK);
if(rv == 0)
{
paths.push_back(Path(*iter,path));
return;
}
}
return;
}
void
ffwp(const vector<string> &basepaths,
const string fusepath,
PathVector &paths)
{
Path fallback;
for(vector<string>::const_iterator
iter = basepaths.begin(), eiter = basepaths.end();
iter != eiter;
++iter)
{
int rv;
string path;
path = fs::make_path(*iter,fusepath);
rv = ((::eaccess(path.c_str(),R_OK) == 0) ||
(::eaccess(path.c_str(),X_OK) == 0) ||
(::eaccess(path.c_str(),W_OK) == 0));
if(rv)
{
paths.push_back(Path(*iter,path));
return;
}
else if(errno == EACCES)
{
fallback.base = *iter;
fallback.full = path;
}
}
if(!fallback.base.empty())
paths.push_back(fallback);
return;
}
void
newest(const vector<string> &basepaths,
const string fusepath,
PathVector &paths)
{
time_t newest;
string npath;
vector<string>::const_iterator niter;
newest = 0;
for(vector<string>::const_iterator
iter = basepaths.begin(), eiter = basepaths.end();
iter != eiter;
++iter)
{
int rv;
struct stat st;
const string path = fs::make_path(*iter,fusepath);
rv = ::lstat(path.c_str(),&st);
if(rv == 0 && st.st_mtime > newest)
{
newest = st.st_mtime;
niter = iter;
npath = path;
}
}
if(newest)
paths.push_back(Path(*niter,npath));
return;
}
void
all(const vector<string> &basepaths,
const string fusepath,
PathVector &paths)
{
for(vector<string>::const_iterator
iter = basepaths.begin(), eiter = basepaths.end();
iter != eiter;
++iter)
{
int rv;
string path;
path = fs::make_path(*iter,fusepath);
rv = ::eaccess(path.c_str(),F_OK);
if(rv == 0)
paths.push_back(Path(*iter,path));
}
return;
}
void
mfs(const vector<string> &basepaths,
const string fusepath,
PathVector &paths)
{
fsblkcnt_t mfs = 0;
string mfspath;
string fullmfspath;
for(vector<string>::const_iterator
iter = basepaths.begin(), eiter = basepaths.end();
iter != eiter;
++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 = fs::make_path(mfspath,fusepath);
paths.push_back(Path(mfspath,fullmfspath));
return;
}
void
epmfs(const vector<string> &basepaths,
const string fusepath,
PathVector &paths)
{
fsblkcnt_t existingmfs = 0;
fsblkcnt_t generalmfs = 0;
string path;
string generalmfspath;
string existingmfspath;
vector<string>::const_iterator iter = basepaths.begin();
vector<string>::const_iterator eiter = basepaths.end();
do
{
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 > generalmfs)
{
generalmfs = spaceavail;
generalmfspath = mountpoint;
}
path = fs::make_path(mountpoint,fusepath);
rv = ::eaccess(path.c_str(),F_OK);
if(rv == 0)
{
if(spaceavail > existingmfs)
{
existingmfs = spaceavail;
existingmfspath = mountpoint;
}
}
}
++iter;
}
while(iter != eiter);
if(existingmfspath.empty())
existingmfspath = generalmfspath;
paths.push_back(Path(existingmfspath,path));
return;
}
void
rand(const vector<string> &basepaths,
const string fusepath,
PathVector &paths)
{
string randombasepath;
string randomfullpath;
randombasepath = *random_element(basepaths.begin(),
basepaths.end());
randomfullpath = fs::make_path(randombasepath,
fusepath);
paths.push_back(Path(randombasepath,randomfullpath));
}
}
};
+36 -53
View File
@@ -48,8 +48,8 @@ namespace fs
string full;
};
typedef fs::Path (*SearchFunc)(const vector<string>&,const string);
typedef void (*VecSearchFunc)(const vector<string>&,const string,vector<Path>&);
typedef vector<Path> PathVector;
typedef void (*SearchFunc)(const vector<string>&,const string,PathVector&);
string dirname(const string path);
string basename(const string path);
@@ -65,57 +65,6 @@ namespace fs
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);
@@ -154,6 +103,40 @@ namespace fs
int copyattr(const string from,
const string to);
void glob(const vector<string> &patterns,
vector<string> &strs);
void erase_fnmatches(const vector<string> &patterns,
vector<string> &strs);
namespace find
{
void invalid(const vector<string> &basepaths,
const string fusepath,
PathVector &paths);
void ff(const vector<string> &basepaths,
const string fusepath,
PathVector &paths);
void ffwp(const vector<string> &paths,
const string fusepath,
PathVector &rv);
void newest(const vector<string> &paths,
const string fusepath,
PathVector &rv);
void all(const vector<string> &paths,
const string fusepath,
PathVector &rv);
void mfs(const vector<string> &paths,
const string fusepath,
PathVector &rv);
void epmfs(const vector<string> &paths,
const string fusepath,
PathVector &rv);
void rand(const vector<string> &paths,
const string fusepath,
PathVector &rv);
}
};
#endif // __FS_HPP__
+2 -9
View File
@@ -34,8 +34,6 @@
#include <unistd.h>
#include <errno.h>
#include "config.hpp"
#include "ugid.hpp"
#include "fileinfo.hpp"
static
@@ -59,14 +57,9 @@ namespace mergerfs
int
fsync(const char *fusepath,
int isdatasync,
struct fuse_file_info *fi)
struct fuse_file_info *ffi)
{
const ugid::SetResetGuard ugid;
const config::Config &config = config::get();
const FileInfo *fileinfo = (FileInfo*)fi->fh;
if(fusepath == config.controlfile)
return 0;
const FileInfo *fileinfo = (FileInfo*)ffi->fh;
return _fsync(fileinfo->fd,
isdatasync);
+2 -9
View File
@@ -28,8 +28,6 @@
#include <sys/types.h>
#include <errno.h>
#include "config.hpp"
#include "ugid.hpp"
#include "fileinfo.hpp"
static
@@ -51,14 +49,9 @@ namespace mergerfs
int
ftruncate(const char *fusepath,
off_t size,
struct fuse_file_info *fi)
struct fuse_file_info *ffi)
{
const ugid::SetResetGuard ugid;
const config::Config &config = config::get();
const FileInfo *fileinfo = (FileInfo*)fi->fh;
if(fusepath == config.controlfile)
return -EPERM;
const FileInfo *fileinfo = (FileInfo*)ffi->fh;
return _ftruncate(fileinfo->fd,
size);
+41 -16
View File
@@ -35,27 +35,49 @@
#include "config.hpp"
#include "fs.hpp"
#include "ugid.hpp"
#include "assert.hpp"
#include "rwlock.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)
_getattr_controlfile(struct stat &buf)
{
time_t now = time(NULL);
buf.st_dev = 0;
buf.st_ino = 0;
buf.st_mode = (S_IFREG|S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
buf.st_nlink = 1;
buf.st_uid = ::geteuid();
buf.st_gid = ::getegid();
buf.st_rdev = 0;
buf.st_size = 0;
buf.st_blksize = 1024;
buf.st_blocks = 0;
buf.st_atime = now;
buf.st_mtime = now;
buf.st_ctime = now;
return 0;
}
static
int
_getattr(const fs::SearchFunc searchFunc,
const vector<string> &srcmounts,
const string fusepath,
struct stat &buf)
{
int rv;
string path;
fs::PathVector paths;
path = searchFunc(srcmounts,fusepath).full;
if(path.empty())
searchFunc(srcmounts,fusepath,paths);
if(paths.empty())
return -ENOENT;
rv = ::lstat(path.c_str(),&buf);
rv = ::lstat(paths[0].full.c_str(),&buf);
return ((rv == -1) ? -errno : 0);
}
@@ -66,18 +88,21 @@ namespace mergerfs
{
int
getattr(const char *fusepath,
struct stat *buf)
struct stat *st)
{
const ugid::SetResetGuard ugid;
const config::Config &config = config::get();
const config::Config &config = config::get();
if(fusepath == config.controlfile)
return (*buf = config.controlfilestat,0);
return _getattr_controlfile(*st);
return _getattr(config.policy.search,
const struct fuse_context *fc = fuse_get_context();
const ugid::SetResetGuard ugid(fc->uid,fc->gid);
const rwlock::ReadGuard readlock(&config.srcmountslock);
return _getattr(*config.search,
config.srcmounts,
fusepath,
*buf);
*st);
}
}
}
+86 -17
View File
@@ -22,40 +22,103 @@
THE SOFTWARE.
*/
#include <fuse.h>
#include <string>
#include <vector>
#include <errno.h>
#include <sys/types.h>
#include <attr/xattr.h>
#include <string.h>
#include "config.hpp"
#include "fs.hpp"
#include "ugid.hpp"
#include "assert.hpp"
#include "rwlock.hpp"
#include "xattr.hpp"
#include "str.hpp"
using std::string;
using std::vector;
using mergerfs::Policy;
using namespace mergerfs::config;
static
int
_getxattr(const Policy::Search::Func searchFunc,
const vector<string> &srcmounts,
const string fusepath,
const char *attrname,
char *buf,
const size_t count)
_getxattr_controlfile(const Config &config,
const string attrname,
char *buf,
const size_t count)
{
size_t len;
string attrvalue;
if(attrname == "user.mergerfs.action")
attrvalue = (std::string)*config.action;
else if(attrname == "user.mergerfs.create")
attrvalue = (std::string)*config.create;
else if(attrname == "user.mergerfs.search")
attrvalue = (std::string)*config.search;
else if(attrname == "user.mergerfs.srcmounts")
attrvalue = str::join(config.srcmounts,':');
if(attrvalue.empty())
return -ENOATTR;
len = attrvalue.size();
if(count == 0)
return len;
if(count < len)
return -ERANGE;
memcpy(buf,attrvalue.c_str(),len);
return (int)len;
}
static
int
_getxattr_from_string(char *destbuf,
const size_t destbufsize,
const string &src)
{
const size_t srcbufsize = src.size();
if(destbufsize == 0)
return srcbufsize;
if(srcbufsize > destbufsize)
return (errno = ERANGE, -1);
memcpy(destbuf,src.data(),srcbufsize);
return srcbufsize;
}
static
int
_getxattr(const fs::SearchFunc searchFunc,
const vector<string> &srcmounts,
const string fusepath,
const char *attrname,
char *buf,
const size_t count)
{
#ifndef WITHOUT_XATTR
int rv;
string path;
fs::PathVector paths;
path = searchFunc(srcmounts,fusepath).full;
if(path.empty())
searchFunc(srcmounts,fusepath,paths);
if(paths.empty())
return -ENOENT;
rv = ::lgetxattr(path.c_str(),attrname,buf,count);
if(!strcmp(attrname,"user.mergerfs.basepath"))
rv = ::_getxattr_from_string(buf,count,paths[0].base);
else if(!strcmp(attrname,"user.mergerfs.fullpath"))
rv = ::_getxattr_from_string(buf,count,paths[0].full);
else
rv = ::lgetxattr(paths[0].full.c_str(),attrname,buf,count);
return ((rv == -1) ? -errno : rv);
#else
@@ -73,13 +136,19 @@ namespace mergerfs
char *buf,
size_t count)
{
const ugid::SetResetGuard ugid;
const config::Config &config = config::get();
const config::Config &config = config::get();
if(fusepath == config.controlfile)
return -ENOTSUP;
return _getxattr_controlfile(config,
attrname,
buf,
count);
return _getxattr(config.policy.search,
const struct fuse_context *fc = fuse_get_context();
const ugid::SetResetGuard ugid(fc->uid,fc->gid);
const rwlock::ReadGuard readlock(&config.srcmountslock);
return _getxattr(*config.search,
config.srcmounts,
fusepath,
attrname,
+16 -9
View File
@@ -30,9 +30,7 @@
#include <sys/ioctl.h>
#include <linux/fs.h>
#include "config.hpp"
#include "fileinfo.hpp"
#include "ugid.hpp"
static
int
@@ -46,8 +44,22 @@ _ioctl(const int fd,
switch(cmd)
{
#ifdef FS_IOC_GETFLAGS
case FS_IOC_GETFLAGS:
case FS_IOC_SETFLAGS:
#endif
#ifdef FS_IOC32_GETFLAGS
case FS_IOC32_SETFLAGS:
case FS_IOC32_GETFLAGS:
#endif
#ifdef FS_IOC_GETVERSION
case FS_IOC_GETVERSION:
case FS_IOC_SETVERSION:
#endif
#ifdef FS_IOC32_GETVERSION
case FS_IOC32_GETVERSION:
case FS_IOC32_SETVERSION:
#endif
rv = ::ioctl(fd,cmd,data);
break;
@@ -68,16 +80,11 @@ namespace mergerfs
ioctl(const char *fusepath,
int cmd,
void *arg,
struct fuse_file_info *fi,
struct fuse_file_info *ffi,
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;
const FileInfo *fileinfo = (FileInfo*)ffi->fh;
return _ioctl(fileinfo->fd,
cmd,
+30 -13
View File
@@ -22,6 +22,8 @@
THE SOFTWARE.
*/
#include <fuse.h>
#include <string>
#include <vector>
@@ -31,7 +33,7 @@
#include "config.hpp"
#include "fs.hpp"
#include "ugid.hpp"
#include "assert.hpp"
#include "rwlock.hpp"
using std::string;
using std::vector;
@@ -39,14 +41,14 @@ using mergerfs::Policy;
static
int
_link(const Policy::Action::Func searchFunc,
const vector<string> &srcmounts,
const string from,
const string to)
_link(const fs::SearchFunc searchFunc,
const vector<string> &srcmounts,
const string from,
const string to)
{
int rv;
int error;
vector<fs::Path> paths;
fs::PathVector paths;
searchFunc(srcmounts,from,paths);
if(paths.empty())
@@ -54,13 +56,29 @@ _link(const Policy::Action::Func searchFunc,
rv = -1;
error = 0;
for(vector<fs::Path>::const_iterator
for(fs::PathVector::const_iterator
i = paths.begin(), ei = paths.end(); i != ei; ++i)
{
int lrv;
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());
lrv = ::link(pathfrom.c_str(),pathto.c_str());
if(lrv == -1 && errno == ENOENT)
{
string todir;
fs::PathVector topaths;
todir = fs::dirname(to);
fs::find::ffwp(srcmounts,todir,topaths);
if(topaths.size() > 0)
{
fs::clonepath(topaths[0].base,i->base,todir);
lrv = ::link(pathfrom.c_str(),pathto.c_str());
}
}
rv &= lrv;
if(rv == -1)
error = errno;
}
@@ -76,13 +94,12 @@ namespace mergerfs
link(const char *from,
const char *to)
{
const ugid::SetResetGuard ugid;
const struct fuse_context *fc = fuse_get_context();
const config::Config &config = config::get();
const ugid::SetResetGuard ugid(fc->uid,fc->gid);
const rwlock::ReadGuard readlock(&config.srcmountslock);
if(from == config.controlfile)
return -EPERM;
return _link(config.policy.action,
return _link(*config.action,
config.srcmounts,
from,
to);
+47 -16
View File
@@ -22,39 +22,66 @@
THE SOFTWARE.
*/
#include <fuse.h>
#include <string>
#include <vector>
#include <sys/types.h>
#include <attr/xattr.h>
#include <errno.h>
#include <string.h>
#include "config.hpp"
#include "category.hpp"
#include "fs.hpp"
#include "ugid.hpp"
#include "assert.hpp"
#include "rwlock.hpp"
#include "xattr.hpp"
using std::string;
using std::vector;
using mergerfs::Policy;
using namespace mergerfs;
static
int
_listxattr(const Policy::Search::Func searchFunc,
const vector<string> &srcmounts,
const string fusepath,
char *list,
const size_t size)
_listxattr_controlfile(char *list,
const size_t size)
{
const char xattrs[] =
"user.mergerfs.srcmounts\0"
"user.mergerfs.action\0"
"user.mergerfs.create\0"
"user.mergerfs.search"
;
if(size == 0)
return sizeof(xattrs);
if(size < sizeof(xattrs))
return -ERANGE;
memcpy(list,xattrs,sizeof(xattrs));
return sizeof(xattrs);
}
static
int
_listxattr(const fs::SearchFunc searchFunc,
const vector<string> &srcmounts,
const string fusepath,
char *list,
const size_t size)
{
#ifndef WITHOUT_XATTR
int rv;
string path;
fs::PathVector paths;
path = searchFunc(srcmounts,fusepath).full;
if(path.empty())
searchFunc(srcmounts,fusepath,paths);
if(paths.empty())
return -ENOENT;
rv = ::llistxattr(path.c_str(),list,size);
rv = ::llistxattr(paths[0].full.c_str(),list,size);
return ((rv == -1) ? -errno : rv);
#else
@@ -71,13 +98,17 @@ namespace mergerfs
char *list,
size_t size)
{
const ugid::SetResetGuard ugid;
const config::Config &config = config::get();
const config::Config &config = config::get();
if(fusepath == config.controlfile)
return -ENOTSUP;
return _listxattr_controlfile(list,
size);
return _listxattr(config.policy.search,
const struct fuse_context *fc = fuse_get_context();
const ugid::SetResetGuard ugid(fc->uid,fc->gid);
const rwlock::ReadGuard readlock(&config.srcmountslock);
return _listxattr(*config.search,
config.srcmounts,
fusepath,
list,
+9 -9
View File
@@ -39,6 +39,8 @@
#include "chown.hpp"
#include "create.hpp"
#include "fallocate.hpp"
#include "fgetattr.hpp"
#include "flush.hpp"
#include "fsync.hpp"
#include "ftruncate.hpp"
#include "getattr.hpp"
@@ -50,6 +52,7 @@
#include "mknod.hpp"
#include "open.hpp"
#include "read.hpp"
#include "read_buf.hpp"
#include "readdir.hpp"
#include "readlink.hpp"
#include "release.hpp"
@@ -63,6 +66,7 @@
#include "unlink.hpp"
#include "utimens.hpp"
#include "write.hpp"
#include "write_buf.hpp"
static
struct fuse_operations
@@ -80,9 +84,9 @@ get_fuse_operations()
ops.create = mergerfs::create::create;
ops.destroy = NULL;
ops.fallocate = mergerfs::fallocate::fallocate;
ops.fgetattr = NULL;
ops.fgetattr = mergerfs::fgetattr::fgetattr;
ops.flock = NULL;
ops.flush = NULL; /* called on close() of fd */
ops.flush = mergerfs::flush::flush;
ops.fsync = mergerfs::fsync::fsync;
ops.fsyncdir = NULL;
ops.ftruncate = mergerfs::ftruncate::ftruncate;
@@ -90,7 +94,6 @@ get_fuse_operations()
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;
@@ -101,7 +104,7 @@ get_fuse_operations()
ops.opendir = NULL;
ops.poll = NULL;
ops.read = mergerfs::read::read;
ops.read_buf = NULL;
ops.read_buf = mergerfs::read_buf::read_buf;
ops.readdir = mergerfs::readdir::readdir;
ops.readlink = mergerfs::readlink::readlink;
ops.release = mergerfs::release::release;
@@ -117,7 +120,7 @@ get_fuse_operations()
ops.utime = NULL; /* deprecated; use utimens() */
ops.utimens = mergerfs::utimens::utimens;
ops.write = mergerfs::write::write;
ops.write_buf = NULL;
ops.write_buf = mergerfs::write_buf::write_buf;
return ops;
}
@@ -154,10 +157,7 @@ main(int argc,
mergerfs::options::parse(args,config);
if(config.testmode == false)
rv = mergerfs::main(args,config);
else
rv = mergerfs::test(args,config);
rv = mergerfs::main(args,config);
return rv;
}
+20 -22
View File
@@ -35,39 +35,38 @@
#include "fileinfo.hpp"
#include "fs.hpp"
#include "config.hpp"
#include "assert.hpp"
#include "rwlock.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)
_mkdir(const fs::SearchFunc searchFunc,
const fs::SearchFunc createPathFunc,
const vector<string> &srcmounts,
const string fusepath,
const mode_t mode)
{
int rv;
string path;
string dirname;
fs::Path createpath;
fs::Path existingpath;
fs::PathVector createpath;
fs::PathVector existingpath;
if(fs::path_exists(srcmounts,fusepath))
return -EEXIST;
dirname = fs::dirname(fusepath);
existingpath = searchFunc(srcmounts,dirname);
if(existingpath.base.empty())
searchFunc(srcmounts,dirname,existingpath);
if(existingpath.empty())
return -ENOENT;
createpath = createPathFunc(srcmounts,dirname);
if(createpath.base != existingpath.base)
fs::clonepath(existingpath.base,createpath.base,dirname);
createPathFunc(srcmounts,dirname,createpath);
if(createpath[0].base != existingpath[0].base)
fs::clonepath(existingpath[0].base,createpath[0].base,dirname);
path = fs::make_path(createpath.base,fusepath);
path = fs::make_path(createpath[0].base,fusepath);
rv = ::mkdir(path.c_str(),mode);
@@ -82,17 +81,16 @@ namespace mergerfs
mkdir(const char *fusepath,
mode_t mode)
{
const ugid::SetResetGuard ugid;
const struct fuse_context *fc = fuse_get_context();
const config::Config &config = config::get();
const ugid::SetResetGuard ugid(fc->uid,fc->gid);
const rwlock::ReadGuard readlock(&config.srcmountslock);
if(fusepath == config.controlfile)
return -EEXIST;
return _mkdir(config.policy.search,
config.policy.create,
return _mkdir(*config.search,
*config.create,
config.srcmounts,
fusepath,
mode);
(mode & ~fc->umask));
}
}
}
+22 -24
View File
@@ -36,40 +36,39 @@
#include "ugid.hpp"
#include "fs.hpp"
#include "config.hpp"
#include "assert.hpp"
#include "rwlock.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)
_mknod(const fs::SearchFunc searchFunc,
const fs::SearchFunc 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;
fs::PathVector createpath;
fs::PathVector existingpath;
if(fs::path_exists(srcmounts,fusepath))
return -EEXIST;
dirname = fs::dirname(fusepath);
existingpath = searchFunc(srcmounts,dirname);
if(existingpath.base.empty())
dirname = fs::dirname(fusepath);
searchFunc(srcmounts,dirname,existingpath);
if(existingpath.empty())
return -ENOENT;
createpath = createPathFunc(srcmounts,dirname);
if(existingpath.base != createpath.base)
fs::clonepath(existingpath.base,createpath.base,dirname);
createPathFunc(srcmounts,dirname,createpath);
if(existingpath[0].base != createpath[0].base)
fs::clonepath(existingpath[0].base,createpath[0].base,dirname);
path = fs::make_path(createpath.base,fusepath);
path = fs::make_path(createpath[0].base,fusepath);
rv = ::mknod(path.c_str(),mode,dev);
@@ -85,17 +84,16 @@ namespace mergerfs
mode_t mode,
dev_t rdev)
{
const ugid::SetResetGuard ugid;
const struct fuse_context *fc = fuse_get_context();
const config::Config &config = config::get();
const ugid::SetResetGuard ugid(fc->uid,fc->gid);
const rwlock::ReadGuard readlock(&config.srcmountslock);
if(fusepath == config.controlfile)
return -EEXIST;
return _mknod(config.policy.search,
config.policy.create,
return _mknod(*config.search,
*config.create,
config.srcmounts,
fusepath,
mode,
(mode & ~fc->umask),
rdev);
}
}
+15 -26
View File
@@ -35,42 +35,32 @@
#include "fileinfo.hpp"
#include "fs.hpp"
#include "config.hpp"
#include "assert.hpp"
#include "rwlock.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)
_open(const fs::SearchFunc searchFunc,
const vector<string> &srcmounts,
const string fusepath,
const int flags,
uint64_t &fh)
{
int fd;
fs::Path path;
fs::PathVector paths;
path = searchFunc(srcmounts,fusepath);
if(path.full.empty())
searchFunc(srcmounts,fusepath,paths);
if(paths.empty())
return -ENOENT;
fd = ::open(path.full.c_str(),flags);
fd = ::open(paths[0].full.c_str(),flags);
if(fd == -1)
return -errno;
fh = (uint64_t)new FileInfo(fd,flags,path.full);
fh = (uint64_t)new FileInfo(fd,flags,paths[0].full);
return 0;
}
@@ -83,13 +73,12 @@ namespace mergerfs
open(const char *fusepath,
struct fuse_file_info *fileinfo)
{
const ugid::SetResetGuard ugid;
const struct fuse_context *fc = fuse_get_context();
const config::Config &config = config::get();
const ugid::SetResetGuard ugid(fc->uid,fc->gid);
const rwlock::ReadGuard readlock(&config.srcmountslock);
if(fusepath == config.controlfile)
return _open_controlfile(fileinfo->fh);
return _open(config.policy.search,
return _open(*config.search,
config.srcmounts,
fusepath,
fileinfo->flags,
+57 -43
View File
@@ -26,35 +26,21 @@
#include <stddef.h>
#include <string.h>
#include <stdio.h>
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <iostream>
#include "str.hpp"
#include "config.hpp"
#include "policy.hpp"
using std::string;
using std::vector;
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,
@@ -62,36 +48,53 @@ process_opt(config::Config &config,
{
int rv = 0;
std::vector<std::string> argvalue;
Policy &policy = config.policy;
split(argvalue,arg,'=');
str::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]);
if(argvalue[0] == "create")
config.create = Policy::find(argvalue[1]);
else if(argvalue[0] == "search")
policy.search.fromString(argvalue[1]);
config.search = Policy::find(argvalue[1]);
else if(argvalue[0] == "action")
policy.action.fromString(argvalue[1]);
config.action = Policy::find(argvalue[1]);
else
rv = 1;
break;
default:
case 1:
if(argvalue[0] == "test")
config.testmode = true;
else
rv = 1;
rv = 1;
break;
};
return rv;
}
static
int
process_srcmounts(const char *arg,
config::Config &config)
{
vector<string> paths;
str::split(paths,arg,':');
fs::glob(paths,config.srcmounts);
return 0;
}
static
int
process_destmounts(const char *arg,
config::Config &config)
{
config.destmount = arg;
return 1;
}
static
int
option_processor(void *data,
@@ -109,15 +112,9 @@ option_processor(void *data,
break;
case FUSE_OPT_KEY_NONOPT:
if(config.destmount.empty())
{
rv = 1;
config.destmount = arg;
}
else
{
split(config.srcmounts,arg,':');
}
rv = config.srcmounts.empty() ?
process_srcmounts(arg,config) :
process_destmounts(arg,config);
break;
default:
@@ -127,6 +124,21 @@ option_processor(void *data,
return rv;
}
static
void
set_fsname(struct fuse_args &args,
const config::Config &config)
{
if(config.srcmounts.size() > 0)
{
std::string fsname;
fsname = "-ofsname=" + str::join(config.srcmounts,':');
fuse_opt_insert_arg(&args,1,fsname.c_str());
}
}
namespace mergerfs
{
namespace options
@@ -135,11 +147,13 @@ namespace mergerfs
parse(struct fuse_args &args,
config::Config &config)
{
fuse_opt_parse(&args,
&config,
NULL,
::option_processor);
config.updateReadStr();
set_fsname(args,config);
}
}
}
+40 -268
View File
@@ -22,285 +22,57 @@
THE SOFTWARE.
*/
#include <vector>
#include "policy.hpp"
#include "fs.hpp"
#include "buildvector.hpp"
#define POLICY(X) (Policy(Policy::Enum::X,#X,fs::find::X))
namespace mergerfs
{
Policy::Create::Create(Type value)
const std::vector<Policy> Policy::_policies_ =
buildvector<Policy,true>
(POLICY(invalid))
(POLICY(all))
(POLICY(epmfs))
(POLICY(ff))
(POLICY(ffwp))
(POLICY(mfs))
(POLICY(newest))
(POLICY(rand));
const Policy * const Policy::policies = &_policies_[1];
const Policy &Policy::invalid = Policy::policies[Policy::Enum::invalid];
const Policy &Policy::all = Policy::policies[Policy::Enum::all];
const Policy &Policy::epmfs = Policy::policies[Policy::Enum::epmfs];
const Policy &Policy::ff = Policy::policies[Policy::Enum::ff];
const Policy &Policy::ffwp = Policy::policies[Policy::Enum::ffwp];
const Policy &Policy::mfs = Policy::policies[Policy::Enum::mfs];
const Policy &Policy::newest = Policy::policies[Policy::Enum::newest];
const Policy &Policy::rand = Policy::policies[Policy::Enum::rand];
const Policy&
Policy::find(const std::string str)
{
*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)
for(int i = Enum::BEGIN; i != Enum::END; ++i)
{
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";
if(policies[i] == str)
return policies[i];
}
return invalid;
}
Policy::Create::Func
Policy::Create::findFunc(Policy::Create::Type value)
const Policy&
Policy::find(const Policy::Enum::Type i)
{
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;
}
}
if(i >= Policy::Enum::BEGIN &&
i < Policy::Enum::END)
return policies[i];
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";
}
return invalid;
}
}
+65 -158
View File
@@ -26,6 +26,7 @@
#define __POLICY_HPP__
#include <string>
#include <map>
#include "fs.hpp"
@@ -34,173 +35,79 @@ namespace mergerfs
class Policy
{
public:
enum Type
{
STATFS,
ACTION,
CREATE,
SEARCH
};
struct Enum
{
enum Type
{
invalid = -1,
BEGIN = 0,
all = BEGIN,
epmfs,
ff,
ffwp,
mfs,
newest,
rand,
END
};
};
private:
Enum::Type _enum;
std::string _str;
fs::SearchFunc _func;
public:
class StatFS
Policy()
: _enum(invalid),
_str(invalid),
_func(invalid)
{
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
Policy(const Enum::Type enum_,
const std::string str_,
const fs::SearchFunc func_)
: _enum(enum_),
_str(str_),
_func(func_)
{
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)
{}
operator const Enum::Type() const { return _enum; }
operator const std::string() const { return _str; }
operator const fs::SearchFunc() const { return _func; }
operator const Policy*() const { return this; }
bool operator==(const Enum::Type enum_) const
{ return _enum == enum_; }
bool operator==(const std::string str_) const
{ return _str == str_; }
bool operator==(const fs::SearchFunc func_) const
{ return _func == func_; }
bool operator!=(const Policy &r) const
{ return _enum != r._enum; }
bool operator<(const Policy &r) const
{ return _enum < r._enum; }
public:
StatFS statfs;
Create create;
Search search;
Action action;
static const Policy &find(const std::string);
static const Policy &find(const Enum::Type);
public:
static const std::vector<Policy> _policies_;
static const Policy * const policies;
static const Policy &invalid;
static const Policy &all;
static const Policy &epmfs;
static const Policy &ff;
static const Policy &ffwp;
static const Policy &mfs;
static const Policy &newest;
static const Policy &rand;
};
}
+2 -30
View File
@@ -29,29 +29,9 @@
#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,
@@ -75,17 +55,9 @@ namespace mergerfs
char *buf,
size_t count,
off_t offset,
struct fuse_file_info *fi)
struct fuse_file_info *ffi)
{
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,
return _read(((FileInfo*)ffi->fh)->fd,
buf,
count,
offset);
+74
View File
@@ -0,0 +1,74 @@
/*
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 <stdlib.h>
#include <errno.h>
#include <string.h>
#include "fileinfo.hpp"
static
int
_read_buf(const int fd,
struct fuse_bufvec **bufp,
const size_t size,
const off_t offset)
{
struct fuse_bufvec *src;
src = (fuse_bufvec*)malloc(sizeof(struct fuse_bufvec));
if(src == NULL)
return -ENOMEM;
*src = FUSE_BUFVEC_INIT(size);
src->buf->flags = (fuse_buf_flags)(FUSE_BUF_IS_FD|FUSE_BUF_FD_SEEK|FUSE_BUF_FD_RETRY);
src->buf->fd = fd;
src->buf->pos = offset;
*bufp = src;
return 0;
}
namespace mergerfs
{
namespace read_buf
{
int
read_buf(const char *fusepath,
struct fuse_bufvec **bufp,
size_t size,
off_t offset,
struct fuse_file_info *ffi)
{
return _read_buf(((FileInfo*)ffi->fh)->fd,
bufp,
size,
offset);
}
}
}
+36
View File
@@ -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_buf
{
int
read_buf(const char *fusepath,
struct fuse_bufvec **buf,
size_t size,
off_t offset,
struct fuse_file_info *fi);
}
}
+7 -17
View File
@@ -35,6 +35,7 @@
#include "config.hpp"
#include "ugid.hpp"
#include "fs.hpp"
#include "rwlock.hpp"
using std::string;
using std::vector;
@@ -69,25 +70,12 @@ _readdir(const vector<string> &srcmounts,
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)
if(found.insert(d_name).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);
}
if(filler(buf,de->d_name,NULL,NO_OFFSET) != 0)
return -ENOMEM;
}
::closedir(dh);
@@ -135,8 +123,10 @@ namespace mergerfs
off_t offset,
struct fuse_file_info *fi)
{
const ugid::SetResetGuard ugid;
const struct fuse_context *fc = fuse_get_context();
const config::Config &config = config::get();
const ugid::SetResetGuard ugid(fc->uid,fc->gid);
const rwlock::ReadGuard readlock(&config.srcmountslock);
return _readdir(config.srcmounts,
fusepath,
+14 -15
View File
@@ -33,7 +33,7 @@
#include "config.hpp"
#include "ugid.hpp"
#include "fs.hpp"
#include "assert.hpp"
#include "rwlock.hpp"
using std::string;
using std::vector;
@@ -41,20 +41,20 @@ using mergerfs::Policy;
static
int
_readlink(const Policy::Search::Func searchFunc,
const vector<string>& srcmounts,
const string fusepath,
char *buf,
const size_t size)
_readlink(const fs::SearchFunc searchFunc,
const vector<string>& srcmounts,
const string fusepath,
char *buf,
const size_t size)
{
int rv;
string path;
fs::PathVector paths;
path = searchFunc(srcmounts,fusepath).full;
if(path.empty())
searchFunc(srcmounts,fusepath,paths);
if(paths.empty())
return -ENOENT;
rv = ::readlink(path.c_str(),buf,size);
rv = ::readlink(paths[0].full.c_str(),buf,size);
if(rv == -1)
return -errno;
@@ -72,13 +72,12 @@ namespace mergerfs
char *buf,
size_t size)
{
const ugid::SetResetGuard ugid;
const struct fuse_context *fc = fuse_get_context();
const config::Config &config = config::get();
const ugid::SetResetGuard ugid(fc->uid,fc->gid);
const rwlock::ReadGuard readlock(&config.srcmountslock);
if(fusepath == config.controlfile)
return -EINVAL;
return _readlink(config.policy.search,
return _readlink(*config.search,
config.srcmounts,
fusepath,
buf,
+3 -24
View File
@@ -24,30 +24,15 @@
#include <fuse.h>
#include <unistd.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)
@@ -69,15 +54,9 @@ namespace mergerfs
{
int
release(const char *fusepath,
struct fuse_file_info *fi)
struct fuse_file_info *ffi)
{
const ugid::SetResetGuard ugid;
const config::Config &config = config::get();
if(fusepath == config.controlfile)
return _release_controlfile(fi->fh);
return _release(fi->fh);
return _release(ffi->fh);
}
}
}
+16 -12
View File
@@ -22,33 +22,34 @@
THE SOFTWARE.
*/
#include <fuse.h>
#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"
#include "rwlock.hpp"
#include "xattr.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)
_removexattr(const fs::SearchFunc searchFunc,
const vector<string> &srcmounts,
const string fusepath,
const char *attrname)
{
#ifndef WITHOUT_XATTR
int rv;
int error;
vector<fs::Path> paths;
fs::PathVector paths;
searchFunc(srcmounts,fusepath,paths);
if(paths.empty())
@@ -56,7 +57,7 @@ _removexattr(const Policy::Action::Func searchFunc,
rv = -1;
error = 0;
for(vector<fs::Path>::const_iterator
for(fs::PathVector::const_iterator
i = paths.begin(), ei = paths.end(); i != ei; ++i)
{
rv &= ::lremovexattr(i->full.c_str(),attrname);
@@ -78,13 +79,16 @@ namespace mergerfs
removexattr(const char *fusepath,
const char *attrname)
{
const ugid::SetResetGuard uid;
const config::Config &config = config::get();
const config::Config &config = config::get();
if(fusepath == config.controlfile)
return -ENOTSUP;
return _removexattr(config.policy.action,
const struct fuse_context *fc = fuse_get_context();
const ugid::SetResetGuard ugid(fc->uid,fc->gid);
const rwlock::ReadGuard readlock(&config.srcmountslock);
return _removexattr(*config.action,
config.srcmounts,
fusepath,
attrname);
+18 -15
View File
@@ -22,6 +22,8 @@
THE SOFTWARE.
*/
#include <fuse.h>
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
@@ -32,29 +34,31 @@
#include "ugid.hpp"
#include "fs.hpp"
#include "config.hpp"
#include "rwlock.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)
_rename(const fs::SearchFunc searchFunc,
const vector<string> &srcmounts,
const string from,
const string to)
{
int rv;
string pathto;
fs::Path pathfrom;
fs::PathVector pathfrom;
pathfrom = searchFunc(srcmounts,from);
if(pathfrom.base.empty())
searchFunc(srcmounts,from,pathfrom);
if(pathfrom.empty())
return -ENOENT;
pathto = fs::make_path(pathfrom.base,to);
pathto = fs::make_path(pathfrom[0].base,to);
rv = ::rename(pathfrom.full.c_str(),pathto.c_str());
rv = ::rename(pathfrom[0].full.c_str(),pathto.c_str());
if(rv == -1 && errno == ENOENT)
return -EXDEV;
return ((rv == -1) ? -errno : 0);
}
@@ -67,13 +71,12 @@ namespace mergerfs
rename(const char *from,
const char *to)
{
const ugid::SetResetGuard uid;
const struct fuse_context *fc = fuse_get_context();
const config::Config &config = config::get();
const ugid::SetResetGuard ugid(fc->uid,fc->gid);
const rwlock::ReadGuard readlock(&config.srcmountslock);
if(from == config.controlfile)
return -ENOENT;
return _rename(config.policy.search,
return _rename(*config.search,
config.srcmounts,
from,
to);
+12 -11
View File
@@ -22,6 +22,8 @@
THE SOFTWARE.
*/
#include <fuse.h>
#include <errno.h>
#include <unistd.h>
@@ -30,7 +32,7 @@
#include "ugid.hpp"
#include "fs.hpp"
#include "config.hpp"
#include "assert.hpp"
#include "rwlock.hpp"
using std::string;
using std::vector;
@@ -38,13 +40,13 @@ using mergerfs::Policy;
static
int
_rmdir(const Policy::Action::Func searchFunc,
const vector<string> &srcmounts,
const string fusepath)
_rmdir(const fs::SearchFunc searchFunc,
const vector<string> &srcmounts,
const string fusepath)
{
int rv;
int error;
vector<fs::Path> paths;
fs::PathVector paths;
searchFunc(srcmounts,fusepath,paths);
if(paths.empty())
@@ -52,7 +54,7 @@ _rmdir(const Policy::Action::Func searchFunc,
rv = -1;
error = 0;
for(vector<fs::Path>::const_iterator
for(fs::PathVector::const_iterator
i = paths.begin(), ei = paths.end(); i != ei; ++i)
{
rv &= ::rmdir(i->full.c_str());
@@ -70,13 +72,12 @@ namespace mergerfs
int
rmdir(const char *fusepath)
{
const ugid::SetResetGuard uid;
const struct fuse_context *fc = fuse_get_context();
const config::Config &config = config::get();
const ugid::SetResetGuard ugid(fc->uid,fc->gid);
const rwlock::ReadGuard readguard(&config.srcmountslock);
if(fusepath == config.controlfile)
return -ENOTDIR;
return _rmdir(config.policy.action,
return _rmdir(*config.action,
config.srcmounts,
fusepath);
}
+71
View File
@@ -0,0 +1,71 @@
/*
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 <pthread.h>
namespace mergerfs
{
namespace rwlock
{
class ReadGuard
{
public:
ReadGuard(pthread_rwlock_t *lock)
{
_lock = lock;
pthread_rwlock_rdlock(_lock);
}
~ReadGuard()
{
pthread_rwlock_unlock(_lock);
}
pthread_rwlock_t *_lock;
private:
ReadGuard();
};
class WriteGuard
{
public:
WriteGuard(pthread_rwlock_t *lock)
{
_lock = lock;
pthread_rwlock_wrlock(_lock);
}
~WriteGuard()
{
pthread_rwlock_unlock(_lock);
}
pthread_rwlock_t *_lock;
private:
WriteGuard();
};
}
}
+211 -21
View File
@@ -22,36 +22,218 @@
THE SOFTWARE.
*/
#include <fuse.h>
#include <string>
#include <vector>
#include <sstream>
#include <errno.h>
#include <sys/types.h>
#include <attr/xattr.h>
#include "config.hpp"
#include "fs.hpp"
#include "ugid.hpp"
#include "assert.hpp"
#include "rwlock.hpp"
#include "xattr.hpp"
#include "str.hpp"
using std::string;
using std::vector;
using mergerfs::Policy;
using mergerfs::Category;
using namespace mergerfs;
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)
_add_srcmounts(vector<string> &srcmounts,
pthread_rwlock_t &srcmountslock,
const string destmount,
const string values,
vector<string>::iterator pos)
{
vector<string> patterns;
vector<string> additions;
str::split(patterns,values,':');
fs::glob(patterns,additions);
if(!additions.empty())
{
const rwlock::WriteGuard wrg(&srcmountslock);
srcmounts.insert(pos,
additions.begin(),
additions.end());
}
return 0;
}
static
int
_erase_srcmounts(vector<string> &srcmounts,
pthread_rwlock_t srcmountslock,
const string &values)
{
if(srcmounts.empty())
return 0;
vector<string> patterns;
str::split(patterns,values,':');
const rwlock::WriteGuard wrg(&srcmountslock);
fs::erase_fnmatches(patterns,srcmounts);
return 0;
}
static
int
_replace_srcmounts(vector<string> &srcmounts,
pthread_rwlock_t srcmountslock,
const string destmount,
const string values)
{
vector<string> patterns;
vector<string> newmounts;
str::split(patterns,values,':');
fs::glob(patterns,newmounts);
{
const rwlock::WriteGuard wrg(&srcmountslock);
srcmounts.swap(newmounts);
}
return 0;
}
static
void
_split_attrval(const string attrval,
string &instruction,
string &values)
{
size_t offset;
offset = attrval.find_first_of('/');
instruction = attrval.substr(0,offset);
if(offset != string::npos)
values = attrval.substr(offset);
}
static
int
_setxattr_srcmounts(vector<string> &srcmounts,
pthread_rwlock_t &srcmountslock,
const string destmount,
const string attrval,
const int flags)
{
string instruction;
string values;
if((flags & XATTR_CREATE) == XATTR_CREATE)
return -EEXIST;
_split_attrval(attrval,instruction,values);
if(instruction == "+")
return _add_srcmounts(srcmounts,srcmountslock,destmount,values,srcmounts.end());
else if(instruction == "+<")
return _add_srcmounts(srcmounts,srcmountslock,destmount,values,srcmounts.begin());
else if(instruction == "+>")
return _add_srcmounts(srcmounts,srcmountslock,destmount,values,srcmounts.end());
else if(instruction == "-")
return _erase_srcmounts(srcmounts,srcmountslock,values);
else if(instruction == "-<")
return _erase_srcmounts(srcmounts,srcmountslock,srcmounts.front());
else if(instruction == "->")
return _erase_srcmounts(srcmounts,srcmountslock,srcmounts.back());
else if(instruction == "=")
return _replace_srcmounts(srcmounts,srcmountslock,destmount,values);
else if(instruction.empty())
return _replace_srcmounts(srcmounts,srcmountslock,destmount,values);
return -EINVAL;
}
static
int
_setxattr_policy(const Policy *policies[],
const string attrname,
const string attrval,
const int flags)
{
const Category *cat;
const Policy *policy;
cat = Category::find(attrname);
if(cat == Category::invalid)
return -ENOATTR;
if((flags & XATTR_CREATE) == XATTR_CREATE)
return -EEXIST;
policy = Policy::find(attrval);
if(policy == Policy::invalid)
return -EINVAL;
policies[*cat] = policy;
return 0;
}
static
int
_setxattr_controlfile(config::Config &config,
const string attrname,
const string attrval,
const int flags)
{
vector<string> nameparts;
str::split(nameparts,attrname,'.');
if(nameparts.size() != 3 ||
nameparts[0] != "user" ||
nameparts[1] != "mergerfs")
return -ENOATTR;
if(attrval.empty())
return -EINVAL;
if(nameparts[2] == "srcmounts")
return _setxattr_srcmounts(config.srcmounts,
config.srcmountslock,
config.destmount,
attrval,
flags);
return _setxattr_policy(config.policies,
nameparts[2],
attrval,
flags);
}
static
int
_setxattr(const fs::SearchFunc 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;
fs::PathVector paths;
searchFunc(srcmounts,fusepath,paths);
if(paths.empty())
@@ -59,7 +241,7 @@ _setxattr(const Policy::Action::Func searchFunc,
rv = -1;
error = 0;
for(vector<fs::Path>::const_iterator
for(fs::PathVector::const_iterator
i = paths.begin(), ei = paths.end(); i != ei; ++i)
{
rv &= ::lsetxattr(i->full.c_str(),attrname,attrval,attrvalsize,flags);
@@ -83,19 +265,27 @@ namespace mergerfs
size_t attrvalsize,
int flags)
{
const ugid::SetResetGuard uid;
const config::Config &config = config::get();
const config::Config &config = config::get();
if(fusepath == config.controlfile)
return -ENOTSUP;
return _setxattr_controlfile(config::get_writable(),
attrname,
string(attrval,attrvalsize),
flags);
return _setxattr(config.policy.action,
config.srcmounts,
fusepath,
attrname,
attrval,
attrvalsize,
flags);
{
const struct fuse_context *fc = fuse_get_context();
const ugid::SetResetGuard ugid(fc->uid,fc->gid);
const rwlock::ReadGuard readlock(&config.srcmountslock);
return _setxattr(*config.action,
config.srcmounts,
fusepath,
attrname,
attrval,
attrvalsize,
flags);
}
}
}
}
+20 -44
View File
@@ -34,12 +34,12 @@
#include "ugid.hpp"
#include "config.hpp"
#include "rwlock.hpp"
using std::string;
using std::vector;
using std::map;
using std::pair;
using mergerfs::Policy;
static
void
@@ -48,56 +48,31 @@ _normalize_statvfs(struct statvfs *fsstat,
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;
fsstat->f_blocks = (fsblkcnt_t)((fsstat->f_blocks * fsstat->f_frsize) / min_frsize);
fsstat->f_bfree = (fsblkcnt_t)((fsstat->f_bfree * fsstat->f_frsize) / min_frsize);
fsstat->f_bavail = (fsblkcnt_t)((fsstat->f_bavail * fsstat->f_frsize) / min_frsize);
fsstat->f_bsize = min_bsize;
fsstat->f_frsize = min_frsize;
fsstat->f_namemax = min_namemax;
}
static
void
_merge_statvfs(struct statvfs * const out,
const struct statvfs * const in,
const Policy::StatFS policy)
const struct statvfs * const in)
{
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_blocks += in->f_blocks;
out->f_bfree += in->f_bfree;
out->f_bavail += in->f_bavail;
out->f_files += in->f_files;
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,
_statfs(const vector<string> &srcmounts,
struct statvfs &fsstat)
{
unsigned long min_bsize = ULONG_MAX;
@@ -129,12 +104,12 @@ _statfs(const Policy::StatFS policy,
map<unsigned long,struct statvfs>::iterator endfsstatiter = fsstats.end();
if(fsstatiter != endfsstatiter)
{
fsstat = (fsstatiter++)->second;
fsstat = fsstatiter->second;
_normalize_statvfs(&fsstat,min_bsize,min_frsize,min_namemax);
for(;fsstatiter != endfsstatiter;++fsstatiter)
for(++fsstatiter;fsstatiter != endfsstatiter;++fsstatiter)
{
_normalize_statvfs(&fsstatiter->second,min_bsize,min_frsize,min_namemax);
_merge_statvfs(&fsstat,&fsstatiter->second,policy);
_merge_statvfs(&fsstat,&fsstatiter->second);
}
}
@@ -149,11 +124,12 @@ namespace mergerfs
statfs(const char *fusepath,
struct statvfs *stat)
{
const ugid::SetResetGuard uid;
const struct fuse_context *fc = fuse_get_context();
const config::Config &config = config::get();
const ugid::SetResetGuard ugid(fc->uid,fc->gid);
const rwlock::ReadGuard readlock(&config.srcmountslock);
return _statfs(config.policy.statfs,
config.srcmounts,
return _statfs(config.srcmounts,
*stat);
}
}
+60
View File
@@ -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.
*/
#include <string>
#include <vector>
#include <sstream>
using std::string;
using std::vector;
using std::istringstream;
namespace str
{
void
split(vector<string> &result,
const string str,
const char delimiter)
{
string part;
istringstream ss(str);
while(std::getline(ss,part,delimiter))
result.push_back(part);
}
string
join(const vector<string> &vec,
const char sep)
{
if(vec.empty())
return string();
string rv = vec[0];
for(size_t i = 1; i < vec.size(); i++)
rv += sep + vec[i];
return rv;
}
}
+36
View File
@@ -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 <string>
#include <vector>
namespace str
{
void split(std::vector<std::string> &result,
const std::string str,
const char delimiter);
std::string join(const std::vector<std::string> &vec,
const char sep);
}
+9 -10
View File
@@ -32,6 +32,7 @@
#include "fs.hpp"
#include "config.hpp"
#include "ugid.hpp"
#include "rwlock.hpp"
using std::string;
using std::vector;
@@ -43,15 +44,15 @@ _symlink(const vector<string> &srcmounts,
const string to)
{
int rv;
fs::Path path;
fs::PathVector paths;
path = fs::find_first(srcmounts,fs::dirname(to));
if(path.base.empty())
fs::find::ff(srcmounts,fs::dirname(to),paths);
if(paths.empty())
return -ENOENT;
path.full = fs::make_path(path.base,to);
paths[0].full = fs::make_path(paths[0].base,to);
rv = symlink(from.c_str(),path.full.c_str());
rv = symlink(from.c_str(),paths[0].full.c_str());
return ((rv == -1) ? -errno : 0);
}
@@ -64,12 +65,10 @@ namespace mergerfs
symlink(const char *oldpath,
const char *newpath)
{
const ugid::SetResetGuard uid;
const struct fuse_context *fc = fuse_get_context();
const config::Config &config = config::get();
if(oldpath == config.controlfile ||
newpath == config.controlfile)
return -EPERM;
const ugid::SetResetGuard ugid(fc->uid,fc->gid);
const rwlock::ReadGuard readlock(&config.srcmountslock);
return _symlink(config.srcmounts,
oldpath,
+2
View File
@@ -48,6 +48,8 @@ namespace mergerfs
test(const struct fuse_args &args,
const mergerfs::config::Config &config)
{
return 0;
}
}
+11 -13
View File
@@ -34,22 +34,21 @@
#include "ugid.hpp"
#include "fs.hpp"
#include "config.hpp"
#include "assert.hpp"
#include "rwlock.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)
_truncate(const fs::SearchFunc searchFunc,
const vector<string> &srcmounts,
const string fusepath,
const off_t size)
{
int rv;
int error;
vector<fs::Path> paths;
fs::PathVector paths;
searchFunc(srcmounts,fusepath,paths);
if(paths.empty())
@@ -57,7 +56,7 @@ _truncate(const Policy::Action::Func searchFunc,
rv = -1;
error = 0;
for(vector<fs::Path>::const_iterator
for(fs::PathVector::const_iterator
i = paths.begin(), ei = paths.end(); i != ei; ++i)
{
rv &= ::truncate(i->full.c_str(),size);
@@ -76,13 +75,12 @@ namespace mergerfs
truncate(const char *fusepath,
off_t size)
{
const ugid::SetResetGuard uid;
const struct fuse_context *fc = fuse_get_context();
const config::Config &config = config::get();
const ugid::SetResetGuard ugid(fc->uid,fc->gid);
const rwlock::ReadGuard readlock(&config.srcmountslock);
if(fusepath == config.controlfile)
return -EPERM;
return _truncate(config.policy.action,
return _truncate(*config.action,
config.srcmounts,
fusepath,
size);
+31
View File
@@ -0,0 +1,31 @@
/*
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 "ugid.hpp"
#if defined LINUX
#elif defined OSX
#else
pthread_mutex_t mergerfs::ugid::SetResetGuard::lock = PTHREAD_MUTEX_INITIALIZER;
#endif
+7 -65
View File
@@ -25,70 +25,12 @@
#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;
};
}
}
#if defined LINUX
#include "ugid_linux.hpp"
#elif defined OSX
#include "ugid_osx.hpp"
#else
#include "ugid_mutex.hpp"
#endif
#endif /* __UGID_HPP__ */
+64
View File
@@ -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.
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/syscall.h>
namespace mergerfs
{
namespace ugid
{
struct SetResetGuard
{
SetResetGuard(const uid_t _newuid,
const gid_t _newgid)
{
olduid = ::syscall(SYS_geteuid);
oldgid = ::syscall(SYS_getegid);
newuid = _newuid;
newgid = _newgid;
if(newgid != oldgid)
::syscall(SYS_setregid,-1,newgid);
if(newuid != olduid)
::syscall(SYS_setreuid,-1,newuid);
}
~SetResetGuard()
{
if(olduid != newuid)
::syscall(SYS_setreuid,-1,olduid);
if(oldgid != newgid)
::syscall(SYS_setregid,-1,oldgid);
}
uid_t olduid;
gid_t oldgid;
uid_t newuid;
gid_t newgid;
};
}
}
+68
View File
@@ -0,0 +1,68 @@
/*
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 <pthread.h>
namespace mergerfs
{
namespace ugid
{
struct SetResetGuard
{
SetResetGuard(const uid_t _newuid,
const gid_t _newgid)
{
pthread_mutex_lock(&lock);
olduid = ::geteuid();
oldgid = ::getegid();
newuid = _newuid;
newgid = _newgid;
if(newgid != oldgid)
setegid(newgid);
if(newuid != olduid)
seteuid(newuid);
}
~SetResetGuard()
{
if(olduid != newuid)
seteuid(newuid);
if(oldgid != newgid)
setegid(newgid);
}
uid_t olduid;
gid_t oldgid;
uid_t newuid;
gid_t newgid;
static pthread_mutex_t lock;
};
}
}
+58
View File
@@ -0,0 +1,58 @@
/*
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/unistd.h>
namespace mergerfs
{
namespace ugid
{
struct SetResetGuard
{
SetResetGuard(const uid_t _newuid,
const gid_t _newgid)
{
olduid = ::geteuid();
oldgid = ::getegid();
newuid = _newuid;
newgid = _newgid;
if(newgid != oldgid || newuid != olduid)
pthread_setugid_np(newuid,newgid);
}
~SetResetGuard()
{
if(newgid != oldgid || newuid != olduid)
pthread_setugid_np(newuid,newgid);
}
uid_t olduid;
gid_t oldgid;
uid_t newuid;
gid_t newgid;
};
}
}
+12 -12
View File
@@ -22,6 +22,8 @@
THE SOFTWARE.
*/
#include <fuse.h>
#include <errno.h>
#include <unistd.h>
@@ -31,21 +33,20 @@
#include "ugid.hpp"
#include "fs.hpp"
#include "config.hpp"
#include "assert.hpp"
#include "rwlock.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)
_unlink(const fs::SearchFunc searchFunc,
const vector<string> &srcmounts,
const string fusepath)
{
int rv;
int error;
vector<fs::Path> paths;
fs::PathVector paths;
searchFunc(srcmounts,fusepath,paths);
if(paths.empty())
@@ -53,7 +54,7 @@ _unlink(const Policy::Action::Func searchFunc,
rv = -1;
error = 0;
for(vector<fs::Path>::const_iterator
for(fs::PathVector::const_iterator
i = paths.begin(), ei = paths.end(); i != ei; ++i)
{
rv &= ::unlink(i->full.c_str());
@@ -71,13 +72,12 @@ namespace mergerfs
int
unlink(const char *fusepath)
{
const ugid::SetResetGuard ugid;
const struct fuse_context *fc = fuse_get_context();
const config::Config &config = config::get();
const ugid::SetResetGuard ugid(fc->uid,fc->gid);
const rwlock::ReadGuard readlock(&config.srcmountslock);
if(fusepath == config.controlfile)
return -EPERM;
return _unlink(config.policy.action,
return _unlink(*config.action,
config.srcmounts,
fusepath);
}
+16 -15
View File
@@ -22,8 +22,11 @@
THE SOFTWARE.
*/
#include <fuse.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <string>
#include <vector>
@@ -31,22 +34,21 @@
#include "ugid.hpp"
#include "fs.hpp"
#include "config.hpp"
#include "assert.hpp"
#include "rwlock.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])
_utimens(const fs::SearchFunc searchFunc,
const vector<string> &srcmounts,
const string fusepath,
const struct timespec ts[2])
{
int rv;
int error;
vector<fs::Path> paths;
fs::PathVector paths;
searchFunc(srcmounts,fusepath,paths);
if(paths.empty())
@@ -54,7 +56,7 @@ _utimens(const Policy::Action::Func searchFunc,
rv = -1;
error = 0;
for(vector<fs::Path>::const_iterator
for(fs::PathVector::const_iterator
i = paths.begin(), ei = paths.end(); i != ei; ++i)
{
rv &= ::utimensat(0,i->full.c_str(),ts,AT_SYMLINK_NOFOLLOW);
@@ -73,13 +75,12 @@ namespace mergerfs
utimens(const char *fusepath,
const struct timespec ts[2])
{
ugid::SetResetGuard ugid;
const config::Config &config = config::get();
const struct fuse_context *fc = fuse_get_context();
const config::Config &config = config::get();
const ugid::SetResetGuard ugid(fc->uid,fc->gid);
const rwlock::ReadGuard readlock(&config.srcmountslock);
if(fusepath == config.controlfile)
return -EPERM;
return _utimens(config.policy.action,
return _utimens(*config.action,
config.srcmounts,
fusepath,
ts);
+2 -102
View File
@@ -27,99 +27,8 @@
#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,
@@ -143,18 +52,9 @@ namespace mergerfs
const char *buf,
size_t count,
off_t offset,
struct fuse_file_info *fi)
struct fuse_file_info *ffi)
{
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,
return _write(((FileInfo*)ffi->fh)->fd,
buf,
count,
offset);
+65
View File
@@ -0,0 +1,65 @@
/*
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 <stdlib.h>
#include "fileinfo.hpp"
#include "write.hpp"
static
int
_write_buf(const int fd,
struct fuse_bufvec &src,
const off_t offset)
{
size_t size = fuse_buf_size(&src);
struct fuse_bufvec dst = FUSE_BUFVEC_INIT(size);
const fuse_buf_copy_flags cpflags =
(fuse_buf_copy_flags)(FUSE_BUF_SPLICE_MOVE|FUSE_BUF_SPLICE_NONBLOCK);
dst.buf->flags = (fuse_buf_flags)(FUSE_BUF_IS_FD|FUSE_BUF_FD_SEEK);
dst.buf->fd = fd;
dst.buf->pos = offset;
return fuse_buf_copy(&dst,&src,cpflags);
}
namespace mergerfs
{
namespace write_buf
{
int
write_buf(const char *fusepath,
struct fuse_bufvec *src,
off_t offset,
struct fuse_file_info *ffi)
{
return _write_buf(((FileInfo*)ffi->fh)->fd,
*src,
offset);
}
}
}
+35
View File
@@ -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 write_buf
{
int
write_buf(const char *fusepath,
struct fuse_bufvec *buf,
off_t offset,
struct fuse_file_info *fi);
}
}
+39
View File
@@ -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.
*/
#ifndef WITHOUT_XATTR
#include <attr/xattr.h>
#endif
#ifndef ENOATTR
#define ENOATTR ENODATA
#endif
#ifndef XATTR_CREATE
#define XATTR_CREATE 0x1
#endif
#ifndef XATTR_REPLACE
#define XATTR_REPLACE 0x2
#endif