Change MakeRelativePath() implementation 64/117264/3
authorDamian Pietruchowski <d.pietruchow@samsung.com>
Fri, 3 Mar 2017 12:10:57 +0000 (13:10 +0100)
committerBartlomiej <b.kunikowski@partner.samsung.com>
Fri, 10 Mar 2017 07:49:37 +0000 (08:49 +0100)
Sometimes current_path(), which is default argument of absolute,
throws exception, that directory doesn't exist.
This implementation don't require the existence of directories.

Change-Id: I0eea7febb6c8acc263f639cda2bcd2b58e0a9f67
Signed-off-by: Damian Pietruchowski <d.pietruchow@samsung.com>
src/common/utils/file_util.cc

index 599d66a..004df24 100644 (file)
@@ -593,9 +593,11 @@ bool HasDirectoryClimbing(const boost::filesystem::path& path) {
 
 boost::filesystem::path MakeRelativePath(const boost::filesystem::path& input,
                                          const boost::filesystem::path& base) {
-  bf::path input_absolute = bf::absolute(input);
-  bf::path base_absolute = bf::absolute(base);
-  return input_absolute.string().substr(base_absolute.string().length() + 1);
+  if (input.string().find(base.string()) == std::string::npos) {
+      LOG(ERROR) << base.string() << " is not base path for " << input.string();
+      return input;
+  }
+  return input.string().substr(base.string().length() + 1);
 }
 
 bool IsSubDir(const boost::filesystem::path& path,