Skip to content

Commit

Permalink
SR-9550: xdgURLs assumes home directory location for .userDirectory
Browse files Browse the repository at this point in the history
- Parse /etc/default/useradd to find the default home directory,
  falling back to /home.
  • Loading branch information
spevans committed Jan 15, 2019
1 parent f033eaa commit f94b023
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions Foundation/FileManager.swift
Expand Up @@ -231,7 +231,24 @@ open class FileManager : NSObject {
return urls
}
#endif


private let 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

0 comments on commit f94b023

Please sign in to comment.