Switch writing to <utils/file.h>.

Change-Id: Idb2de24414f4dd8e926e625b62e4d12152dc4527
diff --git a/server/RouteController.cpp b/server/RouteController.cpp
index cd5300d..b47acd4 100644
--- a/server/RouteController.cpp
+++ b/server/RouteController.cpp
@@ -23,6 +23,8 @@
 #include <net/if.h>
 #include <sys/stat.h>
 
+#include <private/android_filesystem_config.h>
+
 #include <map>
 
 #include "Fwmark.h"
@@ -31,6 +33,7 @@
 #define LOG_TAG "Netd"
 #include "log/log.h"
 #include "logwrap/logwrap.h"
+#include "utils/file.h"
 #include "resolv_netid.h"
 
 namespace {
@@ -91,7 +94,6 @@
 const bool MODIFY_NON_UID_BASED_RULES = true;
 
 const char* const RT_TABLES_PATH = "/data/misc/net/rt_tables";
-const int RT_TABLES_FLAGS = O_CREAT | O_TRUNC | O_WRONLY | O_NOFOLLOW | O_CLOEXEC;
 const mode_t RT_TABLES_MODE = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;  // mode 0644, rw-r--r--
 
 const unsigned ROUTE_FLUSH_ATTEMPTS = 2;
@@ -162,21 +164,10 @@
         addTableName(entry.second, entry.first, &contents);
     }
 
-    int fd = open(RT_TABLES_PATH, RT_TABLES_FLAGS, RT_TABLES_MODE);
-    if (fd == -1) {
-        ALOGE("failed to create %s (%s)", RT_TABLES_PATH, strerror(errno));
+    if (!android::WriteStringToFile(contents, RT_TABLES_PATH, RT_TABLES_MODE, AID_SYSTEM, AID_WIFI)) {
+        ALOGE("failed to write to %s (%s)", RT_TABLES_PATH, strerror(errno));
         return;
     }
-    // File creation is affected by umask, so make sure the right mode bits are set.
-    if (fchmod(fd, RT_TABLES_MODE) == -1) {
-        ALOGE("failed to set mode 0%o on %s (%s)", RT_TABLES_MODE, RT_TABLES_PATH, strerror(errno));
-    }
-    ssize_t bytesWritten = write(fd, contents.data(), contents.size());
-    if (bytesWritten != static_cast<ssize_t>(contents.size())) {
-        ALOGE("failed to write to %s (%zd vs %zu bytes) (%s)", RT_TABLES_PATH, bytesWritten,
-              contents.size(), strerror(errno));
-    }
-    close(fd);
 }
 
 // Sends a netlink request and expects an ack.
diff --git a/server/SoftapController.cpp b/server/SoftapController.cpp
index 17982d5..558aa06 100644
--- a/server/SoftapController.cpp
+++ b/server/SoftapController.cpp
@@ -37,6 +37,8 @@
 #include <cutils/log.h>
 #include <netutils/ifc.h>
 #include <private/android_filesystem_config.h>
+#include <utils/file.h>
+#include <utils/stringprintf.h>
 #include "wifi.h"
 #include "ResponseCode.h"
 
