+/* Use fstat if possible, fstatat otherwise. */
+static int
+fd_stat (int fd, char const *file, struct stat *st, int atflag)
+{
+ return (0 <= fd
+ ? fstat (fd, st)
+ : fstatat (chdir_fd, file, st, atflag));
+}
+
+/* Set the mode for FILE_NAME to MODE.
+ MODE_MASK specifies the bits of MODE that we care about;
+ thus if MODE_MASK is zero, do nothing.
+ If FD is nonnegative, it is a file descriptor for the file.
+ CURRENT_MODE and CURRENT_MODE_MASK specify information known about
+ the file's current mode, using the style of struct delayed_set_stat.
+ TYPEFLAG specifies the type of the file.
+ ATFLAG specifies the flag to use when statting the file. */
+static void
+set_mode (char const *file_name,
+ mode_t mode, mode_t mode_mask, int fd,
+ mode_t current_mode, mode_t current_mode_mask,
+ char typeflag, int atflag)
+{
+ if (((current_mode ^ mode) | ~ current_mode_mask) & mode_mask)
+ {
+ if (MODE_ALL & ~ mode_mask & ~ current_mode_mask)