Thursday, February 17, 2011

Using Samba for random access without mounting the file system?

I am using a machine on which I do not have root access and would like to access files on a Samba server in random access mode. I know I can transfer the files in their entirety using smbclient but the files are very large (>1GB) and I would rather just treat them as remote files on which I can do random access.

The problem as noted is that I don't have root access to this machine (a Linux box) so I can't mount the remote Samba file system.

Is there a user-level solution that will let me randomly access the contents of a file on a Samba server? Seems it should be possible to do everything that the kernel file system client is doing but from a user-level application.

I only need read-only access btw and the remote file is guaranteed not to change.

From stackoverflow
  • Try to use smbmount to mount the filesystem without root permissions:

    mkdir ~/temp
    smbmount //{server}/{share} ~/temp -o username=username={username},password={password}
    

    Edit: Updated to use smbmount instead of mount.

    Dave Griffiths : Tried that. It says: mount: only root can do that
    xsl : I updated the post to use smbmount instead of mount.
    Dave Griffiths : Hmmm, well I downloaded and installed samba and tried mount.cifs which appears to be the replacement for smbmount. That also fails and the reason appears to be that the command needs the suid root bit set. Catch 22!
  • To answer my own question after digging around in the Samba source: there is a client library libsmbclient which includes all the usual file handling stuff: smbc_open, smbc_fstat, smbc_lseek, smbc_read etc. For instance, here is a snippet I just wrote which reads a file backwards (just to check it was doing a true seek):

    fd = smbc_open(path, O_RDONLY, 0);
    smbc_fstat(fd, &st);
    
    for (offset = st.st_size - BUFLEN; offset > 0; offset -= BUFLEN) {
        smbc_lseek(fd, offset, SEEK_SET);
        smbc_read(fd, buffer, BUFLEN);
    }
    

    (error checking removed for clarity)

    Dave Griffiths : Since writing the above I found a much better solution (for me at least because it's in Java) here: http://jcifs.samba.org/

0 comments:

Post a Comment