LCOV - code coverage report
Current view: top level - lib/fs - freespace.c (source / functions) Hit Total Coverage
Test: lcov.info Lines: 9 11 81.8 %
Date: 2021-11-24 03:28:48 Functions: 1 1 100.0 %

          Line data    Source code
       1             : /* Copyright (c) 2003-2004, Roger Dingledine
       2             :  * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
       3             :  * Copyright (c) 2007-2021, The Tor Project, Inc. */
       4             : /* See LICENSE for licensing information */
       5             : 
       6             : /**
       7             :  * \file freespace.c
       8             :  *
       9             :  * \brief Find the available disk space on the current volume.
      10             :  **/
      11             : 
      12             : #include "lib/fs/files.h"
      13             : #include "lib/cc/torint.h"
      14             : 
      15             : #ifdef HAVE_SYS_STATVFS_H
      16             : #include <sys/statvfs.h>
      17             : #endif
      18             : #ifdef _WIN32
      19             : #include <windows.h>
      20             : #endif
      21             : 
      22             : #include <errno.h>
      23             : #include <string.h>
      24             : 
      25             : /** Return the amount of free disk space we have permission to use, in
      26             :  * bytes. Return -1 if the amount of free space can't be determined. */
      27             : int64_t
      28           2 : tor_get_avail_disk_space(const char *path)
      29             : {
      30             : #ifdef HAVE_STATVFS
      31           2 :   struct statvfs st;
      32           2 :   int r;
      33           2 :   memset(&st, 0, sizeof(st));
      34             : 
      35           2 :   r = statvfs(path, &st);
      36           2 :   if (r < 0)
      37             :     return -1;
      38             : 
      39           1 :   int64_t result = st.f_bavail;
      40           1 :   if (st.f_frsize) {
      41           1 :     result *= st.f_frsize;
      42           0 :   } else if (st.f_bsize) {
      43           0 :     result *= st.f_bsize;
      44             :   } else {
      45             :     return -1;
      46             :   }
      47             : 
      48             :   return result;
      49             : #elif defined(_WIN32)
      50             :   ULARGE_INTEGER freeBytesAvail;
      51             :   BOOL ok;
      52             : 
      53             :   ok = GetDiskFreeSpaceEx(path, &freeBytesAvail, NULL, NULL);
      54             :   if (!ok) {
      55             :     return -1;
      56             :   }
      57             :   return (int64_t)freeBytesAvail.QuadPart;
      58             : #else
      59             :   (void)path;
      60             :   errno = ENOSYS;
      61             :   return -1;
      62             : #endif /* defined(HAVE_STATVFS) || ... */
      63             : }

Generated by: LCOV version 1.14