hidl-gen: work as expected with various path args

Add root path to paths only if non-absolute

Test: all of these pass and provide expected results:
    hidl-gen -Lhash -p ~/android/aosp/ android.hardware.nfc@1.0
    hidl-gen -Lhash -p /usr/local/google/home/smoreland/android/aosp/ android.hardware.nfc@1.0
    hidl-gen -Lhash -p /usr/local/google/home/smoreland/android \
                    -randroid.hidl:master/system/libhidl/transport \
                    -randroid.hardware:aosp/hardware/interfaces android.hardware.nfc@1.0
    hidl-gen -Lhash android.hardware.nfc@1.0
    hidl-gen -Lhash -randroid.hidl:/usr/local/google/home/smoreland/android/master/system/libhidl/transport \
                    -randroid.hardware:/usr/local/google/home/smoreland/android/master/hardware/interfaces \
                    android.hardware.nfc@1.0
    hidl-gen -Lhash -randroid.hidl:system/libhidl/transport \
                    -randroid.hardware:/usr/local/google/home/smoreland/android/aosp/hardware/interfaces \
                    android.hardware.nfc@1.0
Fixes: 62106647

Change-Id: Ibfce35f68e6b2ac0eaecc08fed3c78701464f1d6
diff --git a/Coordinator.cpp b/Coordinator.cpp
index b4cff49..19cb381 100644
--- a/Coordinator.cpp
+++ b/Coordinator.cpp
@@ -91,7 +91,7 @@
         // fall through.
     }
 
-    std::string path = mRootPath + getPackagePath(fqName);
+    std::string path = getAbsolutePackagePath(fqName);
 
     path.append(fqName.name());
     path.append(".hal");
@@ -216,6 +216,16 @@
     return ret;
 }
 
+std::string Coordinator::getAbsolutePackagePath(const FQName& fqName) const {
+    const std::string packagePath = getPackagePath(fqName);
+
+    if (StringHelper::StartsWith(packagePath, "/") || mRootPath.empty()) {
+        return packagePath;
+    }
+
+    return StringHelper::RTrim(mRootPath, "/") + "/" + packagePath;
+}
+
 std::string Coordinator::getPackageRoot(const FQName &fqName) const {
     auto it = findPackageRoot(fqName);
     auto prefix = *it;
@@ -280,7 +290,7 @@
         std::vector<std::string> *fileNames) const {
     fileNames->clear();
 
-    const std::string packagePath = mRootPath + getPackagePath(package);
+    const std::string packagePath = getAbsolutePackagePath(package);
 
     DIR *dir = opendir(packagePath.c_str());
 
@@ -421,7 +431,7 @@
     FQName prevPackage = currentPackage;
     while (prevPackage.getPackageMinorVersion() > 0) {
         prevPackage = prevPackage.downRev();
-        if (existdir((mRootPath + getPackagePath(prevPackage)).c_str())) {
+        if (existdir(getAbsolutePackagePath(prevPackage).c_str())) {
             hasPrevPackage = true;
             break;
         }
diff --git a/Coordinator.h b/Coordinator.h
index efa1cad..c401995 100644
--- a/Coordinator.h
+++ b/Coordinator.h
@@ -58,7 +58,6 @@
     // FQName of "android.hardware.nfc@1.0::INfc, then getPackagePath()
     // will return "hardware/interfaces/nfc/1.0" (if sanitized = false)
     // or "hardware/interfaces/nfc/V1_0" (if sanitized = true).
-
     std::string getPackagePath(
             const FQName &fqName, bool relative = false,
             bool sanitized = false) const;
@@ -67,7 +66,6 @@
     // "vendor.<something>.hardware"] and a FQName of
     // "android.hardware.nfc@1.0::INfc, then getPackageRoot() will
     // return "android.hardware".
-
     std::string getPackageRoot(const FQName &fqName) const;
 
     // Given package-root paths of ["hardware/interfaces",
@@ -75,7 +73,6 @@
     // ["android.hardware", "vendor.<something>.hardware"], and a
     // FQName of "android.hardware.nfc@1.0::INfc, then getPackageRootPath()
     // will return "hardware/interfaces".
-
     std::string getPackageRootPath(const FQName &fqName) const;
 
     // return getPackageRoot + ":" + getPackageRootPath
@@ -122,6 +119,14 @@
     std::vector<std::string>::const_iterator findPackageRoot(
             const FQName &fqName) const;
 
+    // Returns abs package path by prepending the root path if a package
+    // path is non-absolute.
+    // If root is '/android/master' and getPackagePath returns 'h/i/nfc/V1_0'
+    // this will return '/android/master/h/i/nfc/V1_0'.
+    // If root is '/android/master' and getPackagePath returns '/abs/path/to/nfc/V1_0'
+    // this will return '/abs/path/to/nfc/V1_0'
+    std::string getAbsolutePackagePath(const FQName& fqName) const;
+
     // Rules of enforceRestrictionsOnPackage are listed below.
     status_t enforceMinorVersionUprevs(const FQName &fqName) const;
     status_t enforceHashes(const FQName &fqName) const;