@@ -112,13 +114,8 @@
  *  argv[7] - Key
  */
 int SoftapController::setSoftap(int argc, char *argv[]) {
-    char psk_str[2*SHA256_DIGEST_LENGTH+1];
-    int ret = ResponseCode::SoftapStatusResult;
-    int fd;
     int hidden = 0;
     int channel = AP_CHANNEL_DEFAULT;
-    char *wbuf = NULL;
-    char *fbuf = NULL;
 
     if (argc < 5) {
         ALOGE("Softap set is missing arguments. Please use:");
@@ -135,62 +132,42 @@
             channel = AP_CHANNEL_DEFAULT;
     }
 
-    asprintf(&wbuf, "interface=%s\ndriver=nl80211\nctrl_interface="
-            "/data/misc/wifi/hostapd\nssid=%s\nchannel=%d\nieee80211n=1\n"
-            "hw_mode=g\nignore_broadcast_ssid=%d\nwowlan_triggers=any\n",
-            argv[2], argv[3], channel, hidden);
+    std::string wbuf(android::StringPrintf("interface=%s\n"
+            "driver=nl80211\n"
+            "ctrl_interface=/data/misc/wifi/hostapd\n"
+            "ssid=%s\n"
+            "channel=%d\n"
+            "ieee80211n=1\n"
+            "hw_mode=g\n"
+            "ignore_broadcast_ssid=%d\n"
+            "wowlan_triggers=any\n",
+            argv[2], argv[3], channel, hidden));
 
+    std::string fbuf;
     if (argc > 7) {
+        char psk_str[2*SHA256_DIGEST_LENGTH+1];
         if (!strcmp(argv[6], "wpa-psk")) {
             generatePsk(argv[3], argv[7], psk_str);
-            asprintf(&fbuf, "%swpa=3\nwpa_pairwise=TKIP CCMP\nwpa_psk=%s\n", wbuf, psk_str);
+            fbuf = android::StringPrintf("%swpa=3\nwpa_pairwise=TKIP CCMP\nwpa_psk=%s\n", wbuf.c_str(), psk_str);
         } else if (!strcmp(argv[6], "wpa2-psk")) {
             generatePsk(argv[3], argv[7], psk_str);
-            asprintf(&fbuf, "%swpa=2\nrsn_pairwise=CCMP\nwpa_psk=%s\n", wbuf, psk_str);
+            fbuf = android::StringPrintf("%swpa=2\nrsn_pairwise=CCMP\nwpa_psk=%s\n", wbuf.c_str(), psk_str);
         } else if (!strcmp(argv[6], "open")) {
-            asprintf(&fbuf, "%s", wbuf);
+            fbuf = wbuf;
         }
     } else if (argc > 6) {
         if (!strcmp(argv[6], "open")) {
-            asprintf(&fbuf, "%s", wbuf);
+            fbuf = wbuf;
         }
     } else {
-        asprintf(&fbuf, "%s", wbuf);
+        fbuf = wbuf;
     }
 
-    fd = open(HOSTAPD_CONF_FILE, O_CREAT | O_TRUNC | O_WRONLY | O_NOFOLLOW | O_CLOEXEC, 0660);
-    if (fd < 0) {
-        ALOGE("Cannot update \"%s\": %s", HOSTAPD_CONF_FILE, strerror(errno));
-        free(wbuf);
-        free(fbuf);
-        return ResponseCode::OperationFailed;
-    }
-    if (write(fd, fbuf, strlen(fbuf)) < 0) {
+    if (!android::WriteStringToFile(fbuf, HOSTAPD_CONF_FILE, 0660, AID_SYSTEM, AID_WIFI)) {
         ALOGE("Cannot write to \"%s\": %s", HOSTAPD_CONF_FILE, strerror(errno));
-        ret = ResponseCode::OperationFailed;
-    }
-    free(wbuf);
-    free(fbuf);
-
-    /* Note: apparently open can fail to set permissions correctly at times */
-    if (fchmod(fd, 0660) < 0) {
-        ALOGE("Error changing permissions of %s to 0660: %s",
-                HOSTAPD_CONF_FILE, strerror(errno));
-        close(fd);
-        unlink(HOSTAPD_CONF_FILE);
         return ResponseCode::OperationFailed;
     }
-
-    if (fchown(fd, AID_SYSTEM, AID_WIFI) < 0) {
-        ALOGE("Error changing group ownership of %s to %d: %s",
-                HOSTAPD_CONF_FILE, AID_WIFI, strerror(errno));
-        close(fd);
-        unlink(HOSTAPD_CONF_FILE);
-        return ResponseCode::OperationFailed;
-    }
-
-    close(fd);
-    return ret;
+    return ResponseCode::SoftapStatusResult;
 }
 
 /*