File tree Expand file tree Collapse file tree 2 files changed +9
-11
lines changed Expand file tree Collapse file tree 2 files changed +9
-11
lines changed Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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! " )
You can’t perform that action at this time.
0 commit comments