lib.filesystem.pathType: Fix for filesystem root argument

Previously this function couldn't handle / being passed, it would throw
an error:

error: attribute '' missing

       at nixpkgs/lib/filesystem.nix:24:20:

           23|   */
           24|   pathType = path: (readDir (dirOf path)).${baseNameOf path};
             |                    ^
           25|

Consequently this also fixes the
lib.filesystem.{pathIsDirectory,pathIsRegularFile} functions.
This commit is contained in:
Silvan Mosberger 2023-04-05 16:47:30 +02:00
parent 5346636c20
commit bb6eab0bdb
2 changed files with 9 additions and 1 deletions

View File

@ -22,7 +22,12 @@ in
Returns the type of a path: regular (for file), symlink, or directory. Returns the type of a path: regular (for file), symlink, or directory.
*/ */
pathType = path: pathType = path:
(readDir (dirOf path)).${baseNameOf path}; # The filesystem root is the only path where `dirOf / == /` and
# `baseNameOf /` is not valid. We can detect this and directly return
# "directory", since we know the filesystem root can't be anything else.
if dirOf path == path
then "directory"
else (readDir (dirOf path)).${baseNameOf path};
/* /*
Returns true if the path exists and is a directory, false otherwise. Returns true if the path exists and is a directory, false otherwise.

View File

@ -46,6 +46,7 @@ checkPathType() {
fi fi
} }
checkPathType "/" '"directory"'
checkPathType "$PWD/directory" '"directory"' checkPathType "$PWD/directory" '"directory"'
checkPathType "$PWD/regular" '"regular"' checkPathType "$PWD/regular" '"regular"'
checkPathType "$PWD/symlink" '"symlink"' checkPathType "$PWD/symlink" '"symlink"'
@ -62,6 +63,7 @@ checkPathIsDirectory() {
fi fi
} }
checkPathIsDirectory "/" "true"
checkPathIsDirectory "$PWD/directory" "true" checkPathIsDirectory "$PWD/directory" "true"
checkPathIsDirectory "$PWD/regular" "false" checkPathIsDirectory "$PWD/regular" "false"
checkPathIsDirectory "$PWD/symlink" "false" checkPathIsDirectory "$PWD/symlink" "false"
@ -79,6 +81,7 @@ checkPathIsRegularFile() {
fi fi
} }
checkPathIsRegularFile "/" "false"
checkPathIsRegularFile "$PWD/directory" "false" checkPathIsRegularFile "$PWD/directory" "false"
checkPathIsRegularFile "$PWD/regular" "true" checkPathIsRegularFile "$PWD/regular" "true"
checkPathIsRegularFile "$PWD/symlink" "false" checkPathIsRegularFile "$PWD/symlink" "false"