Skip to content

Implement FileManager.url{s,}(for:…) and NSSearchPathForDirectoriesInDomains. #1456

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 8 commits into from
Jun 12, 2018
Merged
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions Foundation.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
/* Begin PBXBuildFile section */
0383A1751D2E558A0052E5D1 /* TestStream.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0383A1741D2E558A0052E5D1 /* TestStream.swift */; };
03B6F5841F15F339004F25AF /* TestURLProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = 03B6F5831F15F339004F25AF /* TestURLProtocol.swift */; };
1513A8432044893F00539722 /* FileManager_XDG.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1513A8422044893F00539722 /* FileManager_XDG.swift */; };
1520469B1D8AEABE00D02E36 /* HTTPServer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1520469A1D8AEABE00D02E36 /* HTTPServer.swift */; };
153E951120111DC500F250BE /* CFKnownLocations.h in Headers */ = {isa = PBXBuildFile; fileRef = 153E950F20111DC500F250BE /* CFKnownLocations.h */; settings = {ATTRIBUTES = (Private, ); }; };
153E951220111DC500F250BE /* CFKnownLocations.c in Sources */ = {isa = PBXBuildFile; fileRef = 153E951020111DC500F250BE /* CFKnownLocations.c */; };
Expand Down Expand Up @@ -515,6 +516,7 @@
/* Begin PBXFileReference section */
0383A1741D2E558A0052E5D1 /* TestStream.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestStream.swift; sourceTree = "<group>"; };
03B6F5831F15F339004F25AF /* TestURLProtocol.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestURLProtocol.swift; sourceTree = "<group>"; };
1513A8422044893F00539722 /* FileManager_XDG.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FileManager_XDG.swift; sourceTree = "<group>"; };
1520469A1D8AEABE00D02E36 /* HTTPServer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HTTPServer.swift; sourceTree = "<group>"; };
153E950F20111DC500F250BE /* CFKnownLocations.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = CFKnownLocations.h; sourceTree = "<group>"; };
153E951020111DC500F250BE /* CFKnownLocations.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = CFKnownLocations.c; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1822,6 +1824,7 @@
children = (
EADE0B5D1BD15DFF00C49C64 /* FileHandle.swift */,
EADE0B5E1BD15DFF00C49C64 /* FileManager.swift */,
1513A8422044893F00539722 /* FileManager_XDG.swift */,
EADE0B7A1BD15DFF00C49C64 /* Process.swift */,
5BDC3F2F1BCC5DCB00ED97BB /* Bundle.swift */,
5BDC3F411BCC5DCB00ED97BB /* ProcessInfo.swift */,
Expand Down Expand Up @@ -2344,6 +2347,7 @@
5BECBA3A1D1CAE9A00B39B1F /* NSMeasurement.swift in Sources */,
5BF7AEB21BCD51F9008F214A /* NSNumber.swift in Sources */,
61D2F9AF1FECFB3E0033306A /* NativeProtocol.swift in Sources */,
1513A8432044893F00539722 /* FileManager_XDG.swift in Sources */,
B9974B991EDF4A22007F15B8 /* HTTPURLProtocol.swift in Sources */,
5BCD03821D3EE35C00E3FF9B /* TimeZone.swift in Sources */,
EADE0BBC1BD15E0000C49C64 /* URLCache.swift in Sources */,
Expand Down Expand Up @@ -2708,6 +2712,7 @@
DYLIB_COMPATIBILITY_VERSION = 150;
DYLIB_CURRENT_VERSION = 1303;
DYLIB_INSTALL_NAME_BASE = "@rpath";
ENABLE_TESTABILITY = YES;
FRAMEWORK_VERSION = A;
GCC_PREFIX_HEADER = CoreFoundation/Base.subproj/CoreFoundation_Prefix.h;
HEADER_SEARCH_PATHS = (
Expand Down Expand Up @@ -2780,6 +2785,7 @@
DYLIB_COMPATIBILITY_VERSION = 150;
DYLIB_CURRENT_VERSION = 1303;
DYLIB_INSTALL_NAME_BASE = "@rpath";
ENABLE_TESTABILITY = YES;
FRAMEWORK_VERSION = A;
GCC_PREFIX_HEADER = CoreFoundation/Base.subproj/CoreFoundation_Prefix.h;
HEADER_SEARCH_PATHS = (
Expand Down
325 changes: 321 additions & 4 deletions Foundation/FileManager.swift

Large diffs are not rendered by default.

125 changes: 125 additions & 0 deletions Foundation/FileManager_XDG.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//

import CoreFoundation

enum _XDGUserDirectory: String {
case desktop = "DESKTOP"
case download = "DOWNLOAD"
case publicShare = "PUBLICSHARE"
case documents = "DOCUMENTS"
case music = "MUSIC"
case pictures = "PICTURES"
case videos = "VIDEOS"

static let allDirectories: [_XDGUserDirectory] = [
.desktop,
.download,
.publicShare,
.documents,
.music,
.pictures,
.videos,
]

var url: URL {
return url(userConfiguration: _XDGUserDirectory.configuredDirectoryURLs,
osDefaultConfiguration: _XDGUserDirectory.osDefaultDirectoryURLs,
stopgaps: _XDGUserDirectory.stopgapDefaultDirectoryURLs)
}

func url(userConfiguration: [_XDGUserDirectory: URL],
osDefaultConfiguration: [_XDGUserDirectory: URL],
stopgaps: [_XDGUserDirectory: URL]) -> URL {
if let url = userConfiguration[self] {
return url
} else if let url = osDefaultConfiguration[self] {
return url
} else {
return stopgaps[self]!
}
}

static let stopgapDefaultDirectoryURLs: [_XDGUserDirectory: URL] = {
let home = URL(fileURLWithPath: NSHomeDirectory(), isDirectory: true)
return [
.desktop: home.appendingPathComponent("Desktop"),
.download: home.appendingPathComponent("Downloads"),
.publicShare: home.appendingPathComponent("Public"),
.documents: home.appendingPathComponent("Documents"),
.music: home.appendingPathComponent("Music"),
.pictures: home.appendingPathComponent("Pictures"),
.videos: home.appendingPathComponent("Videos"),
]
}()

static func userDirectories(fromConfigurationFileAt url: URL) -> [_XDGUserDirectory: URL]? {
if let configuration = try? String(contentsOf: url, encoding: .utf8) {
return userDirectories(fromConfiguration: configuration)
} else {
return nil
}
}

static func userDirectories(fromConfiguration configuration: String) -> [_XDGUserDirectory: URL] {
var entries: [_XDGUserDirectory: URL] = [:]
let home = URL(fileURLWithPath: NSHomeDirectory(), isDirectory: true)

// Parse it:
let lines = configuration.split(separator: "\n")
for line in lines {
if let range = line.range(of: "=") {
var variable = String(line[line.startIndex ..< range.lowerBound].trimmingCharacters(in: .whitespaces))

let prefix = "XDG_"
let suffix = "_DIR"
if variable.hasPrefix(prefix) && variable.hasSuffix(suffix) {
let endOfPrefix = variable.index(variable.startIndex, offsetBy: prefix.length)
let startOfSuffix = variable.index(variable.endIndex, offsetBy: -suffix.length)

variable = String(variable[endOfPrefix ..< startOfSuffix])
}

guard let directory = _XDGUserDirectory(rawValue: variable) else {
continue
}

let path = String(line[range.upperBound ..< line.endIndex]).trimmingCharacters(in: .whitespaces)
if !path.isEmpty {
entries[directory] = URL(fileURLWithPath: path, isDirectory: true, relativeTo: home)
}
} else {
return [:] // Incorrect syntax.
}
}

return entries
}

static let configuredDirectoryURLs: [_XDGUserDirectory: URL] = {
let configurationHome = _SwiftValue.fetch(nonOptional: _CFXDGCreateConfigHomePath()) as! String
let configurationFile = URL(fileURLWithPath: "user-dirs.dirs", isDirectory: false, relativeTo: URL(fileURLWithPath: configurationHome, isDirectory: true))

return userDirectories(fromConfigurationFileAt: configurationFile) ?? [:]
}()

static let osDefaultDirectoryURLs: [_XDGUserDirectory: URL] = {
let configurationDirs = _SwiftValue.fetch(nonOptional: _CFXDGCreateConfigDirectoriesPaths()) as! [String]

for directory in configurationDirs {
let configurationFile = URL(fileURLWithPath: directory, isDirectory: true).appendingPathComponent("user-dirs.defaults")

if let result = userDirectories(fromConfigurationFileAt: configurationFile) {
return result
}
}

return [:]
}()
}
24 changes: 23 additions & 1 deletion Foundation/NSPathUtilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,29 @@ extension FileManager {
}

public func NSSearchPathForDirectoriesInDomains(_ directory: FileManager.SearchPathDirectory, _ domainMask: FileManager.SearchPathDomainMask, _ expandTilde: Bool) -> [String] {
NSUnimplemented()
let knownDomains: [FileManager.SearchPathDomainMask] = [
.userDomainMask,
.networkDomainMask,
.localDomainMask,
.systemDomainMask,
]

var result: [URL] = []

for domain in knownDomains {
if domainMask.contains(domain) {
result.append(contentsOf: FileManager.default.urls(for: directory, in: domain))
}
}

return result.map { (url) in
var path = url.absoluteURL.path
if expandTilde {
path = NSString(string: path).expandingTildeInPath
}

return path
}
}

public func NSHomeDirectory() -> String {
Expand Down
Loading