Skip to content

Commit 9a0609b

Browse files
committed
SR-9550: xdgURLs assumes home directory location for .userDirectory
- Parse /etc/default/useradd to find the default home directory, falling back to /home.
1 parent 12ee2c8 commit 9a0609b

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
@@ -231,7 +231,24 @@ open class FileManager : NSObject {
231231
return urls
232232
}
233233
#endif
234-
234+
235+
private lazy var xdgHomeDirectory: String = {
236+
let key = "HOME="
237+
if let contents = try? String(contentsOfFile: "/etc/default/useradd", encoding: .utf8) {
238+
for line in contents.components(separatedBy: "\n") {
239+
if line.hasPrefix(key) {
240+
let index = line.index(line.startIndex, offsetBy: key.count)
241+
let str = String(line[index...]) as NSString
242+
let homeDir = str.trimmingCharacters(in: CharacterSet.whitespaces)
243+
if homeDir.count > 0 {
244+
return homeDir
245+
}
246+
}
247+
}
248+
}
249+
return "/home"
250+
}()
251+
235252
private func xdgURLs(for directory: SearchPathDirectory, in domain: _SearchPathDomain) -> [URL] {
236253
// FHS/XDG-compliant OSes:
237254
switch directory {
@@ -263,7 +280,7 @@ open class FileManager : NSObject {
263280

264281
case .userDirectory:
265282
guard domain == .local else { return [] }
266-
return [ URL(fileURLWithPath: "/home", isDirectory: true) ]
283+
return [ URL(fileURLWithPath: xdgHomeDirectory, isDirectory: true) ]
267284

268285
case .moviesDirectory:
269286
return [ _XDGUserDirectory.videos.url ]

0 commit comments

Comments
 (0)