title: Ota Update in AOSP
body: <p>I have been trying to make a Custom ROM in android. So, far I have been able to make changes and flash in Nexus 4 successfully. But my main aim is to provide Ota updates to my ROM. Right now I am able to update the device with update.zip using adb sideload and now I want it to be done automatically through a system app. For that I have made a system app which can download the update.zip from my server.
  I have gone through this link without any progress:
  <a href="https://stackoverflow.com/questions/15123742/android-development-recoverysystem-installpackage-cannot-write-to-cache-recov">Android development RecoverySystem.installPackage() cannot write to /cache/recovery/command permission denied</a></p>
  
  <p>I have used the following code in Asynctask to install the update:</p>
  
  <pre><code>    File update = new File("/data/update.zip");
      try {
          RecoverySystem.installPackage(mContext, update);
      } catch (IOException e) {
          e.printStackTrace();
      }
  </code></pre>
  
  <p>But I am getting the following error:</p>
  
  <pre><code>    W/RecoverySystem( 3900): !!! REBOOTING TO INSTALL /data/update.zip !!!
      W/System.err( 3900): java.io.FileNotFoundException: /cache/recovery/command: open failed: ENOENT (No such file or directory)
      W/System.err( 3900):    at libcore.io.IoBridge.open(IoBridge.java:456)
      W/System.err( 3900):    at java.io.FileOutputStream.&lt;init&gt;(FileOutputStream.java:87)
      W/System.err( 3900):    at java.io.FileOutputStream.&lt;init&gt;(FileOutputStream.java:72)
      W/System.err( 3900):    at java.io.FileWriter.&lt;init&gt;(FileWriter.java:42)
      W/System.err( 3900):    at android.os.RecoverySystem.bootCommand(RecoverySystem.java:454)
      W/com.test.ota( 3966): type=1400 audit(0.0:8): avc: denied { write } for comm=4173796E635461736B202331 name="/" dev="mmcblk0p22" ino=2 scontext=u:r:system_app:s0 tcontext=u:object_r:cache_file:s0 tclass=dir
  </code></pre>
  
  <p>The thing I am confused about is I am able to access data directory(which is not accessible to normal apps) when I am downloading, but I am not able to access /cache.</p>
  
  <p>How should I proceed so that i don't get permission denied to system app when accessing /cache ?</p>
  
  <p>My manifest file is:</p>
  
  <pre><code>    &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
  android:sharedUserId="android.uid.system"
  xmlns:tools="http://schemas.android.com/tools"
  package="com.test.ota"&gt;
  
  &lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/&gt;
  &lt;uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/&gt;
  &lt;uses-permission android:name="android.permission.INTERNET"/&gt;
  &lt;uses-permission android:name="android.permission.WRITE_SETTINGS" /&gt;
  &lt;uses-permission android:name="android.permission.ACCESS_SUPERUSER" /&gt;
  &lt;uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /&gt;
  &lt;uses-permission android:name="android.permission.WAKE_LOCK" /&gt;
  &lt;uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /&gt;
  &lt;uses-permission android:name="android.permission.VIBRATE" /&gt;
  &lt;uses-permission
      android:name="android.permission.ACCESS_CACHE_FILESYSTEM"
      tools:ignore="ProtectedPermissions"/&gt;
  &lt;uses-permission
      android:name="android.permission.DELETE_CACHE_FILES"
      tools:ignore="ProtectedPermissions"/&gt;
  &lt;uses-permission
      android:name="android.permission.REBOOT"
      tools:ignore="ProtectedPermissions" /&gt;
  &lt;uses-permission android:name="android.permission.RECOVERY" /&gt;
  </code></pre>
  
  <p>Btw, my build branch is android-5.1.1_r19</p>
  
  <p><strong>Update 1:</strong>
  I built using full_mako-userdebug. So, I have su when needed in adb. In adb shell when I try creating <code>cache/recovery/command</code> after su, it allows me to create file. But through app it is not allowing me to create file in <code>cache/recovery/</code>, even though its system app. I don't want the user to root the phone just to install an ota update. Still having the same problem.</p>