Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

withBinaryFileDurableAtomic and similar override permissions set by user with fchmod() #63

Open
nh2 opened this issue Jul 24, 2020 · 1 comment

Comments

@nh2
Copy link
Member

nh2 commented Jul 24, 2020

When I do

withBinaryFileDurableAtomic "myfile" WriteMode $ \handle -> do
  
  fd <- handleToFd handle
  fm <- Posix.fileMode <$> Posix.getFdStatus fd
  Posix.setFdMode fd myPermissionsHere

  return x

then myPermissionsHere results in an fchmod(), but those permissions are later overridden by an fchmod() that unliftio does just before the file is closed.

Reason

withBinaryFileDurableAtomic does

      mFileMode <- copyFileHandle iomode filePath tmpFileHandle
      res <- action tmpFileHandle
      atomicDurableTempFileRename dirFd mFileMode tmpFileHandle mTmpFilePath filePath

where mFileMode = Nothing when copyFileHandle says that the file doesn't already exist (the normal case), and atomicDurableTempFileRename runs this:

        let fileMode = fromMaybe Posix.stdFileMode mFileMode
        -- work around for the glibc bug: https://sourceware.org/bugzilla/show_bug.cgi?id=17523
        Posix.setFdMode fd fileMode

This means that atomicDurableTempFileRename will always call setFdMode with stdFileMode, thus erasing any permissions the user may have set in action.

Proposed solution

We should call our setFdMode before res <- action tmpFileHandle is run.


CC @lehins

@lehins
Copy link
Contributor

lehins commented Jul 24, 2020

The only way the user can set permission in the action is by getting access to the Fd, like you did, which is not something that is supported by the API because it is not OS agnostic.

I do however realize why the functionality of setting file permissions atomically at the same time as file modification happens would be desired, especially since we already have the Fd opened.

We should call our setFdMode before res <- action tmpFileHandle is run.

The proposed solution does seem logical, but I am afraid it will require some restructure of current code, since access to Fd right from the start was not designed to be provided to the user.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants