From 2d6fff6b22117c20233b8b758146da93ef0bf09a Mon Sep 17 00:00:00 2001 From: "Lukas R. Mansour" Date: Wed, 2 Aug 2023 18:11:57 +0200 Subject: [PATCH] Remove all remaining references of Guava's Files class. Signed-off-by: Lukas R. Mansour --- .../scanner/data/PoolOperationMap.java | 5 +- .../dcache/services/billing/text/Indexer.java | 84 ++++++++++--------- .../src/main/java/org/dcache/srm/SRM.java | 23 +++-- 3 files changed, 65 insertions(+), 47 deletions(-) diff --git a/modules/dcache-qos/src/main/java/org/dcache/qos/services/scanner/data/PoolOperationMap.java b/modules/dcache-qos/src/main/java/org/dcache/qos/services/scanner/data/PoolOperationMap.java index 94360e39540..c2e0766b919 100644 --- a/modules/dcache-qos/src/main/java/org/dcache/qos/services/scanner/data/PoolOperationMap.java +++ b/modules/dcache-qos/src/main/java/org/dcache/qos/services/scanner/data/PoolOperationMap.java @@ -61,7 +61,6 @@ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING import com.google.common.annotations.VisibleForTesting; import com.google.common.collect.ImmutableSet; -import com.google.common.io.Files; import diskCacheV111.poolManager.PoolSelectionUnit; import diskCacheV111.poolManager.PoolSelectionUnit.SelectionPool; import diskCacheV111.pools.PoolV2Mode; @@ -171,8 +170,8 @@ private static Collection load(String excludedPoolsFile) { Collection excluded = new ArrayList<>(); - try (BufferedReader fr = new BufferedReader(new FileReader(current))) { - excluded = Files.readLines(current, StandardCharsets.US_ASCII); + try (BufferedReader fr = new BufferedReader(new FileReader(current, StandardCharsets.US_ASCII))) { + excluded = fr.lines().collect(Collectors.toList()); current.delete(); } catch (FileNotFoundException e) { LOGGER.error("Unable to reload excluded pools file: {}", e.getMessage()); diff --git a/modules/dcache/src/main/java/org/dcache/services/billing/text/Indexer.java b/modules/dcache/src/main/java/org/dcache/services/billing/text/Indexer.java index f9c804801be..7f3d4097241 100644 --- a/modules/dcache/src/main/java/org/dcache/services/billing/text/Indexer.java +++ b/modules/dcache/src/main/java/org/dcache/services/billing/text/Indexer.java @@ -1,22 +1,20 @@ package org.dcache.services.billing.text; import static com.google.common.base.Preconditions.checkArgument; -import static com.google.common.io.Files.isFile; import static java.nio.charset.StandardCharsets.UTF_8; import static java.util.Arrays.asList; import static java.util.stream.Collectors.toList; import static org.dcache.util.ByteUnit.KiB; import com.google.common.base.Function; -import com.google.common.base.Predicate; import com.google.common.base.Strings; -import com.google.common.collect.FluentIterable; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableMap.Builder; import com.google.common.collect.Maps; import com.google.common.collect.Ordering; import com.google.common.collect.Sets; -import com.google.common.collect.TreeTraverser; +import com.google.common.graph.Traverser; import com.google.common.hash.BloomFilter; import com.google.common.hash.Funnels; import com.google.common.io.ByteSource; @@ -61,9 +59,12 @@ import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; +import java.util.function.Predicate; import java.util.logging.LogManager; import java.util.regex.Matcher; import java.util.regex.Pattern; +import java.util.stream.Stream; +import java.util.stream.StreamSupport; import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream; import org.apache.commons.compress.compressors.bzip2.BZip2CompressorOutputStream; import org.dcache.boot.LayoutBuilder; @@ -102,20 +103,18 @@ public class Indexer { * Almost identical to the file tree traverser from Guava, sorts directory entries * lexicographically. */ - private static final TreeTraverser SORTED_FILE_TREE_TRAVERSER = new TreeTraverser() { - @Override - public Iterable children(File file) { - // check isDirectory() just because it may be faster than listFiles() on a non-directory - if (file.isDirectory()) { - File[] files = file.listFiles(); - if (files != null) { - return Ordering.natural().sortedCopy(asList(files)); - } - } - - return Collections.emptyList(); - } - }; + private static final Traverser SORTED_FILE_TREE_TRAVERSER = Traverser.forTree( + file -> { + // check isDirectory() just because it may be faster than listFiles() on a non-directory + if (file.isDirectory()) { + File[] files = file.listFiles(); + if (files != null) { + return Ordering.natural().sortedCopy(asList(files)); + } + } + + return Collections.emptyList(); + }); private final boolean isFlat; private final File dir; @@ -140,9 +139,9 @@ private Indexer(Args args) throws IOException, URISyntaxException, ClassNotFound searchTerms = ImmutableList.of(""); } - FluentIterable filesWithPossibleMatch = - SORTED_FILE_TREE_TRAVERSER - .preOrderTraversal(dir); + Stream fileStream = StreamSupport.stream( + SORTED_FILE_TREE_TRAVERSER.depthFirstPreOrder(dir).spliterator(), false + ); if (args.hasOption("since") || args.hasOption("until")) { LocalDate since = args.hasOption("since") ? LocalDate.parse(args.getOption("since"), CLI_DATE_FORMAT) @@ -150,36 +149,38 @@ private Indexer(Args args) throws IOException, URISyntaxException, ClassNotFound LocalDate until = args.hasOption("until") ? LocalDate.parse(args.getOption("until"), CLI_DATE_FORMAT) : LocalDate.now().plusDays(1); - filesWithPossibleMatch = - filesWithPossibleMatch.filter(file -> isInRange(file, since, until)); + fileStream = fileStream.filter(file -> isInRange(file, since, until)); } if (searchTerms.contains("")) { - filesWithPossibleMatch = - filesWithPossibleMatch.filter(file -> isBillingFile(file)); + fileStream = fileStream.filter(Indexer::isBillingFile); } else { - filesWithPossibleMatch = - filesWithPossibleMatch.filter(isBillingFileAndMightContain(searchTerms)); + fileStream = fileStream.filter(isBillingFileAndMightContain(searchTerms)); } + Iterable fileIterable = fileStream.collect(toList()); + if (args.hasOption("files")) { - for (File file : filesWithPossibleMatch) { + for (File file : fileIterable) { System.out.println(file); } } else if (args.hasOption("yaml")) { try (OutputWriter out = toYaml(System.out)) { - find(searchTerms, filesWithPossibleMatch, out); + find(searchTerms, fileIterable, out); } } else if (args.hasOption("json")) { try (OutputWriter out = toJson(System.out)) { - find(searchTerms, filesWithPossibleMatch, out); + find(searchTerms, fileIterable, out); } } else { try (OutputWriter out = toText(System.out)) { - find(searchTerms, filesWithPossibleMatch, out); + find(searchTerms, fileIterable, out); } } } else if (args.hasOption("all")) { - for (File file : SORTED_FILE_TREE_TRAVERSER.preOrderTraversal(dir).filter(isFile())) { + for (File file : StreamSupport.stream( + SORTED_FILE_TREE_TRAVERSER.depthFirstPreOrder(dir).spliterator(), false + ).filter(File::isFile).collect(toList()) + ) { Matcher matcher = BILLING_NAME_PATTERN.matcher(file.getName()); if (matcher.matches()) { System.out.println("Indexing " + file); @@ -323,7 +324,7 @@ public void close() { /** * Searches for searchTerm in files and writes any matching lines to out. */ - private static void find(final Collection searchTerms, FluentIterable files, + private static void find(final Collection searchTerms, Iterable files, final OutputWriter out) throws IOException { int threads = Runtime.getRuntime().availableProcessors(); @@ -408,7 +409,7 @@ private static void decompress(File compressedFile) throws IOException { new BufferedInputStream(new FileInputStream(file)))) { Files.copy(in, file.toPath()); } - java.nio.file.Files.delete(compressedFile.toPath()); + Files.delete(compressedFile.toPath()); } private static void compress(File file) throws IOException { @@ -419,7 +420,7 @@ private static void compress(File file) throws IOException { try (OutputStream out = new BZip2CompressorOutputStream(bufOut)) { Files.copy(file.toPath(), out); } - java.nio.file.Files.delete(file.toPath()); + Files.delete(file.toPath()); } private static void help(PrintStream out) { @@ -528,7 +529,12 @@ public InputStream openStream() throws IOException { } }; } else { - source = com.google.common.io.Files.asByteSource(file); + source = new ByteSource() { + @Override + public InputStream openStream() throws IOException { + return new FileInputStream(file); + } + }; } return source.asCharSource(charset); } @@ -567,7 +573,7 @@ private static boolean isBillingFile(File file) { */ private static ImmutableMap getBillingFormats( ConfigurationProperties configuration) { - ImmutableMap.Builder formats = ImmutableMap.builder(); + Builder formats = ImmutableMap.builder(); for (String name : configuration.stringPropertyNames()) { if (name.startsWith(BILLING_TEXT_FORMAT_PREFIX)) { formats.put(name.substring(BILLING_TEXT_FORMAT_PREFIX.length()), @@ -581,9 +587,9 @@ private static Predicate isBillingFileAndMightContain(Collection t final List searchTerms = terms.stream() .map(str -> str.endsWith("/") ? str.substring(0, str.length() - 1) : str) .collect(toList()); - return new Predicate() { + return new Predicate<>() { @Override - public boolean apply(File file) { + public boolean test(File file) { if (!file.isFile()) { return false; } diff --git a/modules/srm-server/src/main/java/org/dcache/srm/SRM.java b/modules/srm-server/src/main/java/org/dcache/srm/SRM.java index a66413e2ec4..e574983eabe 100644 --- a/modules/srm-server/src/main/java/org/dcache/srm/SRM.java +++ b/modules/srm-server/src/main/java/org/dcache/srm/SRM.java @@ -75,11 +75,11 @@ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Iterables; -import com.google.common.io.Files; import dmg.cells.nucleus.CellLifeCycleAware; import java.io.IOException; import java.lang.reflect.Method; import java.net.URI; +import java.nio.file.Path; import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; @@ -93,7 +93,6 @@ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Stream; -import java.util.stream.StreamSupport; import javax.annotation.Nonnull; import org.dcache.commons.stats.MonitoringProxy; import org.dcache.commons.stats.RequestCounters; @@ -401,7 +400,7 @@ public void listRequest(StringBuilder sb, long requestId, boolean longformat) public void cancelRequest(StringBuilder sb, long requestId) throws SRMInvalidRequestException { Job job = Job.getJob(requestId, Job.class); - if (job == null || !(job instanceof ContainerRequest)) { + if (!(job instanceof ContainerRequest)) { sb.append("request with id ").append(requestId) .append(" is not found\n"); return; @@ -609,7 +608,7 @@ public > Iterable getActiveFileRequests(Class typ public Stream getActivePutFileRequests(URI surl) throws DataAccessException { String path = getPath(surl); - return StreamSupport.stream(getActiveJobs(PutFileRequest.class).spliterator(), false) + return getActiveJobs(PutFileRequest.class).stream() .filter(r -> getPath(r.getSurl()).startsWith(path)); } @@ -688,6 +687,10 @@ public void checkRemoveDirectory(URI surl) throws SRMException { private static String getPath(URI surl) { String path = surl.getPath(); + if (path == null) { + throw new NullPointerException("path cannot be null."); + } + // Handle the query. String query = surl.getQuery(); if (query != null) { int i = query.indexOf(SFN_STRING); @@ -705,7 +708,17 @@ private static String getPath(URI surl) { * paths to an absolute path, which requires additional name space * lookups. */ - path = Files.simplifyPath(path); + + // Trim away any whitespace. Shouldn't be the case though. + path = path.trim(); + // Now we need to simplify the path + // Just return the base directory. + if (path.isEmpty() || path.isBlank() || path.equals(".")) { + return "/"; + } + // Use Path.normalize() to remove trailin slashes and simplify "./" and "/../" + path = Path.of(path).normalize().toString(); + if (!path.endsWith("/")) { path = path + "/"; }