Skip to content

Commit 480caee

Browse files
authored
Merge pull request #1823 from spevans/pr_sr_9550
2 parents 79ac432 + 9a0609b commit 480caee

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

Foundation/FileManager.swift

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,24 @@ open class FileManager : NSObject {
223223
return urls
224224
}
225225
#endif
226-
226+
227+
private lazy var xdgHomeDirectory: String = {
228+
let key = "HOME="
229+
if let contents = try? String(contentsOfFile: "/etc/default/useradd", encoding: .utf8) {
230+
for line in contents.components(separatedBy: "\n") {
231+
if line.hasPrefix(key) {
232+
let index = line.index(line.startIndex, offsetBy: key.count)
233+
let str = String(line[index...]) as NSString
234+
let homeDir = str.trimmingCharacters(in: CharacterSet.whitespaces)
235+
if homeDir.count > 0 {
236+
return homeDir
237+
}
238+
}
239+
}
240+
}
241+
return "/home"
242+
}()
243+
227244
private func xdgURLs(for directory: SearchPathDirectory, in domain: _SearchPathDomain) -> [URL] {
228245
// FHS/XDG-compliant OSes:
229246
switch directory {
@@ -255,7 +272,7 @@ open class FileManager : NSObject {
255272

256273
case .userDirectory:
257274
guard domain == .local else { return [] }
258-
return [ URL(fileURLWithPath: "/home", isDirectory: true) ]
275+
return [ URL(fileURLWithPath: xdgHomeDirectory, isDirectory: true) ]
259276

260277
case .moviesDirectory:
261278
return [ _XDGUserDirectory.videos.url ]

0 commit comments

Comments
 (0)