Skip to content

Commit 0ea41af

Browse files
keithaciidgh
authored andcommitted
Add support for replacing files atomically
Previously if you tried to write a file to a destination atomically it would error because moveItem doesn't support overwriting.
1 parent e11ef6f commit 0ea41af

File tree

2 files changed

+9
-11
lines changed

2 files changed

+9
-11
lines changed

Sources/TSCBasic/FileSystem.swift

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -380,17 +380,9 @@ private class LocalFileSystem: FileSystem {
380380
if !atomically {
381381
return try writeFileContents(path, bytes: bytes)
382382
}
383-
try withTemporaryFile(dir: path.parentDirectory, deleteOnClose: false) { temp in
384-
do {
385-
try writeFileContents(temp.path, bytes: bytes)
386-
try FileManager.default.moveItem(atPath: temp.path.pathString, toPath: path.pathString)
387-
} catch {
388-
// Write or rename failed, delete the temporary file.
389-
// Rethrow the original error, however, as that's the
390-
// root cause of the failure.
391-
_ = try? self.removeFileTree(temp.path)
392-
throw error
393-
}
383+
384+
try bytes.withData {
385+
try $0.write(to: URL(fileURLWithPath: path.pathString), options: .atomic)
394386
}
395387
}
396388

Tests/TSCBasicTests/FileSystemTests.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,11 +161,17 @@ class FileSystemTests: XCTestCase {
161161
let read1 = try fs.readFileContents(filePath1)
162162
XCTAssertEqual(read1, byteString)
163163

164+
// Test overwriting file non-atomically
165+
XCTAssertNoThrow(try fs.writeFileContents(filePath1, bytes: byteString, atomically: false))
166+
164167
let filePath2 = tmpDirPath.appending(components: "test-data-2.txt")
165168
XCTAssertNoThrow(try fs.writeFileContents(filePath2, bytes: byteString, atomically: true))
166169
let read2 = try fs.readFileContents(filePath2)
167170
XCTAssertEqual(read2, byteString)
168171

172+
// Test overwriting file atomically
173+
XCTAssertNoThrow(try fs.writeFileContents(filePath2, bytes: byteString, atomically: true))
174+
169175
// Check overwrite of a file.
170176
try! fs.writeFileContents(filePath, bytes: "Hello, new world!")
171177
XCTAssertEqual(try! fs.readFileContents(filePath), "Hello, new world!")

0 commit comments

Comments
 (0)