jak@sactoh0.UUCP (Jay A. Konigsberg) (06/28/90)
In article <797@massey.ac.nz> GEustace@massey.ac.nz (Glen Eustace) writes: >We are trying to write some code that will check whether a file will >fit on a given file system prior to copying it. It would be great if >the information given in a 'df' were available with some system call. >Does anyone know whether there is one and if so how one uses it. > This looks like a job for the stat() system call. (Sys V) #include <sys/types.h> #include <sys/stat.h> #include <errno.h> main() { struct stat *buf; buf = (struct stat *)malloc(sizeof(struct stat)); /* error check removed */ if ( stat("filename", buf) == -1 ) { fprintf("stat: error=%d\n", errno); exit(2); } else { printf("%s is %ld bytes.\n", "filename", buf->st_size); } } -- ------------------------------------------------------------- Jay @ SAC-UNIX, Sacramento, Ca. UUCP=...pacbell!sactoh0!jak If something is worth doing, its worth doing correctly.
cpcahil@virtech.uucp (Conor P. Cahill) (06/28/90)
In article <3394@sactoh0.UUCP> jak@sactoh0.UUCP (Jay A. Konigsberg) writes: >In article <797@massey.ac.nz> GEustace@massey.ac.nz (Glen Eustace) writes: >>We are trying to write some code that will check whether a file will >>fit on a given file system prior to copying it. It would be great if >>the information given in a 'df' were available with some system call. >>Does anyone know whether there is one and if so how one uses it. >> >This looks like a job for the stat() system call. (Sys V) First off, you answer the wrong question. The poster wanted to know how much space was left on the file system, not how big a file was. (Read before you post) >buf = (struct stat *)malloc(sizeof(struct stat)); /* error check removed */ > >if ( stat("filename", buf) == -1 ) Using the return from a malloc before you check to see if it is null? tsk, tsk, tsk. > { > fprintf("stat: error=%d\n", errno); Calling fprintf without a file pointer???? tsk, tsk, tsk. -- Conor P. Cahill (703)430-9247 Virtual Technologies, Inc., uunet!virtech!cpcahil 46030 Manekin Plaza, Suite 160 Sterling, VA 22170
guy@auspex.auspex.com (Guy Harris) (06/29/90)
>This looks like a job for the stat() system call. (Sys V)
It's not. He doesn't want to know how big the file he's copying is, he
wants to know how much room there is on some file system....
mpl@pegasus.ATT.COM (Michael P. Lindner) (06/29/90)
In article <3394@sactoh0.UUCP> jak@sactoh0.UUCP (Jay A. Konigsberg) writes: >In article <797@massey.ac.nz> GEustace@massey.ac.nz (Glen Eustace) writes: >>We are trying to write some code that will check whether a file will >>fit on a given file system prior to copying it. It would be great if >>the information given in a 'df' were available with some system call. >>Does anyone know whether there is one and if so how one uses it. >> >This looks like a job for the stat() system call. (Sys V) code deleted Close, but NO CIGAR! Glen KNOWS how big his file is. The problem is to find out how much space is available in the destination filesystem. To do that call statfs() (Sys V once again). On older systems you could use ustat(). See section 2 of the programmer's reference manual for more info. Mike Lindner AT&T Bell Labs attmail!mplindner
boyd@necisa.ho.necisa.oz (Boyd Roberts) (06/29/90)
In article <3394@sactoh0.UUCP> jak@sactoh0.UUCP (Jay A. Konigsberg) writes: >In article <797@massey.ac.nz> GEustace@massey.ac.nz (Glen Eustace) writes: >>... It would be great if >>the information given in a 'df' were available with some system call. >>Does anyone know whether there is one and if so how one uses it. >> >This looks like a job for the stat() system call. (Sys V) > Gak! Chigaimasu! You require ustat(2) on Sys V or statfs(2) on NFS varients. stat(2) reports information on files, NOT _file-systems_. Aside from that, do you realise that the blocks that were there when you looked, may not be there when you actually start writing? Or will your target file-system be idle? Boyd Roberts boyd@necisa.ho.necisa.oz.au ``When the going gets wierd, the weird turn pro...''
mboen@nixpbe.UUCP (Martin Boening) (06/29/90)
In <3394@sactoh0.UUCP> jak@sactoh0.UUCP (Jay A. Konigsberg) writes: >In article <797@massey.ac.nz> GEustace@massey.ac.nz (Glen Eustace) writes: >>We are trying to write some code that will check whether a file will >>fit on a given file system prior to copying it. It would be great if >>the information given in a 'df' were available with some system call. >>Does anyone know whether there is one and if so how one uses it. >> >This looks like a job for the stat() system call. (Sys V) >#include <sys/types.h> >#include <sys/stat.h> >#include <errno.h> >main() >{ >struct stat *buf; >buf = (struct stat *)malloc(sizeof(struct stat)); /* error check removed */ >if ( stat("filename", buf) == -1 ) > { > fprintf("stat: error=%d\n", errno); > exit(2); > } >else > { > printf("%s is %ld bytes.\n", "filename", buf->st_size); > } >} >-- >------------------------------------------------------------- >Jay @ SAC-UNIX, Sacramento, Ca. UUCP=...pacbell!sactoh0!jak >If something is worth doing, its worth doing correctly. I think you missed the point, there. The question was how to find out if a file system still provides enough room for a file prior to copying it, not to get the size of the file to copy. Of course the above program will determine the size of the file to copy. Now, to get the remaining space of the target file system, you have to use ustat(2). Man Page excerpt: USTAT(2-att) TARGON /35 Operating System USTAT(2-att) NAME ustat - get file system statistics SYNOPSIS #include <sys/types.h> #include <ustat.h> int ustat (dev, buf) int dev; struct ustat *buf; DESCRIPTION Ustat returns information about a mounted file system. Dev is a device number identifying a device containing a mounted file system. Buf is a pointer to a ustat structure that includes the following elements: daddr_t f_tfree; /* Total free blocks */ ino_t f_tinode; /* Number of free inodes */ char f_fname[6]; /* Filsys name */ char f_fpack[6]; /* Filsys pack name */ ... and so on ... Question is: how to get the number for dev. I think the stat call comes to new honor here: do a stat on the directory you wish to copy the file to, then use the struct element st_dev. So, extending your program a bit, I think the following might work: #include <sys/types.h> #include <sys/stat.h> #include <sys/ustat.h> #include <errno.h> main() { struct stat *buf; struct ustat *ubuf; buf = (struct stat *)malloc(sizeof(struct stat)); /* error check removed */ ubuf = (struct ustat *)malloc(sizeof(struct ustat)); /* ditto */ if ( stat("target-dir", buf) ) { perror("stat failed"); exit (errno); } else if ( ustat(buf->st_dev, ubuf) ) { perror("ustat failed"); exit(errno); } else { fprintf(stdout, "%ld free blocks and %ld free inodes remain on target fs\n", ubuf->f_tfree, ubuf->f_inode); exit(0); } } Something similar is done in the nntpd, BTW. I have not tried if it works, yet. I hope this helps and isn't improper to post to the net (enough general inter- est??) Martin -- Email: in the USA -> ...!uunet!philabs!linus!nixbur!mboening.pad outside USA -> {...!mcvax}!unido!nixpbe!mboening.pad Paper Mail: Martin Boening, Nixdorf Computer AG, DS-CC22, Pontanusstr. 55, 4790 Paderborn, W.-Germany
swfc@ulysses.att.com (Shu-Wie F Chen) (06/29/90)
In article <4881@pegasus.ATT.COM>, mpl@pegasus.ATT.COM (Michael P. Lindner) writes: |>In article <3394@sactoh0.UUCP> jak@sactoh0.UUCP (Jay A. Konigsberg) writes: |>>In article <797@massey.ac.nz> GEustace@massey.ac.nz (Glen Eustace) writes: |>>>We are trying to write some code that will check whether a file will |>>>fit on a given file system prior to copying it. It would be great if |>>>the information given in a 'df' were available with some system call. |>>>Does anyone know whether there is one and if so how one uses it. |>>> |>>This looks like a job for the stat() system call. (Sys V) |> code deleted |> |>Close, but NO CIGAR! |>Glen KNOWS how big his file is. The problem is to find out how much space |>is available in the destination filesystem. To do that call statfs() (Sys |>V once again). On older systems you could use ustat(). See section 2 of |>the programmer's reference manual for more info. |> |>Mike Lindner |>AT&T Bell Labs |>attmail!mplindner So what happens if two of these things execute simultaneously. Both calculate that they can copy their files onto the file system and try to do so. If the sum of the sizes of both files is greater than the available space, one of those processes will lose. Or is this not a problem? That is, will you check afterwards if the copy was successful, etc. *swfc