cygwinmsysfile-attributes

How to set a file to write-only or read-only in Msys or Cygwin?


The function chmod is implemented in Cygwin but does not fully match access rights in Windows. This appears in the following change log: https://cygwin.com/cygwin-ug-net/ov-new1.7.html

Since 1.7.34, chmod does not always affect the POSIX permission mask as returned by stat(2) or printed by ls(1), due to the improved POSIX ACL handling. However, that's still far from perfect, so, as a temporary workaround, [...]

The implementation of chmod in Msys is not working, as mentioned in the following bug report: https://sourceforge.net/p/mingw/bugs/1475/

This question is asking how to provide full access rights to a file in Msys.

How can I change the rights of a file to respectively "read-only" and "write-only" in Msys/Cygwin?

Thanks


Solution

  • You can use the commands provided by windows: cacls and icacls

    More information on these commands:

    It is now recommended to use icacls to change permissions easily:

    Note: the option :r make the new Permissions to replace previously granted explicit permissions

    If you want to hide the output of the command (details about "processed file xxx" and "Successfully processed ... Failed processing ..."), then you can redirect the standard output to "null" as such:


    If you want to keep a compatibility with Windows XP, you will have to use the older calcs instead by typing 2 lines:

    Read-Only with cacls

    $ cacls "<file_path>" //E //P Everyone:N 1>/dev/null
    $ cacls "<file_path>" //E //G Everyone:R 1>/dev/null
    

    Write-Only with cacls

    $ cacls "$file_path" //E //P Everyone:N 1>/dev/null
    $ cacls "$file_path" //E //G Everyone:W 1>/dev/null
    

    Notes: