Skip to content

Commit

Permalink
wsd: cleanup realpath call
Browse files Browse the repository at this point in the history
The new utility is safer and more readable.

Change-Id: I3a86675378d458cb004e5534dbf2b401936d0e57
Reviewed-on: https://gerrit.libreoffice.org/c/online/+/98183
Tested-by: Jenkins
Tested-by: Jenkins CollaboraOffice <[email protected]>
Reviewed-by: Ashod Nakashian <[email protected]>
  • Loading branch information
Ashod committed Jul 6, 2020
1 parent 71a9d21 commit 9f7f6dc
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 12 deletions.
14 changes: 14 additions & 0 deletions common/FileUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,20 @@ namespace FileUtil
#endif
}

std::string realpath(const char* path)
{
char* resolved = ::realpath(path, nullptr);
if (resolved)
{
std::string real = resolved;
free(resolved);
return real;
}

LOG_SYS("Failed to get the realpath of [" << path << "]");
return path;
}

} // namespace FileUtil

namespace
Expand Down
7 changes: 7 additions & 0 deletions common/FileUtil.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,13 @@ namespace FileUtil
/// Link source to target, and copy if linking fails.
bool linkOrCopyFile(const char* source, const char* target);

/// Returns the realpath(3) of the provided path.
std::string realpath(const char* path);
inline std::string realpath(const std::string& path)
{
return realpath(path.c_str());
}

/// File/Directory stat helper.
class Stat
{
Expand Down
10 changes: 3 additions & 7 deletions common/JailUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -312,13 +312,9 @@ void setupLoSymlink(const std::string& sysTemplate, const std::string& loTemplat
symlinkPathToJail(sysTemplate, loTemplate, loSubPath);

// Font paths can end up as realpaths so match that too.
char* resolved = realpath(loTemplate.c_str(), nullptr);
if (resolved)
{
if (strcmp(loTemplate.c_str(), resolved) != 0)
symlinkPathToJail(sysTemplate, std::string(resolved), loSubPath);
free(resolved);
}
const std::string resolved = FileUtil::realpath(loTemplate);
if (loTemplate != resolved)
symlinkPathToJail(sysTemplate, resolved, loSubPath);
}

void setupRandomDeviceLink(const std::string& sysTemplate, const std::string& name)
Expand Down
2 changes: 1 addition & 1 deletion gtk/mobile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ int main(int argc, char* argv[])

gtk_container_add(GTK_CONTAINER(mainWindow), GTK_WIDGET(webView));

fileURL = "file://" + std::string(realpath(argv[1], nullptr));
fileURL = "file://" + FileUtil::realpath(argv[1]);

std::string urlAndQuery =
"file://" TOPSRCDIR "/loleaflet/dist/loleaflet.html"
Expand Down
7 changes: 3 additions & 4 deletions kit/Kit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -338,13 +338,12 @@ namespace
const Path& destination,
LinkOrCopyType type)
{
char* resolved = realpath(source.c_str(), nullptr);
if (resolved && resolved != source)
std::string resolved = FileUtil::realpath(source);
if (resolved != source)
{
LOG_DBG("linkOrCopy: Using real path [" << resolved << "] instead of original link ["
<< source << "].");
source = resolved;
free(resolved);
source = std::move(resolved);
}

LOG_INF("linkOrCopy " << linkOrCopyTypeString(type) << " from [" << source << "] to ["
Expand Down

0 comments on commit 9f7f6dc

Please sign in to comment.