URL: https://linuxfr.org/forums/general-general/posts/manipulation-des-permissions-unix Title: Manipulation des permissions Unix Authors: Marotte ⛧ Date: 2016年10月30日T19:13:43+01:00 License: CC By-SA Tags: shell, unix et git Score: 2 Ce post fait suite à [ma question sur Git](https://linuxfr.org/forums/general-general/posts/git). En effet, Git n’enregistre pas les permissions Unix des fichiers qu’il suit, excepté le droit d’exécution. Je sais qu’il existe des sur-couches à Git pour répondre à ce genre de problématique (Git possède un mécanisme de _hooks_) mais je suis arrivé à la conclusion que la sauvegarde des droits Unix était un problème plus général qui ne concerne pas seulement Git. Il y a plusieurs autres cas de figure qui peuvent faire que l’on se retrouve avec une copie des fichiers, en ayant perdu l’information sur les droits : copie sur un système de fichier qui ne les gère pas toujours (NTFS, CIFS), mauvaise manipulation ou bug dans un script de maintenance. Par ailleurs, même si on est pas censé avoir perdu cette information il peut être judicieux de vérifier que les permissions effectives soient bien conformes à une référence extérieure, voir forcer cette conformité. J’ai donc écrit un petit outil que je vous présente ici dans le but d’avoir vos retours. Peut-être que je réinvente la roue... je n’ai pas cherché si un tel outil existait déjà. Voici le script : ```bash #!/usr/bin/env bash # Store and deploy Unix file permissions. # Write on stdout and read on stdin. # # File format : # user0円group0円PERM0円FILENAME # user0円group0円PERM0円FILENAME # ... # ... # Help message help() { echo " Usage: 0ドル [DIR] " } # Show help and exit if no action specified. test -z 1ドル && help && exit 1 # Set directory from second argument. Current directory if not specified. DIR=2ドル; test -z "$DIR" && DIR='.' # store : write file permisions on stdout using the null character as field separator function store() { find "1ドル" -printf "%u0円%g0円%m0円%h/%f\n" } # deploy : read file permissions from stdin and apply them function deploy() { cd "1ドル" awk 'BEGIN {FS="0円";} {print "chmod -v " 3ドル " \"" 4ドル "\"" | "sh"; print "chown -v " 1ドル ":" 2ドル " \"" 4ドル "\"" | "sh";}' - cd - 1>/dev/null } # Switch according to chosen action case 1ドル in # store : write file permisions on stdout using the null character as field separator store|s) store "$DIR" ;; # deploy : read file permissions from stdin and apply them deploy|d) deploy "$DIR" ;; # cmd : read file permissions from stdin (separator = ^@) and write shell commands on stdout cmd|c) cd "$DIR" awk 'BEGIN {FS="0円"} {print "chmod -v " 3ドル " \"" 4ドル "\""; print "chown -v " 1ドル ":" 2ドル " \"" 4ドル "\"";}' - cd - 1>/dev/null ;; # list : write file permissions on stdout using a tabulation as field separator list|l) find "$DIR" -printf "%u\t%g\t%m\t%h/%f\n" ;; # keep : add a '.pkeep' file to the directory itself so it can be deployed with action 'recover' keep|k) store "${DIR}"> "${DIR}/.pkeep" ;; # recover : deploy permission from the '.pkeep' file recover|r) deploy "${DIR}" < "${DIR}/.pkeep" ;; # Show help and exit if unrecognized action *) help && exit 1 esac exit 0 ``` Il est hébergé [ici](https://gitlab.com/Marotte/PermKeeper). Voici la documentation : # PermKeeper This script can be used to store owner, group and permissions of all files in a given directory to restore them later. $ ./pkeep store /var/www/html> files ... install things from a developper archive ... then: $ ./pkeep deploy < files Of course, it wont set permissions on newly created files. In addition of working with standard input and output, the 'keep' and 'recover' actions can be used to store/recover the permissions in/from a '.pkeep' file inside the directory itself. $ ./pkeep keep /tmp will create a /tmp/.pkeep file storing the permissions of /tmp $ ./pkeep recover /tmp will then re-apply them. The 'list' action print out the permissions using tabulations for a human readable output (it still reads its input using the null character as separator). $ .pkeep list < /tmp/.pkeep

AltStyle によって変換されたページ (->オリジナル) /