utils_expand_path fix
A message that is not in the archive (sent off-list or not yet received): 20131115154539.GA6986@thessa
A message that is not in the archive (sent off-list or not yet received): [email protected]
PATCH 1/5 Add missing copyright to utils.c
--- src/common/utils.c | 1 + 1 file changed, 1 insertion(+)
src/common/utils.c +1 -0
@@ -1,5 +1,6 @@/* * Copyright (C) 2012 - David Goulet <[email protected]> +* Copyright (C) 2013 - Raphaël Beamonte <[email protected]> * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License, version 2 only, as
Signature · 7 lines
1.7.10.4 _______________________________________________ lttng-dev mailing list [email protected] http://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev
PATCH 2/5 Fix: comment indentation
In reply to a message not in the archive--- src/common/utils.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
src/common/utils.c +1 -1
@@ -198,7 +198,7 @@ char *utils_expand_path(const char *path)if (*path !='/' &&strncmp (path,"./" ,2 ) !=0 &&strncmp (path,"../" ,3 ) !=0 ) {snprintf (absolute_path,PATH_MAX ,"./%s" , path); -/* Else, we just copy the path */ +/* Else, we just copy the path */ }else {strncpy (absolute_path, path,PATH_MAX ); }
Signature · 7 lines
1.7.10.4 _______________________________________________ lttng-dev mailing list [email protected] http://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev
PATCH 3/5 Fix: utils_expand_path now works for paths that ends with '/.' or '/..'
In reply to a message not in the archiveCases where the path given ended with '/.' or '/..' were not handled properly using the utils_expand_path function and the resulting path was still showing this end part. This fix aims to treat that last part and 'expand' it as expected.
--- src/common/utils.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+)
src/common/utils.c +27 -0
@@ -178,6 +178,8 @@ LTTNG_HIDDENchar *utils_expand_path (const char *path) {char *next, *previous, *slash, *start_path, *absolute_path =NULL ; +char *last_token; +int is_dot, is_dotdot;/* Safety net */ if (path ==NULL ) {
@@ -241,6 +243,31 @@ char *utils_expand_path(const char *path) absolute_path, PATH_MAX); } +/* Identify the last token */ + last_token =strrchr (absolute_path,'/' ); + +/* Verify that this token is not a relative path */ + is_dotdot = (strcmp (last_token,"/.." ) ==0 ); + is_dot = (strcmp (last_token,"/." ) ==0 ); + +/* If it is, take action */ +if (is_dot || is_dotdot) { +/* For both, remove this token */ + *last_token ='\0' ; + +/* If it was a reference to parent directory, go back one more time */ +if (is_dotdot) { + last_token =strrchr (absolute_path,'/' ); + +/* If there was only one level left, we keep the first '/' */ +if (last_token == absolute_path) { + last_token++; + } + + *last_token ='\0' ; + } + } +return absolute_path; error:
Signature · 7 lines
1.7.10.4 _______________________________________________ lttng-dev mailing list [email protected] http://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev
PATCH 4/5 Tests: add symlink tests for test_utils_expand_path
In reply to a message not in the archiveThese new test cases allows to test the utils_expand_path function with an existing tree using symlinks. It allows to verify the right behavior of the function in complex situation.
--- tests/unit/test_utils_expand_path.c | 173 ++++++++++++++++++++++++++++++++++- 1 file changed, 168 insertions(+), 5 deletions(-)
tests/unit/test_utils_expand_path.c +168 -5
@@ -21,6 +21,9 @@#include <stdlib.h> #include <limits.h> +#include <sys/stat.h> +#include <sys/types.h> +#include <tap/tap.h> #include <src/common/utils.h>
@@ -35,6 +38,16 @@ struct valid_test_input {char *absolute_part; }; +struct tree_symlink { +char *orig; +char *dest; +}; + +struct symlink_test_input { +char *input; +char *expected_result; +}; +/* Valid test cases */ static struct valid_test_input valid_tests_inputs[] = { {"/a/b/c/d/e" ,"" ,"/a/b/c/d/e" },
@@ -62,6 +75,39 @@ char **valid_tests_expected_results;static const int num_valid_tests =sizeof (valid_tests_inputs) /sizeof (valid_tests_inputs[0 ]); +/* Symlinks test cases */ +char tree_origin[] ="/tmp/test_utils_expand_path.XXXXXX" ; + +static const char *const tree_dirs[] = { +"a" , +"a/b" , +"a/b/c" , +"a/e" , +}; +static const int num_tree_dirs = +sizeof (tree_dirs) /sizeof (tree_dirs[0 ]); + +static struct tree_symlink tree_symlinks[] = { + {"a/d" ,"b/c/" }, + {"a/g" ,"d/" }, + {"a/b/f" ,"../e/" }, + {"a/b/h" ,"../g/" }, + {"a/b/k" ,"c/g/" }, + {"a/b/c/g" ,"../../../" }, +}; +static const int num_tree_symlinks = +sizeof (tree_symlinks) /sizeof (tree_symlinks[0 ]); + +static struct symlink_test_input symlink_tests_inputs[] = { + {"a/g/../l/." ,"a/b/l" }, + {"a/g/../l/./" ,"a/b/l/" }, + {"a/g/../l/.." ,"a/b" }, + {"a/g/../l/../" ,"a/b/" }, + {"a/b/h/g/" ,"" }, +}; +static const int num_symlink_tests = +sizeof (symlink_tests_inputs) /sizeof (symlink_tests_inputs[0 ]); +/* Invalid test cases */ static char *invalid_tests_inputs[] = {NULL ,
@@ -69,6 +115,13 @@ static char *invalid_tests_inputs[] = {static const int num_invalid_tests =sizeof (invalid_tests_inputs) /sizeof (invalid_tests_inputs[0 ]); +#define ERRSIZE 100 +char errmsg[ERRSIZE ]; +static void printerr (char *msg) +{ +fprintf (stderr,"test_utils_expand_path: error: %s\n" , msg); +} +int prepare_valid_results () {int i;
@@ -85,7 +138,7 @@ int prepare_valid_results()for (i =0 ; i < num_valid_tests; i++) { valid_tests_expected_results[i] =malloc (PATH_MAX );if (valid_tests_expected_results[i] ==NULL ) { -fprintf (stderr,"malloc expected results" ); +printerr ("malloc expected results" );return 1 ; }
@@ -124,14 +177,96 @@ int free_valid_results()return 0 ; } +int prepare_symlink_tree () +{ +int i; +char tmppath[PATH_MAX ]; + +/* Create the temporary directory */ +if (mkdtemp (tree_origin) ==NULL ) { +printerr ("mkdtemp" ); + goto error; + } + +/* Create the directories of the test tree */ +for (i =0 ; i < num_tree_dirs; i++) { +snprintf (tmppath,PATH_MAX ,"%s/%s" , tree_origin, tree_dirs[i]); + +if (mkdir (tmppath,0755 ) !=0 ) { +snprintf (errmsg,ERRSIZE ,"mkdir %s" , tmppath); +printerr (errmsg); + goto error; + } + } + +/* Create the symlinks of the test tree */ +for (i =0 ; i < num_tree_symlinks; i++) { +snprintf (tmppath,PATH_MAX ,"%s/%s" , + tree_origin, tree_symlinks[i].orig); + +if (symlink (tree_symlinks[i].dest, tmppath) !=0 ) { +snprintf (errmsg,ERRSIZE ,"symlink %s to %s" , + tmppath, tree_symlinks[i].dest); +printerr (errmsg); + goto error; + } + } + +return 0 ; + +error: +return 1 ; +} + +int free_symlink_tree () +{ +int i; +char tmppath[PATH_MAX ]; + +/* Remove the symlinks from the test tree */ +for (i = num_tree_symlinks -1 ; i >-1 ; i--) { +snprintf (tmppath,PATH_MAX ,"%s/%s" , + tree_origin, tree_symlinks[i].orig); + +if (unlink (tmppath) !=0 ) { +snprintf (errmsg,ERRSIZE ,"unlink %s" , tmppath); +printerr (errmsg); + goto error; + } + } + +/* Remove the directories from the test tree */ +for (i = num_tree_dirs -1 ; i >-1 ; i--) { +snprintf (tmppath,PATH_MAX ,"%s/%s" , tree_origin, tree_dirs[i]); + +if (rmdir (tmppath) !=0 ) { +snprintf (errmsg,ERRSIZE ,"rmdir %s" , tmppath); +printerr (errmsg); + goto error; + } + } + +/* Remove the temporary directory */ +if (rmdir (tree_origin) !=0 ) { +snprintf (errmsg,ERRSIZE ,"rmdir %s" , tree_origin); +printerr (errmsg); + goto error; + } + +return 0 ; + +error: +return 1 ; +} +static void test_utils_expand_path (void ) {char *result; +char name[100 ], tmppath[PATH_MAX ];int i;/* Test valid cases */ for (i =0 ; i < num_valid_tests; i++) { -char name[100 ];sprintf (name,"valid test case: %s" , valid_tests_inputs[i].input); result =utils_expand_path (valid_tests_inputs[i].input);
@@ -141,9 +276,23 @@ static void test_utils_expand_path(void)free (result); } +/* Test symlink tree cases */ +int treelen =strlen (tree_origin) +1 ; +for (i =0 ; i < num_symlink_tests; i++) { +sprintf (name,"symlink tree test case: [tmppath/]%s" , + symlink_tests_inputs[i].input); + +snprintf (tmppath,PATH_MAX ,"%s/%s" , + tree_origin, symlink_tests_inputs[i].input); + result =utils_expand_path (tmppath); +ok (result !=NULL &&strcmp (result + treelen, + symlink_tests_inputs[i].expected_result) ==0 , name); + +free (result); + } +/* Test invalid cases */ for (i =0 ; i < num_invalid_tests; i++) { -char name[100 ];sprintf (name,"invalid test case: %s" , invalid_tests_inputs[i]); result =utils_expand_path (invalid_tests_inputs[i]);
@@ -156,16 +305,30 @@ static void test_utils_expand_path(void)int main (int argc,char **argv) { +if (prepare_symlink_tree () !=0 ) { + goto error_mkdir; + } +if (prepare_valid_results () !=0 ) { -return 1 ; + goto error_malloc; } -plan_tests (num_valid_tests + num_invalid_tests); +plan_tests (num_valid_tests + num_invalid_tests + num_symlink_tests);diag ("utils_expand_path tests" );test_utils_expand_path ();free_valid_results (); +free_symlink_tree (); +return exit_status (); + +error_malloc: +free_valid_results (); + +error_mkdir: +free_symlink_tree (); + +return 1 ; }
Signature · 7 lines
1.7.10.4 _______________________________________________ lttng-dev mailing list [email protected] http://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev
PATCH 5/5 Tests: add valid test cases to test_utils_expand_path
In reply to a message not in the archive--- tests/unit/test_utils_expand_path.c | 2 ++ 1 file changed, 2 insertions(+)
tests/unit/test_utils_expand_path.c +2 -0
@@ -70,6 +70,8 @@ static struct valid_test_input valid_tests_inputs[] = { { ".", ".", "" }, { "/../a/b/c/d/e", "", "/a/b/c/d/e" }, { "/a/b/c/d/../../../../../e", "", "/e" }, + { "/..", "", "/" }, + { "/a/..", "", "/" }, };char **valid_tests_expected_results; static const int num_valid_tests =
Signature · 7 lines
1.7.10.4 _______________________________________________ lttng-dev mailing list [email protected] http://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev
Fixes every use case I had that failed previously :).
Merged. Thanks!
David
Quoted 18 lines · Raphaël Beamonte
Hello David!
Here is the fix needed to have the expected behavior for path that ends with '/.' or '/..'. I also profit to send patches for the unit test to allow to add tests using an existing tree and symlinks, adding the missing copyright in
utils.cand correcting the indentation of one comment.Waiting for your comments!
Thanks,
RaphaëlRaphaël Beamonte (5):
Add missing copyright toutils.c
Fix: comment indentation
Fix:utils_expand_pathnow works for paths that ends with '/.' or
Quoted 11 lines
'/..'Tests: add symlink tests for
test_utils_expand_path
Tests: add valid test cases totest_utils_expand_pathsrc/common/utils.c | 30 +++++- tests/unit/test_utils_expand_path.c | 175 ++++++++++++++++++++++++++++++++++- 2 files changed, 199 insertions(+), 6 deletions(-)Signature · 1 line
1.7.10.4
_______________________________________________
lttng-dev mailing list
[email protected]
http://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev
cover utils_expand_path fix
In reply to a message not in the archiveHello David!
Here is the fix needed to have the expected behavior for path that ends with '/.' or '/..'. I also profit to send patches for the unit test to allow to add tests using an existing tree and symlinks, adding the missing copyright in utils.c and correcting the indentation of one comment.
Waiting for your comments!
Thanks,
Raphaël
Raphaël Beamonte (5):
Add missing copyright to utils.c
Fix: comment indentation
Fix: utils_expand_path now works for paths that ends with '/.' or
'/..'
Tests: add symlink tests for test_utils_expand_path
Tests: add valid test cases to test_utils_expand_path
src/common/utils.c | 30 +++++- tests/unit/test_utils_expand_path.c | 175 ++++++++++++++++++++++++++++++++++- 2 files changed, 199 insertions(+), 6 deletions(-)
Signature · 7 lines
1.7.10.4 _______________________________________________ lttng-dev mailing list [email protected] http://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev