Skip to content

SR-9550: xdgURLs assumes home directory location for .userDirectory #1823

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

Merged
merged 1 commit into from
Feb 1, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions Foundation/FileManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,24 @@ open class FileManager : NSObject {
return urls
}
#endif


private lazy var xdgHomeDirectory: String = {
let key = "HOME="
if let contents = try? String(contentsOfFile: "/etc/default/useradd", encoding: .utf8) {
for line in contents.components(separatedBy: "\n") {
if line.hasPrefix(key) {
let index = line.index(line.startIndex, offsetBy: key.count)
let str = String(line[index...]) as NSString
let homeDir = str.trimmingCharacters(in: CharacterSet.whitespaces)
if homeDir.count > 0 {
return homeDir
}
}
}
}
return "/home"
}()

private func xdgURLs(for directory: SearchPathDirectory, in domain: _SearchPathDomain) -> [URL] {
// FHS/XDG-compliant OSes:
switch directory {
Expand Down Expand Up @@ -263,7 +280,7 @@ open class FileManager : NSObject {

case .userDirectory:
guard domain == .local else { return [] }
return [ URL(fileURLWithPath: "/home", isDirectory: true) ]
return [ URL(fileURLWithPath: xdgHomeDirectory, isDirectory: true) ]

case .moviesDirectory:
return [ _XDGUserDirectory.videos.url ]
Expand Down