-
-
Notifications
You must be signed in to change notification settings - Fork 672
dmd.root.filename: Fix portability issues with FileName.canonicalName. #9261
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,11 +15,6 @@ module dmd.root.filename; | |
| import core.stdc.ctype; | ||
| import core.stdc.errno; | ||
| import core.stdc.string; | ||
| import core.sys.posix.stdlib; | ||
| import core.sys.posix.sys.stat; | ||
| import core.sys.windows.winbase; | ||
| import core.sys.windows.windef; | ||
| import core.sys.windows.winnls; | ||
| import dmd.root.array; | ||
| import dmd.root.file; | ||
| import dmd.root.outbuffer; | ||
|
|
@@ -28,14 +23,29 @@ import dmd.root.rmem; | |
| import dmd.root.rootobject; | ||
| import dmd.utils; | ||
|
|
||
| nothrow | ||
| version (Posix) | ||
| { | ||
| import core.sys.posix.stdlib; | ||
| import core.sys.posix.sys.stat; | ||
| import core.sys.posix.unistd : getcwd; | ||
| } | ||
|
|
||
| version (Windows) | ||
| { | ||
| import core.sys.windows.winbase; | ||
| import core.sys.windows.windef; | ||
| import core.sys.windows.winnls; | ||
|
|
||
| extern (Windows) DWORD GetFullPathNameW(LPCWSTR, DWORD, LPWSTR, LPWSTR*) nothrow @nogc; | ||
| extern (Windows) void SetLastError(DWORD) nothrow @nogc; | ||
| extern (C) char* getcwd(char* buffer, size_t maxlen) nothrow; | ||
| } | ||
|
|
||
| version (CRuntime_Glibc) | ||
| { | ||
| version (Windows) extern (Windows) DWORD GetFullPathNameW(LPCWSTR, DWORD, LPWSTR, LPWSTR*) @nogc; | ||
| version (Windows) extern (Windows) void SetLastError(DWORD) @nogc; | ||
| version (Windows) extern (C) char* getcwd(char* buffer, size_t maxlen); | ||
| version (Posix) extern (C) char* canonicalize_file_name(const char*); | ||
| version (Posix) import core.sys.posix.unistd : getcwd; | ||
| extern (C) char* canonicalize_file_name(const char*) nothrow; | ||
| } | ||
|
|
||
| alias Strings = Array!(const(char)*); | ||
| alias Files = Array!(File*); | ||
|
|
||
|
|
@@ -856,8 +866,45 @@ nothrow: | |
| { | ||
| version (Posix) | ||
| { | ||
| // NULL destination buffer is allowed and preferred | ||
| return name.toCStringThen!((n) => realpath(n.ptr, null)).toDString; | ||
| import core.stdc.limits; // PATH_MAX | ||
| import core.sys.posix.unistd; // _PC_PATH_MAX | ||
|
|
||
| // Have realpath(), passing a NULL destination pointer may return an | ||
| // internally malloc'd buffer, however it is implementation defined | ||
| // as to what happens, so cannot rely on it. | ||
| static if (__traits(compiles, PATH_MAX)) | ||
| { | ||
| // Have compile time limit on filesystem path, use it with realpath. | ||
| char[PATH_MAX] buf = void; | ||
| auto path = name.toCStringThen!((n) => realpath(n.ptr, buf.ptr)); | ||
| if (path !is null) | ||
| return mem.xstrdup(path).toDString; | ||
| } | ||
| else static if (__traits(compiles, canonicalize_file_name)) | ||
| { | ||
| // Have canonicalize_file_name, which malloc's memory. | ||
| auto path = name.toCStringThen!((n) => canonicalize_file_name(n.ptr)); | ||
| if (path !is null) | ||
| return path.toDString; | ||
| } | ||
| else static if (__traits(compiles, _PC_PATH_MAX)) | ||
| { | ||
| // Panic! Query the OS for the buffer limit. | ||
| auto path_max = pathconf("/", _PC_PATH_MAX); | ||
| if (path_max > 0) | ||
| { | ||
| char *buf = cast(char*)mem.xmalloc(path_max); | ||
| scope(exit) mem.xfree(buf); | ||
| auto path = name.toCStringThen!((n) => realpath(n.ptr, buf)); | ||
| if (path !is null) | ||
| return mem.xstrdup(path).toDString; | ||
| } | ||
| } | ||
| // Give up trying to support this platform, just duplicate the filename | ||
| // unless there is nothing to copy from. | ||
| if (!name.length) | ||
| return null; | ||
| return mem.xstrdup(name.ptr)[0 .. name.length]; | ||
| } | ||
| else version (Windows) | ||
| { | ||
|
|
@@ -1078,10 +1125,10 @@ version (Posix) | |
| */ | ||
| auto absPathThen(alias F)(const(char)[] fileName) | ||
| { | ||
| import core.sys.posix.stdlib: realpath, free; | ||
| char* absPath = fileName.toCStringThen!((fn) => realpath(&fn[0], null /* realpath allocates */)); | ||
| scope(exit) free(absPath); | ||
| return F(absPath.toDString()); | ||
| import core.sys.posix.stdlib: free; | ||
| auto absPath = FileName.canonicalName(fileName); | ||
| scope(exit) free(cast(void*)absPath.ptr); | ||
| return F(cast(char[])absPath); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be simpler just to swap lines 1130 and 1131:
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If this was a void function, then I'd agree. |
||
| } | ||
| } | ||
| else | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not a
static assertinstead of doing nothing ? It does not look like a good default behavior and might be hard to track.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just returning null would mean nothing would compile. We already have the relative paths to druntime and phobos, let the compiler use them instead of erroring that it can't find any sources.