getting 'df' info from inside a program?

Martin Boening mboen at nixpbe.UUCP
Fri Jun 29 15:42:06 AEST 1990


In <3394 at sactoh0.UUCP> jak at sactoh0.UUCP (Jay A. Konigsberg) writes:

>In article <797 at massey.ac.nz> GEustace at 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



More information about the Comp.sys.pyramid mailing list