From 4ad384d4647cf9ac9e7961cf28554c6434183f32 Mon Sep 17 00:00:00 2001 From: Brad King <brad.king@kitware.com> Date: Mon, 12 Mar 2007 13:50:28 -0400 Subject: [PATCH] ENH: Added kwsys SystemTools::CreateSymlink and SystemTools::ReadSymlink. --- SystemTools.cxx | 38 ++++++++++++++++++++++++++++++++++++++ SystemTools.hxx.in | 12 ++++++++++++ 2 files changed, 50 insertions(+) diff --git a/SystemTools.cxx b/SystemTools.cxx index d5616d9..1359bc3 100644 --- a/SystemTools.cxx +++ b/SystemTools.cxx @@ -2356,6 +2356,44 @@ bool SystemTools::FileIsSymlink(const char* name) #endif } +#if defined(_WIN32) && !defined(__CYGWIN__) +bool SystemTools::CreateSymlink(const char*, const char*) +{ + return false; +} +#else +bool SystemTools::CreateSymlink(const char* origName, const char* newName) +{ + return symlink(origName, newName) >= 0; +} +#endif + +#if defined(_WIN32) && !defined(__CYGWIN__) +bool SystemTools::ReadSymlink(const char*, kwsys_stl::string&) +{ + return false; +} +#else +bool SystemTools::ReadSymlink(const char* newName, + kwsys_stl::string& origName) +{ + char buf[KWSYS_SYSTEMTOOLS_MAXPATH+1]; + int count = + static_cast<int>(readlink(newName, buf, KWSYS_SYSTEMTOOLS_MAXPATH)); + if(count >= 0) + { + // Add null-terminator. + buf[count] = 0; + origName = buf; + return true; + } + else + { + return false; + } +} +#endif + int SystemTools::ChangeDirectory(const char *dir) { return Chdir(dir); diff --git a/SystemTools.hxx.in b/SystemTools.hxx.in index 9c3ea0e..4539364 100644 --- a/SystemTools.hxx.in +++ b/SystemTools.hxx.in @@ -588,6 +588,18 @@ public: unsigned long length = 256, double percent_bin = 0.05); + /** + * Create a symbolic link if the platform supports it. Returns whether + * creation succeded. + */ + static bool CreateSymlink(const char* origName, const char* newName); + + /** + * Read the contents of a symbolic link. Returns whether reading + * succeded. + */ + static bool ReadSymlink(const char* newName, kwsys_stl::string& origName); + /** * Try to locate the file 'filename' in the directory 'dir'. * If 'filename' is a fully qualified filename, the basename of the file is -- GitLab