mirror of
https://github.com/AsamK/signal-cli
synced 2025-08-30 02:50:39 +00:00
Prevent corrupting account file, when serialization fails
This commit is contained in:
parent
87c0282af5
commit
7334c78450
1 changed files with 12 additions and 5 deletions
|
@ -34,6 +34,8 @@ import org.whispersystems.libsignal.util.Pair;
|
||||||
import org.whispersystems.signalservice.api.push.SignalServiceAddress;
|
import org.whispersystems.signalservice.api.push.SignalServiceAddress;
|
||||||
import org.whispersystems.util.Base64;
|
import org.whispersystems.util.Base64;
|
||||||
|
|
||||||
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.Closeable;
|
import java.io.Closeable;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
@ -305,12 +307,17 @@ public class SignalAccount implements Closeable {
|
||||||
.putPOJO("profileStore", profileStore)
|
.putPOJO("profileStore", profileStore)
|
||||||
;
|
;
|
||||||
try {
|
try {
|
||||||
|
try (ByteArrayOutputStream output = new ByteArrayOutputStream()) {
|
||||||
|
// Write to memory first to prevent corrupting the file in case of serialization errors
|
||||||
|
jsonProcessor.writeValue(output, rootNode);
|
||||||
|
ByteArrayInputStream input = new ByteArrayInputStream(output.toByteArray());
|
||||||
synchronized (fileChannel) {
|
synchronized (fileChannel) {
|
||||||
fileChannel.position(0);
|
fileChannel.position(0);
|
||||||
jsonProcessor.writeValue(Channels.newOutputStream(fileChannel), rootNode);
|
input.transferTo(Channels.newOutputStream(fileChannel));
|
||||||
fileChannel.truncate(fileChannel.position());
|
fileChannel.truncate(fileChannel.position());
|
||||||
fileChannel.force(false);
|
fileChannel.force(false);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
System.err.println(String.format("Error saving file: %s", e.getMessage()));
|
System.err.println(String.format("Error saving file: %s", e.getMessage()));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue