Follow up to the realpath(3) for unexistent paths
cover Follow up to the realpath(3) for unexistent paths
In reply to a message not in the archiveDavid,
Following the discussions we had on IRC with Mathieu and the last mail exchange, here is a series of patch that should answer to our enquiries.
The first two patches are linked to the utils_expand_path unit test and correct its behavior and the expected results (some input was at first considered invalid, but during our discussions it was mentionned that it should be considered valid).
The next three ones introduce a new utils_partial_realpath that, as its name says it, allows to partially resolve a real path for a given path. This function is then used in utils_expand_path to resolve the paths, and the utils_resolve_relative function is removed as it is not necessary anymore in this new "setup".
Waiting for your comments,
Raphaël
Raphaël Beamonte (5):
Tests: add a check in test_utils_expand_path to avoid segfault if
result is NULL
Tests: move invalid tests in test_utils_expand_path that should be
valid
Introduce a new utils_partial_realpath function
Change the utils_expand_path function to use utils_partial_realpath Remove the utils_resolve_relative function that is not useful anymore
.gitignore | 1 - src/common/utils.c | 256 ++++++++++++++++++------------ src/common/utils.h | 3 +- tests/unit/Makefile.am | 7 +- tests/unit/test_utils_expand_path.c | 7 +- tests/unit/test_utils_resolve_relative.c | 98 ------------ tests/unit_tests | 1 - 7 files changed, 158 insertions(+), 215 deletions(-) delete mode 100644 tests/unit/test_utils_resolve_relative.c
Signature · 7 lines
1.7.10.4 _______________________________________________ lttng-dev mailing list [email protected] http://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev
PATCH 1/5 Tests: add a check in test_utils_expand_path to avoid segfault if result is NULL
In reply to a message not in the archive--- tests/unit/test_utils_expand_path.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
tests/unit/test_utils_expand_path.c +2 -1
@@ -135,7 +135,8 @@ static void test_utils_expand_path(void)sprintf (name,"valid test case: %s" , valid_tests_inputs[i].input); result =utils_expand_path (valid_tests_inputs[i].input); -ok (strcmp (result, valid_tests_expected_results[i]) ==0 , name); +ok (result !=NULL && +strcmp (result, valid_tests_expected_results[i]) ==0 , name);free (result); }
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 Tests: move invalid tests in test_utils_expand_path that should be valid
In reply to a message not in the archive--- tests/unit/test_utils_expand_path.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
tests/unit/test_utils_expand_path.c +2 -2
@@ -55,6 +55,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" }, };char **valid_tests_expected_results; static const int num_valid_tests =
@@ -63,8 +65,6 @@ static const int num_valid_tests =/* Invalid test cases */ static char *invalid_tests_inputs[] = {NULL , -"/../a/b/c/d/e" , -"/a/b/c/d/../../../../../e" , };static const int num_invalid_tests =sizeof (invalid_tests_inputs) /sizeof (invalid_tests_inputs[0 ]);
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 Introduce a new utils_partial_realpath function
In reply to a message not in the archiveThis new utils function allows to resolve partially the paths using realpath. As realpath(3) is not available to use for unexistent paths, this function that allows to resolve partially existent paths can be used in cases we don't know if the path fully exists. It first resolves the existent part using realpath(3) and then concatenate the unexistent part to it.
--- src/common/utils.c | 128 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/common/utils.h | 2 + 2 files changed, 130 insertions(+)
src/common/utils.c +128 -0
@@ -36,6 +36,134 @@#include "defaults.h" /* +* Return a partial realpath(3) of the path even if the full path does not +* exist. For instance, with /tmp/test1/test2/test3, if test2/ does not exist +* but the /tmp/test1 does, the real path for /tmp/test1 is concatened with +* /test2/test3 then returned. In normal time, realpath(3) fails if the end +* point directory does not exist. +* In case resolved_path is NULL, the string returned was allocated in the +* function and thus need to be freed by the caller. The size argument allows +* to specify the size of the resolved_path argument if given, or the size to +* allocate. +*/ +LTTNG_HIDDEN +char *utils_partial_realpath(const char *path, char *resolved_path, size_t size) +{ +char *cut_path, *try_path =NULL , *try_path_prev =NULL ; +const char *next, *prev, *end; + +/* Safety net */ +if (path ==NULL ) { + goto error; + } + +/* +* Identify the end of the path, we don't want to treat the +* last char if it is a '/', we will just keep it on the side +* to be added at the end, and return a value coherent with +* the path given as argument +*/ + end = path +strlen (path); +if (*(end-1 ) =='/' ) { + end--; + } + +/* Initiate the values of the pointers before looping */ + next = path; + prev = next; +/* Only to ensure try_path is not NULL to enter the while */ + try_path = (char *)next; + +/* Resolve the canonical path of the first part of the path */ +while (try_path !=NULL && next != end) { +/* +* If there is not any '/' left, we want to try with +* the full path +*/ + next =strpbrk (next +1 ,"/" ); +if (next ==NULL ) { + next = end; + } + +/* Cut the part we will be trying to resolve */ + cut_path =strndup (path, next - path); + +/* Try to resolve this part */ + try_path =realpath ((char *)cut_path,NULL ); +if (try_path ==NULL ) { +/* +* There was an error, we just want to be assured it +* is linked to an unexistent directory, if it's another +* reason, we spawn an error +*/ +switch (errno) { +case ENOENT : +/* Ignore the error */ +break ; +default : +PERROR ("realpath (partial_realpath)" ); + goto error; +break ; + } + }else { +/* Save the place we are before trying the next step */ +free (try_path_prev); + try_path_prev = try_path; + prev = next; + } + +/* Free the allocated memory */ +free (cut_path); + }; + +/* Allocate memory for the resolved path if necessary */ +if (resolved_path ==NULL ) { + resolved_path =zmalloc (size); +if (resolved_path ==NULL ) { +PERROR ("zmalloc resolved path" ); + goto error; + } + } + +/* +* If we were able to solve at least partially the path, we can concatenate +* what worked and what didn't work +*/ +if (try_path_prev !=NULL ) { +/* If we risk to concatenate two '/', we remove one of them */ +if (try_path_prev[strlen (try_path_prev) -1 ] =='/' && prev[0 ] =='/' ) { + try_path_prev[strlen (try_path_prev) -1 ] ='\0' ; + } + +/* +* Duplicate the memory used by prev in case resolved_path and +* path are pointers for the same memory space +*/ + cut_path =strdup (prev); + +/* Concatenate the strings */ +snprintf (resolved_path, size,"%s%s" , try_path_prev, cut_path); + +/* Free the allocated memory */ +free (cut_path); +free (try_path_prev); +/* +* Else, we just copy the path in our resolved_path to +* return it as is +*/ + }else { +strncpy (resolved_path, path, size); + } + +/* Then we return the 'partially' resolved path */ +return resolved_path; + +error: +free (resolved_path); +return NULL ; +} + +/* * Resolve the './' and '../' strings in the middle of a path using * our very own way to do it, so that it works even if the directory * does not exist
src/common/utils.h +2 -0
@@ -27,6 +27,8 @@#define GIBI_LOG2 30char *utils_resolve_relative (const char *path); +char *utils_partial_realpath (const char *path,char *resolved_path, +size_t size);char *utils_expand_path (const char *path);int utils_create_pipe (int *dst);int utils_create_pipe_cloexec (int *dst);
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 Change the utils_expand_path function to use utils_partial_realpath
In reply to a message not in the archiveAs most of the resolve-related work can now be done using utils_partial_realpath, the utils_expand_path function can call it and concentrate on resolving the relative paths in the middle of a path string, such as '/./' and '/../'.
--- src/common/utils.c | 121 +++++++++++++++++++++++----------------------------- 1 file changed, 53 insertions(+), 68 deletions(-)
src/common/utils.c +53 -68
@@ -230,102 +230,87 @@ error:return NULL ; } -/* -* Return the realpath(3) of the path even if the last directory token does not -* exist. For example, with /tmp/test1/test2, if test2/ does not exist but the -* /tmp/test1 does, the real path is returned. In normal time, realpath(3) -* fails if the end point directory does not exist. +* Make a full resolution of the given path even if it doesn't exist. +* This function uses the utils_partial_realpath function to resolve +* symlinks and relatives paths at the start of the string, and +* implements functionnalities to resolve the './' and '../' strings +* in the middle of a path. This function is only necessary because +* realpath(3) does not accept to resolve unexistent paths. +* The returned string was allocated in the function, it is thus of +* the responsibility of the caller to free this memory. */ LTTNG_HIDDEN char *utils_expand_path(const char *path) { -const char *end_path =NULL ; -char *next, *cut_path =NULL , *expanded_path =NULL ; +char *next, *previous, *slash, *start_path, *absolute_path =NULL ;/* Safety net */ if (path ==NULL ) { goto error; } -/* Allocate memory for the expanded path */ - expanded_path =zmalloc (PATH_MAX ); -if (expanded_path ==NULL ) { +/* Allocate memory for the absolute_path */ + absolute_path =zmalloc (PATH_MAX ); +if (absolute_path ==NULL ) {PERROR ("zmalloc expand path" ); goto error; } -/* If given path is already absolute */ -if (*path =='/' ) { -strncpy (expanded_path, path,PATH_MAX ); -/* Else, we have some work to do */ +/* +* If the path is not already absolute nor explicitly relative, +* consider we're in the current directory +*/ +if (*path !='/' &&strncmp (path,"./" ,2 ) !=0 && +strncmp (path,"../" ,3 ) !=0 ) { +snprintf (absolute_path,PATH_MAX ,"./%s" , path); +/* Else, we just copy the path */ }else { -/* Pointer to the last char of the path */ -const char *last_char = path +strlen (path) -1 ; +strncpy (absolute_path, path,PATH_MAX ); + } - end_path = path; +/* Resolve partially our path */ + absolute_path =utils_partial_realpath (absolute_path, + absolute_path,PATH_MAX ); -/* Split part that will be resolved by realpath (relative path from -* current directory using ./ or ../ only) and part that could not -* (directory names) -*/ -while ((next =strpbrk (end_path,"/" )) && (next != last_char)) { - end_path = next +1 ; -if (strncmp (end_path,"./" ,2 ) !=0 && -strncmp (end_path,"../" ,3 ) !=0 ) { -break ; - } - } +/* As long as we find '/./' in the working_path string */ +while ((next =strstr (absolute_path,"/./" ))) { -/* If this is the end of the string, and we still can resolve it */ -if (strncmp (end_path,"..\0" ,3 ) ==0 || -strncmp (end_path,".\0" ,2 ) ==0 ) { - end_path +=strlen (end_path); - } +/* We prepare the start_path not containing it */ + start_path =strndup (absolute_path, next - absolute_path); -/* If the end part is the whole path, we are in the current dir */ -if (end_path == path) { - cut_path =strdup ("." ); -/* Else, cut the resolvable part from original path */ - }else { - cut_path =strndup (path, end_path - path); - } +/* And we concatenate it with the part after this string */ +snprintf (absolute_path,PATH_MAX ,"%s%s" , start_path, next +2 ); -/* Resolve the canonical path of the first part of the path */ - expanded_path =realpath ((char *)cut_path, expanded_path); -if (expanded_path ==NULL ) { -switch (errno) { -case ENOENT : -ERR ("%s: No such file or directory" , cut_path); -break ; -default : -PERROR ("realpath utils expand path" ); -break ; - } - goto error; - } +free (start_path); + } -/* Add end part to expanded path if not empty */ -if (*end_path !=0 ) { -strncat (expanded_path,"/" ,PATH_MAX -strlen (expanded_path) -1 ); -strncat (expanded_path, end_path, -PATH_MAX -strlen (expanded_path) -1 ); +/* As long as we find '/../' in the working_path string */ +while ((next =strstr (absolute_path,"/../" ))) { +/* We find the last level of directory */ + previous = absolute_path; +while ((slash =strpbrk (previous,"/" )) && slash != next) { + previous = slash +1 ; } - } -/* Resolve the internal './' and '../' strings */ - next =utils_resolve_relative (expanded_path); -if (next ==NULL ) { - goto error; +/* Then we prepare the start_path not containing it */ + start_path =strndup (absolute_path, previous - absolute_path); + +/* And we concatenate it with the part after the '/../' */ +snprintf (absolute_path,PATH_MAX ,"%s%s" , start_path, next +4 ); + +/* We can free the memory used for the start path*/ +free (start_path); + +/* Then we verify for symlinks using partial_realpath */ + absolute_path =utils_partial_realpath (absolute_path, + absolute_path,PATH_MAX ); } -free (expanded_path); -free (cut_path); -return next; +return absolute_path; error: -free (expanded_path); -free (cut_path); +free (absolute_path);return NULL ; }
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 Remove the utils_resolve_relative function that is not useful anymore
In reply to a message not in the archiveAs all of the work is now done in utils_partial_realpath and utils_expand_path, utils_resolve_relative is not necessary anymore and should be deleted from the sources.
--- .gitignore | 1 - src/common/utils.c | 67 -------------------- src/common/utils.h | 1 - tests/unit/Makefile.am | 7 +-- tests/unit/test_utils_resolve_relative.c | 98 ------------------------------ tests/unit_tests | 1 - 6 files changed, 1 insertion(+), 174 deletions(-) delete mode 100644 tests/unit/test_utils_resolve_relative.c
.gitignore +0 -1
@@ -56,7 +56,6 @@ tests/unit/test_session tests/unit/test_uri tests/unit/test_ust_data tests/unit/test_utils_parse_size_suffix -tests/unit/test_utils_resolve_relative tests/unit/test_utils_expand_path kernel_all_events_basic kernel_event_basic
src/common/utils.c +0 -67
73 lines not on this page · raw message
src/common/utils.h +0 -1
@@ -26,7 +26,6 @@#define MEBI_LOG2 20#define GIBI_LOG2 30 -char *utils_resolve_relative (const char *path);char *utils_partial_realpath (const char *path,char *resolved_path,size_t size);char *utils_expand_path (const char *path);
tests/unit/Makefile.am +1 -6
@@ -17,7 +17,7 @@ LIBRELAYD=$(top_builddir)/src/common/relayd/librelayd.la # Define test programs noinst_PROGRAMS = test_uri test_session test_kernel_data -noinst_PROGRAMS += test_utils_parse_size_suffix test_utils_resolve_relative test_utils_expand_path +noinst_PROGRAMS += test_utils_parse_size_suffix test_utils_expand_path if HAVE_LIBLTTNG_UST_CTL noinst_PROGRAMS += test_ust_data
@@ -92,11 +92,6 @@ test_utils_parse_size_suffix_SOURCES = test_utils_parse_size_suffix.c test_utils_parse_size_suffix_LDADD = $(LIBTAP) $(LIBHASHTABLE) $(LIBCOMMON) test_utils_parse_size_suffix_LDADD += $(UTILS_SUFFIX) -# resolve_relative unit test -test_utils_resolve_relative_SOURCES = test_utils_resolve_relative.c -test_utils_resolve_relative_LDADD = $(LIBTAP) $(LIBHASHTABLE) $(LIBCOMMON) -test_utils_resolve_relative_LDADD += $(UTILS_SUFFIX) - # expand_path unit test test_utils_expand_path_SOURCES = test_utils_expand_path.c test_utils_expand_path_LDADD = $(LIBTAP) $(LIBHASHTABLE) $(LIBCOMMON)
tests/unit/test_utils_resolve_relative.c deleted +0 -98
98 lines not on this page · raw message
tests/unit_tests +0 -1
@@ -3,5 +3,4 @@ unit/test_session unit/test_uri unit/test_ust_data unit/test_utils_parse_size_suffix -unit/test_utils_resolve_relative unit/test_utils_expand_path
Signature · 7 lines
1.7.10.4 _______________________________________________ lttng-dev mailing list [email protected] http://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev
Quoted 11 lines · Raphaël Beamonte
David,
Following the discussions we had on IRC with Mathieu and the last mail exchange, here is a series of patch that should answer to our enquiries.
The first two patches are linked to the
utils_expand_pathunit test and correct its behavior and the expected results (some input was at first considered invalid, but during our discussions it was mentionned that it should be considered valid).
Quoted 7 lines
The next three ones introduce a new
utils_partial_realpaththat, as its name says it, allows to partially resolve a real path for a given path. This function is then used inutils_expand_pathto resolve the paths, and theutils_resolve_relativefunction is removed as it is not necessary anymore in this new "setup".Waiting for your comments,
Hey Raph,
I've created a symlink directory maze here to test theses patches and everything seems to work fine but I got one wrong use case. I'll print my maze here so you can recreate:
/tmp/test $
.
└── a
├── b
│ ├── c
│ │ └── g -> ../../../
│ ├── f -> ../e/
│ ├── h -> ../g/
│ └── k -> c/g/
├── d -> b/c/
├── e
└── g -> d/10 directories, 0 files
$ lttng create -o /tmp/test/a/g/../l/.. Session auto-20131115-103416 created. Traces will be written in /tmp/test/a/b/l/..
Note the "/.." at the end. Relative or not, same output. Seems like "./" and "../" *after* an unexisting path fails to parse. In this case, I would throw in an error since there is simply no way of knowing that "l/" is going to point to.
What I'm going to do here is to merge those 5 patches and wait for fix(es) that we'll merge in the RC cycle. The feature is there just not perfect :). 2.4-rc1 is coming up this morning btw.
Thanks!
David
Quoted 11 lines
Raphaël
Raphaël Beamonte (5):
Tests: add a check intest_utils_expand_pathto avoid segfault if
result is NULL
Tests: move invalid tests intest_utils_expand_paththat should be
valid
Introduce a newutils_partial_realpathfunction
Change theutils_expand_pathfunction to useutils_partial_realpathRemove theutils_resolve_relativefunction that is not useful anymore
Quoted 13 lines
.gitignore | 1 - src/common/utils.c | 256 ++++++++++++++++++------------ src/common/utils.h | 3 +- tests/unit/Makefile.am | 7 +- tests/unit/test_utils_expand_path.c | 7 +- tests/unit/test_utils_resolve_relative.c | 98 ------------ tests/unit_tests | 1 - 7 files changed, 158 insertions(+), 215 deletions(-) delete mode 100644 tests/unit/test_utils_resolve_relative.cSignature · 1 line
1.7.10.4
_______________________________________________
lttng-dev mailing list
[email protected]
http://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev