Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

XDG Base Directory compliance #373

Open
kintel opened this issue May 25, 2013 · 1 comment
Open

XDG Base Directory compliance #373

kintel opened this issue May 25, 2013 · 1 comment

Comments

@kintel
Copy link
Member

kintel commented May 25, 2013

Split out from #125 as a separate, Unix/Linux-specific issue.

Idea from chrysn:

on linux and other free unices, the paths should follow the fdo basedir spec. for openscad library includes, that means for inclusion of a relative file $x (in pseudo code):

$XDG_DATA_HOME = $XDG_DATA_HOME if defined, otherwise ${HOME}/.local/share
$XDG_DATA_DIRS = $XDG_DATA_DIRS if defined, otherwise /usr/local/share/:/usr/share/
$xdgpaths = $XDG_DATA_HOME:$XDG_DATA_DIRS
$searchpaths = $xdgpaths, and append /openscad/libraries/ to each part separated by ':'
if $OPENSCADPATH is defined
    $searchpaths = $OPENSCADPATH:$searchpaths
for every $part of $searchpaths split by ':'
    if file $part/$x is readable:
        include that file

this also catches the current default paths /usr/share/openscad/libraries or /usr/local/share/openscad/libraries. if other paths can be configured at compile time, they should be appended to $searchpaths. (ie the default paths should stay there to conform to basedir spec).


Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

@Earnestly
Copy link

Hopefully this can help get you closer to this goal:

From a4e3054a8a57cfaf21570033c3bad6d24949b45c Mon Sep 17 00:00:00 2001
From: Earnestly <zibeon@gmail.com>
Date: Fri, 29 May 2015 15:15:46 +0100
Subject: [PATCH] Avoid hard-coding HOME/.local/share

Adapt the code from userConfigPath to documentsPath in order to avoid
hard-coding the location.  Use XDG_DATA_HOME instead and fallback
accordingly.

This also cleans up a few cases of trailing whitespace.
---
 src/PlatformUtils-posix.cc | 36 ++++++++++++++++++++++--------------
 1 file changed, 22 insertions(+), 14 deletions(-)

diff --git a/src/PlatformUtils-posix.cc b/src/PlatformUtils-posix.cc
index cc45df4..6bdb5c6 100644
--- a/src/PlatformUtils-posix.cc
+++ b/src/PlatformUtils-posix.cc
@@ -19,22 +19,30 @@ std::string PlatformUtils::pathSeparatorChar()

 std::string PlatformUtils::documentsPath()
 {
+    fs::path docpath;
+
+    const char *xdg_env = getenv("XDG_DATA_HOME");
+    if (xdg_env && fs::exists(fs::path(xdg_env))) {
+   docpath = fs::path(xdg_env) / OPENSCAD_FOLDER_NAME;
+    } else {
    const char *home = getenv("HOME");
    if (home) {
-       fs::path docpath(home);
-       docpath = docpath / ".local" / "share";
-       return boosty::stringy(docpath);
-   }
-   else {
-       return "";
+       config_path = fs::path(home) / ".local" / "share" / OPENSCAD_FOLDER_NAME;
    }
+    }
+
+    if (fs::is_directory(config_path)) {
+   return boosty::stringy(boosty::absolute(docpath));
+    }
+
+    return "";
 }

 // see http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
 std::string PlatformUtils::userConfigPath()
 {
     fs::path config_path;
-    
+
     const char *xdg_env = getenv("XDG_CONFIG_HOME");
     if (xdg_env && fs::exists(fs::path(xdg_env))) {
    config_path = fs::path(xdg_env) / OPENSCAD_FOLDER_NAME;
@@ -48,7 +56,7 @@ std::string PlatformUtils::userConfigPath()
     if (fs::is_directory(config_path)) {
    return boosty::stringy(boosty::absolute(config_path));
     }
-    
+
     return "";
 }

@@ -110,7 +118,7 @@ static std::string checkEtcIssue()
     boost::regex esc("\\\\.");
     issue = boost::regex_replace(issue, esc, "");
     boost::algorithm::trim(issue);
-    
+
     return issue;
 }

@@ -125,14 +133,14 @@ static std::string detectDistribution()
     if (!etcissue.empty()) {
    return etcissue;
     }
-    
+
     return "";
 }

 std::string PlatformUtils::sysinfo(bool extended)
 {
     std::string result;
-    
+
     struct utsname osinfo;
     if (uname(&osinfo) == 0) {
            result += osinfo.sysname;
@@ -145,7 +153,7 @@ std::string PlatformUtils::sysinfo(bool extended)
     } else {
            result += "Unknown Linux";
     }
-    
+
     std::string distribution = detectDistribution();
     if (!distribution.empty()) {
            result += " ";
@@ -162,7 +170,7 @@ std::string PlatformUtils::sysinfo(bool extended)
                    result += "s";
                }
            }
-           
+
            long pages = sysconf(_SC_PHYS_PAGES);
            long pagesize = sysconf(_SC_PAGE_SIZE);
            if ((pages > 0) && (pagesize > 0)) {
@@ -171,7 +179,7 @@ std::string PlatformUtils::sysinfo(bool extended)
                result += " RAM";
            }
        }
-       
+
     return result;
 }

-- 
2.4.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants