Jelajahi Sumber

libobs/util: Add function to get free space

Meant as a part of solving mantis issue 105 ("Disk space usage monitor
when recording").

From pull request: jp9000/obs-studio#374
Relevant mantis issue: https://obsproject.com/mantis/view.php?id=105
adray 10 tahun lalu
induk
melakukan
e354a433c7
3 mengubah file dengan 32 tambahan dan 0 penghapusan
  1. 12 0
      libobs/util/platform-nix.c
  2. 19 0
      libobs/util/platform-windows.c
  3. 1 0
      libobs/util/platform.h

+ 12 - 0
libobs/util/platform-nix.c

@@ -18,6 +18,7 @@
 #include <errno.h>
 #include <errno.h>
 #include <sys/types.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <sys/stat.h>
+#include <sys/statvfs.h>
 #include <dirent.h>
 #include <dirent.h>
 #include <stdlib.h>
 #include <stdlib.h>
 #include <limits.h>
 #include <limits.h>
@@ -333,6 +334,17 @@ void os_closedir(os_dir_t *dir)
 	}
 	}
 }
 }
 
 
+int64_t os_get_free_space(const char *path)
+{
+	struct statvfs info;
+	int64_t ret = (int64_t)statvfs(path, &info);
+
+	if (ret == 0)
+		ret = (int64_t)info.f_bsize * (int64_t)info.f_bfree;
+
+	return ret;
+}
+
 struct posix_glob_info {
 struct posix_glob_info {
 	struct os_glob_info base;
 	struct os_glob_info base;
 	glob_t gl;
 	glob_t gl;

+ 19 - 0
libobs/util/platform-windows.c

@@ -362,6 +362,25 @@ void os_closedir(os_dir_t *dir)
 	}
 	}
 }
 }
 
 
+int64_t os_get_free_space(const char *path)
+{
+	ULARGE_INTEGER  remainingSpace;
+	char            abs_path[512];
+	wchar_t         w_abs_path[512];
+
+	if (os_get_abs_path(path, abs_path, 512) > 0) {
+		if (os_utf8_to_wcs(abs_path, 0, w_abs_path, 512) > 0) {
+			BOOL success = GetDiskFreeSpaceExW(w_abs_path,
+					(PULARGE_INTEGER)&remainingSpace,
+					NULL, NULL);
+			if (success)
+				return (int64_t)remainingSpace.QuadPart;
+		}
+	}
+
+	return -1;
+}
+
 static void make_globent(struct os_globent *ent, WIN32_FIND_DATA *wfd,
 static void make_globent(struct os_globent *ent, WIN32_FIND_DATA *wfd,
 		const char *pattern)
 		const char *pattern)
 {
 {

+ 1 - 0
libobs/util/platform.h

@@ -52,6 +52,7 @@ EXPORT bool os_quick_write_mbs_file(const char *path, const char *str,
 		size_t len);
 		size_t len);
 
 
 EXPORT int64_t os_get_file_size(const char *path);
 EXPORT int64_t os_get_file_size(const char *path);
+EXPORT int64_t os_get_free_space(const char *path);
 
 
 EXPORT size_t os_mbs_to_wcs(const char *str, size_t str_len, wchar_t *dst,
 EXPORT size_t os_mbs_to_wcs(const char *str, size_t str_len, wchar_t *dst,
 		size_t dst_size);
 		size_t dst_size);