'Problems with SmbFile.createNewFile() behaviour

I wonder what the catch is with createNewFile() in recent versions of jCIFS and ng (SMB2 specifically). The documentation says it should fail on execution if a file already exists, but that is not the case in my code, the file is always overwritten instead of throwing a SmbException. I need to be able to rename the file so it is not overwritten, but due to server load and quantity I cannot use exists(). This snippet worked perfectly on SMB1:

for (int i = 0; !fileCreated && (i < maxCopies); i++) {
    try {
        smbFile.createNewFile();
        fileCreated = true;
    } catch (SmbException e) {
        smbFile = new SmbFile(smbFilePath.concat(String.valueOf(i)), auth);
    }
}

I've been searching high and low and trying out different versions of SMB2, but createNewFile() always does the opposite of what the javadoc says it should. Is there a simple way (using attributes for example) to make it work just as in SMB1?

EDIT: Solution by jcifs-ng developer on GitHub

Apparently I got the behavior wrong when implementing the SMB2 version. Create disposition should be FILE_CREATE not FILE_OPEN_IF, then ( O_EXCL in SMB1). Let me look into making that change, and how much havoc this will cause. You could potentially work around this by explicitly calling openOutputStream and passing O_EXCL as open flag.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source