Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2026-03-14
03:55 • Closed ticket [516d581c04]: A tcl command to report free usable space of a parti... artifact: d669d7dcba user: apnadkarni
2025-12-12
18:37 • Ticket [516d581c04]: 3 changes artifact: 9acb8bfbf7 user: anonymous
2025-12-11
19:49 • Ticket [516d581c04]: 3 changes artifact: f5f5cf0acd user: chw
14:46 • Ticket [516d581c04]: 3 changes artifact: 55656e68e4 user: anonymous
2025-11-27
13:03 • Ticket [516d581c04]: 3 changes artifact: 67405bf5ef user: anonymous
05:21 • Ticket [516d581c04]: 3 changes artifact: f63609b4ac user: apnadkarni
2025-11-21
13:39 • New ticket [516d581c04]. artifact: a8578d89f6 user: anonymous

A tcl command to report free usable space of a partition of native file system

Closed / Rejected516d581c047786f7a51e1a574e43e8832447cc07Created 1 year agoUpdated 264 days ago
Descriptionplain2025-11-21 13:39:55
With a tcl/tk application using several SQLite databases with SQLite Tcl interface, we need to check available disk space on the partition where the database files reside as they may grow wihout limit.

For example, the VACCUM db operation, (often used on our side), quoting SQLite doc at https://sqlite.org/lang_vacuum.html "works by copying the contents of the database into a temporary database file and then overwriting the original with the contents of the temporary file.... This means that when VACUUMing a database, as much as twice the size of the original database file is required in free disk space"

Not sure if this measure of free space needs byte precision
(on our side, the info is sufficiant in Megabyte unit)

For now, as Tcl lacks this fuctionality, we execute from Tcl the following small java program :

import java.io.File;
import java.lang.Math;

public class java_HDfreespace {
    
    public static void main(String[] args) {
        
        // Espace disque libre utilisable en octet
            
            long freeHDspace_o = new File(args[0]).getUsableSpace();        // getUsableSpace() retourne un entier long
        
        // On convertit la valeur en Mo -> division 1048576.0 (= 1024 * 1024)
            
            double freeHDspace_mo = freeHDspace_o / 1048576.0;
        
        // Retour de la valeur arrondie à 2 décimales
            
            System.out.println(Math.round(freeHDspace_mo * 100.0) / 100.0);
        
        System.exit(0);
    }
}


Thank you

Jean-Marie
apnadkarnimarkdown2025-11-27 05:21:18

Not that it's up to me, but I think these kinds of features (system information like list of drives/mounts, disk space, memory etc.) are more appropriate to be implemented as libraries or extensions, not part of core Tcl. On Windows, you can use the twapi package (get_volume_info). On Unix, tuapi might have something similar, not sure.

Also to be noted, the definition of "free disk space" is a bit nebulous. If the file path passed is a link or junction, should the function return space for the link or the link target? In case of spanned volumes, should return free space on the physical drive or logical drive? Etc.

anonymousplain2025-11-27 13:03:25
Dear Ashok(*)

Thank you for pointing to tuapi package I wasn't aware of. However, a quick look at the doc at
 https://chiselapp.com/user/rkeene/repository/tuapi/file?name=USAGE.md&ci=tip
while interesting, gives the impression the need expressed in this ticket is not fullfilled by this package.

I understand your point of vue about system information in general more apropriately provided by specialized libraries/packages.

However, consider for example, an application needing a layout of several directories or mount points (admitedly less usal).
Fortunately Tcl provides [pwd], [cd] and [file volume] to operate properly on this kind of layout.
IMHO, the information of usable space on the persistent media where the application is living belongs in the same vein (and is a feature allowing some basic grantee of secure fonctionning at application level).

In our application, efforts are made to bring the less dependencies possible, for lowering the maintenance burden (and so love "batteries included" Tcl' philosophy)

I agree "free disk space" is nebulous.
However "usable free disk space" seems more precise to me (yes it's me ...) about the meaning of the information to provide.

On our side Java's getUsableSpace() is simple enough and fullfill our need.
I can't dive into Java internals (not the skills for) but I guess a study of Java's getUsableSpace() may bring usefull clarications

I understand, in regard to the burden of maintenance work needed for such a wide software as Tcl langage, that the kind of new feature expressed in this ticket can't be top priority for Tcl developpers' team, but remain convinced that such security feature belongs to the core langage (especialy when "batteries included" philosophy is considered)

Thank you again

Jean-Marie

(*) bought your 2d edition book, thank you :)
anonymousplain2025-12-11 14:46:10
Sorry for this very late complementary explanation of the need expressed in this ticket.
Once a year, our application makes a copy of some of its SQLite db files, outside of its working area for long term archival purpose.
Below is a small Tcl script exerpt of what we do :

============================================================================

# a) Check available free usable space on the target (in Mo)
     
     set freeArchiveSpace [exec java -classpath [file join $::_currentdir Lib] java_HDfreespace $tclArchivesPath]

# b) Needed additional free space (in Mo)
                
     set trentordbSpace  [format %.2f [expr {[file size [file join $::_currentdir Data trentordb.tr]]  / 1048576.0}]]
        set trentordb2Space [format %.2f [expr {[file size [file join $::_currentdir Data trentordb2.tr]] / 1048576.0}]]
        set dbdocSpace      [format %.2f [expr {[file size [file join $::_currentdir Etats dbdoc.tr]]     / 1048576.0}]]
        

        if {$freeArchiveSpace < $trentordbSpace + $trentordb2Space + $dbdocSpace} {
            
            # [A044] Erreur : espace de stockage disponible ($freeArchiveSpace Mo) insuffisant.
            tk_messageBox -parent  .menuGeneral \
                          -message [::msgcat::mc A044 $freeArchiveSpace] \
                          -type ok -icon error -title $::_appName
            
            # arrêt des traitements
            return 9
        }

============================================================================

where :
- "java_HDfreespace" : java program' name whose code was provided in my previous post
- $tclArchivesPath contains the absolute path to the directory where the db files are to be copied on the target, and passed as argument to java_HDfreespace program
- trentordb.tr, trentordb2.tr and dbdoc.tr are three SQlites database files to be archived

Final note : before executing the above excerpt, the application tries to shrink the db files as much as possible with VACUUM SQL command, whose logic by making a temporary copy of the db file processed, needs beforehand a check of available space where the application resides. It is again done in invoking the java program described in this ticket.

Thank you again
Jean-Marie
chwplain2025-12-11 19:49:00
The tclbsd package provides the bsd::statfs command which might
give you the required filesystem insight.

Refer to https://github.com/flightaware/tclbsd for more information.
anonymousplain2025-12-12 18:37:00
Thank you, I was not aware of this tclbsd package. Very interesting indeed.
After reading its doc, I see it is feasible while a little tortuous.

I allso learned that OS view of available free space on persistent media can be different wether the calling process has superuser rights or not.

On the Java side, getUsableSpace() seems to manage this difference under the hood in a transparent manner, but in counter part the wake-up process of the JVM can be laborious ...

I need time + experiments to take a decision

Thank you again
Jean-Marie
apnadkarniplain2026-03-14 03:55:02
Closing as not likely to be implemented in the core.
Log in (anonymous is fine) to add a comment.