Determine free space available on a USB flash drive in C (linux)
I would like to figure out the available free space on a USB flash drive in a C program in Linux. I have earlier used system("df -h /dev/sda1")
to figure it out but using system command in a Linux C application is causing some timing issues. Hence need to know how to determine available free space using any other kind of system call/library function.
linux usb usb-drive c system-calls
add a comment |
I would like to figure out the available free space on a USB flash drive in a C program in Linux. I have earlier used system("df -h /dev/sda1")
to figure it out but using system command in a Linux C application is causing some timing issues. Hence need to know how to determine available free space using any other kind of system call/library function.
linux usb usb-drive c system-calls
2
Either I don't understand what you want, or you left out the most important word in your question. "Hence need to know how to determine available free space using any kind of system call" doesn't make sense since you are already using a system call.
– pipe
21 hours ago
The questioner is asking about system calls, not about thesystem
call. They are asking what libraries exist, e.g. can they ask the kernel. However this may be off topic, as it is a programming question.
– ctrl-alt-delor
19 hours ago
"using system command in a Linux C application is causing some timing issues". I would suggest that you elaborate on this, because otherwise someone may suggest a solution that causes these same timing issues.
– roaima
19 hours ago
add a comment |
I would like to figure out the available free space on a USB flash drive in a C program in Linux. I have earlier used system("df -h /dev/sda1")
to figure it out but using system command in a Linux C application is causing some timing issues. Hence need to know how to determine available free space using any other kind of system call/library function.
linux usb usb-drive c system-calls
I would like to figure out the available free space on a USB flash drive in a C program in Linux. I have earlier used system("df -h /dev/sda1")
to figure it out but using system command in a Linux C application is causing some timing issues. Hence need to know how to determine available free space using any other kind of system call/library function.
linux usb usb-drive c system-calls
linux usb usb-drive c system-calls
edited 21 hours ago
OpenSourceEnthusiast
asked yesterday
OpenSourceEnthusiastOpenSourceEnthusiast
316
316
2
Either I don't understand what you want, or you left out the most important word in your question. "Hence need to know how to determine available free space using any kind of system call" doesn't make sense since you are already using a system call.
– pipe
21 hours ago
The questioner is asking about system calls, not about thesystem
call. They are asking what libraries exist, e.g. can they ask the kernel. However this may be off topic, as it is a programming question.
– ctrl-alt-delor
19 hours ago
"using system command in a Linux C application is causing some timing issues". I would suggest that you elaborate on this, because otherwise someone may suggest a solution that causes these same timing issues.
– roaima
19 hours ago
add a comment |
2
Either I don't understand what you want, or you left out the most important word in your question. "Hence need to know how to determine available free space using any kind of system call" doesn't make sense since you are already using a system call.
– pipe
21 hours ago
The questioner is asking about system calls, not about thesystem
call. They are asking what libraries exist, e.g. can they ask the kernel. However this may be off topic, as it is a programming question.
– ctrl-alt-delor
19 hours ago
"using system command in a Linux C application is causing some timing issues". I would suggest that you elaborate on this, because otherwise someone may suggest a solution that causes these same timing issues.
– roaima
19 hours ago
2
2
Either I don't understand what you want, or you left out the most important word in your question. "Hence need to know how to determine available free space using any kind of system call" doesn't make sense since you are already using a system call.
– pipe
21 hours ago
Either I don't understand what you want, or you left out the most important word in your question. "Hence need to know how to determine available free space using any kind of system call" doesn't make sense since you are already using a system call.
– pipe
21 hours ago
The questioner is asking about system calls, not about the
system
call. They are asking what libraries exist, e.g. can they ask the kernel. However this may be off topic, as it is a programming question.– ctrl-alt-delor
19 hours ago
The questioner is asking about system calls, not about the
system
call. They are asking what libraries exist, e.g. can they ask the kernel. However this may be off topic, as it is a programming question.– ctrl-alt-delor
19 hours ago
"using system command in a Linux C application is causing some timing issues". I would suggest that you elaborate on this, because otherwise someone may suggest a solution that causes these same timing issues.
– roaima
19 hours ago
"using system command in a Linux C application is causing some timing issues". I would suggest that you elaborate on this, because otherwise someone may suggest a solution that causes these same timing issues.
– roaima
19 hours ago
add a comment |
1 Answer
1
active
oldest
votes
For a mounted USB flash drive, you can do this via statvfs(3) function, where you need to provide path to the mountpoint, and you basically have a small version of df
(which also btw only operates on mounted filesystems):
$ cat fs_usage.c
#include <stdio.h>
#include <sys/statvfs.h>
int main(int argc, char **argv){
struct statvfs fs_usage;
statvfs(argv[1],&fs_usage);
printf("%s:%f bytes available, %f bytes usedn",argv[1],
fs_usage.f_frsize*(double)fs_usage.f_bavail,
fs_usage.f_frsize * (double)(fs_usage.f_blocks - fs_usage.f_bfree));
return 0;
}
$ gcc fs_usage.c -o fs_usage
$ df -B 1 /mnt/ubuntu
Filesystem 1B-blocks Used Available Use% Mounted on
/dev/sdb1 118013599744 105134329856 6860865536 94% /mnt/ubuntu
$ ./fs_usage /mnt/ubuntu/
/mnt/ubuntu/:6860865536.000000 bytes available, 105134329856.000000 bytes used
Note also that statvfs()
takes const char *path
as one of the parameters, and that can be pathname of any file within the filesystem, e.g. /dev/sda7
will return usage of /dev
filesystem ( because it is in fact one of virtual filesystems ), and not the sda7
partition of a device.
Note that I am using f_frsize
here, which is equivalent to f_bsize
, however in some filesystems fragment size may be smaller than block size. See https://unix.stackexchange.com/a/463370/85039 for details
statvfs(3) is not a system call; it's a library function implemented on top of the statfs(2) (without v) system call.
– Uncle Billy
yesterday
@UncleBilly Thanks, corrected
– Sergiy Kolodyazhnyy
yesterday
1
maybe you should mention that statvfs it will work on any file contained in the fs, not just the mount point --statvfs("/dev/sda7", &vfs)
will return info about the devtmpfs file system, not about the filesystem contained in/dev/sda7
.
– Uncle Billy
yesterday
1
@OpenSourceEnthusiast Did you rundf
with-B 1
or without ? By default GNUdf
defaults to blocks of 1024 bytes, but with-B 1
block size is set to 1 byte.
– Sergiy Kolodyazhnyy
yesterday
1
@OpenSourceEnthusiastprintf("%llun", (uint64_t)vfs.f_bavail * vfs.f_frsize)
will get you the value down to the last byte; it's up to you to round it as you see fit (using 2^20 or 10^6 for M, etc).
– Uncle Billy
yesterday
|
show 10 more comments
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f499212%2fdetermine-free-space-available-on-a-usb-flash-drive-in-c-linux%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
For a mounted USB flash drive, you can do this via statvfs(3) function, where you need to provide path to the mountpoint, and you basically have a small version of df
(which also btw only operates on mounted filesystems):
$ cat fs_usage.c
#include <stdio.h>
#include <sys/statvfs.h>
int main(int argc, char **argv){
struct statvfs fs_usage;
statvfs(argv[1],&fs_usage);
printf("%s:%f bytes available, %f bytes usedn",argv[1],
fs_usage.f_frsize*(double)fs_usage.f_bavail,
fs_usage.f_frsize * (double)(fs_usage.f_blocks - fs_usage.f_bfree));
return 0;
}
$ gcc fs_usage.c -o fs_usage
$ df -B 1 /mnt/ubuntu
Filesystem 1B-blocks Used Available Use% Mounted on
/dev/sdb1 118013599744 105134329856 6860865536 94% /mnt/ubuntu
$ ./fs_usage /mnt/ubuntu/
/mnt/ubuntu/:6860865536.000000 bytes available, 105134329856.000000 bytes used
Note also that statvfs()
takes const char *path
as one of the parameters, and that can be pathname of any file within the filesystem, e.g. /dev/sda7
will return usage of /dev
filesystem ( because it is in fact one of virtual filesystems ), and not the sda7
partition of a device.
Note that I am using f_frsize
here, which is equivalent to f_bsize
, however in some filesystems fragment size may be smaller than block size. See https://unix.stackexchange.com/a/463370/85039 for details
statvfs(3) is not a system call; it's a library function implemented on top of the statfs(2) (without v) system call.
– Uncle Billy
yesterday
@UncleBilly Thanks, corrected
– Sergiy Kolodyazhnyy
yesterday
1
maybe you should mention that statvfs it will work on any file contained in the fs, not just the mount point --statvfs("/dev/sda7", &vfs)
will return info about the devtmpfs file system, not about the filesystem contained in/dev/sda7
.
– Uncle Billy
yesterday
1
@OpenSourceEnthusiast Did you rundf
with-B 1
or without ? By default GNUdf
defaults to blocks of 1024 bytes, but with-B 1
block size is set to 1 byte.
– Sergiy Kolodyazhnyy
yesterday
1
@OpenSourceEnthusiastprintf("%llun", (uint64_t)vfs.f_bavail * vfs.f_frsize)
will get you the value down to the last byte; it's up to you to round it as you see fit (using 2^20 or 10^6 for M, etc).
– Uncle Billy
yesterday
|
show 10 more comments
For a mounted USB flash drive, you can do this via statvfs(3) function, where you need to provide path to the mountpoint, and you basically have a small version of df
(which also btw only operates on mounted filesystems):
$ cat fs_usage.c
#include <stdio.h>
#include <sys/statvfs.h>
int main(int argc, char **argv){
struct statvfs fs_usage;
statvfs(argv[1],&fs_usage);
printf("%s:%f bytes available, %f bytes usedn",argv[1],
fs_usage.f_frsize*(double)fs_usage.f_bavail,
fs_usage.f_frsize * (double)(fs_usage.f_blocks - fs_usage.f_bfree));
return 0;
}
$ gcc fs_usage.c -o fs_usage
$ df -B 1 /mnt/ubuntu
Filesystem 1B-blocks Used Available Use% Mounted on
/dev/sdb1 118013599744 105134329856 6860865536 94% /mnt/ubuntu
$ ./fs_usage /mnt/ubuntu/
/mnt/ubuntu/:6860865536.000000 bytes available, 105134329856.000000 bytes used
Note also that statvfs()
takes const char *path
as one of the parameters, and that can be pathname of any file within the filesystem, e.g. /dev/sda7
will return usage of /dev
filesystem ( because it is in fact one of virtual filesystems ), and not the sda7
partition of a device.
Note that I am using f_frsize
here, which is equivalent to f_bsize
, however in some filesystems fragment size may be smaller than block size. See https://unix.stackexchange.com/a/463370/85039 for details
statvfs(3) is not a system call; it's a library function implemented on top of the statfs(2) (without v) system call.
– Uncle Billy
yesterday
@UncleBilly Thanks, corrected
– Sergiy Kolodyazhnyy
yesterday
1
maybe you should mention that statvfs it will work on any file contained in the fs, not just the mount point --statvfs("/dev/sda7", &vfs)
will return info about the devtmpfs file system, not about the filesystem contained in/dev/sda7
.
– Uncle Billy
yesterday
1
@OpenSourceEnthusiast Did you rundf
with-B 1
or without ? By default GNUdf
defaults to blocks of 1024 bytes, but with-B 1
block size is set to 1 byte.
– Sergiy Kolodyazhnyy
yesterday
1
@OpenSourceEnthusiastprintf("%llun", (uint64_t)vfs.f_bavail * vfs.f_frsize)
will get you the value down to the last byte; it's up to you to round it as you see fit (using 2^20 or 10^6 for M, etc).
– Uncle Billy
yesterday
|
show 10 more comments
For a mounted USB flash drive, you can do this via statvfs(3) function, where you need to provide path to the mountpoint, and you basically have a small version of df
(which also btw only operates on mounted filesystems):
$ cat fs_usage.c
#include <stdio.h>
#include <sys/statvfs.h>
int main(int argc, char **argv){
struct statvfs fs_usage;
statvfs(argv[1],&fs_usage);
printf("%s:%f bytes available, %f bytes usedn",argv[1],
fs_usage.f_frsize*(double)fs_usage.f_bavail,
fs_usage.f_frsize * (double)(fs_usage.f_blocks - fs_usage.f_bfree));
return 0;
}
$ gcc fs_usage.c -o fs_usage
$ df -B 1 /mnt/ubuntu
Filesystem 1B-blocks Used Available Use% Mounted on
/dev/sdb1 118013599744 105134329856 6860865536 94% /mnt/ubuntu
$ ./fs_usage /mnt/ubuntu/
/mnt/ubuntu/:6860865536.000000 bytes available, 105134329856.000000 bytes used
Note also that statvfs()
takes const char *path
as one of the parameters, and that can be pathname of any file within the filesystem, e.g. /dev/sda7
will return usage of /dev
filesystem ( because it is in fact one of virtual filesystems ), and not the sda7
partition of a device.
Note that I am using f_frsize
here, which is equivalent to f_bsize
, however in some filesystems fragment size may be smaller than block size. See https://unix.stackexchange.com/a/463370/85039 for details
For a mounted USB flash drive, you can do this via statvfs(3) function, where you need to provide path to the mountpoint, and you basically have a small version of df
(which also btw only operates on mounted filesystems):
$ cat fs_usage.c
#include <stdio.h>
#include <sys/statvfs.h>
int main(int argc, char **argv){
struct statvfs fs_usage;
statvfs(argv[1],&fs_usage);
printf("%s:%f bytes available, %f bytes usedn",argv[1],
fs_usage.f_frsize*(double)fs_usage.f_bavail,
fs_usage.f_frsize * (double)(fs_usage.f_blocks - fs_usage.f_bfree));
return 0;
}
$ gcc fs_usage.c -o fs_usage
$ df -B 1 /mnt/ubuntu
Filesystem 1B-blocks Used Available Use% Mounted on
/dev/sdb1 118013599744 105134329856 6860865536 94% /mnt/ubuntu
$ ./fs_usage /mnt/ubuntu/
/mnt/ubuntu/:6860865536.000000 bytes available, 105134329856.000000 bytes used
Note also that statvfs()
takes const char *path
as one of the parameters, and that can be pathname of any file within the filesystem, e.g. /dev/sda7
will return usage of /dev
filesystem ( because it is in fact one of virtual filesystems ), and not the sda7
partition of a device.
Note that I am using f_frsize
here, which is equivalent to f_bsize
, however in some filesystems fragment size may be smaller than block size. See https://unix.stackexchange.com/a/463370/85039 for details
edited yesterday
answered yesterday
Sergiy KolodyazhnyySergiy Kolodyazhnyy
10.1k32660
10.1k32660
statvfs(3) is not a system call; it's a library function implemented on top of the statfs(2) (without v) system call.
– Uncle Billy
yesterday
@UncleBilly Thanks, corrected
– Sergiy Kolodyazhnyy
yesterday
1
maybe you should mention that statvfs it will work on any file contained in the fs, not just the mount point --statvfs("/dev/sda7", &vfs)
will return info about the devtmpfs file system, not about the filesystem contained in/dev/sda7
.
– Uncle Billy
yesterday
1
@OpenSourceEnthusiast Did you rundf
with-B 1
or without ? By default GNUdf
defaults to blocks of 1024 bytes, but with-B 1
block size is set to 1 byte.
– Sergiy Kolodyazhnyy
yesterday
1
@OpenSourceEnthusiastprintf("%llun", (uint64_t)vfs.f_bavail * vfs.f_frsize)
will get you the value down to the last byte; it's up to you to round it as you see fit (using 2^20 or 10^6 for M, etc).
– Uncle Billy
yesterday
|
show 10 more comments
statvfs(3) is not a system call; it's a library function implemented on top of the statfs(2) (without v) system call.
– Uncle Billy
yesterday
@UncleBilly Thanks, corrected
– Sergiy Kolodyazhnyy
yesterday
1
maybe you should mention that statvfs it will work on any file contained in the fs, not just the mount point --statvfs("/dev/sda7", &vfs)
will return info about the devtmpfs file system, not about the filesystem contained in/dev/sda7
.
– Uncle Billy
yesterday
1
@OpenSourceEnthusiast Did you rundf
with-B 1
or without ? By default GNUdf
defaults to blocks of 1024 bytes, but with-B 1
block size is set to 1 byte.
– Sergiy Kolodyazhnyy
yesterday
1
@OpenSourceEnthusiastprintf("%llun", (uint64_t)vfs.f_bavail * vfs.f_frsize)
will get you the value down to the last byte; it's up to you to round it as you see fit (using 2^20 or 10^6 for M, etc).
– Uncle Billy
yesterday
statvfs(3) is not a system call; it's a library function implemented on top of the statfs(2) (without v) system call.
– Uncle Billy
yesterday
statvfs(3) is not a system call; it's a library function implemented on top of the statfs(2) (without v) system call.
– Uncle Billy
yesterday
@UncleBilly Thanks, corrected
– Sergiy Kolodyazhnyy
yesterday
@UncleBilly Thanks, corrected
– Sergiy Kolodyazhnyy
yesterday
1
1
maybe you should mention that statvfs it will work on any file contained in the fs, not just the mount point --
statvfs("/dev/sda7", &vfs)
will return info about the devtmpfs file system, not about the filesystem contained in /dev/sda7
.– Uncle Billy
yesterday
maybe you should mention that statvfs it will work on any file contained in the fs, not just the mount point --
statvfs("/dev/sda7", &vfs)
will return info about the devtmpfs file system, not about the filesystem contained in /dev/sda7
.– Uncle Billy
yesterday
1
1
@OpenSourceEnthusiast Did you run
df
with -B 1
or without ? By default GNU df
defaults to blocks of 1024 bytes, but with -B 1
block size is set to 1 byte.– Sergiy Kolodyazhnyy
yesterday
@OpenSourceEnthusiast Did you run
df
with -B 1
or without ? By default GNU df
defaults to blocks of 1024 bytes, but with -B 1
block size is set to 1 byte.– Sergiy Kolodyazhnyy
yesterday
1
1
@OpenSourceEnthusiast
printf("%llun", (uint64_t)vfs.f_bavail * vfs.f_frsize)
will get you the value down to the last byte; it's up to you to round it as you see fit (using 2^20 or 10^6 for M, etc).– Uncle Billy
yesterday
@OpenSourceEnthusiast
printf("%llun", (uint64_t)vfs.f_bavail * vfs.f_frsize)
will get you the value down to the last byte; it's up to you to round it as you see fit (using 2^20 or 10^6 for M, etc).– Uncle Billy
yesterday
|
show 10 more comments
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f499212%2fdetermine-free-space-available-on-a-usb-flash-drive-in-c-linux%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
Either I don't understand what you want, or you left out the most important word in your question. "Hence need to know how to determine available free space using any kind of system call" doesn't make sense since you are already using a system call.
– pipe
21 hours ago
The questioner is asking about system calls, not about the
system
call. They are asking what libraries exist, e.g. can they ask the kernel. However this may be off topic, as it is a programming question.– ctrl-alt-delor
19 hours ago
"using system command in a Linux C application is causing some timing issues". I would suggest that you elaborate on this, because otherwise someone may suggest a solution that causes these same timing issues.
– roaima
19 hours ago