Skip to content

Commit

Permalink
Removed atomic copy from PR.
Browse files Browse the repository at this point in the history
  • Loading branch information
shartte committed Dec 12, 2023
1 parent 5d99295 commit 28b5073
Showing 1 changed file with 0 additions and 57 deletions.
57 changes: 0 additions & 57 deletions utils/src/main/java/net/neoforged/gradle/util/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,16 @@

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.nio.file.*;
import java.util.List;
import java.util.Set;
import java.util.TimeZone;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipOutputStream;

/**
Expand Down Expand Up @@ -137,60 +134,6 @@ public static void atomicMove(Path source, Path destination) throws IOException
}
}

/**
* Tries to atomically copy the given source file to the given destination file by using a temporary
* file in the destination path that is then renamed.
*
* @param source The source file
* @param destination The destination file
* @throws IOException If an I/O error occurs
*/
public static void atomicCopy(Path source, Path destination) throws IOException {
Path destinationDir = getAndCreateParentDirectory(destination);

Path copyTempFile = Files.createTempFile(destinationDir, destination.getFileName().toString(), ".copy");
try {
Files.copy(source, copyTempFile, StandardCopyOption.COPY_ATTRIBUTES, StandardCopyOption.REPLACE_EXISTING);
FileUtils.atomicMove(copyTempFile, destination);
} finally {
Files.deleteIfExists(copyTempFile);
}
}

/**
* Tries to atomically copy the given input stream to the given destination file by using a temporary
* file in the destination path that is then renamed.
*
* @param source The source file
* @param destination The destination file
* @throws IOException If an I/O error occurs
*/
public static void atomicCopy(InputStream source, Path destination) throws IOException {
Path destinationDir = getAndCreateParentDirectory(destination);

Path copyTempFile = Files.createTempFile(destinationDir, destination.getFileName().toString(), ".copy");
try {
Files.copy(source, copyTempFile, StandardCopyOption.REPLACE_EXISTING);
FileUtils.atomicMove(copyTempFile, destination);
} finally {
Files.deleteIfExists(copyTempFile);
}
}

private static Path getAndCreateParentDirectory(Path path) throws IOException {
// We need to be able to get a parent directory
if (path.getParent() == null) {
path = path.toAbsolutePath();
if (path.getParent() == null) {
throw new IllegalArgumentException("Cannot determine parent directory of " + path);
}
}

Path parentDir = path.getParent();
Files.createDirectories(parentDir);
return parentDir;
}

/**
* Gets the size in bytes of the file.
*
Expand Down

0 comments on commit 28b5073

Please sign in to comment.