lib: only readDir if path exists with safeReadDir

This commit is contained in:
Pacman99 2021-03-30 12:47:54 -07:00
parent f14dcdaf78
commit 3d1501c384
6 changed files with 16 additions and 7 deletions

View file

@ -40,4 +40,6 @@ rec {
lib.isDerivation v && !meta.broken && builtins.elem system platforms); lib.isDerivation v && !meta.broken && builtins.elem system platforms);
in in
lib.filterAttrs filter packages; lib.filterAttrs filter packages;
safeReadDir = path: lib.optionalAttrs (builtins.pathExists path) (builtins.readDir path);
} }

View file

@ -17,7 +17,7 @@ lib.makeExtensible (final:
lists = callLibs ./lists.nix; lists = callLibs ./lists.nix;
strings = callLibs ./strings.nix; strings = callLibs ./strings.nix;
inherit (attrs) mapFilterAttrs genAttrs' inherit (attrs) mapFilterAttrs genAttrs' safeReadDir
pathsToImportedAttrs concatAttrs filterPackages; pathsToImportedAttrs concatAttrs filterPackages;
inherit (lists) pathsIn; inherit (lists) pathsIn;
inherit (strings) rgxToString; inherit (strings) rgxToString;

View file

@ -1,4 +1,4 @@
{ lib, ... }: { lib, dev, ... }:
let mkProfileAttrs = let mkProfileAttrs =
/** /**
@ -16,7 +16,7 @@ let mkProfileAttrs =
let let
imports = imports =
let let
files = builtins.readDir dir; files = dev.safeReadDir dir;
p = n: v: p = n: v:
v == "directory" v == "directory"

View file

@ -9,4 +9,4 @@ dev.mapFilterAttrs
let name = lib.removeSuffix ".nix" n; in lib.nameValuePair (name) (_import name) let name = lib.removeSuffix ".nix" n; in lib.nameValuePair (name) (_import name)
else else
lib.nameValuePair ("") (null)) lib.nameValuePair ("") (null))
(builtins.readDir dir) (dev.safeReadDir dir)

View file

@ -1,9 +1,8 @@
{ lib, ... }: { lib, dev, ... }:
{ {
pathsIn = dir: pathsIn = dir:
let let
fullPath = name: "${toString dir}/${name}"; fullPath = name: "${toString dir}/${name}";
in in
map fullPath (lib.attrNames (lib.optionalAttrs map fullPath (lib.attrNames (dev.safeReadDir dir));
(builtins.pathExists dir) (builtins.readDir dir)));
} }

View file

@ -69,6 +69,14 @@ lib.runTests {
(rgxToString "hat" "foohatbar" == "hat") (rgxToString "hat" "foohatbar" == "hat")
]; ];
testSafeReadDir = {
expr = safeReadDir "${self}/tests/profiles" // safeReadDir "${self}/nonexistentdir";
expected = {
foo = "directory";
t = "directory";
};
};
testSuites = testSuites =
let let
profiles = os.mkProfileAttrs (toString ./profiles); profiles = os.mkProfileAttrs (toString ./profiles);