diff --git a/src/main/groovy/com/netflix/gradle/plugins/daemon/OspackageDaemonPlugin.groovy b/src/main/groovy/com/netflix/gradle/plugins/daemon/OspackageDaemonPlugin.groovy index 19a69d83..6860dc67 100644 --- a/src/main/groovy/com/netflix/gradle/plugins/daemon/OspackageDaemonPlugin.groovy +++ b/src/main/groovy/com/netflix/gradle/plugins/daemon/OspackageDaemonPlugin.groovy @@ -19,6 +19,7 @@ package com.netflix.gradle.plugins.daemon import com.netflix.gradle.plugins.packaging.SystemPackagingBasePlugin import com.netflix.gradle.plugins.packaging.SystemPackagingTask import com.netflix.gradle.plugins.rpm.Rpm +import com.netflix.gradle.plugins.utils.FilePermissionUtil import com.netflix.gradle.plugins.utils.WrapUtil import groovy.text.GStringTemplateEngine import groovy.transform.CompileDynamic @@ -136,7 +137,7 @@ class OspackageDaemonPlugin implements Plugin { task.from(rendered) { into(destDir) rename('.*', destFile) - fileMode 0555 // Since source files don't have the correct permissions + FilePermissionUtil.setFilePermission(it, 0555) user 'root' } } diff --git a/src/main/groovy/com/netflix/gradle/plugins/deb/DebCopyAction.groovy b/src/main/groovy/com/netflix/gradle/plugins/deb/DebCopyAction.groovy index fd79615f..ba4bf627 100755 --- a/src/main/groovy/com/netflix/gradle/plugins/deb/DebCopyAction.groovy +++ b/src/main/groovy/com/netflix/gradle/plugins/deb/DebCopyAction.groovy @@ -24,12 +24,14 @@ import com.netflix.gradle.plugins.packaging.Directory import com.netflix.gradle.plugins.packaging.Link import com.netflix.gradle.plugins.utils.ApacheCommonsFileSystemActions import com.netflix.gradle.plugins.utils.DeprecationLoggerUtils +import com.netflix.gradle.plugins.utils.FilePermissionUtil import groovy.transform.Canonical import org.apache.commons.lang3.StringUtils import org.apache.commons.lang3.time.DateFormatUtils import org.gradle.api.GradleException import org.gradle.api.internal.file.copy.CopyAction import org.gradle.api.internal.file.copy.FileCopyDetailsInternal +import org.gradle.util.GradleVersion import org.slf4j.Logger import org.slf4j.LoggerFactory import org.vafer.jdeb.Compression @@ -119,7 +121,7 @@ class DebCopyAction extends AbstractPackagingCopyAction { String group = lookup(specToLookAt, 'permissionGroup') ?: task.permissionGroup Integer gid = (Integer) lookup(specToLookAt, 'gid') ?: task.gid ?: 0 - int fileMode = fileDetails.mode + int fileMode = FilePermissionUtil.getUnixPermission(fileDetails) debFileVisitorStrategy.addFile(fileDetails, inputFile, user, uid, group, gid, fileMode) } @@ -137,7 +139,8 @@ class DebCopyAction extends AbstractPackagingCopyAction { Integer gid = (Integer) lookup(specToLookAt, 'gid') ?: task.gid ?: 0 Boolean setgid = lookup(specToLookAt, 'setgid') ?: task.setgid - int fileMode = dirDetails.mode + int fileMode = FilePermissionUtil.getUnixPermission(dirDetails) + if (setgid) { fileMode = fileMode | 02000 } diff --git a/src/main/groovy/com/netflix/gradle/plugins/packaging/ProjectPackagingExtension.groovy b/src/main/groovy/com/netflix/gradle/plugins/packaging/ProjectPackagingExtension.groovy index 13617f5a..df8720b7 100644 --- a/src/main/groovy/com/netflix/gradle/plugins/packaging/ProjectPackagingExtension.groovy +++ b/src/main/groovy/com/netflix/gradle/plugins/packaging/ProjectPackagingExtension.groovy @@ -1,5 +1,6 @@ package com.netflix.gradle.plugins.packaging +import com.netflix.gradle.plugins.utils.FilePermissionUtil import groovy.transform.CompileDynamic import org.gradle.api.Action import org.gradle.api.Project @@ -259,19 +260,21 @@ class ProjectPackagingExtension extends SystemPackagingExtension { // } public Integer getFileMode() { - return getDelegateCopySpec().getFileMode(); + return FilePermissionUtil.getFileMode(getDelegateCopySpec()) } public CopyProcessingSpec setFileMode(Integer mode) { - return getDelegateCopySpec().setFileMode(mode); + FilePermissionUtil.setFilePermission(getDelegateCopySpec(), mode) + return getDelegateCopySpec() } public Integer getDirMode() { - return getDelegateCopySpec().getDirMode(); + return FilePermissionUtil.getDirMode(getDelegateCopySpec()) } public CopyProcessingSpec setDirMode(Integer mode) { - return getDelegateCopySpec().setDirMode(mode); + FilePermissionUtil.setDirPermission(getDelegateCopySpec(), mode) + return getDelegateCopySpec() } public Set getIncludes() { diff --git a/src/main/groovy/com/netflix/gradle/plugins/rpm/RpmCopyAction.groovy b/src/main/groovy/com/netflix/gradle/plugins/rpm/RpmCopyAction.groovy index c5d58c46..2152268d 100755 --- a/src/main/groovy/com/netflix/gradle/plugins/rpm/RpmCopyAction.groovy +++ b/src/main/groovy/com/netflix/gradle/plugins/rpm/RpmCopyAction.groovy @@ -22,6 +22,7 @@ import com.netflix.gradle.plugins.packaging.Directory import com.netflix.gradle.plugins.packaging.Link import com.netflix.gradle.plugins.rpm.validation.RpmTaskPropertiesValidator import com.netflix.gradle.plugins.utils.DeprecationLoggerUtils +import com.netflix.gradle.plugins.utils.FilePermissionUtil import groovy.transform.CompileDynamic import org.apache.commons.lang3.StringUtils import org.gradle.api.internal.file.copy.CopyAction @@ -161,7 +162,7 @@ class RpmCopyAction extends AbstractPackagingCopyAction { String user = lookup(specToLookAt, 'user') ?: task.user String group = lookup(specToLookAt, 'permissionGroup') ?: task.permissionGroup - int fileMode = lookup(specToLookAt, 'fileMode') ?: fileDetails.mode + int fileMode = FilePermissionUtil.getFileMode(specToLookAt) ?: FilePermissionUtil.getUnixPermission(fileDetails) def specAddParentsDir = lookup(specToLookAt, 'addParentDirs') boolean addParentsDir = specAddParentsDir != null ? specAddParentsDir : task.addParentDirs @@ -182,7 +183,7 @@ class RpmCopyAction extends AbstractPackagingCopyAction { if (createDirectoryEntry) { logger.debug 'adding directory {}', dirDetails.relativePath.pathString - int dirMode = lookup(specToLookAt, 'dirMode') ?: dirDetails.mode + int dirMode = lookup(specToLookAt, 'dirMode') ?: FilePermissionUtil.getUnixPermission(dirDetails) Directive directive = (Directive) lookup(specToLookAt, 'fileType') ?: task.fileType String user = lookup(specToLookAt, 'user') ?: task.user String group = lookup(specToLookAt, 'permissionGroup') ?: task.permissionGroup diff --git a/src/main/groovy/com/netflix/gradle/plugins/utils/FilePermissionUtil.groovy b/src/main/groovy/com/netflix/gradle/plugins/utils/FilePermissionUtil.groovy new file mode 100644 index 00000000..e7a87266 --- /dev/null +++ b/src/main/groovy/com/netflix/gradle/plugins/utils/FilePermissionUtil.groovy @@ -0,0 +1,94 @@ +package com.netflix.gradle.plugins.utils + +import org.gradle.api.file.FileCopyDetails +import org.gradle.api.file.SyncSpec +import org.gradle.util.GradleVersion + +/** + * Utility class to get the unix permission of a file. + */ +class FilePermissionUtil { + + /** + * Get the unix permission of a file. + * Gradle 8.3 introduced a new way to get the unix permission of a file. + * Gradle 8.8 deprecated the old way. @see https://docs.gradle.org/8.8-rc-1/userguide/upgrading_version_8.html#unix_file_permissions_deprecated + * @param details + * @return + */ + static int getUnixPermission(FileCopyDetails details) { + return isOlderThanGradle8_3() ? details.mode : details.permissions.toUnixNumeric() + } + + /** + * Get the unix permission of a file. + * Gradle 8.3 introduced a new way to get the unix permission of a file. + * Gradle 8.8 deprecated the old way. @see https://docs.gradle.org/8.8-rc-1/userguide/upgrading_version_8.html#unix_file_permissions_deprecated + * @param details + * @return + */ + static Integer getFileMode(SyncSpec copySpecInternal) { + if(!copySpecInternal) { + return null + } + + if(isOlderThanGradle8_3()) { + return copySpecInternal.fileMode + } else if(copySpecInternal.filePermissions.present){ + copySpecInternal.filePermissions.get().toUnixNumeric() + } else { + return null + } + } + + /** + * Get the unix permission of a directory. + * Gradle 8.3 introduced a new way to get the unix permission of a file. + * Gradle 8.8 deprecated the old way. @see https://docs.gradle.org/8.8-rc-1/userguide/upgrading_version_8.html#unix_file_permissions_deprecated + * @param copySpecInternal + * @return + */ + static Integer getDirMode(SyncSpec copySpecInternal) { + if(!copySpecInternal) { + return null + } + + if(isOlderThanGradle8_3()) { + return copySpecInternal.dirMode + } else if(copySpecInternal.dirPermissions.present){ + copySpecInternal.dirPermissions.get().toUnixNumeric() + } else { + return null + } + } + + /** + * Set the unix permission of a file. + * Gradle 8.3 introduced a new way to set the unix permission of a file. + * @param copySpec + * @param permission + */ + static void setFilePermission(SyncSpec copySpec, int permission) { + isOlderThanGradle8_3() ? + copySpec.setFileMode(permission) : copySpec.filePermissions { + it.unix(permission) + } + } + + /** + * Set the unix permission of a directory. + * Gradle 8.3 introduced a new way to set the unix permission of a file. + * @param copySpec + * @param permission + */ + static void setDirPermission(SyncSpec copySpec, int permission) { + isOlderThanGradle8_3() ? + copySpec.setDirMode(permission) : copySpec.dirPermissions { + it.unix(permission) + } + } + + private static final isOlderThanGradle8_3() { + return GradleVersion.current().baseVersion < GradleVersion.version('8.3').baseVersion + } +} diff --git a/src/test/groovy/com/netflix/gradle/plugins/rpm/RpmPluginTest.groovy b/src/test/groovy/com/netflix/gradle/plugins/rpm/RpmPluginTest.groovy index acc27140..cb40b689 100755 --- a/src/test/groovy/com/netflix/gradle/plugins/rpm/RpmPluginTest.groovy +++ b/src/test/groovy/com/netflix/gradle/plugins/rpm/RpmPluginTest.groovy @@ -360,18 +360,25 @@ class RpmPluginTest extends ProjectSpec { os = LINUX into '/tiny' - fileMode 0555 + + filePermissions { + unix(0555) + } from(srcDir1) { // should be default group } from(srcDir2) { - fileMode 0666 + filePermissions { + unix(0666) + } } from(srcDir3) { - fileMode 0555 + filePermissions { + unix(0555) + } } })