From: John L. <mov...@us...> - 2003-09-22 13:46:55
|
Update of /cvsroot/oprofile/oprofile/libutil++ In directory sc8-pr-cvs1:/tmp/cvs-serv25051/libutil++ Modified Files: file_manip.cpp file_manip.h Log Message: fully resolve sym links passed on the command line Index: file_manip.cpp =================================================================== RCS file: /cvsroot/oprofile/oprofile/libutil++/file_manip.cpp,v retrieving revision 1.19 retrieving revision 1.20 diff -u -p -d -r1.19 -r1.20 --- file_manip.cpp 23 Aug 2003 15:09:06 -0000 1.19 +++ file_manip.cpp 22 Sep 2003 13:46:47 -0000 1.20 @@ -36,13 +36,6 @@ bool is_directory(string const & dirname } -/** - * is_file_identical - check for identical files - * @param file1 first filename - * @param file2 scond filename - * - * return true if the two filenames belong to the same file - */ bool is_files_identical(string const & file1, string const & file2) { struct stat st1; @@ -56,21 +49,26 @@ bool is_files_identical(string const & f return false; } -/** - * op_read_link - read the contents of a symbolic link file - * @param name the file name - * - * return an empty string on failure - */ -string op_read_link(string const & name) + +string const op_follow_link(string const & name) { - char * linkbuff = op_get_link(name.c_str()); - if (linkbuff == NULL) - return string(); + string tmp = name; + int iterate = 20; - string result(linkbuff); - free(linkbuff); - return result; + while (iterate--) { + char * linkbuf = op_get_link(tmp.c_str()); + if (linkbuf == NULL) + return tmp; + string base; + if (is_directory(tmp)) + base = tmp; + else + base = dirname(tmp); + tmp = relative_to_absolute_path(linkbuf, base); + free(linkbuf); + } + + return name; } Index: file_manip.h =================================================================== RCS file: /cvsroot/oprofile/oprofile/libutil++/file_manip.h,v retrieving revision 1.15 retrieving revision 1.16 diff -u -p -d -r1.15 -r1.16 --- file_manip.h 23 Aug 2003 16:37:12 -0000 1.15 +++ file_manip.h 22 Sep 2003 13:46:47 -0000 1.16 @@ -19,10 +19,24 @@ /// return true if dir is an existing directory bool is_directory(std::string const & dirname); -/// return true if the two files are the same file +/** + * is_file_identical - check for identical files + * @param file1 first filename + * @param file2 scond filename + * + * return true if the two filenames belong to the same file + */ bool is_files_identical(std::string const & file1, std::string const & file2); -/// return the contents of a symbolic link or an empty string on failure -std::string op_read_link(std::string const & name); + +/** + * op_follow_link - follow a symbolic link + * @param name the file name + * + * Resolve a symbolic link as far as possible. + * Returns the original string on failure. + */ +std::string const op_follow_link(std::string const & name); + /// return true if the given file is readable bool op_file_readable(std::string const & file); |