473,406 Members | 2,352 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

get disk usage

Hi:

Somebody knows how I can obtain disk space usage and the space that left in disk.
Sep 20 '06 #1
2 24003
tyreld
144 100+
On a POSIX system you would use the file system statistics data type and function calls. These are defined in <sys/statvfs.h>.

Here is what the data structure and funtion prototypes should look like in <sys/statvfs.h>

Expand|Select|Wrap|Line Numbers
  1. struct statvfs {
    unsigned long  f_bsize;    /* file system block size */
  2. unsigned long  f_frsize;   /* fragment size */
  3. fsblkcnt_t     f_blocks;   /* size of fs in f_frsize units */
  4. fsblkcnt_t     f_bfree;    /* # free blocks */
  5. fsblkcnt_t     f_bavail;   /* # free blocks for non-root */
  6. fsfilcnt_t     f_files;    /* # inodes */
  7. fsfilcnt_t     f_ffree;    /* # free inodes */
  8. fsfilcnt_t     f_favail;   /* # free inodes for non-root */
  9. unsigned long  f_fsid;     /* file system ID */
  10. unsigned long  f_flag;     /* mount flags */
  11. unsigned long  f_namemax;  /* maximum filename length */};
  12.  
  13. /* These functions return 0 on success and -1 on failure */
  14.  
  15. int statvfs(const char *filename, struct statvfs buf);
  16. int fstatvfs(int fd, struct statvfs buf);
  17.  
This sample code outputs the disk usage and free space in bytes.

Expand|Select|Wrap|Line Numbers
  1. include <sys/statvfs.h>
  2.  
  3. ...
  4.  
  5. /* Any file on the filesystem in question */
  6. char *filename = "/home/tyreld/somefile.txt";
  7.  
  8. struct statvfs buf;
  9. if (!statvfs(filename, &buf)) {
    unsigned long blksize, blocks, freeblks, disk_size, used, free;
  10.  
  11. blksize = buf.f_bsize;
  12. blocks = buf.f_blocks;
  13. freeblks = buf.f_bfree;
  14.  
  15. disk_size = blocks * blksize;
  16. free = freeblks * blksize;
  17. used = disk_size - free;
  18.  
  19. printf("Disk usage : %lu \t Free space %lu\n", used, free);} else {
    printf("Couldn't get file system statistics\n");
    }
  20.  
  21.  
Sep 20 '06 #2
Thanks tyreld

Good post..Aftr searching long time got this post..
I have used it as part of my code.
Jan 31 '12 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Peter Mount | last post by:
Hello Are there any good open source examples (or tutorials) for developing jsp pages to display web stats (like number of hits etc) and disk usage for a web site? I'm currently using JSP 2 with...
1
by: Gh! | last post by:
How can I see how much disk space is being occupied by an InnoDB table? Looking into /var/lib/mysql doesn't seem to work anymore for InnoDB tables. Please tell also if there is a way to see how...
6
by: Matthew Nuzum | last post by:
We've had a chance to talk to some of our competitors who have recently gone out of business. One of the major failings that contributed to their collapse was not accurately understanding how much...
2
by: Joe | last post by:
Hi Guys, Is it possible to create a disk management system using .NET entirely. What I mean by the above is having an application possibly a .NET windows service monitoring the disk, creating...
1
by: Nils Rennebarth | last post by:
I have a table that is essentially a log where new entries are streaming in continually and from time to time I throw old entries away to keep the table from growing. I understand that in...
3
by: UJ | last post by:
I've now got the way to figure out how much disk space is on a disk. Is there any way to get the disk space usage for a specific directory and below? I could write a routine easily but if there...
34
by: Tom | last post by:
I'd greatly appreciate advice and code snippets on how to create a ram disk within a C/C++ program. I also need to be able to determine the free space. Thanks in advance for any help.
0
by: fchen00 | last post by:
Hi there, I’m running Postgres on SuSe Linux and some times the database uses so much disk space that we got disk full problems. Is there a way in postgres to specify how much disk space to use?...
12
by: Sankar | last post by:
Dear all, I am programming in Linux , wherein I need to know a couple of things. 1) Does an API exist that can copy file onto another file ( an API equivalent of 'cp') ? 2) Is there an API...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.