statvfs function

I am currently porting some application software from QNX 4 to 6.2.1. On
QNX 4, part of this software used the disk_space() function to get the
number of blocks and number of free blocks on the disk.

When migrating this, I have tried to use statvfs() to get the same
information, but it doesn’t seem to work very well. It correctly reports
the filesystem blocksize and the total number of blocks, but the number of
free blocks is reported as zero. The fstatvfs() function is even worse,
reporting the blocksize and total number of blocks as 1, and free blocks as
zero. To prove to myself that I wasn’t doing something silly, I tried the
same source code on a solaris platform, where it worked perfectly.

Can anyone explain this and/or suggest a solution? I would like to avoid
execing to a shell to run df, as the use of exec is causing problems in this
application. But the fact that df works means that the information must be
available.

Thanks

David Scott.

David Scott <djsco@bgs.ac.uk> wrote:

I am currently porting some application software from QNX 4 to 6.2.1. On
QNX 4, part of this software used the disk_space() function to get the
number of blocks and number of free blocks on the disk.

When migrating this, I have tried to use statvfs() to get the same
information, but it doesn’t seem to work very well. It correctly reports
the filesystem blocksize and the total number of blocks, but the number of
free blocks is reported as zero. The fstatvfs() function is even worse,
reporting the blocksize and total number of blocks as 1, and free blocks as
zero. To prove to myself that I wasn’t doing something silly, I tried the
same source code on a solaris platform, where it worked perfectly.

Can anyone explain this and/or suggest a solution? I would like to avoid
execing to a shell to run df, as the use of exec is causing problems in this
application. But the fact that df works means that the information must be
available.

Thanks

David Scott.

Dunno. The following code snippet works for me – we use it as part of
midnight diagnostics to verify basic sanity, in this case, that there is
still at least 1 MB of free disk space…

The key might be which filesystem you are using to verify – it could be
some package-filesystem junk. Try a “real” file – below, we use “/var”…

static int
diags_test_free_disk (void)
{
struct statvfs statbuf;

// free disk test – there must be at least 1 MB of free disk space in “/var”
if (statvfs ("/var", &statbuf) == -1) {
sl_printf (“DIAGS: statvfs (/var) failed, errno %d (%s)\n”, errno, strerror (errno));
return (DIAGS_CANT_STAT_VAR);
}

if (statbuf.f_bfree * statbuf.f_bsize < 1000000) {
sl_printf (“DIAGS: less than 1000000 bytes of disk space free (%d blocks of %ld bytes (total %ld) free)\n”, statbuf.f_bfree, statbuf.f_bsize, statbuf.f_bfree * statbuf.f_bsize);
return (DIAGS_OUT_OF_DISK);
}

return (DIAGS_OK);
}

Cheers,
-RK

[If replying via email, you’ll need to click on the URL that’s emailed to you
afterwards to forward the email to me – spam filters and all that]
Robert Krten, PDP minicomputer collector http://www.parse.com/~museum/

I think this is, as you say, a filesystem thing, but how to get around it?
The following code:

struct statvfs stfs;
char *dir = “/dev/hd0t79”;

if (statvfs(dir, &stfs) == -1)
{
printf (“error on statvfs\n”);
exit (1);
}
printf (“f_bsize %ld f_frsize %ld f_blocks %ld f_bfree %ld\n”,
stfs.f_bsize,stfs.f_frsize,stfs.f_blocks,stfs.f_bfree);

produces this output:

f_bsize 512 f_frsize 512 f_blocks 16498692 f_bfree 0

and it is the number of free blocks that is the important bit! Here I have
hard-coded the directory, but in the real program this is deduced by reading
/dev and testing which entries are mount points, so this is not easily
changed.



“Robert Krten” <rk@parse.com> wrote in message
news:csrdn2$g93$1@inn.qnx.com

Dunno. The following code snippet works for me – we use it as part of
midnight diagnostics to verify basic sanity, in this case, that there is
still at least 1 MB of free disk space…

The key might be which filesystem you are using to verify – it could be
some package-filesystem junk. Try a “real” file – below, we use
“/var”…

static int
diags_test_free_disk (void)
{
struct statvfs statbuf;

// free disk test – there must be at least 1 MB of free disk space in
“/var”
if (statvfs ("/var", &statbuf) == -1) {
sl_printf (“DIAGS: statvfs (/var) failed, errno %d (%s)\n”,
errno, strerror (errno));
return (DIAGS_CANT_STAT_VAR);
}

if (statbuf.f_bfree * statbuf.f_bsize < 1000000) {
sl_printf (“DIAGS: less than 1000000 bytes of disk space free (%d
blocks of %ld bytes (total %ld) free)\n”, statbuf.f_bfree, statbuf.f_bsize,

statbuf.f_bfree * statbuf.f_bsize);

return (DIAGS_OUT_OF_DISK);
}

return (DIAGS_OK);
}

Cheers,
-RK

