fsync directories as well as files
When we create a new file, that file can be lost in a crash unless the containing directory is synced. Bug: 130637124 Bug: 137666560 Test: ensure pattern setting still works. Change-Id: I5ec05a98cb5077950d1a4214aca6801c73450b62
This commit is contained in:
@@ -48,6 +48,8 @@ import java.io.DataOutputStream;
|
|||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.RandomAccessFile;
|
import java.io.RandomAccessFile;
|
||||||
|
import java.nio.channels.FileChannel;
|
||||||
|
import java.nio.file.StandardOpenOption;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -390,6 +392,17 @@ class LockSettingsStorage {
|
|||||||
return stored;
|
return stored;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void fsyncDirectory(File directory) {
|
||||||
|
try {
|
||||||
|
try (FileChannel file = FileChannel.open(directory.toPath(),
|
||||||
|
StandardOpenOption.READ)) {
|
||||||
|
file.force(true);
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
Slog.e(TAG, "Error syncing directory: " + directory, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void writeFile(String name, byte[] hash) {
|
private void writeFile(String name, byte[] hash) {
|
||||||
synchronized (mFileWriteLock) {
|
synchronized (mFileWriteLock) {
|
||||||
RandomAccessFile raf = null;
|
RandomAccessFile raf = null;
|
||||||
@@ -406,6 +419,7 @@ class LockSettingsStorage {
|
|||||||
raf.write(hash, 0, hash.length);
|
raf.write(hash, 0, hash.length);
|
||||||
}
|
}
|
||||||
raf.close();
|
raf.close();
|
||||||
|
fsyncDirectory((new File(name)).getAbsoluteFile().getParentFile());
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
Slog.e(TAG, "Error writing to file " + e);
|
Slog.e(TAG, "Error writing to file " + e);
|
||||||
} finally {
|
} finally {
|
||||||
|
|||||||
Reference in New Issue
Block a user