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

NullPointerException in DiskShare.openFile(...) and DiskShare.openDirectory #531

Closed
davisnw opened this issue Feb 12, 2020 · 4 comments
Closed

Comments

@davisnw
Copy link

davisnw commented Feb 12, 2020

Testing against adb73c8, every attempt to open a file or directory always ends up in a NullPointerException. Since no-one else has reported this, and it would seem to make the library completely unusable, perhaps there is something atypical about the fileshare I am accessing. But not being very well-versed in the SMB protocol, I am at a loss of where to look, and perhaps smbj has a bug or should give a more informative exception.

The NullPointerException always occurs in DiskShare.java on line 112, because response.getFileAttributes() is null:

    protected DiskEntry getDiskEntry(String path, SMB2CreateResponseContext responseContext) {
        SMB2CreateResponse response = responseContext.resp;
        if (response.getFileAttributes().contains(FILE_ATTRIBUTE_DIRECTORY)) {
            return new Directory(response.getFileId(), responseContext.share, responseContext.target.toUncPath());
        } else {
            return new File(response.getFileId(), responseContext.share, responseContext.target.toUncPath());
        }
    }

It is not a total inability to access the share however, because I am able to list folders at the root of the share.

So this works:

AuthenticationContext ac = new AuthenticationContext("USERNAME", "PASSWORD".toCharArray(), "DOMAIN");
		
try (
	Connection connection = client.connect("server");
	Session session = connection.authenticate(ac);
	DiskShare share = (DiskShare) session.connectShare("share");
) {
		
	for (FileIdBothDirectoryInformation f : share.list("")) {
		System.out.println("File : " + f.getFileName());
	}
}

But this dies with the NullPointerException:

AuthenticationContext ac = new AuthenticationContext("USERNAME", "PASSWORD".toCharArray(), "DOMAIN");

//path is: \\server\share\subfolder1\subfolder2\qwerty.txt
try (
	Connection connection = client.connect("server");
	Session session = connection.authenticate(ac);
	DiskShare share = (DiskShare) session.connectShare("share");
	//dies at share.openFile
	File smbFileRead = share.openFile("subfolder1\\subfolder2\\qwerty.txt", EnumSet.of(AccessMask.FILE_READ_DATA), null, SMB2ShareAccess.ALL, SMB2CreateDisposition.FILE_OPEN, null);
) {
  ...
}

And this dies with the NullPointerException:

AuthenticationContext ac = new AuthenticationContext("USERNAME", "PASSWORD".toCharArray(), "DOMAIN");

//path is: \\server\share\subfolder1
try (
	Connection connection = client.connect("server");
	Session session = connection.authenticate(ac);
	DiskShare share = (DiskShare) session.connectShare("share")
) {
	Set<AccessMask> accessMask = new HashSet<AccessMask>();
	accessMask.add(AccessMask.GENERIC_READ);
	accessMask.add(AccessMask.FILE_READ_ATTRIBUTES);
	//accessMask.add(AccessMask.FILE_TRAVERSE);
	accessMask.add(AccessMask.FILE_LIST_DIRECTORY);

	Set<FileAttributes> attributes = new HashSet<FileAttributes>();
	attributes.add(FileAttributes.FILE_ATTRIBUTE_DIRECTORY);
	attributes.add(FileAttributes.FILE_ATTRIBUTE_READONLY);
	
	Set<SMB2ShareAccess> shareAccesses = new HashSet<SMB2ShareAccess>();
	shareAccesses.add(SMB2ShareAccess.FILE_SHARE_READ);
	
	SMB2CreateDisposition createDisposition = SMB2CreateDisposition.FILE_OPEN;
	
	Set<SMB2CreateOptions> createOptions = new HashSet<SMB2CreateOptions>();
	createOptions.add(SMB2CreateOptions.FILE_DIRECTORY_FILE);
	
	//dies at share.openDirectory
	try(Directory dir = share.openDirectory("subfolder1", accessMask, attributes, shareAccesses, createDisposition, createOptions)) {
		for (FileIdBothDirectoryInformation f : dir.list()) {
			System.out.println("DirFile : " + f.getFileName());
		}
	}
}
@davisnw
Copy link
Author

davisnw commented Feb 13, 2020

Somehow missed this in my earlier search for issues - looks like it may be the same issue in an earlier version of smbj: #415

@davisnw
Copy link
Author

davisnw commented Feb 13, 2020

Also see that #342 (comment) mentions a possible issue when re-using SmbClient, Though the exception mentioned is different, the associated pull request #474 mentions that it "Probably fixes issue #415 as well."

@davisnw
Copy link
Author

davisnw commented Feb 13, 2020

I found that the NullPointerException was avoided by not reusing SmbClient, e.g.

		try (
			SMBClient client = new SMBClient(config);
			Connection connection = client.connect(serverName);
			Session session = connection.authenticate(ac);
			DiskShare share = (DiskShare) session.connectShare(shareName);
		) {
                      ...
                }

Closing this in favor of #415 which pre-existed this issue.

@ilia-dot-karelia
Copy link

Have the same issue. I don`t reuse the client. Anyone faced?

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

2 participants