|
| 1 | +// This source file is part of the Swift.org open source project |
| 2 | +// |
| 3 | +// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors |
| 4 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 5 | +// |
| 6 | +// See http://swift.org/LICENSE.txt for license information |
| 7 | +// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 8 | +// |
| 9 | + |
| 10 | +import CoreFoundation |
| 11 | + |
| 12 | +enum _XDGUserDirectory: String { |
| 13 | + case desktop = "DESKTOP" |
| 14 | + case download = "DOWNLOAD" |
| 15 | + case publicShare = "PUBLICSHARE" |
| 16 | + case documents = "DOCUMENTS" |
| 17 | + case music = "MUSIC" |
| 18 | + case pictures = "PICTURES" |
| 19 | + case videos = "VIDEOS" |
| 20 | + |
| 21 | + static let allDirectories: [_XDGUserDirectory] = [ |
| 22 | + .desktop, |
| 23 | + .download, |
| 24 | + .publicShare, |
| 25 | + .documents, |
| 26 | + .music, |
| 27 | + .pictures, |
| 28 | + .videos, |
| 29 | + ] |
| 30 | + |
| 31 | + var url: URL { |
| 32 | + return url(userConfiguration: _XDGUserDirectory.configuredDirectoryURLs, |
| 33 | + osDefaultConfiguration: _XDGUserDirectory.osDefaultDirectoryURLs, |
| 34 | + stopgaps: _XDGUserDirectory.stopgapDefaultDirectoryURLs) |
| 35 | + } |
| 36 | + |
| 37 | + func url(userConfiguration: [_XDGUserDirectory: URL], |
| 38 | + osDefaultConfiguration: [_XDGUserDirectory: URL], |
| 39 | + stopgaps: [_XDGUserDirectory: URL]) -> URL { |
| 40 | + if let url = userConfiguration[self] { |
| 41 | + return url |
| 42 | + } else if let url = osDefaultConfiguration[self] { |
| 43 | + return url |
| 44 | + } else { |
| 45 | + return stopgaps[self]! |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + static let stopgapDefaultDirectoryURLs: [_XDGUserDirectory: URL] = { |
| 50 | + let home = URL(fileURLWithPath: NSHomeDirectory(), isDirectory: true) |
| 51 | + return [ |
| 52 | + .desktop: home.appendingPathComponent("Desktop"), |
| 53 | + .download: home.appendingPathComponent("Downloads"), |
| 54 | + .publicShare: home.appendingPathComponent("Public"), |
| 55 | + .documents: home.appendingPathComponent("Documents"), |
| 56 | + .music: home.appendingPathComponent("Music"), |
| 57 | + .pictures: home.appendingPathComponent("Pictures"), |
| 58 | + .videos: home.appendingPathComponent("Videos"), |
| 59 | + ] |
| 60 | + }() |
| 61 | + |
| 62 | + static func userDirectories(fromConfigurationFileAt url: URL) -> [_XDGUserDirectory: URL]? { |
| 63 | + if let configuration = try? String(contentsOf: url, encoding: .utf8) { |
| 64 | + return userDirectories(fromConfiguration: configuration) |
| 65 | + } else { |
| 66 | + return nil |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + static func userDirectories(fromConfiguration configuration: String) -> [_XDGUserDirectory: URL] { |
| 71 | + var entries: [_XDGUserDirectory: URL] = [:] |
| 72 | + let home = URL(fileURLWithPath: NSHomeDirectory(), isDirectory: true) |
| 73 | + |
| 74 | + // Parse it: |
| 75 | + let lines = configuration.split(separator: "\n") |
| 76 | + for line in lines { |
| 77 | + if let range = line.range(of: "=") { |
| 78 | + var variable = String(line[line.startIndex ..< range.lowerBound].trimmingCharacters(in: .whitespaces)) |
| 79 | + |
| 80 | + let prefix = "XDG_" |
| 81 | + let suffix = "_DIR" |
| 82 | + if variable.hasPrefix(prefix) && variable.hasSuffix(suffix) { |
| 83 | + let endOfPrefix = variable.index(variable.startIndex, offsetBy: prefix.length) |
| 84 | + let startOfSuffix = variable.index(variable.endIndex, offsetBy: -suffix.length) |
| 85 | + |
| 86 | + variable = String(variable[endOfPrefix ..< startOfSuffix]) |
| 87 | + } |
| 88 | + |
| 89 | + guard let directory = _XDGUserDirectory(rawValue: variable) else { |
| 90 | + continue |
| 91 | + } |
| 92 | + |
| 93 | + let path = String(line[range.upperBound ..< line.endIndex]).trimmingCharacters(in: .whitespaces) |
| 94 | + if !path.isEmpty { |
| 95 | + entries[directory] = URL(fileURLWithPath: path, isDirectory: true, relativeTo: home) |
| 96 | + } |
| 97 | + } else { |
| 98 | + return [:] // Incorrect syntax. |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + return entries |
| 103 | + } |
| 104 | + |
| 105 | + static let configuredDirectoryURLs: [_XDGUserDirectory: URL] = { |
| 106 | + let configurationHome = _SwiftValue.fetch(nonOptional: _CFXDGCreateConfigHomePath()) as! String |
| 107 | + let configurationFile = URL(fileURLWithPath: "user-dirs.dirs", isDirectory: false, relativeTo: URL(fileURLWithPath: configurationHome, isDirectory: true)) |
| 108 | + |
| 109 | + return userDirectories(fromConfigurationFileAt: configurationFile) ?? [:] |
| 110 | + }() |
| 111 | + |
| 112 | + static let osDefaultDirectoryURLs: [_XDGUserDirectory: URL] = { |
| 113 | + let configurationDirs = _SwiftValue.fetch(nonOptional: _CFXDGCreateConfigDirectoriesPaths()) as! [String] |
| 114 | + |
| 115 | + for directory in configurationDirs { |
| 116 | + let configurationFile = URL(fileURLWithPath: directory, isDirectory: true).appendingPathComponent("user-dirs.defaults") |
| 117 | + |
| 118 | + if let result = userDirectories(fromConfigurationFileAt: configurationFile) { |
| 119 | + return result |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + return [:] |
| 124 | + }() |
| 125 | +} |
0 commit comments