How can I mount davfs from within application?

827 views Asked by At

I have a remote filesystem I can mount through webdav via

sudo mount -t davfs https://myserver.com /tmp/dav

It prompts me for a username and password at this point.

I then added an entry to fstab and created a ~/.davfs2/secrets file. If I now run

mount /tmp/dav

It works.

Now I need this to be mounted from within my app automatically from C code.

If I call:

int result = mount("https://myserver.com", "/tmp/dav", "davfs", 0, "user,noauto,file_mode=600,dir_mode=700");

Mount returns result -1 and and errno is set to 19: strerror prints "no such device".

The mount man page shows:

ENODEV filesystemtype not configured in the kernel.

How can I get this to work? Is it because davfs is fuse based?

1

There are 1 answers

0
hookenz On BEST ANSWER

The answer turned out to be simpler than I thought. I should point out I wanted to also remove the prompt for password. This is how I solved it.

It turned out mount.davfs is a server that gets executed by the mount command. But it's not a registered filesystem so you can't use the system mount(2) call. Instead we must exec the mount command directly.

To provide my own secrets file I did this.

  1. Mount tmpfs at /etc/davfs2 (hides real davfs2.conf etc)

    mount("none", "/etc/davfs2/secrets", "tmpfs", 0, NULL);

  2. Create /etc/davfs2/secrets mode 0600 containing our secret

  3. Run mount -t davfs url /yourpath (i.e. fork+exec the mount command)
  4. Use webdav by reading and writing files in /yourpath
  5. unmount /yourpath
  6. unmount /etc/davfs2
  7. exit program when done.

In addition I used unshare to hide the mount from outside the process.