LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 10-03-2004, 03:09 PM   #1
Dave31836
LQ Newbie
 
Registered: Sep 2004
Location: Framingham, Massachusetts, USA
Distribution: Redhat 9
Posts: 17

Rep: Reputation: 0
allocated space of a directory


In C++, what is the best way to find the total storage allocated in a directory? I am familiar with stat and statfs which returns allocations of a file and total disk, respectively, but how to you get the allocated space of the contents of a directory?
 
Old 10-03-2004, 06:52 PM   #2
CroMagnon
Member
 
Registered: Sep 2004
Location: New Zealand
Distribution: Debian
Posts: 900

Rep: Reputation: 33
You add up all the files in that directory and its subdirectories
 
Old 10-04-2004, 10:01 AM   #3
Dave31836
LQ Newbie
 
Registered: Sep 2004
Location: Framingham, Massachusetts, USA
Distribution: Redhat 9
Posts: 17

Original Poster
Rep: Reputation: 0
As I'm adding up the directory entries, I do:

struct stat st;
stat( szPath, &st );
long lBytes = st.st_blocks * st.st_blksize;

...

This gives an answer 8 X too large. While st.st_blksize is 4096,
the actual block size is 512 (as derives from the size shown by
the file File Manager.

Any ideas what is wrong?
 
Old 10-04-2004, 03:20 PM   #4
CroMagnon
Member
 
Registered: Sep 2004
Location: New Zealand
Distribution: Debian
Posts: 900

Rep: Reputation: 33
Why are you calculating by blocks? Does st.st_size not contain the right filesize, or do you want to count the total size taken up on the disk?

Apparently st_blksize is the 'preferred blocksize' for talking to the device but st_blocks is always measured in 512-byte blocks, so just do st_blocks << 9.
 
Old 10-04-2004, 03:37 PM   #5
Dave31836
LQ Newbie
 
Registered: Sep 2004
Location: Framingham, Massachusetts, USA
Distribution: Redhat 9
Posts: 17

Original Poster
Rep: Reputation: 0
thanks... I want the actual allocation on disk, not just the file size.

I guiess I'll use 512 (i think you meant st_blocks << 3) and hope it
is portable across other file systems.

thnx again
 
Old 10-04-2004, 03:49 PM   #6
CroMagnon
Member
 
Registered: Sep 2004
Location: New Zealand
Distribution: Debian
Posts: 900

Rep: Reputation: 33
No, I meant << 9, as that multiplies by 2^9, or 512. << 3 only multiplies by 8 (which is fine if you want to know how big your file is in terms of 64-byte blocks)
 
Old 10-04-2004, 04:08 PM   #7
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,481

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
Beware that either methods can be innacurate.

Summing file size can lead to very different results than the real size the directory is using on the disk:
Files smaller that the allocation unit would lead you to think less space is used that the real one.
Sparse files will make you think the opposite (puzzle people by creating a 2 Gb file on a 1Gb partition).

Finally, pay attention not to count multiple times files sharing the same inode (hard-links).
 
Old 10-04-2004, 05:14 PM   #8
CroMagnon
Member
 
Registered: Sep 2004
Location: New Zealand
Distribution: Debian
Posts: 900

Rep: Reputation: 33
Good points jlliagre, I forgot about sparse files. Of course, if he only wants the physical disk space used, then this method won't cause him trouble...

If you check the st_nlink member to make sure it's 1, you won't have a hardlink issue (if it is >1, add st_ino to an array, and check any future files with nlink > 1 against this array, that should be enough to avoid hard link issues. Also, you may want to use lstat instead of stat to correctly count space used by symbolic links).
 
Old 10-04-2004, 06:07 PM   #9
Dave31836
LQ Newbie
 
Registered: Sep 2004
Location: Framingham, Massachusetts, USA
Distribution: Redhat 9
Posts: 17

Original Poster
Rep: Reputation: 0
re No, I meant << 9.... of course -- i misunderstood your point.

thanks again
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Increasing hard-disk space allocated to user guam Linux - General 10 03-01-2005 07:31 AM
Not enough space for root directory on a drive with 50g free space??? auoq Linux - Newbie 1 10-13-2004 12:44 PM
Can't delete directory with space in name budhead Linux - Software 3 09-15-2004 10:57 AM
directory with a space singedwings Linux - Newbie 4 08-24-2004 05:55 AM
change into directory with space in it? sideveloper Linux - Newbie 3 03-06-2004 04:18 PM


All times are GMT -5. The time now is 03:29 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration