Skip to content

Commit 7c68a90

Browse files
CopilotJohnAmadis
andcommitted
Extract path comparison logic into reusable helper function
Co-authored-by: JohnAmadis <17320783+JohnAmadis@users.noreply.github.com>
1 parent 24840f0 commit 7c68a90

1 file changed

Lines changed: 38 additions & 22 deletions

File tree

src/dmdevfs.c

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ static Dmod_Context_t* prepare_driver_module(const char* driver_name, bool* was_
8282
static void cleanup_driver_module(const char* driver_name, bool was_loaded, bool was_enabled);
8383
static int read_driver_parent_directory( const driver_node_t* node, char* path_buffer, size_t buffer_size );
8484
static int read_driver_node_path( const driver_node_t* node, char* path_buffer, size_t buffer_size );
85+
static int compare_paths_ignore_trailing_slash( const char* path1, const char* path2 );
8586
static int compare_driver_directory( const void* data, const void* user_data );
8687
static int compare_driver_node_path( const void* data, const void* user_data );
8788
static int compare_driver(const void* data, const void* user_data );
@@ -643,7 +644,7 @@ dmod_dmfsi_dif_api_declaration( 1.0, dmdevfs, int, _readdir, (dmfsi_context_t ct
643644
return DMFSI_ERR_GENERAL;
644645
}
645646

646-
bool file_should_be_listed = strcmp(dir_node->directory_path, parent_dir) == 0;
647+
bool file_should_be_listed = compare_paths_ignore_trailing_slash(dir_node->directory_path, parent_dir) == 0;
647648
if(file_should_be_listed)
648649
{
649650
strncpy(entry->name, driver->path, sizeof(entry->name));
@@ -1169,46 +1170,61 @@ static int read_driver_node_path( const driver_node_t* node, char* path_buffer,
11691170
}
11701171

11711172
/**
1172-
* @brief Compare the path of a driver directory with a given path
1173+
* @brief Compare two paths, ignoring trailing slashes
1174+
* @return 0 if equal, non-zero if different
11731175
*/
1174-
static int compare_driver_directory( const void* data, const void* user_data )
1176+
static int compare_paths_ignore_trailing_slash( const char* path1, const char* path2 )
11751177
{
1176-
const driver_node_t* node = (const driver_node_t*)data;
1177-
const char* path = (const char*)user_data;
1178-
if (node == NULL || path == NULL)
1179-
{
1180-
return 0;
1181-
}
1182-
1183-
char parent_dir[MAX_PATH_LENGTH] = {0};
1184-
if(read_driver_parent_directory(node, parent_dir, sizeof(parent_dir)) != 0)
1178+
if (path1 == NULL || path2 == NULL)
11851179
{
1186-
return -1;
1180+
return (path1 == path2) ? 0 : 1;
11871181
}
11881182

1189-
// Compare paths, handling optional trailing slashes
11901183
// Get lengths
1191-
size_t path_len = strlen(path);
1192-
size_t parent_len = strlen(parent_dir);
1184+
size_t len1 = strlen(path1);
1185+
size_t len2 = strlen(path2);
11931186

11941187
// Remove trailing slashes from both paths for comparison
1195-
while (path_len > 1 && path[path_len - 1] == '/')
1188+
// Keep at least "/" if that's the entire path
1189+
while (len1 > 1 && path1[len1 - 1] == '/')
11961190
{
1197-
path_len--;
1191+
len1--;
11981192
}
1199-
while (parent_len > 1 && parent_dir[parent_len - 1] == '/')
1193+
while (len2 > 1 && path2[len2 - 1] == '/')
12001194
{
1201-
parent_len--;
1195+
len2--;
12021196
}
12031197

12041198
// Lengths must match
1205-
if (path_len != parent_len)
1199+
if (len1 != len2)
12061200
{
12071201
return 1;
12081202
}
12091203

12101204
// Content must match
1211-
return strncmp(path, parent_dir, path_len);
1205+
return strncmp(path1, path2, len1);
1206+
}
1207+
1208+
/**
1209+
* @brief Compare the path of a driver directory with a given path
1210+
*/
1211+
static int compare_driver_directory( const void* data, const void* user_data )
1212+
{
1213+
const driver_node_t* node = (const driver_node_t*)data;
1214+
const char* path = (const char*)user_data;
1215+
if (node == NULL || path == NULL)
1216+
{
1217+
return 0;
1218+
}
1219+
1220+
char parent_dir[MAX_PATH_LENGTH] = {0};
1221+
if(read_driver_parent_directory(node, parent_dir, sizeof(parent_dir)) != 0)
1222+
{
1223+
return -1;
1224+
}
1225+
1226+
// Use helper function to compare paths, handling optional trailing slashes
1227+
return compare_paths_ignore_trailing_slash(path, parent_dir);
12121228
}
12131229

12141230
/**

0 commit comments

Comments
 (0)