Tor  0.4.7.0-alpha-dev
freespace.c
Go to the documentation of this file.
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 tor_get_avail_disk_space(const char *path)
29 {
30 #ifdef HAVE_STATVFS
31  struct statvfs st;
32  int r;
33  memset(&st, 0, sizeof(st));
34 
35  r = statvfs(path, &st);
36  if (r < 0)
37  return -1;
38 
39  int64_t result = st.f_bavail;
40  if (st.f_frsize) {
41  result *= st.f_frsize;
42  } else if (st.f_bsize) {
43  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 }
Wrappers for reading and writing data to files on disk.
int64_t tor_get_avail_disk_space(const char *path)
Definition: freespace.c:28
Integer definitions used throughout Tor.