Purpose:
- Save a program that writes data to disk from vain attempts of writing to a full filesystem;
- Save bandwidth (don't download if nowhere to store);
- Save user's and programmer's time and nerves (notify them of the problem instead of having them tearing out their hair with reading misleading error messages and "why the heck this software is not working!").
The question comes in 2 parts:
- Reporting storage space statistics (available, used, total etc.), either of all filesystems or of the filesystem that path in question belongs to.
- Reporting a filesystem error on running out of space.
Part 1
Share please NATIVE Raku alternative(s) (TIMTOWTDIBSCINABTE "Tim Toady Bicarbonate") to:
raku -e 'qqx{ df -P $*CWD }.print'
Here, raku
-e
xecutes df
(disk free) external program via shell quoting with interpolation qqx{}
, feeding -P
ortable-format argument and $*CWD
Current Working Directory, then .print
s the df
's output.
The snippet initially had been written as raku -e 'qqx{ df -hP $*CWD }.print'
— with both -h
uman-readable and -P
ortable — but it turned out that it is not a ubiquitously valid command. In OpenBSD 7.0, it exits with an error: df: -h and -i are incompatible with -P
.
For adding human-readability, you may consider Number::Bytes::Human module
[raku]
tag. :) This reads to me like an XY question. For example, on an OS that already has adf
like program, the sensible thing would be to use it (but usingrun
notqx
for safety). On an OS without adf
like program -- which is to say, a very immature OS -- Raku likely has no chance of compensating. But you haven't talked about such aspects. So perhaps your real purpose is to learn about Raku? Whatever it is, I think it would help a lot if you edited your question to add what's really behind your question. – Argonautrun 'df', '-hP', $*CWD
may be more safe since this form facilitates providing parts carefully, but isrun «df -hP $*CWD»
any more safe thanqqx{}
? You are free to throw some garbage at it as well! 3. I choseqqx{}
overrun
because the former makes output available outright. I wouldsplit
it and then check available space programmatically. [continued on the next comment] – Garlicstatvfs()
on a platform that supports that. Likewise you could use an Inline to call a foreign language module that does what you want. That said, why not use FileSystem::Capacity? – Argonautrun «df -hP $*CWD»
any more safe thanqqx ...
? Potentially, if the current path contains unusual characters.qqx
usesshell
, which processes shell escapes, which an odd path name might include. As the doc forshell
notes, "Shell escapes are a severe security concern ... Userun
if you want to be safe." One still needs to be leery of injection attacks when usingrun
, but at least issues due to using a shell are eliminated. See also github.com/Raku/doc/pull/3696. – Argonaut