Check if rename of backed up file fails before persisting new changes.
If not these system services will end up with inconsistent settings files when the device runs out of storage. Delete mangled settings file in PackageManager if the current write fails so that we don't end up overwriting the backed up version with the mangled version Include null check when retrieving fwd locked resource for an existing package
This commit is contained in:
@@ -2760,6 +2760,7 @@ class ApplicationContext extends Context {
|
||||
if (mFile.exists()) {
|
||||
if (!mFile.renameTo(mBackupFile)) {
|
||||
Log.e(TAG, "Couldn't rename file " + mFile + " to backup file " + mBackupFile);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2988,7 +2988,10 @@ public final class BatteryStatsImpl extends BatteryStats {
|
||||
if (mBackupFile.exists()) {
|
||||
mBackupFile.delete();
|
||||
}
|
||||
mFile.renameTo(mBackupFile);
|
||||
if (!mFile.renameTo(mBackupFile)) {
|
||||
Log.w("BatteryStats", "Failed to back up file before writing new stats");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
@@ -3003,8 +3006,14 @@ public final class BatteryStatsImpl extends BatteryStats {
|
||||
mBackupFile.delete();
|
||||
|
||||
mLastWriteTime = SystemClock.elapsedRealtime();
|
||||
return;
|
||||
} catch (IOException e) {
|
||||
Log.e("BatteryStats", "Error writing battery statistics", e);
|
||||
Log.w("BatteryStats", "Error writing battery statistics", e);
|
||||
}
|
||||
if (mFile.exists()) {
|
||||
if (!mFile.delete()) {
|
||||
Log.w(TAG, "Failed to delete mangled file " + mFile);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -816,7 +816,10 @@ class AppWidgetService extends IAppWidgetService.Stub
|
||||
temp.delete();
|
||||
}
|
||||
|
||||
writeStateToFileLocked(temp);
|
||||
if (!writeStateToFileLocked(temp)) {
|
||||
Log.w(TAG, "Failed to persist new settings");
|
||||
return;
|
||||
}
|
||||
|
||||
//noinspection ResultOfMethodCallIgnored
|
||||
real.delete();
|
||||
@@ -824,7 +827,7 @@ class AppWidgetService extends IAppWidgetService.Stub
|
||||
temp.renameTo(real);
|
||||
}
|
||||
|
||||
void writeStateToFileLocked(File file) {
|
||||
boolean writeStateToFileLocked(File file) {
|
||||
FileOutputStream stream = null;
|
||||
int N;
|
||||
|
||||
@@ -877,6 +880,7 @@ class AppWidgetService extends IAppWidgetService.Stub
|
||||
|
||||
out.endDocument();
|
||||
stream.close();
|
||||
return true;
|
||||
} catch (IOException e) {
|
||||
try {
|
||||
if (stream != null) {
|
||||
@@ -889,6 +893,7 @@ class AppWidgetService extends IAppWidgetService.Stub
|
||||
//noinspection ResultOfMethodCallIgnored
|
||||
file.delete();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2049,7 +2049,7 @@ class PackageManagerService extends IPackageManager.Stub {
|
||||
scanMode |= SCAN_FORWARD_LOCKED;
|
||||
}
|
||||
File resFile = destResourceFile;
|
||||
if ((scanMode & SCAN_FORWARD_LOCKED) != 0) {
|
||||
if (ps != null && ((scanMode & SCAN_FORWARD_LOCKED) != 0)) {
|
||||
resFile = getFwdLockedResource(ps.name);
|
||||
}
|
||||
// Note that we invoke the following method only if we are about to unpack an application
|
||||
@@ -6529,15 +6529,19 @@ class PackageManagerService extends IPackageManager.Stub {
|
||||
|FileUtils.S_IRGRP|FileUtils.S_IWGRP
|
||||
|FileUtils.S_IROTH,
|
||||
-1, -1);
|
||||
return;
|
||||
|
||||
} catch(XmlPullParserException e) {
|
||||
Log.w(TAG, "Unable to write package manager settings, current changes will be lost at reboot", e);
|
||||
|
||||
} catch(java.io.IOException e) {
|
||||
Log.w(TAG, "Unable to write package manager settings, current changes will be lost at reboot", e);
|
||||
|
||||
}
|
||||
|
||||
// Clean up partially written file
|
||||
if (mSettingsFilename.exists()) {
|
||||
if (!mSettingsFilename.delete()) {
|
||||
Log.i(TAG, "Failed to clean up mangled file: " + mSettingsFilename);
|
||||
}
|
||||
}
|
||||
//Debug.stopMethodTracing();
|
||||
}
|
||||
|
||||
|
||||
@@ -381,7 +381,10 @@ public final class UsageStatsService extends IUsageStats.Stub {
|
||||
mFileLeaf = getCurrentDateStr(FILE_PREFIX);
|
||||
// Copy current file to back up
|
||||
File backupFile = new File(mFile.getPath() + ".bak");
|
||||
mFile.renameTo(backupFile);
|
||||
if (!mFile.renameTo(backupFile)) {
|
||||
Log.w(TAG, "Failed to persist new stats");
|
||||
return;
|
||||
}
|
||||
try {
|
||||
// Write mStats to file
|
||||
writeStatsFLOCK();
|
||||
|
||||
Reference in New Issue
Block a user