Repository navigation
Add logic to detect recursive symlink - #2252
A (ealexjordan) wants to merge 3 commits into
Conversation
|
Hi A (@ealexjordan), I'm your friendly neighborhood Microsoft Pull Request Bot (You can call me MSBOT). Thanks for your contribution!
TTYL, MSBOT; |
There was a problem hiding this comment.
I think we have the same problem in windows, for example:
Directory of e:\psTest\test\foo
09/13/2016 04:26 PM <DIR> .
09/13/2016 04:26 PM <DIR> ..
09/13/2016 04:26 PM <SYMLINKD> foo [e:\psTest\test]
Can you please solve it for both win and unix?
|
Test |
|
A (@ealexjordan) could you please also add test along with the code change? |
Detects when symlink recursively links back to parent directory and skips recursing into that symlink rather than recursing until the limit is hit. Does not fix more complicated symlink loops, as in sys
|
|
||
| try | ||
| { | ||
| readSymLink = InternalSymbolicLinkLinkCodeMethods.GetTarget(obj).First().ToString(); |
There was a problem hiding this comment.
It looks like InternalSymbolicLinkLinkCodeMethods.GetTarget will take care of both win and unix platforms -- it calls into Platform.NonWindowsInternalGetTarget for non-windows platforms. So you don't need to have if (win) .. else ... anymore.
There was a problem hiding this comment.
GetTarget(obj).First().ToString();
InternalSymbolicLinkLinkCodeMethods.GetTarget returns IEnumerable<string>, so the ToString() is unnecessary.
| } | ||
|
|
||
| //if the read symlink is not null or empty, and what it points to is the parent directory then it is recursive | ||
| if (!String.IsNullOrEmpty(readSymLink) && path.Parent.FullName.Contains(readSymLink)) |
There was a problem hiding this comment.
path.Parent.FullName.Contains(readSymLink))
I think path.FullName.StartsWith is a better check. Considering the following case:
path = /tmp/home/pi/foo
path.Parent.FullName = /tmp/home/pi
readSymLink = /home/pi
in this case, path is pointing to its parent recursively, but this method would return true.
There was a problem hiding this comment.
And also, please use the StartsWith overload with the culture comparison parameter.
| //Platform is Linux or Windows | ||
| else | ||
| { | ||
| readSymLink = Platform.NonWindowsInternalGetTarget(path.FullName); |
There was a problem hiding this comment.
you catch all exception for win-plat above, but not for unix-plat. I think maybe it's better to not catch exception when calling into GetTarget. It shouldn't throw any exception normally, but if anything went wrong, it's probably better to let the user know.
| @@ -1,3 +1,5 @@ | |||
|
|
|||
|
|
|||
There was a problem hiding this comment.
can you please remove the extra new lines?
| { | ||
| recurse=false; | ||
| return; | ||
| } |
There was a problem hiding this comment.
If the current directory is recursive, I think we should just skip this one and carry on with other directories. Return back without processing the rest of directories doesn't look right.
|
A (@ealexjordan) could you please also add test along with the code change? |
|
Aditya Patwardhan (@adityapatwardhan), since you're more familiar with the symbolic link on windows, could you please also review in case I miss anything? |
|
A (@ealexjordan) Thanks for adding the corresponding test changes. We are currently still waiting on you for 2 things:
|
|
I think this is a good start, but I'm worried it sets us off on the wrong foot for eventually supporting extended symlink chains (the likes of which don't link to directories directly under our own prefix). |
|
Thanks Dustin L. Howett (@DHowett) for your feedback. Can you plase elaborate it a bit more? |
|
Dongbo Wang (@daxian-dbw) That's what I mean; I don't think the solution in this pull request is scalable for indirect recursive symbolic link chains. The solution to that one would also fix direct chains, however. |
|
|
||
| It "Should be able to detect a recursive symlink" { | ||
|
|
||
| New-Item -ItemType directory -Name $parentfolder -Path $tmpDirectory |
There was a problem hiding this comment.
this doesn't seem to be defined
| } | ||
| } | ||
|
|
||
| Describe "New-Item" -Tags "CI" { |
There was a problem hiding this comment.
"CI" [](start = 26, length = 4)
this also needs the tag "RequireAdminOnWindows" since you can't create symlinks on windows unless you're elevated
|
I'm closing this PR because the solution in this pull request fixes the direct symbolic chain, but not the indirect recursive symbolic link chains. |
Quick fix that detects when symlink recursively links back to parent directory and skips recursing into that symlink rather than recursing until the limit is hit.
Does not fix more complicated symlink loops, as in sys.