mirror of
https://github.com/trapexit/mergerfs.git
synced 2026-10-26 19:01:56 -04:00
Move everything to negative errno return types (#1499)
This commit is contained in:
parent
2b500f194a
commit
e8cd74cab0
177 changed files with 5952 additions and 5843 deletions
|
|
@ -16,6 +16,8 @@
|
|||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "fs_mktemp.hpp"
|
||||
|
||||
#include "errno.hpp"
|
||||
#include "fs_open.hpp"
|
||||
#include "fs_path.hpp"
|
||||
|
|
@ -36,77 +38,70 @@ static char const CHARS[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmno
|
|||
static size_t const CHARS_SIZE = (sizeof(CHARS) - 1);
|
||||
|
||||
|
||||
namespace l
|
||||
static
|
||||
std::string
|
||||
_generate_tmp_path(const std::string &dirpath_,
|
||||
const std::string &filename_)
|
||||
{
|
||||
static
|
||||
std::string
|
||||
generate_tmp_path(const std::string &dirpath_,
|
||||
const std::string &filename_)
|
||||
{
|
||||
long name_max;
|
||||
size_t substr_len;
|
||||
fs::Path path;
|
||||
std::string filename;
|
||||
long name_max;
|
||||
size_t substr_len;
|
||||
fs::Path path;
|
||||
std::string filename;
|
||||
|
||||
name_max = pathconf(dirpath_.c_str(),_PC_NAME_MAX);
|
||||
if(name_max == -1)
|
||||
name_max = NAME_MAX;
|
||||
name_max = ::pathconf(dirpath_.c_str(),_PC_NAME_MAX);
|
||||
if(name_max == -1)
|
||||
name_max = NAME_MAX;
|
||||
|
||||
substr_len = std::min(filename_.size(),
|
||||
(size_t)(name_max - PAD_LEN - 2ULL));
|
||||
substr_len = std::min(filename_.size(),
|
||||
(size_t)(name_max - PAD_LEN - 2ULL));
|
||||
|
||||
filename = '.';
|
||||
filename += filename_.substr(0,substr_len);
|
||||
filename += '_';
|
||||
for(size_t i = 0; i < PAD_LEN; i++)
|
||||
filename += CHARS[RND::rand64(CHARS_SIZE)];
|
||||
filename = '.';
|
||||
filename += filename_.substr(0,substr_len);
|
||||
filename += '_';
|
||||
for(size_t i = 0; i < PAD_LEN; i++)
|
||||
filename += CHARS[RND::rand64(CHARS_SIZE)];
|
||||
|
||||
path = dirpath_;
|
||||
path /= filename;
|
||||
path = dirpath_;
|
||||
path /= filename;
|
||||
|
||||
return path.string();
|
||||
}
|
||||
return path.string();
|
||||
}
|
||||
|
||||
namespace fs
|
||||
std::tuple<int,std::string>
|
||||
fs::mktemp_in_dir(const std::string &dirpath_,
|
||||
const std::string &filename_,
|
||||
const int flags_)
|
||||
{
|
||||
std::tuple<int,std::string>
|
||||
mktemp_in_dir(const std::string &dirpath_,
|
||||
const std::string &filename_,
|
||||
const int flags_)
|
||||
{
|
||||
int fd;
|
||||
int count;
|
||||
int flags;
|
||||
std::string tmp_filepath;
|
||||
int fd;
|
||||
int count;
|
||||
int flags;
|
||||
std::string tmp_filepath;
|
||||
|
||||
fd = -1;
|
||||
count = MAX_ATTEMPTS;
|
||||
flags = (flags_ | O_EXCL | O_CREAT);
|
||||
while(count-- > 0)
|
||||
{
|
||||
tmp_filepath = l::generate_tmp_path(dirpath_,filename_);
|
||||
count = MAX_ATTEMPTS;
|
||||
flags = (flags_ | O_EXCL | O_CREAT);
|
||||
while(count-- > 0)
|
||||
{
|
||||
tmp_filepath = ::_generate_tmp_path(dirpath_,filename_);
|
||||
|
||||
fd = fs::open(tmp_filepath,flags,S_IRUSR|S_IWUSR);
|
||||
if((fd == -1) && (errno == EEXIST))
|
||||
continue;
|
||||
if(fd == -1)
|
||||
return std::make_tuple(-errno,std::string());
|
||||
fd = fs::open(tmp_filepath,flags,S_IRUSR|S_IWUSR);
|
||||
if(fd == -EEXIST)
|
||||
continue;
|
||||
if(fd < 0)
|
||||
return std::make_tuple(fd,std::string());
|
||||
|
||||
return std::make_tuple(fd,tmp_filepath);
|
||||
}
|
||||
return std::make_tuple(fd,tmp_filepath);
|
||||
}
|
||||
|
||||
return std::make_tuple(-EEXIST,std::string());
|
||||
}
|
||||
|
||||
std::tuple<int,std::string>
|
||||
mktemp(const std::string &filepath_,
|
||||
const int flags_)
|
||||
{
|
||||
fs::Path filepath{filepath_};
|
||||
|
||||
return fs::mktemp_in_dir(filepath.parent_path(),
|
||||
filepath.filename(),
|
||||
flags_);
|
||||
}
|
||||
return std::make_tuple(-EEXIST,std::string());
|
||||
}
|
||||
|
||||
std::tuple<int,std::string>
|
||||
fs::mktemp(const std::string &filepath_,
|
||||
const int flags_)
|
||||
{
|
||||
fs::Path filepath{filepath_};
|
||||
|
||||
return fs::mktemp_in_dir(filepath.parent_path(),
|
||||
filepath.filename(),
|
||||
flags_);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue