Get the size of a directory (not its content)

Lars Nielsen

(please before dismissing this as already answered, read the full question)

In C++ we can get the file size of a regular file by running std::filesystem::file_size(PATH); But this function does not work on directories, which is my problem. I am in a situation where I need to know the size of directory "inode", in (most) linux systems the standard size of a directory is 4kB or block size:

$:~/tmp/test$ mkdir ex
$:~/tmp/test$ ls -l
total 4
drwxrwxr-x 2 secret secret 4096 Oct 15 08:43 ex

These 4kB inlcudes space for having a "list" of the file in that directory. But if the number of files in the directory becomes significantly large, the size of the folder can increase (which is where I am). I need to be able to track this increase. So my question is that besides calling ls -l or du from C++ is there a C++-native way of getting the size of the directory? I am aware that the reason it does not work with std::filesystem::file_size(path) is due file systems different ways of representing directories.

dratenik

https://en.cppreference.com/w/cpp/filesystem/file_size

For a regular file p, returns the size determined as if by reading the st_size member of the structure obtained by POSIX stat (symlinks are followed)

The result of attempting to determine the size of a directory (as well as any other file that is not a regular file or a symlink) is implementation-defined.

file_size on a directory may actually work in some implementations, but I guess that yours doesn't. There doesn't appear to be a pure C++ alterantive, but you can call the POSIX stat function yourself. That does work on directories and reports the number you want.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Get size of directory (including all its content) irrespective of disk usage

From Dev

How to get a border to size to its content with varying data and footer to adjust

From Dev

puppet copy directory but not its content

From Dev

JTextPane - fit size to its content

From Dev

Make a div the size of its content

From Dev

Get cell by its content

From Dev

How do you get a container (ie grid) to fit the rotated size of its content?

From Dev

How do you get a container (ie grid) to fit the rotated size of its content?

From Dev

Lock or unlock a directory and its content recursively

From Dev

How to remove a directory without removing its content?

From Dev

Add specific directory and its content to Universal target

From Dev

Copy recursively a directory excluding a subdirectory and ITS CONTENT

From Dev

delete directory object and its content in powershell

From Dev

How to remove a directory without removing its content?

From Dev

archive a directory and its content to a zip archive

From Dev

Add specific directory and its content to Universal target

From Dev

Robocopy copy directory instead of just its content

From Dev

Adjust size of an SVG-element to its content

From Java

How do I size a UITextView to its content?

From Dev

How to get directory total size?

From Dev

Get directory size from WebHDFS?

From Dev

Get directory size from WebHDFS?

From Dev

Redirecting the content of a directory, but not the directory itself as well as some of its subdirectories and files

From Dev

How to get directory content with Express?

From Dev

Get content view size in onCreate

From Dev

JavaScript get size of content box

From Dev

JavaScript get size of content box

From Dev

UIWebView : Get Webview Content size?

From Dev

_UIReplicantView and snapshotViewAfterScreenUpdates how to get its content?

Related Related

  1. 1

    Get size of directory (including all its content) irrespective of disk usage

  2. 2

    How to get a border to size to its content with varying data and footer to adjust

  3. 3

    puppet copy directory but not its content

  4. 4

    JTextPane - fit size to its content

  5. 5

    Make a div the size of its content

  6. 6

    Get cell by its content

  7. 7

    How do you get a container (ie grid) to fit the rotated size of its content?

  8. 8

    How do you get a container (ie grid) to fit the rotated size of its content?

  9. 9

    Lock or unlock a directory and its content recursively

  10. 10

    How to remove a directory without removing its content?

  11. 11

    Add specific directory and its content to Universal target

  12. 12

    Copy recursively a directory excluding a subdirectory and ITS CONTENT

  13. 13

    delete directory object and its content in powershell

  14. 14

    How to remove a directory without removing its content?

  15. 15

    archive a directory and its content to a zip archive

  16. 16

    Add specific directory and its content to Universal target

  17. 17

    Robocopy copy directory instead of just its content

  18. 18

    Adjust size of an SVG-element to its content

  19. 19

    How do I size a UITextView to its content?

  20. 20

    How to get directory total size?

  21. 21

    Get directory size from WebHDFS?

  22. 22

    Get directory size from WebHDFS?

  23. 23

    Redirecting the content of a directory, but not the directory itself as well as some of its subdirectories and files

  24. 24

    How to get directory content with Express?

  25. 25

    Get content view size in onCreate

  26. 26

    JavaScript get size of content box

  27. 27

    JavaScript get size of content box

  28. 28

    UIWebView : Get Webview Content size?

  29. 29

    _UIReplicantView and snapshotViewAfterScreenUpdates how to get its content?

HotTag

Archive