fix: keep temp file names within short NAME_MAX limits

- fs_mktemp computed the prefix length as name_max - 6 - 2 in size_t.
  Below a NAME_MAX of 8 that wrapped, so the whole source file name
  was used and every temp file name was too long. moveonenospc and
  copy-on-write always failed on such a branch.
- Shorten the random suffix when the limit is that small and never
  let the prefix length go negative. Names on normal filesystems are
  unchanged.
This commit is contained in:
Antonio SJ Musumeci 2026-11-21 14:35:48 -05:00
parent fbca508a31
commit de2dad1a88

View file

@ -44,6 +44,7 @@ _generate_tmp_path(const fs::path &dirpath_,
const std::string &src_filename_)
{
long name_max;
long pad_len;
size_t substr_len;
std::string tmp_filename;
@ -51,13 +52,16 @@ _generate_tmp_path(const fs::path &dirpath_,
if(name_max == -1)
name_max = NAME_MAX;
// The name is '.' + prefix + '_' + random suffix. Where names are
// too short for the full suffix, shorten it so the name still fits.
pad_len = std::clamp(name_max - 2L,1L,(long)PAD_LEN);
substr_len = std::min(src_filename_.size(),
(size_t)(name_max - PAD_LEN - 2ULL));
(size_t)std::max(name_max - 2L - pad_len,0L));
tmp_filename = '.';
tmp_filename += src_filename_.substr(0,substr_len);
tmp_filename += '_';
for(size_t i = 0; i < PAD_LEN; i++)
for(long i = 0; i < pad_len; i++)
tmp_filename += CHARS[RND::rand64(CHARS_SIZE)];
return dirpath_ / tmp_filename;