Adds RealPath utility function

Test: local boot
Bug: 111522903
Change-Id: If1d6f103167fe62858d11854121693f6387b1a21
diff --git a/common/libs/utils/files.cpp b/common/libs/utils/files.cpp
index f1bcea7..85c7ef7 100644
--- a/common/libs/utils/files.cpp
+++ b/common/libs/utils/files.cpp
@@ -16,6 +16,11 @@
 
 #include "common/libs/utils/files.h"
 
+#include <glog/logging.h>
+
+#include <array>
+#include <climits>
+#include <cstdlib>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <unistd.h>
@@ -24,17 +29,34 @@
 
 bool FileHasContent(const std::string& path) {
   struct stat st;
-  if (stat(path.c_str(), &st) == -1)
+  if (stat(path.c_str(), &st) == -1) {
     return false;
-  if (st.st_size == 0)
+  }
+  if (st.st_size == 0) {
     return false;
+  }
   return true;
 }
 
 bool DirectoryExists(const std::string& path) {
   struct stat st;
-  if (stat(path.c_str(), &st) == -1) return false;
-  if ((st.st_mode & S_IFMT) != S_IFDIR) return false;
+  if (stat(path.c_str(), &st) == -1) {
+    return false;
+  }
+  if ((st.st_mode & S_IFMT) != S_IFDIR) {
+    return false;
+  }
   return true;
 }
+
+std::string RealPath(const std::string& path) {
+  std::array<char, PATH_MAX> buffer{};
+  if (!realpath(path.c_str(), buffer.data())) {
+    LOG(WARNING) << "Could not get real path for file " << path << ": "
+                 << strerror(errno);
+    return {};
+  }
+  return {buffer.data()};
+}
+
 }  // namespace cvd
diff --git a/common/libs/utils/files.h b/common/libs/utils/files.h
index 5229b3a..51ba25e 100644
--- a/common/libs/utils/files.h
+++ b/common/libs/utils/files.h
@@ -20,4 +20,7 @@
 namespace cvd {
 bool FileHasContent(const std::string& path);
 bool DirectoryExists(const std::string& path);
+
+// returns empty string if realpath() fails
+std::string RealPath(const std::string& path);
 }  // namespace cvd
diff --git a/host/libs/config/cuttlefish_config.cpp b/host/libs/config/cuttlefish_config.cpp
index 260f050..9104b54 100644
--- a/host/libs/config/cuttlefish_config.cpp
+++ b/host/libs/config/cuttlefish_config.cpp
@@ -29,6 +29,7 @@
 #include <json/json.h>
 
 #include "common/libs/utils/environment.h"
+#include "common/libs/utils/files.h"
 
 DEFINE_string(config_file,
               vsoc::GetDefaultPerInstanceDir() + "/cuttlefish_config.json",
@@ -66,6 +67,7 @@
 
   return instance;
 }
+
 const char* kSerialNumber = "serial_number";
 const char* kInstanceDir = "instance_dir";
 
@@ -404,12 +406,10 @@
 }
 
 void CuttlefishConfig::LoadFromFile(const char* file) {
-  char real_file_path[PATH_MAX];
-  if (realpath(file, real_file_path) == nullptr) {
-    LOG(FATAL) << "Could not get real path for file " << file << ": "
-               << strerror(errno);
+  auto real_file_path = cvd::RealPath(file);
+  if (real_file_path.empty()) {
+    LOG(FATAL) << "Could not get real path for file " << file;
   }
-
   Json::Reader reader;
   std::ifstream ifs(real_file_path);
   if (!reader.parse(ifs, *dictionary_)) {