Support: unlock Windows API support, switch to Windows 10 RS1+ APIs · mstorsjo/llvm-project@a48395f (original) (raw)

`@@ -466,18 +466,39 @@ static std::error_code rename_internal(HANDLE FromHandle, const Twine &To,

`

466

466

` (ToWide.size() * sizeof(wchar_t)));

`

467

467

` FILE_RENAME_INFO &RenameInfo =

`

468

468

` *reinterpret_cast<FILE_RENAME_INFO *>(RenameInfoBuf.data());

`

469

``

`-

RenameInfo.ReplaceIfExists = ReplaceIfExists;

`

``

469

+

``

470

`+

RenameInfo.Flags = FILE_RENAME_FLAG_POSIX_SEMANTICS

`

``

471

`+

| (ReplaceIfExists ? FILE_RENAME_FLAG_REPLACE_IF_EXISTS : 0);

`

470

472

` RenameInfo.RootDirectory = 0;

`

471

473

` RenameInfo.FileNameLength = ToWide.size() * sizeof(wchar_t);

`

472

474

`std::copy(ToWide.begin(), ToWide.end(), &RenameInfo.FileName[0]);

`

473

475

``

474

476

`SetLastError(ERROR_SUCCESS);

`

475

``

`-

if (!SetFileInformationByHandle(FromHandle, FileRenameInfo, &RenameInfo,

`

``

477

`+

// FileRenameInfoEx requires Windows 10 RS1

`

``

478

`+

if (!SetFileInformationByHandle(FromHandle, FileRenameInfoEx, &RenameInfo,

`

476

479

` RenameInfoBuf.size())) {

`

477

480

`unsigned Error = GetLastError();

`

478

481

`if (Error == ERROR_SUCCESS)

`

479

482

` Error = ERROR_CALL_NOT_IMPLEMENTED; // Wine doesn't always set error code.

`

480

``

`-

return mapWindowsError(Error);

`

``

483

`+

if (Error == ERROR_INVALID_PARAMETER || Error == ERROR_CALL_NOT_IMPLEMENTED) {

`

``

484

`+

// If running on an older version of Windows, fall back on FileRenameInfo.

`

``

485

`+

RenameInfo.ReplaceIfExists = ReplaceIfExists;

`

``

486

`+

RenameInfo.RootDirectory = 0;

`

``

487

`+

RenameInfo.FileNameLength = ToWide.size() * sizeof(wchar_t);

`

``

488

`+

std::copy(ToWide.begin(), ToWide.end(), &RenameInfo.FileName[0]);

`

``

489

+

``

490

`+

SetLastError(ERROR_SUCCESS);

`

``

491

`+

if (!SetFileInformationByHandle(FromHandle, FileRenameInfo, &RenameInfo,

`

``

492

`+

RenameInfoBuf.size())) {

`

``

493

`+

unsigned Error = GetLastError();

`

``

494

`+

if (Error == ERROR_SUCCESS)

`

``

495

`+

Error = ERROR_CALL_NOT_IMPLEMENTED; // Wine doesn't always set error code.

`

``

496

`+

return mapWindowsError(Error);

`

``

497

`+

}

`

``

498

`+

} else {

`

``

499

`+

return mapWindowsError(Error);

`

``

500

`+

}

`

``

501

+

481

502

` }

`

482

503

``

483

504

`return std::error_code();

`