Open ronchalant opened 6 years ago
Hi @ronchalant
Do you close the open file at finished?
Regards
Ricardo
It's been a few years :)
The connection and diskshare objects should both be closing by being in the try ()
block.
However in my sample/workaround, the disk entry opening here:
DiskEntry e = share.open(path,
EnumSet.of(AccessMask.FILE_WRITE_ATTRIBUTES),
(Set) null,
SMB2ShareAccess.ALL,
SMB2CreateDisposition.FILE_OPEN,
(Set) null);
appears to not actually be closed. Looks like I messed up there; should be closed explicitly or in a try ()
block.
The above snippets were from a test case I put together to demonstrate the issue and a workaround, not from my prod code - I'll take a look to ensure that it's being closed.
Thanks @ronchalant
You're right the connection and diskshare should be closed in a try()
block.
whether you find anything else in your prod code, I will be grateful that you share it
My production code is pretty similar to the above functionally. I have not run into issues in production with this, though the DiskEntry
looks like it also should be closed.
My guess is that closing the accompanying share will flush and close any open DiskEntry
operations.
Thanks ! Regards
I added the code to remove read-only recently. By the way, this thread helped me. The main thing I see is (as you mentioned): it is required to open file before changing its attributes (openning with write-attribute rights is essential). It is not a workaround, it is the way it works.
I would also replace bitwise xor ^
by bitwise not
combined with and
to ensure setting the flag to false (proposed solution with ^
will set true
in case originally read-only flag was false
, i.e. it will change the value whatever it was):
// this will ensure the flag is set to 0
long newAttrs = current.getFileAttributes() & (~FileAttributes.FILE_ATTRIBUTE_READONLY.getValue());
Using this code to remove read-only flags, had to add the following special-case:
// from the FileAttributes javadoc: "a value of 0x00000000 in the FileAttributes
// field means that the file attributes for this file MUST NOT be changed"
if (newAttrs == 0l)
newAttrs = FileAttributes.FILE_ATTRIBUTE_NORMAL.getValue();
Note: This is with a PureStorage SMBv2 share.
When issuing a
DiskShare.setFileInformation
call to unset a read-only flag for a given file it indicating "access denied":com.hierynomus.mssmb2.SMBApiException: STATUS_ACCESS_DENIED(3221225506/3221225506): Create failed for {file}
This is a code snippet that replicates the issue:
This was the only way I could find to update file attributes, if there is a better approach I'm all ears - just started using SMBJ this week.
The work-around I found that works for this is to open the file with an AccessMask of FILE_WRITE_ATTRIBUTES only:
Without changing the existing
setFileInformation
in a way that might impact othersetFileInformation
use cases, the only way I can see to correct this would be to add asetFileAttributes(String path, long attrs)
method to theDiskShare
class, or overloading thesetFileInformation
to allow for overridingAccessMask
value(s).Alternatively resolving the AccessMask set required by the
FileSettableInformation
implementation is possible, but that may be heavy handed.