Return user paths relative to effective user's homedir#105
Return user paths relative to effective user's homedir#105terminalmage wants to merge 2 commits intoActiveState:masterfrom
Conversation
Since `os.path.expanduser()` is used to expand the `~` to the home directory, paths which use `~` will always return a path relative to the home directory of the user under which the python process is running. However, if the euid has been changed, this means that these paths will be incorrect, leading to problems when the non-privleged user tries to write to these paths. This commit fixes that issue by attempting to get the username matching the euid, and using that username (e.g. `~user/.cache`) when invoking `os.path.expanduser()`.
Kriskras99
left a comment
There was a problem hiding this comment.
Looks great, some small style issues
appdirs.py
Outdated
| system = sys.platform | ||
|
|
||
|
|
||
| def _effective_user(): |
There was a problem hiding this comment.
This function should probably be placed at the bottom of the file, because all the support functions are located there.
appdirs.py
Outdated
| path = os.getenv('XDG_CACHE_HOME', os.path.expanduser('~/.cache')) | ||
| if appname: | ||
| path = os.path.join(path, appname) | ||
| user = _effective_user() |
appdirs.py
Outdated
| path = os.getenv('XDG_CONFIG_HOME', os.path.expanduser("~/.config")) | ||
| if appname: | ||
| path = os.path.join(path, appname) | ||
| user = _effective_user() |
There was a problem hiding this comment.
I think it's cleaner to have _effective_user() inside the format just like you did in user_state_dir() and user_log_dir()
appdirs.py
Outdated
| path = os.path.join( | ||
| os.path.expanduser('~/Library/Logs'), | ||
| os.path.expanduser('~{0}/Library/Logs'.format(_effective_user())), | ||
| appname) |
There was a problem hiding this comment.
Can you put the round bracket on a new line to match what you did in the other functions, or in the other functions don't put the round bracket on a new line.
|
@Kriskras99 Thanks for the review, I've made the requested changes. |
|
Looks great. Now it's up to @zoofood. |
|
Thanks guys, definitely time to get these merged and a new release out. Appreciate all the efforts! |
Since
os.path.expanduser()is used to expand the~to the home directory, paths which use~will always return a path relative to the home directory of the user under which the python process is running. However, if the euid has been changed, this means that these paths will be incorrect, leading to problems when the non-privleged user tries to write to these paths. For example:This PR fixes that issue by attempting to get the username matching the euid, and using that username (e.g.
~user/.cache) when invokingos.path.expanduser(). With the fix, the funcs now return the correct paths relative to the effective user's homedir: