Move everything to negative errno return types (#1499)
Some checks failed
CodeQL / Analyze (push) Has been cancelled
mkdocs / deploy (push) Has been cancelled

This commit is contained in:
trapexit 2025-07-25 17:53:48 -05:00 • committed by GitHub
parent 2b500f194a
commit e8cd74cab0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
177 changed files with 5952 additions and 5843 deletions

View file

@ -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_);
}