branch: externals/compat commit a92aec72518100df0d8f453c15d42d00ecdd28ed Author: Philip Kaludercic <phil...@posteo.net> Commit: Philip Kaludercic <phil...@posteo.net>
Add file-modes-number-to-symbolic --- MANUAL | 2 ++ compat-28.el | 38 ++++++++++++++++++++++++++++++++++++++ compat-tests.el | 14 ++++++++++++++ 3 files changed, 54 insertions(+) diff --git a/MANUAL b/MANUAL index 56d6967cdd..37978a46d6 100644 --- a/MANUAL +++ b/MANUAL @@ -353,6 +353,8 @@ provided by compat by default: - Function ~button-buttonize~ :: Defined in ~button.el~. - Function ~make-directory-autoloads~ :: See [[info:elisp#Autoload][(elisp) Autoload]]. - Function ~color-values-from-color-spec~ :: Defined in ~xfaces.c~. +- Function ~file-modes-number-to-symbolic~ :: See [[info:elisp#Changing Files][(elisp) Changing + Files]]. These functions are prefixed with ~compat~ prefix, and are only loaded when ~compat-28~ is required: diff --git a/compat-28.el b/compat-28.el index 37d12f9e1c..6324126e8c 100644 --- a/compat-28.el +++ b/compat-28.el @@ -625,6 +625,44 @@ See `file-symlink-p' to distinguish symlinks." (and (file-directory-p dir) (null (directory-files dir nil directory-files-no-dot-files-regexp t)))) +(compat-defun file-modes-number-to-symbolic (mode &optional filetype) + "Return a string describing a file's MODE. +For instance, if MODE is #o700, then it produces `-rwx------'. +FILETYPE if provided should be a character denoting the type of file, +such as `?d' for a directory, or `?l' for a symbolic link and will override +the leading `-' char." + (string + (or filetype + (pcase (lsh mode -12) + ;; POSIX specifies that the file type is included in st_mode + ;; and provides names for the file types but values only for + ;; the permissions (e.g., S_IWOTH=2). + + ;; (#o017 ??) ;; #define S_IFMT 00170000 + (#o014 ?s) ;; #define S_IFSOCK 0140000 + (#o012 ?l) ;; #define S_IFLNK 0120000 + ;; (8 ??) ;; #define S_IFREG 0100000 + (#o006 ?b) ;; #define S_IFBLK 0060000 + (#o004 ?d) ;; #define S_IFDIR 0040000 + (#o002 ?c) ;; #define S_IFCHR 0020000 + (#o001 ?p) ;; #define S_IFIFO 0010000 + (_ ?-))) + (if (zerop (logand 256 mode)) ?- ?r) + (if (zerop (logand 128 mode)) ?- ?w) + (if (zerop (logand 64 mode)) + (if (zerop (logand 2048 mode)) ?- ?S) + (if (zerop (logand 2048 mode)) ?x ?s)) + (if (zerop (logand 32 mode)) ?- ?r) + (if (zerop (logand 16 mode)) ?- ?w) + (if (zerop (logand 8 mode)) + (if (zerop (logand 1024 mode)) ?- ?S) + (if (zerop (logand 1024 mode)) ?x ?s)) + (if (zerop (logand 4 mode)) ?- ?r) + (if (zerop (logand 2 mode)) ?- ?w) + (if (zerop (logand 512 mode)) + (if (zerop (logand 1 mode)) ?- ?x) + (if (zerop (logand 1 mode)) ?T ?t)))) + ;;;; Defined in minibuffer.el (compat-defun format-prompt (prompt default &rest format-args) diff --git a/compat-tests.el b/compat-tests.el index 2ad7951ed6..6d781d25ca 100644 --- a/compat-tests.el +++ b/compat-tests.el @@ -1685,5 +1685,19 @@ the compatibility function." ;; (compat--should nil "rgbi: 0/ 0/ 0") (compat--should nil "rgbi : 0/0/0"))) +(ert-deftest compat-file-modes-number-to-symbolic () + "Check if `compat--file-modes-number-to-symbolic' was implemented properly." + (compat-test file-modes-number-to-symbolic + (compat--should "-rwx------" #o700) + (compat--should "-rwxrwx---" #o770) + (compat--should "-rwx---rwx" #o707) + (compat--should "-rw-r-xr--" #o654) + (compat--should "--wx-w---x" #o321) + (compat--should "drwx------" #o700 ?d) + (compat--should "?rwx------" #o700 ??) + (compat--should "lrwx------" #o120700) + (compat--should "prwx------" #o10700) + (compat--should "-rwx------" #o30700))) + (provide 'compat-tests) ;;; compat-tests.el ends here