Merge "Make ModifyPackagingSpec generic to any filesystem" into main
diff --git a/filesystem/filesystem.go b/filesystem/filesystem.go
index 29e5ec3..fa66451 100644
--- a/filesystem/filesystem.go
+++ b/filesystem/filesystem.go
@@ -301,13 +301,24 @@
 	}
 	if proptools.Bool(f.properties.Is_auto_generated) { // TODO (spandandas): Remove this.
 		pt := f.PartitionType()
-		return pt == "ramdisk" || ps.Partition() == pt
+		return ps.Partition() == pt || strings.HasPrefix(ps.Partition(), pt+"/")
 	}
 	return true
 }
 
 func (f *filesystem) ModifyPackagingSpec(ps *android.PackagingSpec) {
-	// do nothing by default
+	// Sometimes, android.modulePartition() returns a path with >1 path components.
+	// This makes the partition field of packagingSpecs have multiple components, like
+	// "system/product". Right now, the filesystem module doesn't look at the partition field
+	// when deciding what path to install the file under, only the RelPathInPackage field, so
+	// we move the later path components from partition to relPathInPackage. This should probably
+	// be revisited in the future.
+	prefix := f.PartitionType() + "/"
+	if strings.HasPrefix(ps.Partition(), prefix) {
+		subPartition := strings.TrimPrefix(ps.Partition(), prefix)
+		ps.SetPartition(f.PartitionType())
+		ps.SetRelPathInPackage(filepath.Join(subPartition, ps.RelPathInPackage()))
+	}
 }
 
 var pctx = android.NewPackageContext("android/soong/filesystem")
diff --git a/filesystem/system_image.go b/filesystem/system_image.go
index 4d176ae..d03eab4 100644
--- a/filesystem/system_image.go
+++ b/filesystem/system_image.go
@@ -18,7 +18,6 @@
 	"android/soong/android"
 	"android/soong/linkerconfig"
 
-	"path/filepath"
 	"strings"
 
 	"github.com/google/blueprint/proptools"
@@ -64,11 +63,3 @@
 		(ps.Partition() == "system" || ps.Partition() == "root" ||
 			strings.HasPrefix(ps.Partition(), "system/"))
 }
-
-func (s *systemImage) ModifyPackagingSpec(ps *android.PackagingSpec) {
-	if strings.HasPrefix(ps.Partition(), "system/") {
-		subPartition := strings.TrimPrefix(ps.Partition(), "system/")
-		ps.SetPartition("system")
-		ps.SetRelPathInPackage(filepath.Join(subPartition, ps.RelPathInPackage()))
-	}
-}