canonical / pebble

Pebble is a lightweight Linux service manager with layered configuration and an HTTP API.
https://canonical-pebble.readthedocs-hosted.com/
GNU General Public License v3.0
145 stars 54 forks source link

feat(osutil.Mkdir): add the chmod flag #423

Closed IronCore864 closed 3 months ago

IronCore864 commented 4 months ago

Add a new Chmod flag to MkdirOptions so that an explicit chmod command is executed on all directories created by osutil.Mkdir.

Closes https://github.com/canonical/pebble/issues/372.

Manual Tests

Comparison

Tests Before After
pebble mkdir ~/normal -m 777 umask set to 0022, result is 0755, not the same as what the user specifies because of umask umask set to 0022, result is 0777, not affected by umask
pebble mkdir -p ~/nested/folder -m 777 umask set to 0022, result is 0755 for both parents and children, not the same as what the user specifies because of umask umask set to 0022, result is 0777 for both parents and children, not affected by umask

Before

Permission is affected by umask settings:

# umask 0022
$ pebble mkdir ~/normal -m 777
$ ll normal/
total 8
drwxr-xr-x  2 ubuntu ubuntu 4096 Jun  6 02:30 ./
$ pebble mkdir -p ~/nested/folder -m 777
$ ll nested/
total 12
drwxr-xr-x  3 ubuntu ubuntu 4096 Jun  6 02:34 ./
drwxr-x--- 19 ubuntu ubuntu 4096 Jun  6 02:34 ../
drwxr-xr-x  2 ubuntu ubuntu 4096 Jun  6 02:34 folder/

After

Permission isn't affected by umask anymore:

# umask 0022
$ pebble mkdir ~/normal -m 777
$ ll normal/
total 8
drwxrwxrwx  2 ubuntu ubuntu 4096 Jun  6 02:31 ./
$ pebble mkdir -p ~/nested/folder -m 777
$ ll nested/
total 12
drwxrwxrwx  3 ubuntu ubuntu 4096 Jun  6 02:32 ./
drwxr-x--- 19 ubuntu ubuntu 4096 Jun  6 02:32 ../
drwxrwxrwx  2 ubuntu ubuntu 4096 Jun  6 02:32 folder/
benhoyt commented 4 months ago

I've tested this manually locally too, and it seems to work nicely.