Skip to content
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

DiskShare.openFile(...) seems to delete the files contents by default?! #786

Closed
DanskerDave opened this issue Aug 19, 2023 · 3 comments
Closed

Comments

@DanskerDave
Copy link

When opening a file, the default SMB2CreateDisposition seems to be to delete its contents.
Thats scary!
As far as I can see, the default is SMB2CreateDisposition.FILE_SUPERSEDE.

A couple of Issues.:

  • the meaning of the term "supersede" is not defined in the SMB2CreateDisposition Javadoc.
  • I would expect the default to be, say, SMB2CreateDisposition.FILE_OPEN.
     Then a file would only be created or overwritten if explicitly requested.
     That would seem to be the safest.

My code follows below...

All the best,
Dave

package smbj.read;

import java.io.IOException;
import java.util.EnumSet;

import com.hierynomus.msdtyp.AccessMask;
import com.hierynomus.msfscc.FileAttributes;
import com.hierynomus.mssmb2.SMB2CreateDisposition;
import com.hierynomus.mssmb2.SMB2CreateOptions;
import com.hierynomus.mssmb2.SMB2ShareAccess;
import com.hierynomus.smbj.SMBClient;
import com.hierynomus.smbj.auth.AuthenticationContext;
import com.hierynomus.smbj.share.DiskShare;

public class SmbReadFileAnon {

	public static void main(final String[] args) throws IOException {

		try(final     var client  = new SMBClient();
			final     var conn    = client.connect("hostname"))
		{
			final     var context = new AuthenticationContext("userid", "password".toCharArray(), null);

			try(final var session = conn.authenticate(context);
				final var share   = (DiskShare) session.connectShare("shareName"))
			{
				final var accessMaskRead       =                      EnumSet.of(AccessMask           .GENERIC_READ);
				final var fileAttrNotDirectory = EnumSet.complementOf(EnumSet.of(FileAttributes       .FILE_ATTRIBUTE_DIRECTORY));
				final var shareAccessRead      =                      EnumSet.of(SMB2ShareAccess      .FILE_SHARE_READ);
				final var optionSequential     =                      EnumSet.of(SMB2CreateOptions    .FILE_SEQUENTIAL_ONLY);
				final var dispositionOpen      =                                 SMB2CreateDisposition.FILE_OPEN;

				final var smbjFile = share.openFile("/path/readMe.txt", accessMaskRead, fileAttrNotDirectory, shareAccessRead, null, optionSequential);
				final var bytes    = smbjFile.getInputStream().readAllBytes();

				System.out.println("Contents.: " + new String(bytes));
			}
		}
	}
}

@LeafyLappa
Copy link

Thumbs up as this made me spend a lot of time until I came across this issue: #166

@DanskerDave
Copy link
Author

Thumbs up as this made me spend a lot of time until I came across this issue: #166

Its certainly quite unique in my 45 years or so in this business:
first time I've ever experienced a file opened with read intent being overwritten!

hierynomus added a commit that referenced this issue Sep 25, 2023
@hierynomus
Copy link
Owner

I've merged a fix to the master branch.

XayahSuSuSu added a commit to XayahSuSuSu/Android-DataBackup that referenced this issue Jun 29, 2024
* Ref: hierynomus/smbj#786

Change-Id: Id7298e852abc81dd2836b042638bd5fd8576e966
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants