From c5aa9dfb4c1dff6d1e12149b7d77ffb5458ecde9 Mon Sep 17 00:00:00 2001
From: Adrien Destugues <pulkomandy@pulkomandy.tk>
Date: Sun, 24 May 2020 11:30:55 +0200
Subject: [PATCH] Create filesystem indices when creating Haiku image
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Fixes #4424.

Change-Id: I737ee4f9bb70ce48c0c94f3a862a3073da8c0c4d
Reviewed-on: https://review.haiku-os.org/c/haiku/+/2796
Reviewed-by: Jérôme Duval <jerome.duval@gmail.com>
---
 build/scripts/build_haiku_image | 42 ++++++++++++++++++++++++++++++++-
 src/tools/fs_shell/fssh.cpp     | 24 +++++++++++++++----
 2 files changed, 60 insertions(+), 6 deletions(-)

diff --git a/build/scripts/build_haiku_image b/build/scripts/build_haiku_image
index c9fe219f07..2fbae691e2 100755
--- a/build/scripts/build_haiku_image
+++ b/build/scripts/build_haiku_image
@@ -77,6 +77,7 @@ if [ $isCD ]; then
 	ln=ln
 	mkdir=mkdir
 	rm=rm
+	mkindex="mkindex -d $tPrefix"
 elif [ $isImage ]; then
 	# If FIFOs are used for the communication with the FS shell, prepare them.
 	if $fsShellCommand --uses-fifos; then
@@ -145,7 +146,7 @@ else
 	ln=ln
 	mkdir=mkdir
 	rm=rm
-	mkindex=mkindex
+	mkindex="mkindex -d $tPrefix"
 fi
 
 
@@ -291,6 +292,45 @@ if [ -n "$updateAllPackages" ]; then
 	$mkdir -p "${tPrefix}system/packages"
 fi
 
+echo "Creating filesystem indices..."
+$mkindex -t string Audio:Album
+$mkindex -t string Audio:Artist
+$mkindex -t string Media:Genre
+$mkindex -t string Media:Title
+$mkindex -t string MAIL:account
+$mkindex -t string MAIL:cc
+$mkindex -t string MAIL:chain
+$mkindex -t string MAIL:draft
+$mkindex -t string MAIL:flags
+$mkindex -t string MAIL:from
+$mkindex -t string MAIL:name
+$mkindex -t string MAIL:pending_chain
+$mkindex -t string MAIL:priority
+$mkindex -t string MAIL:reply
+$mkindex -t string MAIL:status
+$mkindex -t string MAIL:subject
+$mkindex -t string MAIL:thread
+$mkindex -t string MAIL:to
+$mkindex -t string META:address
+$mkindex -t string META:city
+$mkindex -t string META:company
+$mkindex -t string META:county
+$mkindex -t string META:email
+$mkindex -t string META:fax
+$mkindex -t string META:group
+$mkindex -t string META:hphone
+$mkindex -t string META:name
+$mkindex -t string META:nickname
+$mkindex -t string META:state
+$mkindex -t string META:url
+$mkindex -t string META:wphone
+$mkindex -t string META:zip
+
+$mkindex -t int32 Media:Rating
+$mkindex -t int32 Media:Year
+$mkindex -t int32 MAIL:account_id
+$mkindex -t int32 MAIL:read
+$mkindex -t int32 MAIL:when
 
 echo "Populating image ..."
 while [ $# -gt 0 ]; do
diff --git a/src/tools/fs_shell/fssh.cpp b/src/tools/fs_shell/fssh.cpp
index 97fbb2e86e..7a6f87734c 100644
--- a/src/tools/fs_shell/fssh.cpp
+++ b/src/tools/fs_shell/fssh.cpp
@@ -1007,12 +1007,27 @@ command_mkdir(int argc, const char* const* argv)
 static fssh_status_t
 command_mkindex(int argc, const char* const* argv)
 {
-	if (argc != 2) {
-		fprintf(stderr, "Usage: %s <index name>\n", argv[0]);
+	if (argc < 2) {
+		fprintf(stderr, "Usage: %s [-t <type>] <index name>\n", argv[0]);
 		return FSSH_B_BAD_VALUE;
 	}
 
-	const char* indexName = argv[1];
+	int fileArg = 1;
+	int type = FSSH_B_STRING_TYPE;
+
+	if (argc > 3 && strcmp(argv[1], "-t") == 0) {
+		fileArg = 3;
+		if (strcmp(argv[2], "string") == 0)
+			type = FSSH_B_STRING_TYPE;
+		else if (strcmp(argv[2], "int32") == 0)
+			type = FSSH_B_INT32_TYPE;
+		else {
+			fprintf(stderr, "Unhandled attribute type %s\n", argv[2]);
+			return FSSH_B_BAD_VALUE;
+		}
+	}
+
+	const char* indexName = argv[fileArg];
 
 	// get the volume ID
 	fssh_dev_t volumeID = get_volume_id();
@@ -1020,8 +1035,7 @@ command_mkindex(int argc, const char* const* argv)
 		return volumeID;
 
 	// create the index
-	fssh_status_t error =_kern_create_index(volumeID, indexName,
-		FSSH_B_STRING_TYPE, 0);
+	fssh_status_t error =_kern_create_index(volumeID, indexName, type, 0);
 	if (error != FSSH_B_OK) {
 		fprintf(stderr, "Error: Failed to create index \"%s\": %s\n",
 			indexName, fssh_strerror(error));