From 29603a327a8bdbe2241200ccef7df8a36415fcf4 Mon Sep 17 00:00:00 2001 From: Uwe Seimet Date: Sun, 19 Dec 2021 19:49:56 +0100 Subject: [PATCH] Added optional folder name filter --- src/raspberrypi/rascsi.cpp | 6 ++--- src/raspberrypi/rascsi_interface.proto | 6 +++-- src/raspberrypi/rascsi_response.cpp | 33 ++++++++++++++++---------- src/raspberrypi/rascsi_response.h | 9 +++---- 4 files changed, 33 insertions(+), 21 deletions(-) diff --git a/src/raspberrypi/rascsi.cpp b/src/raspberrypi/rascsi.cpp index 6e59afda..e70f639b 100644 --- a/src/raspberrypi/rascsi.cpp +++ b/src/raspberrypi/rascsi.cpp @@ -1488,8 +1488,8 @@ static void *MonThread(void *param) PbResult result; result.set_allocated_server_info(rascsi_response.GetServerInfo( - result, devices, reserved_ids, current_log_level, GetParam(command, "filename_pattern"), - scan_depth)); + result, devices, reserved_ids, current_log_level, GetParam(command, "folder_pattern"), + GetParam(command, "file_pattern"), scan_depth)); SerializeMessage(fd, result); break; } @@ -1517,7 +1517,7 @@ static void *MonThread(void *param) PbResult result; result.set_allocated_image_files_info(rascsi_response.GetAvailableImages(result, - GetParam(command, "filename_pattern"), scan_depth)); + GetParam(command, "folder_pattern"), GetParam(command, "file_pattern"), scan_depth)); SerializeMessage(fd, result); break; } diff --git a/src/raspberrypi/rascsi_interface.proto b/src/raspberrypi/rascsi_interface.proto index c037f602..8d239796 100644 --- a/src/raspberrypi/rascsi_interface.proto +++ b/src/raspberrypi/rascsi_interface.proto @@ -68,7 +68,8 @@ enum PbOperation { // Gets the server information (PbServerInfo). Calling this operation should be avoided because it // may return a lot of data. More specific other operations should be used instead. - // "filename_pattern": Optional filter, only filenames containing the case-insensitive pattern are returned + // "folder_pattern": Optional filter, only folder names containing the case-insensitive pattern are returned + // "file_pattern": Optional filter, only filenames containing the case-insensitive pattern are returned SERVER_INFO = 10; // Get rascsi version information (PbVersionInfo) @@ -83,7 +84,8 @@ enum PbOperation { // Get information on available image files in the default image folder (PbImageFilesInfo) // Parameters: - // "filename_pattern": Optional filter, only filenames containing the case-insensitive pattern are returned + // "folder_pattern": Optional filter, only folder names containing the case-insensitive pattern are returned + // "file_pattern": Optional filter, only filenames containing the case-insensitive pattern are returned DEFAULT_IMAGE_FILES_INFO = 14; // Get information on an image file (not necessarily in the default image folder) based on an absolute path. diff --git a/src/raspberrypi/rascsi_response.cpp b/src/raspberrypi/rascsi_response.cpp index 4a055725..a9439e3e 100644 --- a/src/raspberrypi/rascsi_response.cpp +++ b/src/raspberrypi/rascsi_response.cpp @@ -141,9 +141,12 @@ bool RascsiResponse::GetImageFile(PbImageFile *image_file, const string& filenam } void RascsiResponse::GetAvailableImages(PbImageFilesInfo& image_files_info, const string& default_image_folder, - const string& folder, const string& pattern, int scan_depth) { - string pattern_lower = pattern; - transform(pattern_lower.begin(), pattern_lower.end(), pattern_lower.begin(), ::tolower); + const string& folder, const string& folder_pattern, const string& file_pattern, int scan_depth) { + string folder_pattern_lower = folder_pattern; + transform(folder_pattern_lower.begin(), folder_pattern_lower.end(), folder_pattern_lower.begin(), ::tolower); + + string file_pattern_lower = file_pattern; + transform(file_pattern_lower.begin(), file_pattern_lower.end(), file_pattern_lower.begin(), ::tolower); if (scan_depth-- >= 0) { DIR *d = opendir(folder.c_str()); @@ -153,7 +156,7 @@ void RascsiResponse::GetAvailableImages(PbImageFilesInfo& image_files_info, cons string filename = folder + "/" + dir->d_name; string name_lower = filename; - if (!pattern.empty()) { + if (!file_pattern.empty()) { transform(name_lower.begin(), name_lower.end(), name_lower.begin(), ::tolower); } @@ -169,11 +172,14 @@ void RascsiResponse::GetAvailableImages(PbImageFilesInfo& image_files_info, cons LOGTRACE("Symlink '%s' in image folder '%s' is broken", dir->d_name, folder.c_str()); continue; } else if (dir->d_type == DT_DIR) { - GetAvailableImages(image_files_info, default_image_folder, filename, pattern, scan_depth); + if (folder_pattern_lower.empty() || name_lower.find(folder_pattern_lower) != string::npos) { + GetAvailableImages(image_files_info, default_image_folder, filename, folder_pattern, + file_pattern, scan_depth); + } continue; } - if (pattern.empty() || name_lower.find(pattern_lower) != string::npos) { + if (file_pattern_lower.empty() || name_lower.find(file_pattern_lower) != string::npos) { PbImageFile *image_file = new PbImageFile(); if (GetImageFile(image_file, filename)) { GetImageFile(image_files_info.add_image_files(), filename.substr(default_image_folder.length() + 1)); @@ -188,23 +194,26 @@ void RascsiResponse::GetAvailableImages(PbImageFilesInfo& image_files_info, cons } } -PbImageFilesInfo *RascsiResponse::GetAvailableImages(PbResult& result, const string& pattern, int scan_depth) +PbImageFilesInfo *RascsiResponse::GetAvailableImages(PbResult& result, const string& folder_pattern, + const string& file_pattern, int scan_depth) { PbImageFilesInfo *image_files_info = new PbImageFilesInfo(); string default_image_folder = rascsi_image->GetDefaultImageFolder(); image_files_info->set_default_image_folder(default_image_folder); - GetAvailableImages(*image_files_info, default_image_folder, default_image_folder, pattern, scan_depth); + GetAvailableImages(*image_files_info, default_image_folder, default_image_folder, folder_pattern, file_pattern, + scan_depth); result.set_status(true); return image_files_info; } -void RascsiResponse::GetAvailableImages(PbResult& result, PbServerInfo& server_info, const string& pattern, int scan_depth) +void RascsiResponse::GetAvailableImages(PbResult& result, PbServerInfo& server_info, const string& folder_pattern, + const string& file_pattern, int scan_depth) { - PbImageFilesInfo *image_files_info = GetAvailableImages(result, pattern, scan_depth); + PbImageFilesInfo *image_files_info = GetAvailableImages(result, folder_pattern, file_pattern, scan_depth); image_files_info->set_default_image_folder(rascsi_image->GetDefaultImageFolder()); server_info.set_allocated_image_files_info(image_files_info); @@ -283,14 +292,14 @@ PbDeviceTypesInfo *RascsiResponse::GetDeviceTypesInfo(PbResult& result, const Pb } PbServerInfo *RascsiResponse::GetServerInfo(PbResult& result, const vector& devices, const set& reserved_ids, - const string& current_log_level, const string& filename_pattern, int scan_depth) + const string& current_log_level, const string& folder_pattern, const string& file_pattern, int scan_depth) { PbServerInfo *server_info = new PbServerInfo(); server_info->set_allocated_version_info(GetVersionInfo(result)); server_info->set_allocated_log_level_info(GetLogLevelInfo(result, current_log_level)); GetAllDeviceTypeProperties(*server_info->mutable_device_types_info()); - GetAvailableImages(result, *server_info, filename_pattern, scan_depth); + GetAvailableImages(result, *server_info, folder_pattern, file_pattern, scan_depth); server_info->set_allocated_network_interfaces_info(GetNetworkInterfacesInfo(result)); server_info->set_allocated_mapping_info(GetMappingInfo(result)); GetDevices(*server_info, devices); diff --git a/src/raspberrypi/rascsi_response.h b/src/raspberrypi/rascsi_response.h index d11ca74b..47ed05d5 100644 --- a/src/raspberrypi/rascsi_response.h +++ b/src/raspberrypi/rascsi_response.h @@ -29,13 +29,14 @@ public: ~RascsiResponse() {}; bool GetImageFile(PbImageFile *, const string&); - PbImageFilesInfo *GetAvailableImages(PbResult&, const string&, int); + PbImageFilesInfo *GetAvailableImages(PbResult&, const string&, const string&, int); PbReservedIdsInfo *GetReservedIds(PbResult&, const set&); void GetDevices(PbServerInfo&, const vector&); void GetDevicesInfo(PbResult&, const PbCommand&, const vector&, int); PbDeviceTypesInfo *GetDeviceTypesInfo(PbResult&, const PbCommand&); PbVersionInfo *GetVersionInfo(PbResult&); - PbServerInfo *GetServerInfo(PbResult&, const vector&, const set&, const string&, const string&, int); + PbServerInfo *GetServerInfo(PbResult&, const vector&, const set&, const string&, const string&, + const string&, int); PbNetworkInterfacesInfo *GetNetworkInterfacesInfo(PbResult&); PbMappingInfo *GetMappingInfo(PbResult&); PbLogLevelInfo *GetLogLevelInfo(PbResult&, const string&); @@ -51,6 +52,6 @@ private: void GetDevice(const Device *, PbDevice *); void GetAllDeviceTypeProperties(PbDeviceTypesInfo&); void GetDeviceTypeProperties(PbDeviceTypesInfo&, PbDeviceType); - void GetAvailableImages(PbImageFilesInfo&, const string&, const string&, const string&, int); - void GetAvailableImages(PbResult& result, PbServerInfo&, const string&, int); + void GetAvailableImages(PbImageFilesInfo&, const string&, const string&, const string&, const string&, int); + void GetAvailableImages(PbResult& result, PbServerInfo&, const string&, const string&, int); };