From 6c6708be00b3cb0dbb2b83c06e457c8d1ec61e66 Mon Sep 17 00:00:00 2001 From: Jens Petersen Date: Tue, 19 May 2020 11:22:37 +0800 Subject: [PATCH] remove-old-stack-work-libs.hs: check more carefully for libdir hash to avoid accidents, eg if run in wrong directory --- etc/diskspace/remove-old-stack-work-libs.hs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/etc/diskspace/remove-old-stack-work-libs.hs b/etc/diskspace/remove-old-stack-work-libs.hs index 210e5a1a..f99d0ebd 100755 --- a/etc/diskspace/remove-old-stack-work-libs.hs +++ b/etc/diskspace/remove-old-stack-work-libs.hs @@ -23,7 +23,9 @@ main = do pkgDirName p = if countDashes p < 2 then error $ p ++ " not in name-version-hash format" - else (removeDashSegment . removeDashSegment) p + else let nv_ = dropEnd 22 p in + if last nv_ == '-' then removeDashSegment $ init nv_ + else error $ p ++ " not in name-version-hash format" countDashes = length . filter (== '-') @@ -47,3 +49,9 @@ main = do return $ map fst $ sortBy compareSnd fileTimes compareSnd (_,t1) (_,t2) = compare t1 t2 + +-- from Data.List.Extra +dropEnd :: Int -> [a] -> [a] +dropEnd i xs = f xs (drop i xs) + where f (x:xs) (y:ys) = x : f xs ys + f _ _ = []