Determine free space available on a USB flash drive in C (linux)












4















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 kind of system call.










share|improve this question





























    4















    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 kind of system call.










    share|improve this question



























      4












      4








      4








      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 kind of system call.










      share|improve this question
















      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 kind of system call.







      linux usb usb-drive c system-calls






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 3 hours ago









      PRY

      2,01931025




      2,01931025










      asked 3 hours ago









      OpenSourceEnthusiastOpenSourceEnthusiast

      335




      335






















          1 Answer
          1






          active

          oldest

          votes


















          3














          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






          share|improve this answer


























          • 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
            3 hours ago













          • @UncleBilly Thanks, corrected

            – Sergiy Kolodyazhnyy
            3 hours ago











          • When I ran the above code, I see some precision loss as compared to the values reported by df command. For instance, df reports available space as 21M and the above code reports available space as 20M. How do we get very accurate values for available space (till . precision)

            – OpenSourceEnthusiast
            3 hours ago






          • 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
            3 hours ago











          • @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
            2 hours ago











          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
          });


          }
          });














          draft saved

          draft discarded


















          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









          3














          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






          share|improve this answer


























          • 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
            3 hours ago













          • @UncleBilly Thanks, corrected

            – Sergiy Kolodyazhnyy
            3 hours ago











          • When I ran the above code, I see some precision loss as compared to the values reported by df command. For instance, df reports available space as 21M and the above code reports available space as 20M. How do we get very accurate values for available space (till . precision)

            – OpenSourceEnthusiast
            3 hours ago






          • 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
            3 hours ago











          • @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
            2 hours ago
















          3














          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






          share|improve this answer


























          • 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
            3 hours ago













          • @UncleBilly Thanks, corrected

            – Sergiy Kolodyazhnyy
            3 hours ago











          • When I ran the above code, I see some precision loss as compared to the values reported by df command. For instance, df reports available space as 21M and the above code reports available space as 20M. How do we get very accurate values for available space (till . precision)

            – OpenSourceEnthusiast
            3 hours ago






          • 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
            3 hours ago











          • @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
            2 hours ago














          3












          3








          3







          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






          share|improve this answer















          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







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 51 mins ago

























          answered 3 hours ago









          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
            3 hours ago













          • @UncleBilly Thanks, corrected

            – Sergiy Kolodyazhnyy
            3 hours ago











          • When I ran the above code, I see some precision loss as compared to the values reported by df command. For instance, df reports available space as 21M and the above code reports available space as 20M. How do we get very accurate values for available space (till . precision)

            – OpenSourceEnthusiast
            3 hours ago






          • 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
            3 hours ago











          • @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
            2 hours ago



















          • 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
            3 hours ago













          • @UncleBilly Thanks, corrected

            – Sergiy Kolodyazhnyy
            3 hours ago











          • When I ran the above code, I see some precision loss as compared to the values reported by df command. For instance, df reports available space as 21M and the above code reports available space as 20M. How do we get very accurate values for available space (till . precision)

            – OpenSourceEnthusiast
            3 hours ago






          • 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
            3 hours ago











          • @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
            2 hours ago

















          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
          3 hours ago







          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
          3 hours ago















          @UncleBilly Thanks, corrected

          – Sergiy Kolodyazhnyy
          3 hours ago





          @UncleBilly Thanks, corrected

          – Sergiy Kolodyazhnyy
          3 hours ago













          When I ran the above code, I see some precision loss as compared to the values reported by df command. For instance, df reports available space as 21M and the above code reports available space as 20M. How do we get very accurate values for available space (till . precision)

          – OpenSourceEnthusiast
          3 hours ago





          When I ran the above code, I see some precision loss as compared to the values reported by df command. For instance, df reports available space as 21M and the above code reports available space as 20M. How do we get very accurate values for available space (till . precision)

          – OpenSourceEnthusiast
          3 hours ago




          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
          3 hours ago





          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
          3 hours ago













          @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
          2 hours ago





          @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
          2 hours ago


















          draft saved

          draft discarded




















































          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.




          draft saved


          draft discarded














          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





















































          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







          Popular posts from this blog

          Liste der Baudenkmale in Friedland (Mecklenburg)

          Single-Malt-Whisky

          Czorneboh