MCPcopy Create free account
hub / github.com/cloudfoundry/java-buildpack / isValidGroovyFile

Function isValidGroovyFile

src/java/containers/groovy_utils.go:82–112  ·  view source on GitHub ↗

isValidGroovyFile checks if a file is a valid, readable Groovy script Filters out binary files, empty files, and files with invalid content

(filePath string)

Source from the content-addressed store, hash-verified

80// isValidGroovyFile checks if a file is a valid, readable Groovy script
81// Filters out binary files, empty files, and files with invalid content
82func isValidGroovyFile(filePath string) bool {
83 info, err := os.Stat(filePath)
84 if err != nil {
85 return false
86 }
87
88 // Skip empty or very small files (likely invalid)
89 if info.Size() < 10 {
90 return false
91 }
92
93 // Try to read the file
94 content, err := os.ReadFile(filePath)
95 if err != nil {
96 return false
97 }
98
99 // Check if content is valid UTF-8 and not binary
100 // Binary files or invalid groovy files will fail this check
101 for i, b := range content {
102 // Allow common text characters and control chars (newlines, tabs, etc.)
103 if b < 9 || (b > 13 && b < 32 && b != 27) || b == 127 {
104 // Check if it's part of a valid UTF-8 sequence
105 if !isPartOfUTF8Sequence(content, i) {
106 return false
107 }
108 }
109 }
110
111 return true
112}
113
114// isPartOfUTF8Sequence checks if a byte at position i is part of a valid UTF-8 sequence
115func isPartOfUTF8Sequence(content []byte, i int) bool {

Callers 2

DetectMethod · 0.85
FindMainGroovyScriptFunction · 0.85

Calls 1

isPartOfUTF8SequenceFunction · 0.85

Tested by

no test coverage detected