]> gitweb.factorcode.org Git - factor.git/commitdiff
mason.disk: Better handling of free disk space.
authorDoug Coleman <doug.coleman@gmail.com>
Fri, 3 Jan 2020 16:48:24 +0000 (10:48 -0600)
committerDoug Coleman <doug.coleman@gmail.com>
Fri, 3 Jan 2020 17:10:53 +0000 (11:10 -0600)
- Works if the build directory does not exist, assuming it would be created on the root disk containing the path of the first existing parent directory
- Space calculation is wrong, we should be using the available space
- MB is GB, comment was wrong
- Call find-mount-point-info to get the real disk mount point even if the dir does not exist

basis/io/files/info/info.factor
basis/io/files/info/unix/linux/linux.factor
extra/mason/disk/disk.factor

index f5c45881b6cb3b12bf83cafe573167c6e2369c4e..57f7e730d29340cae150c72c314608b55773d183 100644 (file)
@@ -34,6 +34,20 @@ HOOK: file-readable? os ( path -- ? )
 HOOK: file-writable? os ( path -- ? )
 HOOK: file-executable? os ( path -- ? )
 
+: mount-points ( -- assoc )
+    file-systems [ [ mount-point>> ] keep ] H{ } map>assoc ;
+
+: (find-mount-point-info) ( path assoc -- mtab-entry )
+    [ resolve-symlinks ] dip
+    2dup at* [
+        2nip
+    ] [
+        drop [ parent-directory ] dip (find-mount-point-info)
+    ] if ;
+
+: find-mount-point-info ( path -- file-system-info )
+    mount-points (find-mount-point-info) ;
+
 {
     { [ os unix? ] [ "io.files.info.unix" ] }
     { [ os windows? ] [ "io.files.info.windows" ] }
index dd04aecf6decf746d2dc824472f6a0c57548d376..1bf37532f167717e720744df552b456f2090af75 100644 (file)
@@ -81,17 +81,6 @@ frequency pass-number ;
 M: linux file-systems
     parse-mtab [ mtab-entry>file-system-info ] map sift ;
 
-: (find-mount-point) ( path mtab-paths -- mtab-entry )
-    2dup at* [
-        2nip
-    ] [
-        drop [ parent-directory ] dip (find-mount-point)
-    ] if ;
-
-: find-mount-point ( path -- mtab-entry )
-    resolve-symlinks
-    parse-mtab [ [ mount-point>> ] keep ] H{ } map>assoc (find-mount-point) ;
-
 M: linux file-system-info ( path -- file-system-info )
     normalize-path
     [
@@ -100,7 +89,7 @@ M: linux file-system-info ( path -- file-system-info )
         [ file-system-statvfs statvfs>file-system-info ] bi
         file-system-calculations
     ] keep
-    find-mount-point
+    find-mount-point-info
     {
         [ file-system-name>> >>device-name drop ]
         [ mount-point>> >>mount-point drop ]
index 214b8f13fecdf05ab1bad344fd3d0e0e659cc67c..88b0414fe4c91ae5d4dc3cdd5e27bf1b2c914734 100644 (file)
@@ -1,26 +1,29 @@
-! Copyright (C) 2010 Slava Pestov.
+! Copyright (C) 2010 Slava Pestov, Doug Coleman.
 ! See http://factorcode.org/license.txt for BSD license.
-USING: accessors io.files.info io.pathnames kernel mason.config
-math math.parser namespaces sequences ;
+USING: accessors formatting io.files.info io.pathnames kernel
+mason.config math namespaces ;
 IN: mason.disk
 
-: gb ( -- n ) 30 2^ ; inline
+: Gi ( n -- gibibits ) 30 2^ * ; inline
 
 : sufficient-disk-space? ( -- ? )
-    ! We want at least 300Mb to be available before starting
-    ! a build.
-    "." file-system-info available-space>> gb > ;
+    current-directory get find-mount-point-info
+    file-system-info available-space>>
+    1 Gi > ;
 
 : check-disk-space ( -- )
     sufficient-disk-space? [
-        "Less than 1 Gb free disk space." throw
+        "Less than 1 Gi free disk space." throw
     ] unless ;
 
-: mb-str ( n -- string ) gb /i number>string ;
+: Gi-str ( n -- string ) 1 Gi /f ;
+
+: path>disk-usage ( path -- string )
+    find-mount-point-info
+    [ used-space>> ] [ available-space>> ] [ total-space>> ] tri
+    2dup /f 100 *
+    [ [ Gi-str ] tri@ ] dip
+    "%0.2fGi used, %0.2fGi avail, %0.2fGi total, %0.2f%% free" sprintf ;
 
 : disk-usage ( -- string )
-    builds-dir get file-system-info
-    [ used-space>> ] [ total-space>> ] bi
-    [ [ mb-str ] bi@ " / " glue " Gb used" append ]
-    [ [ 100 * ] dip /i number>string "(" "%)" surround ] 2bi
-    " " glue ;
+    builds-dir get path>disk-usage ;
\ No newline at end of file