刚刚接手RK3588 Android13,测试以太网IP设置问题,发现重启设备无法保存,跟踪logcat日志发现,目标目录不存在导致无法写文件,而且写完文件后没有fflush,会概率性出现断电重启数据没有保存。主要集中在仓库:packages/modules/Connectivity
修改一:增加目录和文件创建
diff --git a/service-t/src/com/android/server/net/IpConfigStore.java b/service-t/src/com/android/server/net/IpConfigStore.java
index 3a9a54415..155ea1323 100644
--- a/service-t/src/com/android/server/net/IpConfigStore.java
+++ b/service-t/src/com/android/server/net/IpConfigStore.java
@@ -39,6 +39,7 @@ import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
+import java.io.File;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.util.ArrayList;
@@ -190,6 +191,7 @@ public class IpConfigStore {
@Deprecated
public void writeIpAndProxyConfigurationsToFile(String filePath,
final SparseArray<IpConfiguration> networks) {
+ createFilepath(filePath);
mWriter.write(filePath, out -> {
out.writeInt(IPCONFIG_FILE_VERSION);
for (int i = 0; i < networks.size(); i++) {
@@ -203,6 +205,7 @@ public class IpConfigStore {
*/
public void writeIpConfigurations(String filePath,
ArrayMap<String, IpConfiguration> networks) {
+ createFilepath(filePath);
mWriter.write(filePath, out -> {
out.writeInt(IPCONFIG_FILE_VERSION);
for (int i = 0; i < networks.size(); i++) {
@@ -446,4 +449,20 @@ public class IpConfigStore {
protected static void log(String s) {
Log.d(TAG, s);
}
+
+ private void createFilepath(String filepath){
+ try {
+ String path = filepath.substring(0, filepath.lastIndexOf("/"));
+ File dir = new File(path);
+ if (!dir.exists()) {
+ dir.mkdirs();
+ }
+
+ File file = new File(filepath);
+ if (!file.exists()) {
+ file.createNewFile();
+ }
+ } catch (Exception e) {
+ }
+ }
}
修改二:完善文件写机制(写完及时将缓存写到硬盘)
--- a/service/src/com/android/server/net/DelayedDiskWrite.java
+++ b/service/src/com/android/server/net/DelayedDiskWrite.java
@@ -24,6 +24,7 @@ import android.util.Log;
import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
+import java.io.FileDescriptor;
import java.io.IOException;
/**
@@ -81,10 +82,13 @@ public class DelayedDiskWrite {
private void doWrite(String filePath, Writer w, boolean open) {
DataOutputStream out = null;
+ FileOutputStream file = null;
+ FileDescriptor fd =null;
try {
if (open) {
- out = new DataOutputStream(new BufferedOutputStream(
- new FileOutputStream(filePath)));
+ file = new FileOutputStream(filePath);
+ fd = file.getFD();
+ out = new DataOutputStream(new BufferedOutputStream(file));
}
w.onWriteCalled(out);
} catch (IOException e) {
@@ -92,6 +96,8 @@ public class DelayedDiskWrite {
} finally {
if (out != null) {
try {
+ out.flush();
+ fd.sync();
out.close();
} catch (Exception e) { }
}