I am trying to set the mod of a new file to match the old one. My Qt5/C++ codes is roughly:
stat(oldFileName.toLatin1().data(), &sb);
bool success = chmod(newFileName.toLatin1().data(),sb.st_mode)
The sb.st_mode value at the time of chmod being called is 33261 (octal 100755). The chmod() call fails and sets errno to 13 (permission denied). yet the program is running as uid 0 gid 0.
UPDATE: Looks like stat() also includes file TYPE information in the higher bits I need to remove. I tried to use (sb.st_mode & 0x1FF which yields 0755) as the value for chmod but still fails.
Interestingly chmod() and chown() return 0 for success, -1 for failure. Which is the opposite of C/C++ standard (0 for false, non-0 for true).
So you have to reverse the function return value to use as a boolean.