haskell / directory

Platform-independent library for basic file system operations
https://hackage.haskell.org/package/directory
Other
58 stars 47 forks source link

removePathForcibly can affect the permissions of files outside the path being deleted #135

Closed joeyh closed 2 years ago

joeyh commented 2 years ago

When a file is a hard link, removePathForcibly will modify the permissions of files outside the directory being deleted:

joey@darkstar:~/tmp>mkdir test
joey@darkstar:~/tmp>cd test
joey@darkstar:~/tmp/test>l
joey@darkstar:~/tmp/test>mkdir foo
joey@darkstar:~/tmp/test>mkdir bar
joey@darkstar:~/tmp/test>echo hi > bar/f
joey@darkstar:~/tmp/test>chmod 400 bar/f
joey@darkstar:~/tmp/test>ln bar/f foo/f
joey@darkstar:~/tmp/test>ghci
GHCi, version 8.8.4: https://www.haskell.org/ghc/  :? for help
Loaded GHCi configuration from /home/joey/.etc/.ghci
ghci> import System.Directory
ghci> removePathForcibly "foo"
ghci> 
Leaving GHCi.
joey@darkstar:~/tmp/test>ls -l bar
total 4
-rwx------ 1 joey joey 3 May  2 13:34 f*

I doubt that changing the permissions of a file is ever necessary to let it be deleted.

If it only changed the permissions of directories, which can be necessary, it would avoid this problem.

joeyh commented 2 years ago

The haddock does actually warn about this, but in my experience this is easy to miss.

-- Unlike other removal functions, this function will also attempt to delete -- files marked as read-only or otherwise made unremovable due to permissions. -- As a result, if the removal is incomplete, the permissions or attributes on -- the remaining files may be altered. If there are hard links in the -- directory, then permissions on all related hard links may be altered.

Rufflewind commented 2 years ago

I doubt that changing the permissions of a file is ever necessary to let it be deleted.

On Windows, read-only files cannot be removed unless the read-only attribute is removed.

One possible workaround might be to only modify permissions of files on Windows platforms, at the cost of additional complexity.