This is an archive of the discontinued LLVM Phabricator instance.

[libcxx] Migrate posix_compat.h layer to not use CRT filesystem functions.
ClosedPublic

Authored by jyknight on Jun 15 2023, 8:35 AM.

Details

Summary

Right now, the filesystem APIs _mostly_ use Win32 API calls, but a
small number of functions use CRT APIs instead. The semantics are
effectively the same, except for which sorts of error codes are
returned. We want to be consistent about returning only native Win32
error codes, as a prerequisite for https://reviews.llvm.org/D151493.

This change switches getcwd, chdir, and mkdir. It does _not_ switch
open/close, because there are difficulties around the use of C-runtime
file descriptor numbers. Instead, those two APIs are removed from
posix_compat.h, and the win32-specific code inlined into the
operations.cpp FileDescriptor class (with a TODO comment).

Diff Detail

Event Timeline

jyknight created this revision.Jun 15 2023, 8:35 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 15 2023, 8:35 AM
jyknight requested review of this revision.Jun 15 2023, 8:35 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 15 2023, 8:35 AM
Herald added a reviewer: Restricted Project. · View Herald Transcript
Mordante added a subscriber: Mordante.

This patch will get conflicts when D152377 is merged.

libcxx/src/filesystem/posix_compat.h
353

Please add comment why this magic value (10) is used.

jyknight added inline comments.Jun 15 2023, 12:34 PM
libcxx/src/filesystem/posix_compat.h
353

I'm not sure why it was chosen...I copied that from realpath, below. It doesn't make a difference functionally, since if we go over, we simply reallocate.

mstorsjo accepted this revision.Jun 15 2023, 1:23 PM

This looks good to me, thanks! I guess it's mostly up to whether others are ok with the kludge moved into FileDescriptor::create.

libcxx/src/filesystem/posix_compat.h
353

I don't quite remember my reasoning or whether this was something I made (feels plausible) or that I was inspired to from somewhere else. I guess it was some sort of fudge factor.

MAX_PATH isn't an absolute max for all paths on Windows in all cases anyway - but I wanted to make sure that most paths that are dealt with do fit in here, even if they are MAX_PATH + null terminator. (I don't remember if paths that are limited by MAX_PATH include the null terminator in that size or not.) So for that purpose, maybe MAX_PATH+1 could be just as good. But I guess I chose 10 for some extra margin...

Mordante requested changes to this revision.Jun 19 2023, 9:14 AM

Can you rebase the patch on main and fix the CI so we can validate the patch works?

libcxx/src/filesystem/posix_compat.h
353

Ah I missed it was copy pasted. This is why I dislike magic, nobody remembers the secret invocation afterwards ;-)

This revision now requires changes to proceed.Jun 19 2023, 9:14 AM
Mordante accepted this revision.Jun 24 2023, 5:33 AM

LGTM!

This revision is now accepted and ready to land.Jun 24 2023, 5:33 AM
jyknight updated this revision to Diff 537321.Jul 5 2023, 5:44 AM

Rebase to rerun tests before submitting.

ldionne added a subscriber: ldionne.Jul 5 2023, 6:02 AM
ldionne added inline comments.
libcxx/src/filesystem/file_descriptor.h
127–138 ↗(On Diff #537321)

Is there anything blocking us from doing this now? I'm a bit concerned that this patch is adding a TODO without much of an intent to remove it (unless I'm mistaken).

jyknight added inline comments.Jul 5 2023, 6:27 AM
libcxx/src/filesystem/file_descriptor.h
127–138 ↗(On Diff #537321)

There is nothing blocking it, other than someone's time to work on it. I was indeed not planning to volunteer to do it at the moment, but would be happy if someone else wants to tackle it.

(I'll also note that CopyFile2 ought to be used on windows, regardless; the TODO just documents that deficiency).