Skip to content

Commit

Permalink
Prevent wrong data directory selection.
Browse files Browse the repository at this point in the history
Validate data directory by checking for known file.

Issue #189.
  • Loading branch information
thezbyg committed Jan 31, 2021
1 parent ff0a3c0 commit 98d5981
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 10 deletions.
5 changes: 4 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ function(set_compile_options target)
target_compile_options(${target} PRIVATE -Werror)
endif()
endif()
if (DEV_BUILD)
target_compile_definitions(${target} PRIVATE GPICK_DEV_BUILD)
endif()
endfunction()

function(add_gtk_options target)
Expand Down Expand Up @@ -203,7 +206,7 @@ install(FILES share/applications/gpick.desktop DESTINATION share/applications)
install(FILES share/mime/packages/gpick.xml DESTINATION share/mime/packages)
install(FILES share/doc/gpick/copyright DESTINATION share/doc/gpick)
install(FILES share/man/man1/gpick.1 DESTINATION share/man/man1)
file(GLOB RESOURCE_FILES share/gpick/*.png share/gpick/*.lua share/gpick/*.txt)
file(GLOB RESOURCE_FILES share/gpick/*.png share/gpick/*.lua share/gpick/*.txt share/gpick/.gpick-data-directory)
install(FILES ${RESOURCE_FILES} DESTINATION share/gpick)
install(DIRECTORY share/icons DESTINATION share)
if (ENABLE_NLS)
Expand Down
5 changes: 4 additions & 1 deletion SConscript
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@ else:
LINKFLAGS = ['/MANIFEST', '/LTCG'],
)

if env['DEV_BUILD']:
env.Append(CPPDEFINES = ['GPICK_DEV_BUILD'])

env.Append(CPPPATH = ['#source'])

def buildVersion(env):
Expand Down Expand Up @@ -369,7 +372,7 @@ env.Alias(target = "install", source = [
env.InstallData(dir = env['DESTDIR'] +'/share/applications', source = ['share/applications/gpick.desktop']),
env.InstallData(dir = env['DESTDIR'] +'/share/mime/packages', source = ['share/mime/packages/gpick.xml']),
env.InstallData(dir = env['DESTDIR'] +'/share/doc/gpick', source = ['share/doc/gpick/copyright']),
env.InstallData(dir = env['DESTDIR'] +'/share/gpick', source = [env.Glob('share/gpick/*.png'), env.Glob('share/gpick/*.lua'), env.Glob('share/gpick/*.txt')]),
env.InstallData(dir = env['DESTDIR'] +'/share/gpick', source = [env.Glob('share/gpick/*.png'), env.Glob('share/gpick/*.lua'), env.Glob('share/gpick/*.txt'), env.Glob('share/gpick/.gpick-data-directory')]),
env.InstallData(dir = env['DESTDIR'] +'/share/man/man1', source = ['share/man/man1/gpick.1']),
env.InstallData(dir = env['DESTDIR'] +'/share/icons/hicolor/48x48/apps/', source = [env.Glob('share/icons/hicolor/48x48/apps/*.png')]),
env.InstallData(dir = env['DESTDIR'] +'/share/icons/hicolor/scalable/apps/', source = [env.Glob('share/icons/hicolor/scalable/apps/*.svg')]),
Expand Down
Empty file.
48 changes: 40 additions & 8 deletions source/Paths.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#include <exception>
namespace fs = boost::filesystem;
using path = fs::path;
struct PathException : std::runtime_error {
struct PathException: std::runtime_error {
PathException(const char *message):
std::runtime_error(message) {
}
Expand Down Expand Up @@ -54,23 +54,55 @@ static path &getUserConfigPath() {
configPath = path(g_get_user_config_dir());
return *configPath;
}
static bool validateDataPath(const path &path) {
try {
if (!fs::is_directory(fs::status(path)))
return false;
if (!fs::is_regular_file(fs::status(path / ".gpick-data-directory")))
return false;
return true;
} catch (const fs::filesystem_error &) {
return false;
}
}
static bool getRelativeDataPath(boost::optional<path> &dataPath) {
try {
path testPath;
if (validateDataPath(testPath = (path(getExecutablePath()).remove_filename() / "share" / "gpick"))) {
dataPath = testPath;
return true;
}
if (validateDataPath(testPath = (path(getExecutablePath()).remove_filename().remove_filename() / "share" / "gpick"))) {
dataPath = testPath;
return true;
}
return false;
} catch (const PathException &) {
return false;
} catch (const fs::filesystem_error &) {
return false;
}
}
static path &getDataPath() {
static boost::optional<path> dataPath;
if (dataPath)
return *dataPath;
path testPath;
try {
if (fs::is_directory(fs::status(testPath = (path(getExecutablePath()).remove_filename() / "share" / "gpick"))))
return *(dataPath = testPath);
} catch (const PathException &) {
}
if (fs::is_directory(fs::status(testPath = (path(g_get_user_data_dir()) / "gpick"))))
#ifdef GPICK_DEV_BUILD
if (getRelativeDataPath(dataPath))
return *dataPath;
#endif
if (validateDataPath(testPath = (path(g_get_user_data_dir()) / "gpick")))
return *(dataPath = testPath);
auto dataPaths = g_get_system_data_dirs();
for (size_t i = 0; dataPaths[i]; ++i) {
if (fs::is_directory(fs::status(testPath = (path(dataPaths[i]) / "gpick"))))
if (validateDataPath(testPath = (path(dataPaths[i]) / "gpick")))
return *(dataPath = testPath);
}
#ifndef GPICK_DEV_BUILD
if (getRelativeDataPath(dataPath))
return *dataPath;
#endif
dataPath = path();
return *dataPath;
}
Expand Down

0 comments on commit 98d5981

Please sign in to comment.