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
|
|
|
*/
|
|
|
|
|
|
2025-07-25 17:53:48 -05:00
|
|
|
#include "fuse_release.hpp"
|
|
|
|
|
|
2025-06-05 23:47:42 -05:00
|
|
|
#include "state.hpp"
|
|
|
|
|
|
2017-02-18 13:45:43 -05:00
|
|
|
#include "config.hpp"
|
2015-09-15 18:49:18 -04:00
|
|
|
#include "fileinfo.hpp"
|
2020-08-17 16:29:22 -04:00
|
|
|
#include "fs_close.hpp"
|
|
|
|
|
#include "fs_fadvise.hpp"
|
2025-06-05 23:47:42 -05:00
|
|
|
#include "fuse_passthrough.hpp"
|
2015-09-15 18:49:18 -04:00
|
|
|
|
2020-12-19 15:06:08 -05:00
|
|
|
#include "fuse.h"
|
2019-01-06 12:52:55 -05:00
|
|
|
|
2025-06-05 23:47:42 -05:00
|
|
|
static
|
|
|
|
|
constexpr
|
|
|
|
|
auto
|
|
|
|
|
_erase_if_lambda(const fuse_context *fc_,
|
|
|
|
|
FileInfo *fi_,
|
|
|
|
|
bool *existed_in_map_)
|
|
|
|
|
{
|
|
|
|
|
return
|
|
|
|
|
[=](auto &val_)
|
|
|
|
|
{
|
|
|
|
|
*existed_in_map_ = true;
|
2019-01-06 12:52:55 -05:00
|
|
|
|
2025-06-05 23:47:42 -05:00
|
|
|
if(fi_ != val_.second.fi)
|
|
|
|
|
{
|
|
|
|
|
fs::close(fi_->fd);
|
|
|
|
|
delete fi_;
|
|
|
|
|
}
|
2020-12-19 15:06:08 -05:00
|
|
|
|
2025-06-05 23:47:42 -05:00
|
|
|
val_.second.ref_count--;
|
|
|
|
|
if(val_.second.ref_count > 0)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
if(val_.second.backing_id > 0)
|
|
|
|
|
FUSE::passthrough_close(fc_,val_.second.backing_id);
|
|
|
|
|
fs::close(val_.second.fi->fd);
|
|
|
|
|
delete val_.second.fi;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
};
|
|
|
|
|
}
|
2017-02-18 13:45:43 -05:00
|
|
|
|
2025-06-05 23:47:42 -05:00
|
|
|
static
|
|
|
|
|
int
|
|
|
|
|
_release(const fuse_context *fc_,
|
|
|
|
|
FileInfo *fi_,
|
|
|
|
|
const bool dropcacheonclose_)
|
|
|
|
|
{
|
|
|
|
|
bool existed_in_map;
|
2014-05-12 12:41:46 -04:00
|
|
|
|
2025-06-05 23:47:42 -05:00
|
|
|
// according to Feh of nocache calling it once doesn't always work
|
|
|
|
|
// https://github.com/Feh/nocache
|
|
|
|
|
if(dropcacheonclose_)
|
|
|
|
|
{
|
|
|
|
|
fs::fadvise_dontneed(fi_->fd);
|
|
|
|
|
fs::fadvise_dontneed(fi_->fd);
|
|
|
|
|
}
|
2014-05-12 12:41:46 -04:00
|
|
|
|
2025-06-05 23:47:42 -05:00
|
|
|
// Because of course the API doesn't tell you if the key
|
|
|
|
|
// existed. Just how many it erased and in this case I only want to
|
|
|
|
|
// erase if there are no more open files.
|
|
|
|
|
existed_in_map = false;
|
|
|
|
|
state.open_files.erase_if(fc_->nodeid,
|
|
|
|
|
::_erase_if_lambda(fc_,fi_,&existed_in_map));
|
|
|
|
|
if(existed_in_map)
|
2019-01-06 12:52:55 -05:00
|
|
|
return 0;
|
2025-06-05 23:47:42 -05:00
|
|
|
|
|
|
|
|
fs::close(fi_->fd);
|
|
|
|
|
delete fi_;
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static
|
|
|
|
|
int
|
|
|
|
|
_release(const fuse_context *fc_,
|
|
|
|
|
const fuse_file_info_t *ffi_)
|
|
|
|
|
{
|
|
|
|
|
FileInfo *fi = reinterpret_cast<FileInfo*>(ffi_->fh);
|
|
|
|
|
|
2025-09-06 12:03:51 -04:00
|
|
|
return ::_release(fc_,fi,cfg.dropcacheonclose);
|
2014-05-12 12:41:46 -04:00
|
|
|
}
|
|
|
|
|
|
2025-07-25 17:53:48 -05:00
|
|
|
int
|
|
|
|
|
FUSE::release(const fuse_file_info_t *ffi_)
|
2014-05-12 12:41:46 -04:00
|
|
|
{
|
2025-07-25 17:53:48 -05:00
|
|
|
const fuse_context *fc = fuse_get_context();
|
2019-01-06 12:52:55 -05:00
|
|
|
|
2025-07-25 17:53:48 -05:00
|
|
|
return ::_release(fc,ffi_);
|
2014-05-12 12:41:46 -04:00
|
|
|
}
|