R – 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.

Best Solution

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)

Related Question