473,544 Members | 1,024 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

get disk usage

2 New Member
Hi:

Somebody knows how I can obtain disk space usage and the space that left in disk.
Sep 20 '06 #1
2 24064
tyreld
144 New Member
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
naveenese
1 New Member
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
1695
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 jdk1.4.2_05 and Tomcat 5.0.28 Thanks Peter Mount info@petermount.au.com
1
5132
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 much space is occupied by a particular row or column of a table. Thanks
6
2540
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 disk space and bandwidth they were using up. Therefore, they often lost money unexpectedly which, as most of you know, is not sustainable. ...
2
3734
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 and adding users and giving them access to store a limited amount of data. The only thing which does this in my knowledge is the FileSystemWatcher...
1
3419
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 addition to issue a DELETE FROM log WHERE date < xxx I also need to issue a VACUUM log so that new entries will use the space of deleted entries. Now...
3
6330
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 is code already out there that does it...... TIA - Jeff.
34
29815
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
2155
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? For instance, I’d like it to use no more than 50G. And if there is such parameter, what is the effect? Will postgres simply stop writing to the disk?...
12
2399
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 that tells me the disk usage ( equivalent for cmdline
0
7424
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7607
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
5909
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5297
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4918
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3415
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3409
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1841
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
661
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.