jcifs-ng's SmbFile#listFiles() file is different than it really is (the parent directory just above is lost from the full path)

Asked 1 years ago, Updated 1 years ago, 363 views

When I tried to use jcifs-ng to connect to the SMB server from an android terminal and use the result of the listFiles method in the SmbFile class, I got the error "SmbException: The system cannot find the file specified".
The following simplified code found that the first SmbFile instance was correct, but the full path of the child files obtained by the listFiles().path method was missing the parent directory (the filename of the first SmbFile instance) directly above each file.
The SmbFile-type instance obtained by listFiles() also appears to have the wrong path.
Don't you know how to get the correct result from listFiles()?

import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import jcifs.CIFSContext
import jcifs.config.PropertyConfiguration
import jcifs.context.BaseContext
import jcifs.smb.NtlmPasswordAuthentication
import jcifs.smb.SmbFile
import kotlinx.coroutines.*
import java.util.*
import kotlin.coroutines.CoroutineContext


classMainActivity:AppCompatActivity(), CoroutineScope{
    // authentication information
    //////////////////////////////////////////////
    // Please replace these values to your data//
    val user="USER"
    val password = "PASS"
    val domain = "192.168.1.1"
    val smbroot="smb://"+domain+"/smb/my/tmp"
    //////////////////////////////////////////////

    val TAG: String="MySMB"

    // coroutine preparation
    private val job = Job()
    override value coroutineContext
        get() = Dispatchers.Main+job

    // Coroutine Cancellation Settings at Exit
    override fun onDestroy() {
        job.cancel()
        super.onDestroy()
    }

    override fun onCreate (savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        launch {
            withContext (Dispatchers.IO) {
                val smb = connectSMB (user, password, domain, smbroot)
                Log.d(TAG, "Got SMB:" + smb.path)


                if(smb.isDirectory){
                    for(eachFile in smb.listFiles()) {
                        Log.d(TAG, "CASE1" + eachFile.path)
                    }
                } else{
                    Log.d (TAG, "CASE2" + smb.path)
                }
            }
        }
    }

    private suspend fun connectSMB(user:String, password:String, domain:String, smroot:String):SmbFile{
        lateinit varsmb —SmbFile
        US>coroutineScope{
            smb = async (Dispatchers.IO) {
                val prop=Properties()
                prop.setProperty("jcifs.smb.client.minVersion", "SMB202")
                prop.setProperty("jcifs.smb.client.maxVersion", "SMB300")
                valbc = BaseContext (PropertyConfiguration(prop))
                valcreds=NtlmPasswordAuthentication(bc,domain,user,password)
                val auth —CIFSContext=bc. with Credentials (creds)
                SmbFile (smbroot, auth)
            }.await()
        }
        return smb
    }
}

2021-08-13 18:33:56.249 18700-18700/PACKAGE.PROJECT.smbclient D/MySMB: Got SMB:smb://192.168.1.1/smb/my/tmp
2021-08-13 18:33:57.807 18700-18914/PACKAGE.PROJECT.smbclient D/MySMB: CASE1smb://192.168.1.1/smb/my/file2
2021-08-13 18:33:57.807 18700-18914/PACKAGE.PROJECT.smbclient D/MySMB: CASE1smb://192.168.1.1/smb/my/file1
2021-08-13 18:33:57.808 18700-18914/PACKAGE.PROJECT.smbclient D/MySMB: CASE1smb://192.168.1.1/smb/my/tmp1/
2021-08-13 18:33:57.808 18700-18914/PACKAGE.PROJECT.smbclient D/MySMB: CASE1smb://192.168.1.1/smb/my/tmp2/

The output is incorrect, where smb/my/tmp/file2 is smb/my/file2, smb/my/tmp/file1 is smb/my/file1, smb/my/tmp1/ is smb/my/tmp1/, and smb/my/tmp2/ is smb/my/tmp2/.

smb/my/
 --tmp
   <
   ├-- file1
   ├-- file2
   ├-- tmp1/
   └-- tmp2/

android samba smb

2022-09-30 21:59

1 Answers

We modified it by referring to the description on the page below and got the correct output.

https://javadoc.io/static/eu.agno3.jcifs/jcifs-ng/2.0.3/jcifs/smb/SmbFile.html

all SMB URLs that represent workgroups, servers, shares, or directors require a tracking flash'/'.

before change:

val smbroot="smb://"+domain+"/smb/my/tmp"

after modification:

val smbroot="smb://"+domain+"/smb/my/tmp/"


2022-09-30 21:59

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.