Net::FTP - How Do I Read The Available Space On A Partition ???

Discussion in 'Perl Misc' started by tommo_blade, Sep 4, 2006.

  1. tommo_blade

    tommo_blade Guest

    Hello All,
    I have read thru the perl docs for this module and I have
    tried a few of the 'methods' available but as yet I have not been able
    to get my script to pull the data I need from the server. basically I
    want to know if there is enough 'available space' on a unix partition
    prior to me FTP'ing a file to that host/partition, I have tried the
    'ls' and 'dir' methods but I cannot get what I need. The data I need at
    unix level can be gained by running a 'df -k' command which when run
    returns the available disk space in the 'avail' column.

    Does anyone know of any neat tricks to get this data.


    cheers, Mark.
     
    tommo_blade, Sep 4, 2006
    #1
    1. Advertising

  2. tommo_blade

    David Squire Guest

    tommo_blade wrote:
    > Hello All,
    > I have read thru the perl docs for this module and I have
    > tried a few of the 'methods' available but as yet I have not been able
    > to get my script to pull the data I need from the server. basically I
    > want to know if there is enough 'available space' on a unix partition
    > prior to me FTP'ing a file to that host/partition, I have tried the
    > 'ls' and 'dir' methods but I cannot get what I need. The data I need at
    > unix level can be gained by running a 'df -k' command which when run
    > returns the available disk space in the 'avail' column.
    >
    > Does anyone know of any neat tricks to get this data.


    This works:

    ----

    #!/usr/bin/perl

    use strict;
    use warnings;

    my $df_out = `df -k`;

    foreach my $Line (split /\n/, $df_out) {
    my ($Filesystem, $OneKBlocks, $Used, $Available, $UsePercent,
    $MountPoint) = split /\s+/, $Line, 6;
    print $Available if ($MountPoint eq '/'); # substitute whatever
    criteria you are interested in
    }

    ----

    but I am sure that someone will come up with a nifty one-liner. Also, it
    depends on your 'df' output having the same format as mine (tested on
    Ubuntu and Mac OS X).


    DS
     
    David Squire, Sep 4, 2006
    #2
    1. Advertising

  3. tommo_blade

    Paul Lalli Guest

    tommo_blade wrote:
    > I have read thru the perl docs for this module and I have
    > tried a few of the 'methods' available but as yet I have not been able
    > to get my script to pull the data I need from the server. basically I
    > want to know if there is enough 'available space' on a unix partition
    > prior to me FTP'ing a file to that host/partition, I have tried the
    > 'ls' and 'dir' methods but I cannot get what I need. The data I need at
    > unix level can be gained by running a 'df -k' command which when run
    > returns the available disk space in the 'avail' column.
    >
    > Does anyone know of any neat tricks to get this data.


    Uh. Is this something you can do through FTP? Forget Perl for a
    moment - can you do this with a normal ftp command line session? If
    not, what makes you think you'd be able to do it with a Perl interphase
    to the File Transfer Proticol?

    Paul Lalli
     
    Paul Lalli, Sep 4, 2006
    #3
  4. tommo_blade

    -berlin.de Guest

    David Squire <> wrote in comp.lang.perl.misc:
    > tommo_blade wrote:
    > > Hello All,
    > > I have read thru the perl docs for this module and I have
    > > tried a few of the 'methods' available but as yet I have not been able
    > > to get my script to pull the data I need from the server. basically I
    > > want to know if there is enough 'available space' on a unix partition
    > > prior to me FTP'ing a file to that host/partition, I have tried the
    > > 'ls' and 'dir' methods but I cannot get what I need. The data I need at
    > > unix level can be gained by running a 'df -k' command which when run
    > > returns the available disk space in the 'avail' column.
    > >
    > > Does anyone know of any neat tricks to get this data.

    >
    > This works:
    >
    > ----
    >
    > #!/usr/bin/perl
    >
    > use strict;
    > use warnings;
    >
    > my $df_out = `df -k`;
    >
    > foreach my $Line (split /\n/, $df_out) {
    > my ($Filesystem, $OneKBlocks, $Used, $Available, $UsePercent,
    > $MountPoint) = split /\s+/, $Line, 6;
    > print $Available if ($MountPoint eq '/'); # substitute whatever
    > criteria you are interested in
    > }


    According to the OPs words "want to know if there is enough 'available
    space' ... prior to me FTP'ing a file to that host/partition" that would
    have to be run on the remote system, through ssh perhaps.

    No solution exists using only the FTP protocol. The designers of FTP
    have wisely decided not to require that. At the time, there were
    machines that measured disk space in odd-sized "words" or "pages".
    How many bytes are 1000 36-bit words?

    Not that any of this has a relation to Perl...

    Anno
     
    -berlin.de, Sep 4, 2006
    #4
  5. tommo_blade

    tommo_blade Guest

    To clarify, I need to get this data using the Net::FTP module and not
    by some preliminary perl script as has been mentioned in this thread.
    Does anyone know if there are any other add-on modules to the Net::FTP
    module that could get this data, i.e. Net::FTP::Common etc, etc.

    cheers, Mark.
     
    tommo_blade, Sep 4, 2006
    #5
  6. tommo_blade

    John Bokma Guest

    David Squire <> wrote:

    > tommo_blade wrote:
    >> Hello All,
    >> I have read thru the perl docs for this module and I have
    >> tried a few of the 'methods' available but as yet I have not been able
    >> to get my script to pull the data I need from the server. basically I
    >> want to know if there is enough 'available space' on a unix partition
    >> prior to me FTP'ing a file to that host/partition, I have tried the
    >> 'ls' and 'dir' methods but I cannot get what I need. The data I need at
    >> unix level can be gained by running a 'df -k' command which when run
    >> returns the available disk space in the 'avail' column.
    >>
    >> Does anyone know of any neat tricks to get this data.

    >
    > This works:


    No it doesn't, it suffers from a race condition as probably all
    "solutions".

    --
    John Experienced Perl programmer: http://castleamber.com/

    Perl help, tutorials, and examples: http://johnbokma.com/perl/
     
    John Bokma, Sep 4, 2006
    #6
  7. tommo_blade

    John Bokma Guest

    "tommo_blade" <> wrote:

    > To clarify, I need to get this data using the Net::FTP module and not
    > by some preliminary perl script as has been mentioned in this thread.
    > Does anyone know if there are any other add-on modules to the Net::FTP
    > module that could get this data, i.e. Net::FTP::Common etc, etc.


    There is no reliable way, even if it was supported by FTP unless the ftp
    server would stop all processes that can generate data on your partition.

    While you request how much free space there is, the information will
    already be outdated by the time you start the upload. During the upload
    another process can generate data. Imagine two programs doing the same:

    free space = 1GB

    program 1 asks free space and gets 1GB
    program 2 asks free space and gets 1GB
    program 1 starts to upload 700 MB
    program 2 starts to upload 700 MB
    ???
    profit

    --
    John Experienced Perl programmer: http://castleamber.com/

    Perl help, tutorials, and examples: http://johnbokma.com/perl/
     
    John Bokma, Sep 4, 2006
    #7
  8. tommo_blade

    Bob Walton Guest

    tommo_blade wrote:
    ....
    > I have read thru the perl docs for this module and I have
    > tried a few of the 'methods' available but as yet I have not been able
    > to get my script to pull the data I need from the server. basically I
    > want to know if there is enough 'available space' on a unix partition
    > prior to me FTP'ing a file to that host/partition, I have tried the
    > 'ls' and 'dir' methods but I cannot get what I need. The data I need at
    > unix level can be gained by running a 'df -k' command which when run
    > returns the available disk space in the 'avail' column.
    >
    > Does anyone know of any neat tricks to get this data.


    Well, I don't know how "neat" it is, but you can use Net::FTP's quot()
    method to execute a command on the remote system. I use it to chmod CGI
    programs to executable on ISP systems that don't provide command line
    access, for example (yeah, I know, I could get a better ISP). To get
    the result of the command returned back to you, you will probably need
    to redirect the STDOUT of the command to a file, then use the get()
    method to get the file, then open and read it. I don't know if the
    remote FTP server will run operating system commands with STDOUT
    redirected -- you'll have to try it and see. You can check out how it
    works with a command-line FTP client -- when you see you can do what you
    want with a manual client, then implement it using Net::FTP.

    You might be just as well off to do your put() and check the return for
    an error.

    HTH.

    > cheers, Mark.


    --
    Bob Walton
    Email: http://bwalton.com/cgi-bin/emailbob.pl
     
    Bob Walton, Sep 4, 2006
    #8
  9. tommo_blade

    tommo_blade Guest

    Thanks to all who replied, I think I will go with the suggestion of
    sticking the file in regardless and then checking that it is the same
    size (->size()) as the source file.
     
    tommo_blade, Sep 5, 2006
    #9
  10. tommo_blade

    Dr.Ruud Guest

    tommo_blade schreef:
    > Thanks to all who replied, I think I will go with the suggestion of
    > sticking the file in regardless and then checking that it is the same
    > size (->size()) as the source file.


    If the transport is compressed, and the quota-calculation isn't, you
    could test by first sending the file with all 0 bytes.

    --
    Affijn, Ruud

    "Gewoon is een tijger."
     
    Dr.Ruud, Sep 5, 2006
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Shuo Xiang

    Stack space, global space, heap space

    Shuo Xiang, Jul 9, 2003, in forum: C Programming
    Replies:
    10
    Views:
    2,925
    Bryan Bullard
    Jul 11, 2003
  2. Christian Seberino
    Replies:
    21
    Views:
    1,693
    Stephen Horne
    Oct 27, 2003
  3. Ian Bicking
    Replies:
    2
    Views:
    1,038
    Steve Lamb
    Oct 23, 2003
  4. bolega
    Replies:
    1
    Views:
    685
    Stan Bischof
    Mar 28, 2011
  5. D. Buck
    Replies:
    2
    Views:
    500
    D. Buck
    Jun 29, 2004
Loading...

Share This Page