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
diff --git a/services/java/com/android/server/AppWidgetService.java b/services/java/com/android/server/AppWidgetService.java
index 3c46954..f8b8ecc 100644
--- a/services/java/com/android/server/AppWidgetService.java
+++ b/services/java/com/android/server/AppWidgetService.java
@@ -816,7 +816,10 @@
             temp.delete();
         }
 
-        writeStateToFileLocked(temp);
+        if (!writeStateToFileLocked(temp)) {
+            Log.w(TAG, "Failed to persist new settings");
+            return;
+        }
 
         //noinspection ResultOfMethodCallIgnored
         real.delete();
@@ -824,7 +827,7 @@
         temp.renameTo(real);
     }
 
-    void writeStateToFileLocked(File file) {
+    boolean writeStateToFileLocked(File file) {
         FileOutputStream stream = null;
         int N;
 
@@ -877,6 +880,7 @@
 
             out.endDocument();
             stream.close();
+            return true;
         } catch (IOException e) {
             try {
                 if (stream != null) {
@@ -889,6 +893,7 @@
                 //noinspection ResultOfMethodCallIgnored
                 file.delete();
             }
+            return false;
         }
     }