Implement pattern matching in dir search.

This commit is contained in:
Michael Jumper 2013-09-17 14:26:05 -07:00
parent c2e1065ea2
commit 7a1b76881f
3 changed files with 28 additions and 21 deletions

View File

@ -39,6 +39,7 @@
#include <sys/stat.h>
#include <dirent.h>
#include <fcntl.h>
#include <fnmatch.h>
#ifdef ENABLE_WINPR
#include <winpr/stream.h>
@ -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;
}

View File

@ -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

View File

@ -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,