Skip to content

Commit

Permalink
Implicitly move tmp file to the target config file
Browse files Browse the repository at this point in the history
  • Loading branch information
dzmipt committed Sep 13, 2024
1 parent 049b195 commit d283d20
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 39 deletions.
41 changes: 3 additions & 38 deletions src/studio/utils/FilesBackup.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
import org.apache.logging.log4j.Logger;
import studio.utils.log4j.EnvConfig;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand All @@ -17,8 +15,6 @@
import java.util.HashMap;
import java.util.Map;

import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;

public class FilesBackup {

private static final String BACKUP_FOLDER = "backup";
Expand Down Expand Up @@ -59,46 +55,14 @@ protected FilesBackup(String backupFolder) {
}
}

public OutputStream newFileOutputStream(String filename) throws IOException {
public TmpfileOutputStream newFileOutputStream(String filename) throws IOException {
try {
backup(filename);
} catch (IOException e) {
log.error("Error on backup {}", filename, e);
}

Path tmpFile = Paths.get(filename).resolveSibling(FilenameUtils.getName(filename) + ".tmp");
log.info("Saving to tmp file {}", tmpFile.toString());
Files.deleteIfExists(tmpFile);

FileOutputStream outputStream = new FileOutputStream(tmpFile.toFile());
return new OutputStream() {
@Override
public void write(byte[] b) throws IOException {
outputStream.write(b);
}

@Override
public void write(byte[] b, int off, int len) throws IOException {
outputStream.write(b, off, len);
}

@Override
public void write(int b) throws IOException {
outputStream.write(b);
}

@Override
public void flush() throws IOException {
outputStream.flush();
}

@Override
public void close() throws IOException {
outputStream.close();
Files.move(tmpFile, Paths.get(filename), REPLACE_EXISTING);
log.info("moved {} -> {}", tmpFile.toString(), filename);
}
};
return new TmpfileOutputStream(filename);
}

protected void backup(String filename) throws IOException {
Expand Down Expand Up @@ -206,4 +170,5 @@ int getIndex() {
}

}

}
4 changes: 3 additions & 1 deletion src/studio/utils/PropertiesConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public void saveToDisk() {

log.info("Saving config to file {} with number of properties {}", filename, size());

try (OutputStream out = FilesBackup.getInstance().newFileOutputStream(filename)) {
try (TmpfileOutputStream out = FilesBackup.getInstance().newFileOutputStream(filename)) {
byte[] lineSeparator = System.getProperty("line.separator").getBytes(CHARSET);

BufferedReader reader = new BufferedReader(
Expand All @@ -122,6 +122,8 @@ public void saveToDisk() {
out.write(lineSeparator);
}

out.writeCompleted();

propertiesToSave = null;
} catch (IOException e) {
log.error("Error in saving config to the file {}", filename, e);
Expand Down
62 changes: 62 additions & 0 deletions src/studio/utils/TmpfileOutputStream.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package studio.utils;

import org.apache.commons.io.FilenameUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;

public class TmpfileOutputStream extends OutputStream {
private static final Logger log = LogManager.getLogger();

private final String filename;
private final Path tmpFile;
private final FileOutputStream outputStream;

public TmpfileOutputStream(String filename) throws IOException {
this.filename = filename;
tmpFile = Paths.get(filename).resolveSibling(FilenameUtils.getName(filename)
+ "." + System.currentTimeMillis() + ".tmp");
log.info("Saving to tmp file {}", tmpFile.toString());
Files.deleteIfExists(tmpFile);
outputStream = new FileOutputStream(tmpFile.toFile());
}

@Override
public void write(byte[] b) throws IOException {
outputStream.write(b);
}

@Override
public void write(byte[] b, int off, int len) throws IOException {
outputStream.write(b, off, len);
}

@Override
public void write(int b) throws IOException {
outputStream.write(b);
}

@Override
public void flush() throws IOException {
outputStream.flush();
}

@Override
public void close() throws IOException {
outputStream.close();
}

public void writeCompleted() throws IOException {
Files.move(tmpFile, Paths.get(filename), REPLACE_EXISTING);
log.info("moved {} -> {}", tmpFile.toString(), filename);
}

}

0 comments on commit d283d20

Please sign in to comment.