-+ len = snprintf(buf, sizeof buf, "%o %u,%u %u:%u",
-+ (int)st->st_mode,
-+ (int)major(st->st_rdev), (int)minor(st->st_rdev),
-+ (int)st->st_uid, (int)st->st_gid);
-+ return sys_lsetxattr(fname, FAKE_XATTR, buf, len, 0);
++
++ am_root = 2; /* get real stat() w/o xattr overlay */
++ do_stat(fname, &fst);
++ am_root = -1;
++ have_xattr = get_stat_xattr(fname, &xst) == 0;
++
++ if (IS_DEVICE(file->mode) || IS_SPECIAL(file->mode))
++ rdev = file->u.rdev;
++ else
++ rdev = 0;
++ if (!IS_DEVICE(fst.st_mode) && !IS_SPECIAL(fst.st_mode))
++ fst.st_rdev = 0; /* just in case */
++
++ if (fst.st_mode == file->mode && fst.st_rdev == rdev
++ && fst.st_uid == file->uid && fst.st_gid == file->gid) {
++ if (have_xattr && sys_lremovexattr(fname, FAKE_XATTR) < 0)
++ return -2;
++ return 0;
++ }
++
++ if (!have_xattr
++ || xst.st_mode != file->mode || xst.st_rdev != rdev
++ || xst.st_uid != file->uid || xst.st_gid != file->gid) {
++ char buf[256];
++ int len = snprintf(buf, sizeof buf, "%o %u,%u %u:%u",
++ (int)file->mode,
++ (int)major(rdev), (int)minor(rdev),
++ (int)file->uid, (int)file->gid);
++ return sys_lsetxattr(fname, FAKE_XATTR, buf, len, 0);
++ }
++ return 0;