Skip to content

Commit

Permalink
support new Gradle's FilePermissions API
Browse files Browse the repository at this point in the history
  • Loading branch information
rpalcolea committed Apr 26, 2024
1 parent c5f99fa commit 60acc1f
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -136,7 +137,7 @@ class OspackageDaemonPlugin implements Plugin<Project> {
task.from(rendered) {
into(destDir)
rename('.*', destFile)
fileMode 0555 // Since source files don't have the correct permissions
FilePermissionUtil.setFilePermission(it, 0555)
user 'root'
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -119,7 +121,7 @@ class DebCopyAction extends AbstractPackagingCopyAction<Deb> {
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)
}
Expand All @@ -137,7 +139,8 @@ class DebCopyAction extends AbstractPackagingCopyAction<Deb> {
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
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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<String> getIncludes() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -161,7 +162,7 @@ class RpmCopyAction extends AbstractPackagingCopyAction<Rpm> {
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

Expand All @@ -182,7 +183,7 @@ class RpmCopyAction extends AbstractPackagingCopyAction<Rpm> {

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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
})

Expand Down

0 comments on commit 60acc1f

Please sign in to comment.