Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The XFS file system doesn't return size of the directory content #25142

Merged
merged 1 commit into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
import java.nio.channels.WritableByteChannel;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.Collection;
import java.util.Locale;
Expand Down Expand Up @@ -743,7 +745,7 @@ public static File copyResource(String resourcePath, File outputFile) throws IOE


/**
* Copies a file.
* Copies a file or directory.
*
* @param fin File to copy
* @param fout New file
Expand All @@ -765,6 +767,34 @@ public static void copy(File fin, File fout) throws IOException {
}


/**
* Computes file or directory size. Follows symlinks just for the provided parameter, but not
* for files under the tree.
*
* @param fileOrDirectory
* @return summary of sizes of regular files.
* @throws IOException if any file size cannot be read.
*/
public static long getSize(File fileOrDirectory) throws IOException {
try {
return Files.walk(fileOrDirectory.getCanonicalFile().toPath())
.filter(p -> Files.isRegularFile(p, LinkOption.NOFOLLOW_LINKS)).mapToLong(FileUtils::getRegularFileSize)
.sum();
} catch (IllegalStateException e) {
throw new IOException("Could not read size of " + fileOrDirectory, e);
}
}


private static long getRegularFileSize(Path path) {
try {
return Files.size(path);
} catch (IOException e) {
throw new IllegalStateException("Could not read file size for " + path, e);
}
}


/**
* Returns a String with uniform slashes such that all the
* occurances of '\\' are replaced with '/'.
Expand Down Expand Up @@ -1057,7 +1087,7 @@ private static class FileOutputStreamWork implements RetriableWork {
private Throwable lastError;
private final File out;

public FileOutputStreamWork(File out) {
private FileOutputStreamWork(File out) {
this.out = out;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public void testCopyDirectoriesFiles() throws Exception {
File outputDir = new File(tempDir, "outputDir");
File testDir = new File(FileUtilsTest.class.getResource("/process").toURI());
FileUtils.copy(testDir, outputDir);
assertEquals(testDir.length(), outputDir.length());
assertEquals(FileUtils.getSize(testDir), FileUtils.getSize(outputDir));
}


Expand Down
Loading