2014-05-12 12:41:46 -04:00
|
|
|
/*
|
2016-01-14 16:50:22 -05:00
|
|
|
Copyright (c) 2016, Antonio SJ Musumeci <trapexit@spawn.link>
|
|
|
|
|
|
|
|
|
|
Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
|
purpose with or without fee is hereby granted, provided that the above
|
|
|
|
|
copyright notice and this permission notice appear in all copies.
|
|
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
|
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
|
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
|
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
|
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
|
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
|
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
2014-05-12 12:41:46 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <fuse.h>
|
|
|
|
|
|
2015-09-15 18:49:18 -04:00
|
|
|
#include "config.hpp"
|
2016-09-14 08:36:06 -04:00
|
|
|
#include "errno.hpp"
|
2015-09-15 18:49:18 -04:00
|
|
|
#include "fileinfo.hpp"
|
2016-10-19 09:28:06 -04:00
|
|
|
#include "fs_base_write.hpp"
|
2015-09-15 18:49:18 -04:00
|
|
|
#include "fs_movefile.hpp"
|
|
|
|
|
#include "rwlock.hpp"
|
|
|
|
|
#include "ugid.hpp"
|
|
|
|
|
|
2016-12-20 17:06:34 -05:00
|
|
|
using namespace mergerfs;
|
|
|
|
|
|
|
|
|
|
typedef int (*WriteFunc)(const int,const void*,const size_t,const off_t);
|
|
|
|
|
|
2016-10-06 13:54:57 -04:00
|
|
|
static
|
|
|
|
|
bool
|
|
|
|
|
_out_of_space(const int error)
|
|
|
|
|
{
|
|
|
|
|
return ((error == ENOSPC) ||
|
|
|
|
|
(error == EDQUOT));
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-12 12:41:46 -04:00
|
|
|
static
|
2016-12-20 17:06:34 -05:00
|
|
|
inline
|
2014-05-12 12:41:46 -04:00
|
|
|
int
|
|
|
|
|
_write(const int fd,
|
|
|
|
|
const void *buf,
|
|
|
|
|
const size_t count,
|
|
|
|
|
const off_t offset)
|
|
|
|
|
{
|
|
|
|
|
int rv;
|
|
|
|
|
|
2016-10-19 09:28:06 -04:00
|
|
|
rv = fs::pwrite(fd,buf,count,offset);
|
2016-12-20 17:06:34 -05:00
|
|
|
if(rv == -1)
|
|
|
|
|
return -errno;
|
|
|
|
|
if(rv == 0)
|
|
|
|
|
return 0;
|
2014-05-12 12:41:46 -04:00
|
|
|
|
2016-12-20 17:06:34 -05:00
|
|
|
return count;
|
2014-05-12 12:41:46 -04:00
|
|
|
}
|
|
|
|
|
|
2016-12-20 17:06:34 -05:00
|
|
|
static
|
|
|
|
|
inline
|
|
|
|
|
int
|
|
|
|
|
_write_direct_io(const int fd,
|
|
|
|
|
const void *buf,
|
|
|
|
|
const size_t count,
|
|
|
|
|
const off_t offset)
|
|
|
|
|
{
|
|
|
|
|
int rv;
|
|
|
|
|
|
|
|
|
|
rv = fs::pwrite(fd,buf,count,offset);
|
|
|
|
|
if(rv == -1)
|
|
|
|
|
return -errno;
|
|
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-12 12:41:46 -04:00
|
|
|
namespace mergerfs
|
|
|
|
|
{
|
2015-07-15 10:04:57 -04:00
|
|
|
namespace fuse
|
2014-05-12 12:41:46 -04:00
|
|
|
{
|
2016-12-20 17:06:34 -05:00
|
|
|
static
|
|
|
|
|
inline
|
2014-05-12 12:41:46 -04:00
|
|
|
int
|
2016-12-20 17:06:34 -05:00
|
|
|
write(WriteFunc func,
|
2015-07-21 14:19:17 -04:00
|
|
|
const char *buf,
|
2016-12-20 17:06:34 -05:00
|
|
|
const size_t count,
|
|
|
|
|
const off_t offset,
|
2015-07-21 14:19:17 -04:00
|
|
|
fuse_file_info *ffi)
|
2014-05-12 12:41:46 -04:00
|
|
|
{
|
2015-09-15 18:49:18 -04:00
|
|
|
int rv;
|
|
|
|
|
FileInfo* fi = reinterpret_cast<FileInfo*>(ffi->fh);
|
|
|
|
|
|
2016-12-20 17:06:34 -05:00
|
|
|
rv = func(fi->fd,buf,count,offset);
|
2016-10-06 13:54:57 -04:00
|
|
|
if(_out_of_space(-rv))
|
2015-09-15 18:49:18 -04:00
|
|
|
{
|
|
|
|
|
const fuse_context *fc = fuse_get_context();
|
|
|
|
|
const Config &config = Config::get(fc);
|
|
|
|
|
|
|
|
|
|
if(config.moveonenospc)
|
|
|
|
|
{
|
|
|
|
|
const ugid::Set ugid(0,0);
|
|
|
|
|
const rwlock::ReadGuard readlock(&config.srcmountslock);
|
|
|
|
|
|
2017-04-11 08:53:46 -04:00
|
|
|
rv = fs::movefile(config.srcmounts,fi->fusepath,count,fi->fd);
|
2015-09-15 18:49:18 -04:00
|
|
|
if(rv == -1)
|
|
|
|
|
return -ENOSPC;
|
|
|
|
|
|
2016-12-20 17:06:34 -05:00
|
|
|
rv = func(fi->fd,buf,count,offset);
|
2015-09-15 18:49:18 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return rv;
|
2014-05-12 12:41:46 -04:00
|
|
|
}
|
2016-12-20 17:06:34 -05:00
|
|
|
|
|
|
|
|
int
|
|
|
|
|
write(const char *fusepath,
|
|
|
|
|
const char *buf,
|
|
|
|
|
size_t count,
|
|
|
|
|
off_t offset,
|
|
|
|
|
fuse_file_info *ffi)
|
|
|
|
|
{
|
2017-04-11 08:53:46 -04:00
|
|
|
return write(_write,buf,count,offset,ffi);
|
2016-12-20 17:06:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
write_direct_io(const char *fusepath,
|
|
|
|
|
const char *buf,
|
|
|
|
|
size_t count,
|
|
|
|
|
off_t offset,
|
|
|
|
|
fuse_file_info *ffi)
|
|
|
|
|
{
|
2017-04-11 08:53:46 -04:00
|
|
|
return write(_write_direct_io,buf,count,offset,ffi);
|
2016-12-20 17:06:34 -05:00
|
|
|
}
|
2017-05-26 16:51:30 -04:00
|
|
|
|
|
|
|
|
int
|
|
|
|
|
write_null(const char *fusepath,
|
|
|
|
|
const char *buf,
|
|
|
|
|
size_t count,
|
|
|
|
|
off_t offset,
|
|
|
|
|
fuse_file_info *ffi)
|
|
|
|
|
{
|
|
|
|
|
return count;
|
|
|
|
|
}
|
2014-05-12 12:41:46 -04:00
|
|
|
}
|
|
|
|
|
}
|