| 1 |
/* |
| 2 |
* Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 |
* contributor license agreements. See the NOTICE file distributed with |
| 4 |
* this work for additional information regarding copyright ownership. |
| 5 |
* The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 |
* (the "License"); you may not use this file except in compliance with |
| 7 |
* the License. You may obtain a copy of the License at |
| 8 |
* |
| 9 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
* |
| 11 |
* Unless required by applicable law or agreed to in writing, software |
| 12 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
* See the License for the specific language governing permissions and |
| 15 |
* limitations under the License. |
| 16 |
*/ |
| 17 |
|
| 18 |
/* |
| 19 |
* A task that generates SHA512 checksums. Cloned from: |
| 20 |
* https://github.com/gradle/gradle-checksum/blob/03351de/src/main/java/org/gradle/crypto/checksum/Checksum.java |
| 21 |
* with custom fixes to make the checksum palatable to shasum, see pending PR: |
| 22 |
* https://github.com/gradle/gradle-checksum/pull/4 |
| 23 |
* |
| 24 |
* Original license ASL: |
| 25 |
* https://github.com/gradle/gradle-checksum/blob/03351de/LICENSE |
| 26 |
*/ |
| 27 |
|
| 28 |
package org.apache.lucene.gradle; |
| 29 |
|
| 30 |
import org.apache.commons.codec.digest.DigestUtils; |
| 31 |
import org.gradle.api.DefaultTask; |
| 32 |
import org.gradle.api.GradleException; |
| 33 |
import org.gradle.api.file.FileCollection; |
| 34 |
import org.gradle.api.file.FileType; |
| 35 |
import org.gradle.api.tasks.Input; |
| 36 |
import org.gradle.api.tasks.InputFiles; |
| 37 |
import org.gradle.api.tasks.OutputDirectory; |
| 38 |
import org.gradle.api.tasks.TaskAction; |
| 39 |
import org.gradle.work.Incremental; |
| 40 |
import org.gradle.work.InputChanges; |
| 41 |
|
| 42 |
import java.io.File; |
| 43 |
import java.io.IOException; |
| 44 |
import java.nio.charset.StandardCharsets; |
| 45 |
import java.nio.file.Files; |
| 46 |
import java.util.Locale; |
| 47 |
|
| 48 |
public class Checksum extends DefaultTask { |
| 49 |
private FileCollection files; |
| 50 |
private File outputDir; |
| 51 |
private Algorithm algorithm; |
| 52 |
|
| 53 |
public enum Algorithm { |
| 54 |
MD5(new DigestUtils(DigestUtils.getMd5Digest())), |
| 55 |
SHA256(new DigestUtils(DigestUtils.getSha256Digest())), |
| 56 |
SHA384(new DigestUtils(DigestUtils.getSha384Digest())), |
| 57 |
SHA512(new DigestUtils(DigestUtils.getSha512Digest())); |
| 58 |
|
| 59 |
private final DigestUtils hashFunction; |
| 60 |
|
| 61 |
Algorithm(DigestUtils hashFunction) { |
| 62 |
this.hashFunction = hashFunction; |
| 63 |
} |
| 64 |
|
| 65 |
public String getExtension() { |
| 66 |
return name().toLowerCase(Locale.ROOT); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
public Checksum() { |
| 71 |
outputDir = new File(getProject().getBuildDir(), "checksums"); |
| 72 |
algorithm = Algorithm.SHA256; |
| 73 |
} |
| 74 |
|
| 75 |
@InputFiles |
| 76 |
@Incremental |
| 77 |
public FileCollection getFiles() { |
| 78 |
return files; |
| 79 |
} |
| 80 |
|
| 81 |
public void setFiles(FileCollection files) { |
| 82 |
this.files = files; |
| 83 |
} |
| 84 |
|
| 85 |
@Input |
| 86 |
public Algorithm getAlgorithm() { |
| 87 |
return algorithm; |
| 88 |
} |
| 89 |
|
| 90 |
public void setAlgorithm(Algorithm algorithm) { |
| 91 |
this.algorithm = algorithm; |
| 92 |
} |
| 93 |
|
| 94 |
@OutputDirectory |
| 95 |
public File getOutputDir() { |
| 96 |
return outputDir; |
| 97 |
} |
| 98 |
|
| 99 |
public void setOutputDir(File outputDir) { |
| 100 |
if (outputDir.exists() && !outputDir.isDirectory()) { |
| 101 |
throw new IllegalArgumentException("Output directory must be a directory."); |
| 102 |
} |
| 103 |
this.outputDir = outputDir; |
| 104 |
} |
| 105 |
|
| 106 |
@TaskAction |
| 107 |
public void generateChecksumFiles(InputChanges inputChanges) throws IOException { |
| 108 |
if (!getOutputDir().exists()) { |
| 109 |
if (!getOutputDir().mkdirs()) { |
| 110 |
throw new IOException("Could not create directory:" + getOutputDir()); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
if (!inputChanges.isIncremental()) { |
| 115 |
getProject().delete(allPossibleChecksumFiles()); |
| 116 |
} |
| 117 |
|
| 118 |
inputChanges |
| 119 |
.getFileChanges(getFiles()) |
| 120 |
.forEach( |
| 121 |
fileChange -> { |
| 122 |
if (fileChange.getFileType() == FileType.DIRECTORY) { |
| 123 |
getProject() |
| 124 |
.getLogger() |
| 125 |
.warn("Checksums can't be applied to directories: " + fileChange.getFile()); |
| 126 |
return; |
| 127 |
} |
| 128 |
|
| 129 |
File input = fileChange.getFile(); |
| 130 |
switch (fileChange.getChangeType()) { |
| 131 |
case REMOVED: |
| 132 |
if (input.isFile()) { |
| 133 |
getProject().delete(outputFileFor(input)); |
| 134 |
} |
| 135 |
break; |
| 136 |
|
| 137 |
case ADDED: |
| 138 |
case MODIFIED: |
| 139 |
input = fileChange.getFile(); |
| 140 |
if (input.isFile()) { |
| 141 |
File checksumFile = outputFileFor(input); |
| 142 |
|
| 143 |
try { |
| 144 |
String checksum = algorithm.hashFunction.digestAsHex(input).trim(); |
| 145 |
|
| 146 |
/* |
| 147 |
* https://man7.org/linux/man-pages/man1/sha1sum.1.html |
| 148 |
* |
| 149 |
* The default mode is to print a line with checksum, a space, a character |
| 150 |
* indicating input mode ('*' for binary, ' ' for text or where |
| 151 |
* binary is insignificant), and name for each FILE. |
| 152 |
*/ |
| 153 |
boolean binaryMode = true; |
| 154 |
|
| 155 |
Files.writeString( |
| 156 |
checksumFile.toPath(), |
| 157 |
String.format( |
| 158 |
Locale.ROOT, |
| 159 |
"%s %s%s", |
| 160 |
checksum, |
| 161 |
binaryMode ? "*" : " ", |
| 162 |
input.getName()), |
| 163 |
StandardCharsets.UTF_8); |
| 164 |
} catch (IOException e) { |
| 165 |
throw new GradleException("Trouble creating checksum: " + e.getMessage(), e); |
| 166 |
} |
| 167 |
} |
| 168 |
break; |
| 169 |
default: |
| 170 |
throw new RuntimeException(); |
| 171 |
} |
| 172 |
}); |
| 173 |
} |
| 174 |
|
| 175 |
private File outputFileFor(File inputFile) { |
| 176 |
return new File(getOutputDir(), inputFile.getName() + "." + algorithm.getExtension()); |
| 177 |
} |
| 178 |
|
| 179 |
private FileCollection allPossibleChecksumFiles() { |
| 180 |
FileCollection possibleFiles = null; |
| 181 |
for (Algorithm algo : Algorithm.values()) { |
| 182 |
if (possibleFiles == null) { |
| 183 |
possibleFiles = filesFor(algo); |
| 184 |
} else { |
| 185 |
possibleFiles = possibleFiles.plus(filesFor(algo)); |
| 186 |
} |
| 187 |
} |
| 188 |
return possibleFiles; |
| 189 |
} |
| 190 |
|
| 191 |
private FileCollection filesFor(final Algorithm algo) { |
| 192 |
return getProject() |
| 193 |
.fileTree(getOutputDir(), files -> files.include("**/*." + algo.toString().toLowerCase())); |
| 194 |
} |
| 195 |
} |