diff --git a/Sources/FoundationEssentials/FileManager/FileManager+Files.swift b/Sources/FoundationEssentials/FileManager/FileManager+Files.swift index 1d9b9fe21..3aa048ac3 100644 --- a/Sources/FoundationEssentials/FileManager/FileManager+Files.swift +++ b/Sources/FoundationEssentials/FileManager/FileManager+Files.swift @@ -841,7 +841,7 @@ extension _FileManagerImpl { ftTime.dwLowDateTime = uiTime.LowPart ftTime.dwHighDateTime = uiTime.HighPart - let hFile: HANDLE = CreateFileW($0, GENERIC_WRITE, FILE_SHARE_WRITE, nil, OPEN_EXISTING, 0, nil) + let hFile: HANDLE = CreateFileW($0, GENERIC_WRITE, FILE_SHARE_WRITE, nil, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, nil) if hFile == INVALID_HANDLE_VALUE { throw CocoaError.errorWithFilePath(path, win32: GetLastError(), reading: true) } diff --git a/Tests/FoundationEssentialsTests/FileManager/FileManagerTests.swift b/Tests/FoundationEssentialsTests/FileManager/FileManagerTests.swift index d592ca438..af5739555 100644 --- a/Tests/FoundationEssentialsTests/FileManager/FileManagerTests.swift +++ b/Tests/FoundationEssentialsTests/FileManager/FileManagerTests.swift @@ -886,6 +886,27 @@ private struct FileManagerTests { } } + @Test func testSetAttributesModificationDate() async throws { + try await FilePlayground { + "testFile" + Directory("testDir") {} + }.test { fileManager in + // Test setAttributes with modificationDate on files + let fileTestDate = Date(timeIntervalSince1970: 1234567890) + try fileManager.setAttributes([.modificationDate: fileTestDate], ofItemAtPath: "testFile") + let fileAttrs = try fileManager.attributesOfItem(atPath: "testFile") + let fileModDate = try #require(fileAttrs[.modificationDate] as? Date) + #expect(abs(fileModDate.timeIntervalSince1970 - fileTestDate.timeIntervalSince1970) < 2.0, "File modification date should be set correctly") + + // Test setAttributes with modificationDate on directories + let dirTestDate = Date(timeIntervalSince1970: 1234567890) + try fileManager.setAttributes([.modificationDate: dirTestDate], ofItemAtPath: "testDir") + let directoryAttrs = try fileManager.attributesOfItem(atPath: "testDir") + let directoryModDate = try #require(directoryAttrs[.modificationDate] as? Date) + #expect(abs(directoryModDate.timeIntervalSince1970 - dirTestDate.timeIntervalSince1970) < 2.0, "Directory modification date should be set correctly") + } + } + @Test func malformedModificationDateAttribute() async throws { let sentinelDate = Date(timeIntervalSince1970: 100) try await FilePlayground {