public void copyToWithPermission(FilePath target) throws IOException, InterruptedException {
        // Use NIO copy with StandardCopyOption.COPY_ATTRIBUTES when copying on the same machine.
        if (this.channel == target.channel) {
            act(new CopyToWithPermission(target));
            return;
        }

        copyTo(target);
        // copy file permission
        target.chmod(mode());
        target.setLastModifiedIfPossible(lastModified());
    }
@Test public void copyToWithPermission() throws IOException, InterruptedException {
        File tmp = temp.getRoot();
            File child = new File(tmp, "child");
            FilePath childP = new FilePath(child);
            childP.touch(4711);

            Chmod chmodTask = new Chmod();
            chmodTask.setProject(new Project());
            chmodTask.setFile(child);
            chmodTask.setPerm("0400");
            chmodTask.execute();

            FilePath copy = new FilePath(channels.british, tmp.getPath()).child("copy");
            childP.copyToWithPermission(copy);

            assertEquals(childP.mode(), copy.mode());
            if (!Functions.isWindows()) {
                assertEquals(childP.lastModified(), copy.lastModified());
            }

            // JENKINS-11073:
            // Windows seems to have random failures when setting the timestamp on newly generated
            // files. So test that:
            for (int i = 0; i < 100; i++) {
                copy = new FilePath(channels.british, tmp.getPath()).child("copy" + i);
                childP.copyToWithPermission(copy);
            }
    }