[If replying via email, you’ll need to click on the URL that’s emailed to
you
afterwards to forward the email to me – spam filters and all that]
Robert Krten, PDP minicomputer collector > http://www.parse.com/~museum/

David Scott <djsco@bgs.ac.uk> wrote:

I think this is, as you say, a filesystem thing, but how to get around it?
The following code:

struct statvfs stfs;
char *dir = “/dev/hd0t79”;

if (statvfs(dir, &stfs) == -1)
{
printf (“error on statvfs\n”);
exit (1);
}
printf (“f_bsize %ld f_frsize %ld f_blocks %ld f_bfree %ld\n”,
stfs.f_bsize,stfs.f_frsize,stfs.f_blocks,stfs.f_bfree);

produces this output:

f_bsize 512 f_frsize 512 f_blocks 16498692 f_bfree 0

and it is the number of free blocks that is the important bit! Here I have
hard-coded the directory, but in the real program this is deduced by reading
/dev and testing which entries are mount points, so this is not easily
changed.

/dev/hd0t79 is the “block special” device corresponding to your entire
QNX partition. You need to find a real “file” on that partition.

Cheers,
-RK

“Robert Krten” <> rk@parse.com> > wrote in message
news:csrdn2$g93$> 1@inn.qnx.com> …

Dunno. The following code snippet works for me – we use it as part of
midnight diagnostics to verify basic sanity, in this case, that there is
still at least 1 MB of free disk space…

The key might be which filesystem you are using to verify – it could be
some package-filesystem junk. Try a “real” file – below, we use
“/var”…

static int
diags_test_free_disk (void)
{
struct statvfs statbuf;

// free disk test – there must be at least 1 MB of free disk space in
“/var”
if (statvfs ("/var", &statbuf) == -1) {
sl_printf (“DIAGS: statvfs (/var) failed, errno %d (%s)\n”,
errno, strerror (errno));
return (DIAGS_CANT_STAT_VAR);
}

if (statbuf.f_bfree * statbuf.f_bsize < 1000000) {
sl_printf (“DIAGS: less than 1000000 bytes of disk space free (%d
blocks of %ld bytes (total %ld) free)\n”, statbuf.f_bfree, statbuf.f_bsize,
statbuf.f_bfree * statbuf.f_bsize);
return (DIAGS_OUT_OF_DISK);
}

return (DIAGS_OK);
}

Cheers,
-RK

[If replying via email, you’ll need to click on the URL that’s emailed to
you
afterwards to forward the email to me – spam filters and all that]
Robert Krten, PDP minicomputer collector > http://www.parse.com/~museum/


[If replying via email, you’ll need to click on the URL that’s emailed to you
afterwards to forward the email to me – spam filters and all that]
Robert Krten, PDP minicomputer collector http://www.parse.com/~museum/

“Robert Krten” <rk@parse.com> wrote in message
news:ct2t45$t6r$1@inn.qnx.com

/dev/hd0t79 is the “block special” device corresponding to your entire
QNX partition. You need to find a real “file” on that partition.

Thanks for your response. If I understand correctly, the corresponding file
is the root directory - ‘/’. I’ve already tried this, and the code gives
the output

f_bsize 1 f_frsize 1 f_blocks 1 f_bfree 0

If I run df explicitly on /, it produces the output

Filesystem 512-blocks Used Available Capacity
Mounted on
/pkgs/repository/pu 0 0 0
100% /
/dev/hd0t79 1 6498692 1473989 15024703 9% /

which may be connected with the problem.

David Scott <djsco@bgs.ac.uk> wrote:

“Robert Krten” <> rk@parse.com> > wrote in message
news:ct2t45$t6r$> 1@inn.qnx.com> …

/dev/hd0t79 is the “block special” device corresponding to your entire
QNX partition. You need to find a real “file” on that partition.


Thanks for your response. If I understand correctly, the corresponding file
is the root directory - ‘/’. I’ve already tried this, and the code gives
the output

f_bsize 1 f_frsize 1 f_blocks 1 f_bfree 0

If I run df explicitly on /, it produces the output

Filesystem 512-blocks Used Available Capacity
Mounted on
/pkgs/repository/pu 0 0 0
100% /
/dev/hd0t79 1 6498692 1473989 15024703 9% /

which may be connected with the problem.

Hmmm… Can we step back from this one level? If I can make some assumptions,
I’m assuming that you want to know when a certain place that you put files
is going to fill up, or perhaps how much room you have in which to place those
files.

Say that place is directory X, whereever X is.

Why not create a file under directory X, say “X/statvfs_test_file”, and then
simply statvfs() that file? Then you can delete the file.

What this does is binds the filesystem that statvfs() will use to the actual
one that you are interested in…

Cheers,
-RK


[If replying via email, you’ll need to click on the URL that’s emailed to you
afterwards to forward the email to me – spam filters and all that]
Robert Krten, PDP minicomputer collector http://www.parse.com/~museum/