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
Remove-Item -Force cannot delete hidden file on Samba #15022
Comments
It seems you are trying to remove OneDrive file which is a Windows reparse point - I guess Samba does not support this. |
No I'm not. It's just a coincidence that the Samba-hosted file I was trying to remove happened to be an old backup copy of some file within my OneDrive tree. Sorry for the confusion. And CMD was able to remove the file. |
@sba923 I'd prefer to have an clean repro steps. Can you please create simple test folder tree with PowerShell cmdlets? What results do you see? |
Will work on that. Stay tuned. |
Here's the test script:
And the outcome:
|
You should use |
That's exactly what I'm doing, and that triggers the "invalid handle" error. And that is the case even if the filename doesn't start with "." |
Thanks, I see. $a=[io.fileinfo]::new('\\pnjnas\Public\foo.txt')
$a.Delete() |
This works:
|
Thanks! Since .Net API works I believe it is PowerShell bug. |
Yes, I checked the code works well for Windows shares. So the label is only about Samba context. |
Fine. Don't hesitate to involve me in testing (candidate) fixes! |
The issue here is the $a=[io.fileinfo]::new($uncpath)
$a.Delete()
# Or just
[IO.File]::Delete($uncpath) It will open the file with the Delete access mask which should still be fine. When you do Because the samba user does not have write access it is failing to open the handle to that file and thus fails to delete the attributes. The fact that it's a compound request is probably the reason why the error back is invalid handle rather than access denied because the last request failed with that. For fixing this problem I see 2 ways this could be done
Personally I think the first option is the way to go, it doesn't add/remove any extra steps than what is currently done and if the user is truly unable to delete the file then the error back from the deletion request will still reflect that. The 2nd option might be quicker in normal cases but it could add an extra step if it does need to remove the attribute (and it's not needed). |
Could you please share a screenshot with how Windows maps this to Windows permissions? I'd want to see Security tab for the Samba file. This could helps to emulate the behavior on Windows shares/local disk and to prepare more unified fix.
I guess PowerShell has long way to the delete call - there is a globbing code, specific provider code - there may be file open operations due to the versatility of this code and it may not be easy to fix. |
So I have 2 files in a share
For For So this confirms that the Unix permissions affect the permissions that Samba uses over SMB. It makes sense as well because if the Samba user is unable to write to the file then it is unable to write data (like attributes) to it. You can even replicate this outside of PowerShell and get the same Invalid handle error message back by trying to change the attributes after removing write access on the remote side. If you are wanting to emulate the fix just make sure that the user does not have the
I don't think the proposed fix changes anything about that. All I'm proposing is to not make that 1 operation a failure but rather a at best operation. If it truly cannot delete the file because it does not have access (or it's in use) then the subsequent delete call will still fail and report that error back to the user.
In this case it does not, the problem lies with how the FileSystem provider tries to delete a file when PowerShell/src/System.Management.Automation/namespaces/FileSystemProvider.cs Lines 3272 to 3276 in 4c40ab1
We can see that if |
Can you confirm the files is hidden? Can you share info for non-hidden files? |
I can confirm, I set it myself. The fact it is hidden has no bearing over the permissions. It’s only to expose this bug that happens when using |
It is exclusive PowerShell smart feature :-) By design. We should keep this. So the proposed fix is to put the "if (force)" to "try-catch" - I am inclined to accept this. |
I confirm that if the file is not hidden,
|
We need to make sure that in no circumstances the operation could fail but have the side effect of modifying the file's attributes. |
Totally understand it’s a feature of PowerShell :) My main point is that the code is unilaterally removing all 3 attributes (ReadOnly, System, Hidden) in this case but really it’s only ReadOnly that needs to be removed. It could even be smarter and only remove the attribute if it’s not currently set to reduce the number of operations that need to occur. I still think a try/catch is also good for this situation but both options will fix this problem. |
That’s already handled in the current code. If the attributes were changed but the delete failed then the attributes are reset back. |
I see your point. Only concern is about how this attributes are mapped to Unix attributes by Samba (I'd want to get a confirmation that .Net API can remove files with the attributes). If there are no issues due to the mapping I think we can accept the fix with the two changes since the code has good test coverage. |
From my understanding of Samba it can store the attributes
In my case I'm testing against a Samba setup that has I can confirm that when using
There are no other mentions for the other attributes and considering SMB is a Microsoft protocol it would make sense that Samba replicates the same behaviour. I also tested |
@jborean93 Thanks for your research! I think we can offer the community fix. |
FWIW I'm running Samba version 4.11.6-Ubuntu and the config file contains nothing but a commented-out |
Note that I also get
What's next towards a solution to this issue? |
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you. |
2 similar comments
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you. |
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you. |
This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes. |
Steps to reproduce
Attempting to delete a hidden file that is stored on a Samba file share (Samba 4.11.6-Ubuntu on Ubuntu 20.04.2 LTS) fails, whereas CMD can delete the file
PowerShell:
CMD:
Expected behavior
Remove-Item -Force
should delete the fileActual behavior
Remove-Item -Force
fails with an "invalid handle" error.Environment data
The text was updated successfully, but these errors were encountered: