-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Filesystem] Add a cross-platform readlink method #17498
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
Conversation
* | ||
* @throws IOException When the link does not exist or is not readable. | ||
*/ | ||
public function readlink($path, $finalTarget = false) |
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.
I would have defaulted to true
here. Or even better, why not create 2 methods?
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.
Or just keep realpath, which is very useful. I'm not sure readlink is that useful anyway.
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.
When you say realpath, what do you mean :) ? With final target to true ?
We are using both the "not final" and the "final" cases in Puli, I think two methods is a good idea indeed. How should we name them though? If we choose realpath/readlink, which convention do we choose? I would prefer the Unix one personnally (realpath = final, readlink = not final).
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.
Indeed, the Linux convention seems the best one.
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.
I did the change, but I think the name "realpath" is misleading. The PHP realpath do more things than resolving links (it also canonicalizes and normalizes the path: http://php.net/manual/fr/function.realpath.php). I'm not sure what happens in our case on Windows / Unix and if the behavior is really the same everywhere. Perhaps should we find a better name.
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.
What do you think of readLink
for "not final" and resolveLink
for "final"?
Any opinion on the methods names here? |
I rebased this on master. What do you think if this feature? |
I would use the method names from the Linux behavior. |
} | ||
|
||
if (!is_link($path)) { | ||
return $path; |
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 shouldn't this method work on regular files/dirs?
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.
I'm not sure what you mean, but the support of non-links paths was something I found useful. This can be changed, I'm not sure what's the excepted behavior.
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.
PHP's readlink()
function will emit a warning if the given path is no link. I think we should throw an InvalidArgumentException
then.
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.
That seems appropriate indeed.
0b94a11
to
f90548a
Compare
I did the changes. |
looks like you need to fix the tests Status: Needs Work |
I fixed the tests |
af2ad79
to
583fdf1
Compare
I think this is ready, targeting 3.1. Any review? |
throw new IOException(sprintf('The link %s does not exist and cannot be read.', $path)); | ||
} | ||
|
||
if (!is_link($path)) { |
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 this limitation ?
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.
I'm not sure what behavior we should have. The native behavior is to return false, but that seems inapropriate to me as we should return a string. What would you expect?
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.
Ok I understood, OK for exception here for me
By canonicalized, do you mean absolute or also fully resolved link? |
Both, ie what realpath does (on PHP/Linux) |
faa689c
to
259b4dd
Compare
@tgalopin Any news on this PR? Is it worth finishing it? |
@fabpot I think it is, I'll work on it. |
After some research, I propose the following behavior (same for every platform), following the way the Signature: By default (
With canonicalization (
Is everyone OK with this behavior? If it's ok for everyone, I'll start working on it. |
Ok for me with this updated version (no exceptions but return null instead) |
8efe4a5
to
e79b1a5
Compare
I applied this behavior and tests are green, I think this is ready for review. |
fa3b7fd
to
7ed04a4
Compare
👍 |
Thank you @tgalopin. |
…lopin) This PR was merged into the 3.2-dev branch. Discussion ---------- [Filesystem] Add a cross-platform readlink method | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - `readlink()` and `realpath()` have a completely different behavior under Windows and Unix: - `realpath()` resolves recursively the children links of a link until a final target is found on Unix and resolves only the next link on Windows ; - `readlink()` resolves recursively the children links of a link until a final target is found on Windows and resolves only the next link on Unix ; I propose to solve this by implementing a helper method in the Filesystem component that would behave always the same way under all platforms. Commits ------- c36507e [Filesystem] Add a cross-platform readlink/realpath methods for nested links
…(tgalopin) This PR was merged into the master branch. Discussion ---------- [Filesystem] Add documentation for the readlink method Following the merge of symfony/symfony#17498, this PR introduces documentation for the new methd `readlink`. Commits ------- 3572a3d [Filesystem] Add documentation for the readlink method
readlink()
andrealpath()
have a completely different behavior under Windows and Unix:realpath()
resolves recursively the children links of a link until a final target is found on Unix and resolves only the next link on Windows ;readlink()
resolves recursively the children links of a link until a final target is found on Windows and resolves only the next link on Unix ;I propose to solve this by implementing a helper method in the Filesystem component that would behave always the same way under all platforms.