mirror of
https://github.com/beemdevelopment/Aegis.git
synced 2025-05-16 06:53:01 +00:00
Merge pull request #306 from alexbakker/atomic-file
Protect writes of the vault file against corruption with AtomicFile
This commit is contained in:
commit
e38121efee
1 changed files with 16 additions and 11 deletions
|
@ -3,13 +3,13 @@ package com.beemdevelopment.aegis.vault;
|
|||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
|
||||
import androidx.core.util.AtomicFile;
|
||||
|
||||
import com.beemdevelopment.aegis.services.NotificationService;
|
||||
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.io.DataInputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
|
@ -41,12 +41,9 @@ public class VaultManager {
|
|||
public void load() throws VaultManagerException {
|
||||
assertState(true, false);
|
||||
|
||||
try (FileInputStream file = _context.openFileInput(FILENAME)) {
|
||||
byte[] fileBytes = new byte[(int) file.getChannel().size()];
|
||||
DataInputStream stream = new DataInputStream(file);
|
||||
stream.readFully(fileBytes);
|
||||
stream.close();
|
||||
|
||||
AtomicFile file = new AtomicFile(new File(_context.getFilesDir(), FILENAME));
|
||||
try {
|
||||
byte[] fileBytes = file.readFully();
|
||||
_file = VaultFile.fromBytes(fileBytes);
|
||||
_encrypt = _file.isEncrypted();
|
||||
if (!isEncryptionEnabled()) {
|
||||
|
@ -77,11 +74,19 @@ public class VaultManager {
|
|||
}
|
||||
}
|
||||
|
||||
public static void save(Context context, VaultFile file) throws VaultManagerException {
|
||||
byte[] bytes = file.toBytes();
|
||||
try (FileOutputStream stream = context.openFileOutput(FILENAME, Context.MODE_PRIVATE)) {
|
||||
public static void save(Context context, VaultFile vaultFile) throws VaultManagerException {
|
||||
byte[] bytes = vaultFile.toBytes();
|
||||
AtomicFile file = new AtomicFile(new File(context.getFilesDir(), FILENAME));
|
||||
|
||||
FileOutputStream stream = null;
|
||||
try {
|
||||
stream = file.startWrite();
|
||||
stream.write(bytes);
|
||||
file.finishWrite(stream);
|
||||
} catch (IOException e) {
|
||||
if (stream != null) {
|
||||
file.failWrite(stream);
|
||||
}
|
||||
throw new VaultManagerException(e);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue