From 7a1b76881f3b2d70e30156c35b5b85a6a91c3270 Mon Sep 17 00:00:00 2001 From: Michael Jumper Date: Tue, 17 Sep 2013 14:26:05 -0700 Subject: [PATCH] Implement pattern matching in dir search. --- src/protocols/rdp/guac_rdpdr/rdpdr_fs.c | 11 ++++++-- src/protocols/rdp/guac_rdpdr/rdpdr_fs.h | 10 +++++++ .../rdp/guac_rdpdr/rdpdr_fs_messages.c | 28 +++++++------------ 3 files changed, 28 insertions(+), 21 deletions(-) diff --git a/src/protocols/rdp/guac_rdpdr/rdpdr_fs.c b/src/protocols/rdp/guac_rdpdr/rdpdr_fs.c index 16269660..6cb772ea 100644 --- a/src/protocols/rdp/guac_rdpdr/rdpdr_fs.c +++ b/src/protocols/rdp/guac_rdpdr/rdpdr_fs.c @@ -39,6 +39,7 @@ #include #include #include +#include #ifdef ENABLE_WINPR #include @@ -109,8 +110,8 @@ static void __guac_rdpdr_fs_translate_path(guac_rdpdr_device* device, } -int guac_rdpdr_fs_open(guac_rdpdr_device* device, const char* path, - int access, int create_disposition) { +int guac_rdpdr_fs_open(guac_rdpdr_device* device, + const char* path, int access, int create_disposition) { guac_rdpdr_fs_data* data = (guac_rdpdr_fs_data*) device->data; char real_path[GUAC_RDPDR_FS_MAX_PATH]; @@ -205,6 +206,7 @@ int guac_rdpdr_fs_open(guac_rdpdr_device* device, const char* path, file = &(data->files[file_id]); file->fd = fd; file->dir = NULL; + file->dir_pattern[0] = '\0'; file->absolute_path = strdup(normalized_path); /* Attempt to pull file information */ @@ -240,7 +242,6 @@ int guac_rdpdr_fs_open(guac_rdpdr_device* device, const char* path, } - data->open_files++; return file_id; @@ -429,3 +430,7 @@ guac_rdpdr_fs_file* guac_rdpdr_fs_get_file(guac_rdpdr_device* device, } +int guac_rdpdr_fs_matches(const char* filename, const char* pattern) { + return fnmatch(pattern, filename, FNM_NOESCAPE) != 0; +} + diff --git a/src/protocols/rdp/guac_rdpdr/rdpdr_fs.h b/src/protocols/rdp/guac_rdpdr/rdpdr_fs.h index 23d7d8a7..d4eb6593 100644 --- a/src/protocols/rdp/guac_rdpdr/rdpdr_fs.h +++ b/src/protocols/rdp/guac_rdpdr/rdpdr_fs.h @@ -168,6 +168,11 @@ typedef struct guac_rdpdr_fs_file { */ struct dirent __dirent; + /** + * The pattern the check directory contents against, if any. + */ + char dir_pattern[GUAC_RDPDR_FS_MAX_PATH]; + /** * Bitwise OR of all associated Windows file attributes. */ @@ -264,5 +269,10 @@ const char* guac_rdpdr_fs_read_dir(guac_rdpdr_device* device, int file_id); guac_rdpdr_fs_file* guac_rdpdr_fs_get_file(guac_rdpdr_device* device, int file_id); +/** + * Returns whether the given filename matches the given pattern. + */ +int guac_rdpdr_fs_matches(const char* filename, const char* pattern); + #endif diff --git a/src/protocols/rdp/guac_rdpdr/rdpdr_fs_messages.c b/src/protocols/rdp/guac_rdpdr/rdpdr_fs_messages.c index 21a6856c..3e4b6540 100644 --- a/src/protocols/rdp/guac_rdpdr/rdpdr_fs_messages.c +++ b/src/protocols/rdp/guac_rdpdr/rdpdr_fs_messages.c @@ -366,37 +366,29 @@ void guac_rdpdr_fs_process_query_directory(guac_rdpdr_device* device, wStream* i /* If this is the first query, the path is included after padding */ if (initial_query) { - char path[GUAC_RDPDR_FS_MAX_PATH]; - Stream_Seek(input_stream, 23); /* Padding */ /* FIXME: Validate path length */ /* Convert path to UTF-8 */ guac_rdp_utf16_to_utf8(Stream_Pointer(input_stream), - path, path_length/2 - 1); - - /* STUB: path */ - guac_client_log_info(device->rdpdr->client, - "%s: STUB: Unused variable: path (%s)", __func__, - path); + file->dir_pattern, path_length/2 - 1); } - /* If entry exists, call appriate handler to send data */ - entry_name = guac_rdpdr_fs_read_dir(device, file_id); - if (entry_name != NULL) { - - int entry_file_id; + /* Find first matching entry in directory */ + while ((entry_name = guac_rdpdr_fs_read_dir(device, file_id)) != NULL) { /* Convert to absolute path */ char entry_path[GUAC_RDPDR_FS_MAX_PATH]; - if (guac_rdpdr_fs_convert_path(file->absolute_path, entry_name, entry_path)) - guac_client_log_info(device->rdpdr->client, - "Conversion failed: parent=\"%s\", name=\"%s\"", - file->absolute_path, entry_name); /* FIXME: Return no-more-files */ + if (guac_rdpdr_fs_convert_path(file->absolute_path, + entry_name, entry_path) == 0) { - else { + int entry_file_id; + + /* Pattern defined and match fails, continue with next file */ + if (guac_rdpdr_fs_matches(entry_path, file->dir_pattern)) + continue; /* Open directory entry */ entry_file_id = guac_rdpdr_fs_open(device, entry_path,