0

I've been said to write a program in c++ to get every hdd and partition's total, free and used size. I can use commands to do this.
I searched and I understood that there are two ways of getting size, lsblk and df. I know there are differences between outputs because of : why df get result inconsistent with lsblk?
so which one should I chose?(also I can't have used and free sizes in lsblk)
I use ubuntu 18.04.

fa7eme
  • 43

1 Answers1

0

As you are writing a C++ program, I'd recommend a third and fourth way:

  • Get partition total size from /sys/block/sd*/sd*/size.
  • Get filesystem used size and filesystem free size by calling statfs, see man statfs for details. Yes, this only works when the partition is mounted.

Faster, and you don't have to parse output, and you are not screwed in the (unlikely) event that df or lsblk decide to change their output format.

dirkt
  • 32,309