Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

winapi: Implement CopyFileA #499

Merged
merged 1 commit into from
Oct 8, 2021
Merged

Conversation

abaire
Copy link
Contributor

@abaire abaire commented Sep 11, 2021

Implements CopyFileA.

Tests can be found at abaire/nxdk_tests

lib/winapi/winioctl.h Outdated Show resolved Hide resolved
lib/winapi/filemanip.c Outdated Show resolved Hide resolved
lib/winapi/winioctl.h Outdated Show resolved Hide resolved
@@ -241,6 +242,130 @@ BOOL MoveFileA (LPCTSTR lpExistingFileName, LPCTSTR lpNewFileName)
}
}

BOOL CopyFileA (LPCTSTR lpExistingFileName, LPCTSTR lpNewFileName, BOOL bFailIfExists)
{
static const DWORD readBufferSize = 65535;
Copy link
Member

@JayFoxRox JayFoxRox Sep 11, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For such constants I prefer constants like 0xFFFF because it will be much more obvious where it originates (not much of an issue for 16 bit, but still; also avoids confusion with 0x10000).

Also: Why not 0x10000? Won't the unaligned value trip the allocator?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, changed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally, I'd prefer it like 64*1024.
As for the buffer size, it currently is only 4KiB (64KiB previously). That's a bit small imho - XDK code seem to copy in 16KiB chunks (at least that's the size Halo 2 uses to copy files), which I think is a reasonable compromise between performance and saving memory.

lib/winapi/filemanip.c Outdated Show resolved Hide resolved
lib/winapi/filemanip.c Outdated Show resolved Hide resolved
lib/winapi/filemanip.c Outdated Show resolved Hide resolved
lib/xboxkrnl/xboxkrnl.h Outdated Show resolved Hide resolved
}
bytesRead -= bytesWritten;
bufferPos += bytesWritten;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't the kernel offer some call or IOCTL to copy files?
This user-space copy loop feels a bit involved and there's probably a lot that can go wrong (stuff like not applying the proper flags to the file when creating it, not taking advantage of file-system optimizations etc.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Totally agree; I didn't see a function that looked appropriate in xboxkrnl.h so I assumed there was no lower level method that could be utilized. Similarly I did not have luck searching the win32 docs.

Is there someplace else I could look?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Poked around a bit more, the only lower level copying I found was added as part of windows server after win2k/xp days.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possibly something more like NtWriteFile() than using the user abstraction WriteFile() is what he's hinting at?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I assume he was looking for something more like https://docs.microsoft.com/en-us/windows/win32/api/winioctl/ni-winioctl-fsctl_duplicate_extents_to_file where the entire copy loop is handled at a lower level (but that's not available in the XBOX kernel).

I think a switch to NtWriteFile would end up duplicating much of the content of WriteFile and still operating in user-space. I don't know enough to say if the efficiency gain warrants the maintenance cost, but happy to make that change if it's preferred.

@JayFoxRox can you clarify?

@abaire abaire marked this pull request as draft September 11, 2021 16:13
@abaire abaire force-pushed the adds_copy_file branch 3 times, most recently from de303b5 to 7091a3d Compare September 13, 2021 16:01
@abaire abaire marked this pull request as ready for review September 13, 2021 16:01
lib/winapi/fileapi.h Outdated Show resolved Hide resolved
lib/winapi/filemanip.c Show resolved Hide resolved
lib/winapi/filemanip.c Outdated Show resolved Hide resolved
lib/winapi/filemanip.c Show resolved Hide resolved
lib/winapi/filemanip.c Outdated Show resolved Hide resolved
lib/winapi/filemanip.c Show resolved Hide resolved
lib/winapi/filemanip.c Outdated Show resolved Hide resolved
lib/winapi/filemanip.c Outdated Show resolved Hide resolved
@abaire abaire force-pushed the adds_copy_file branch 3 times, most recently from aa3ff3f to 7a81aab Compare September 16, 2021 04:06
lib/winapi/filemanip.c Outdated Show resolved Hide resolved
lib/winapi/filemanip.c Show resolved Hide resolved
lib/winapi/filemanip.c Outdated Show resolved Hide resolved
lib/winapi/filemanip.c Show resolved Hide resolved
lib/winapi/filemanip.c Outdated Show resolved Hide resolved
lib/winapi/filemanip.c Outdated Show resolved Hide resolved
lib/winapi/filemanip.c Outdated Show resolved Hide resolved
Copy link
Member

@thrimbor thrimbor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks very close to being ready to merge now, so I should probably mention that, while this isn't explicitly mentioned in this file, the winapi part of nxdk is licensed under the MIT license. Just want to make sure you're aware and fine with that.
You don't have to add any license or copyright info to this file yourself, I'll add that when I slap SPDX headers on everything.

Copy link
Contributor Author

@abaire abaire left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the heads up on the license, MIT is totally fine with me.

lib/winapi/filemanip.c Outdated Show resolved Hide resolved
lib/winapi/filemanip.c Show resolved Hide resolved
lib/winapi/filemanip.c Outdated Show resolved Hide resolved
@abaire abaire force-pushed the adds_copy_file branch 2 times, most recently from 6276e60 to 597921d Compare September 27, 2021 14:54
lib/winapi/filemanip.c Outdated Show resolved Hide resolved
lib/winapi/filemanip.c Show resolved Hide resolved
@thrimbor
Copy link
Member

thrimbor commented Oct 8, 2021

I took the liberty to force push away the merge conflict.

I'll merge once the CI has run through. Thanks for the contribution!

@thrimbor thrimbor merged commit 671b0ce into XboxDev:master Oct 8, 2021
@abaire abaire deleted the adds_copy_file branch October 8, 2021 16:22
@abaire abaire mentioned this pull request Jul 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

5 participants