Skip to content

Commit f5728c6

Browse files
committed
[Foundation] Implemented unlink / rmdir / rmtree
1 parent 3c673df commit f5728c6

File tree

11 files changed

+40
-82
lines changed

11 files changed

+40
-82
lines changed

Sources/ManifestSerializer/Manifest+parse.swift

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,9 @@
88
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
99
*/
1010

11-
import func Utility.realpath
12-
import func POSIX.unlink
1311
import PackageDescription
1412
import PackageType
1513
import Utility
16-
import func Utility.fopen
1714

1815
extension Manifest {
1916
public init(path pathComponents: String..., baseURL: String, swiftc: String, libdir: String) throws {
@@ -75,7 +72,7 @@ private func parse(path manifestPath: String, swiftc: String, libdir: String) th
7572
cmd += ["-fileno", "\(fp.fileDescriptor)"]
7673
try system(cmd)
7774

78-
let toml = try fopen(filePath).reduce("") { $0 + "\n" + $1 }
75+
let toml = try fopen(filePath).enumerate().reduce("") { $0 + "\n" + $1 }
7976
try unlink(filePath) //Delete the temp file after reading it
8077

8178
return toml != "" ? toml : nil

Sources/POSIX/rmdir.swift

Lines changed: 0 additions & 19 deletions
This file was deleted.

Sources/Utility/misc.swift

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,31 +14,7 @@
1414

1515
import POSIX
1616
import var libc.ENOENT
17-
18-
/**
19-
Recursively deletes the provided directory.
20-
*/
21-
public func rmtree(_ components: String...) throws {
22-
let path = Path.join(components)
23-
var dirs = [String]()
24-
for entry in walk(path, recursively: true) {
25-
if entry.isDirectory && !entry.isSymlink {
26-
dirs.append(entry)
27-
} else {
28-
try POSIX.unlink(entry)
29-
}
30-
}
31-
for dir in dirs.reversed() {
32-
do {
33-
try POSIX.rmdir(dir)
34-
} catch .rmdir(let errno, _) as SystemError where errno == ENOENT {
35-
// if the directory is not there then proceed
36-
// this could happen if it was in fact symlinked
37-
// from another part of the tree etc.
38-
}
39-
}
40-
try POSIX.rmdir(path)
41-
}
17+
import Foundation
4218

4319

4420
#if os(OSX) || os(iOS) || os(Linux)
@@ -48,3 +24,13 @@ public func rmtree(_ components: String...) throws {
4824
#else
4925
//ERROR: Unsupported platform
5026
#endif
27+
28+
29+
// Temporary extension until SwiftFoundation API is updated.
30+
#if os(Linux)
31+
extension NSFileManager {
32+
static func `default`() -> NSFileManager {
33+
return defaultManager()
34+
}
35+
}
36+
#endif

Sources/POSIX/unlink.swift renamed to Sources/Utility/unlink.swift

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,8 @@
88
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
99
*/
1010

11-
12-
import func libc.unlink
13-
import var libc.errno
11+
import Foundation
1412

1513
public func unlink(_ path: String) throws {
16-
guard libc.unlink(path) == 0 else {
17-
throw SystemError.unlink(errno, path)
18-
}
14+
try NSFileManager.`default`().removeItem(atPath: path)
1915
}

Sources/swift-build/main.swift

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@
1010

1111
import func POSIX.getcwd
1212
import func POSIX.getenv
13-
import func POSIX.unlink
1413
import func POSIX.chdir
15-
import func POSIX.rmdir
14+
import func Utility.unlink
1615
import func libc.exit
1716
import ManifestSerializer
1817
import PackageType
@@ -64,7 +63,7 @@ do {
6463
try initPackage.writePackageStructure()
6564

6665
case .Update:
67-
try rmtree(opts.path.Packages)
66+
try unlink(opts.path.Packages)
6867
fallthrough
6968

7069
case .Fetch:
@@ -75,25 +74,25 @@ do {
7574

7675
case .Clean(.Dist):
7776
if opts.path.Packages.exists {
78-
try rmtree(opts.path.Packages)
77+
try unlink(opts.path.Packages)
7978
}
8079
fallthrough
8180

8281
case .Clean(.Build):
8382
let artifacts = ["debug", "release"].map{ Path.join(opts.path.build, $0) }.map{ ($0, "\($0).yaml") }
8483
for (dir, yml) in artifacts {
85-
if dir.isDirectory { try rmtree(dir) }
84+
if dir.isDirectory { try unlink(dir) }
8685
if yml.isFile { try unlink(yml) }
8786
}
8887

8988
let db = Path.join(opts.path.build, "build.db")
9089
if db.isFile { try unlink(db) }
9190

9291
let versionData = Path.join(opts.path.build, "versionData")
93-
if versionData.isDirectory { try rmtree(versionData) }
92+
if versionData.isDirectory { try unlink(versionData) }
9493

9594
if opts.path.build.exists {
96-
try rmdir(opts.path.build)
95+
try unlink(opts.path.build)
9796
}
9897

9998
case .Doctor:

Tests/Functional/InvalidLayoutTests.swift

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99
*/
1010

1111
import XCTest
12-
import func POSIX.rmdir
13-
import func POSIX.unlink
12+
import func Utility.unlink
1413

1514
class InvalidLayoutsTestCase: XCTestCase {
1615

@@ -32,7 +31,7 @@ class InvalidLayoutsTestCase: XCTestCase {
3231
*/
3332
fixture(name: "InvalidLayouts/Generic1") { prefix in
3433
XCTAssertBuildFails(prefix)
35-
try POSIX.unlink("\(prefix)/main.swift")
34+
try Utility.unlink("\(prefix)/main.swift")
3635
XCTAssertBuilds(prefix)
3736
}
3837
}
@@ -47,7 +46,7 @@ class InvalidLayoutsTestCase: XCTestCase {
4746
*/
4847
fixture(name: "InvalidLayouts/Generic2") { prefix in
4948
XCTAssertBuildFails(prefix)
50-
try POSIX.unlink("\(prefix)/main.swift")
49+
try Utility.unlink("\(prefix)/main.swift")
5150
XCTAssertBuilds(prefix)
5251
}
5352
}
@@ -62,7 +61,7 @@ class InvalidLayoutsTestCase: XCTestCase {
6261
*/
6362
fixture(name: "InvalidLayouts/Generic3") { prefix in
6463
XCTAssertBuildFails(prefix)
65-
try POSIX.unlink("\(prefix)/Sources/main.swift")
64+
try Utility.unlink("\(prefix)/Sources/main.swift")
6665
XCTAssertBuilds(prefix)
6766
}
6867
}
@@ -77,7 +76,7 @@ class InvalidLayoutsTestCase: XCTestCase {
7776
*/
7877
fixture(name: "InvalidLayouts/Generic4") { prefix in
7978
XCTAssertBuildFails(prefix)
80-
try POSIX.unlink("\(prefix)/main.swift")
79+
try Utility.unlink("\(prefix)/main.swift")
8180
XCTAssertBuilds(prefix)
8281
}
8382
}
@@ -99,8 +98,8 @@ class InvalidLayoutsTestCase: XCTestCase {
9998
// determineTargets() but also we are saying: this
10099
// layout is only for *very* simple projects.
101100

102-
try POSIX.unlink("\(prefix)/Foo/Foo.swift")
103-
try POSIX.rmdir("\(prefix)/Foo")
101+
try Utility.unlink("\(prefix)/Foo/Foo.swift")
102+
try Utility.unlink("\(prefix)/Foo")
104103
XCTAssertBuilds(prefix)
105104
}
106105
}

Tests/Functional/Utilities.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*/
1010

1111
import struct Utility.Path
12-
import func Utility.rmtree
12+
import func Utility.unlink
1313
import func Utility.walk
1414
import func XCTest.XCTFail
1515
import func POSIX.getenv
@@ -31,7 +31,7 @@ func fixture(name fixtureName: String, tags: [String] = [], file: StaticString =
3131

3232
do {
3333
try POSIX.mkdtemp(gsub(fixtureName)) { prefix in
34-
defer { _ = try? rmtree(prefix) }
34+
defer { _ = try? unlink(prefix) }
3535

3636
let rootd = Path.join(#file, "../../../Fixtures", fixtureName).normpath
3737

@@ -167,7 +167,7 @@ func executeSwiftBuild(_ chdir: String, configuration: Configuration = .Debug, p
167167
func mktmpdir(_ file: StaticString = #file, line: UInt = #line, body: @noescape(String) throws -> Void) {
168168
do {
169169
try POSIX.mkdtemp("spm-tests") { dir in
170-
defer { _ = try? rmtree(dir) }
170+
defer { _ = try? unlink(dir) }
171171
try body(dir)
172172
}
173173
} catch {

Tests/Transmute/fixture().swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111
import struct Utility.Path
1212
import func POSIX.mkdir
1313
import func POSIX.system
14-
import func Utility.rmtree
14+
import func Utility.unlink
1515

1616
func fixture(files: [String]) throws -> String {
1717
let testdir = Path.join(#file, "../../../.build/test.out").normpath
1818
if testdir.isDirectory {
19-
try rmtree(testdir)
19+
try unlink(testdir)
2020
}
2121
try mkdir(testdir)
2222
for file in files {

Tests/Utility/MiscTests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ import func Utility.realpath
1212
import func POSIX.mkdtemp
1313
import func POSIX.mkdir
1414
import func POSIX.symlink
15-
import func Utility.rmtree
15+
import func Utility.unlink
1616
import XCTest
1717

18-
class RmtreeTests: XCTestCase {
18+
class UnlinkTests: XCTestCase {
1919

2020
func testDoesNotFollowSymlinks() {
2121
do {
@@ -32,7 +32,7 @@ class RmtreeTests: XCTestCase {
3232
XCTAssertEqual(try! realpath("\(root)/foo/symlink"), "\(root)/bar")
3333
XCTAssertTrue(try! realpath("\(root)/foo/symlink/baz").isDirectory)
3434

35-
try rmtree(root, "foo")
35+
try Utility.unlink("\(root)/foo")
3636

3737
XCTAssertFalse("\(root)/foo".exists)
3838
XCTAssertFalse("\(root)/foo".isDirectory)

Tests/Utility/PathTests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import XCTest
1515
import func POSIX.getenv
1616
import func POSIX.getcwd
1717
import func POSIX.mkdtemp
18-
import func POSIX.rmdir
18+
import func Utility.unlink
1919
import func POSIX.symlink
2020
import func POSIX.mkdir
2121

@@ -176,8 +176,8 @@ class StatTests: XCTestCase {
176176
XCTAssertTrue("\(root)/symlink".isDirectory)
177177
XCTAssertTrue("\(root)/symlink".isSymlink)
178178

179-
try POSIX.rmdir("\(root)/foo/bar")
180-
try POSIX.rmdir("\(root)/foo")
179+
try Utility.unlink("\(root)/foo/bar")
180+
try Utility.unlink("\(root)/foo")
181181

182182
XCTAssertTrue("\(root)/symlink".isSymlink)
183183
XCTAssertFalse("\(root)/symlink".isDirectory)

Tests/Utility/XCTestManifests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ extension FileTests {
4444
}
4545

4646

47-
extension RmtreeTests {
48-
static var allTests : [(String, (RmtreeTests) -> () throws -> Void)] {
47+
extension UnlinkTests {
48+
static var allTests : [(String, (UnlinkTests) -> () throws -> Void)] {
4949
return [
5050
("testDoesNotFollowSymlinks", testDoesNotFollowSymlinks),
5151
]

0 commit comments

Comments
 (0)