You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by ol...@apache.org on 2020/05/16 23:55:14 UTC
[maven-shared-utils] branch master updated: [MRESOURCES-236]
Attempt to copy file permissions when copying files (#29)
This is an automated email from the ASF dual-hosted git repository.
olamy pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-shared-utils.git
The following commit(s) were added to refs/heads/master by this push:
new 77ff09b [MRESOURCES-236] Attempt to copy file permissions when copying files (#29)
77ff09b is described below
commit 77ff09b0ee9732fe203f4c58b45f6c1e00d92f0d
Author: Rob Oxspring <ro...@imapmail.org>
AuthorDate: Sun May 17 00:55:09 2020 +0100
[MRESOURCES-236] Attempt to copy file permissions when copying files (#29)
---
.../apache/maven/shared/utils/io/FileUtils.java | 34 ++++++++++++++++++++++
.../maven/shared/utils/io/FileUtilsTest.java | 14 +++++++++
2 files changed, 48 insertions(+)
diff --git a/src/main/java/org/apache/maven/shared/utils/io/FileUtils.java b/src/main/java/org/apache/maven/shared/utils/io/FileUtils.java
index f7de64c..895cf44 100644
--- a/src/main/java/org/apache/maven/shared/utils/io/FileUtils.java
+++ b/src/main/java/org/apache/maven/shared/utils/io/FileUtils.java
@@ -819,6 +819,8 @@ public class FileUtils
pos += output.transferFrom( input, pos, count );
}
}
+
+ copyFilePermissions( source, destination );
}
/**
@@ -1840,6 +1842,38 @@ public class FileUtils
copyFile( from, to );
}
}
+
+ copyFilePermissions( from, to );
+ }
+
+ /**
+ * Attempts to copy file permissions from the source to the destination file.
+ * Initially attempts to copy posix file permissions, assuming that the files are both on posix filesystems.
+ * If the initial attempts fail then a second attempt using less precise permissions model.
+ * Note that permissions are copied on a best-efforts basis,
+ * failure to copy permissions will not result in an exception.
+ *
+ * @param source the file to copy permissions from.
+ * @param destination the file to copy permissions to.
+ */
+ private static void copyFilePermissions( @Nonnull File source, @Nonnull File destination )
+ throws IOException
+ {
+ try
+ {
+ // attempt to copy posix file permissions
+ Files.setPosixFilePermissions(
+ destination.toPath(),
+ Files.getPosixFilePermissions( source.toPath() )
+ );
+ }
+ catch ( UnsupportedOperationException e )
+ {
+ // fallback to setting partial permissions
+ destination.setExecutable( source.canExecute() );
+ destination.setReadable( source.canRead() );
+ destination.setWritable( source.canWrite() );
+ }
}
/**
diff --git a/src/test/java/org/apache/maven/shared/utils/io/FileUtilsTest.java b/src/test/java/org/apache/maven/shared/utils/io/FileUtilsTest.java
index 69e72a9..b5f17e6 100644
--- a/src/test/java/org/apache/maven/shared/utils/io/FileUtilsTest.java
+++ b/src/test/java/org/apache/maven/shared/utils/io/FileUtilsTest.java
@@ -494,6 +494,20 @@ public class FileUtilsTest
}
@Test
+ public void copyFileWithPermissions()
+ throws Exception
+ {
+ File source = new File( "/bin/sh" );
+ assumeThat( "Need an executable to copy", source.exists(), is( true ) );
+
+ File destination = new File( tempFolder.getRoot(), "executable" );
+
+ FileUtils.copyFile( source, destination );
+
+ assertThat( "Check executable", destination.canExecute(), is( true ) );
+ }
+
+ @Test
public void copyFile2()
throws Exception
{