Why does file deletion not work on cifs (samba) mount when file is still open for reading?

225 Views Asked by At

Look at the following simple example:

  auto fname = "/path/to/__tmp__.txt";

  auto o = std::ofstream(fname); // create file
  // o << "abc"; // write some data, but this is not required to provoke the problem 
  o.close(); // close file

  auto in = std::ifstream(fname); // open file for reading

  std::filesystem::remove(fname); // remove file system entry  

  // this assertion fails if file is located on cifs/smb share, 
  // but works in all other cases I tested
  assert(!std::filesystem::exists(fname)); 

If the file is located on a cifs/samba mount, the behavior is different compared to a file located on an ext4 file system, for example.

As far as I understand std::filesystem::remove (see here in the notes section), the behavior violates the unlink specification:

[...] If one or more processes have the file open when the last link is removed, the link shall be removed before unlink() returns, but the removal of the file contents shall be postponed until all references to the file are closed. [...]

On btrfs or ext4 partitions as well as on nfs4 mounts this works as expected.

My questions:

  • Am I right that cifs/smb is not behaving correctly here?
  • Can I change some mount settings (or server settings) to make this work correctly?
  • Can I change my code to make this work on smb shares?

These are the cifs mount options: rw,relatime,vers=3.0,cache=strict,username=user,domain=mydomain,uid=24757,noforceuid,gid=0,noforcegid,addr=192.168.x.x,file_mode=0755,dir_mode=0755,soft,nounix,serverino,mapposix,rsize=8388608,wsize=4194304,bsize=1048576,echo_interval=60,actimeo=1,closetimeo=5,user=user

0

There are 0 best solutions below