Browse Source

libobs/util: Add os_copyfile function

Adds a function to copy a file.  On unix-based systems, manually copies
the data with fopen/fread/fwrite/fclose.
jp9000 10 năm trước cách đây
mục cha
commit
09b78b12de
3 tập tin đã thay đổi với 54 bổ sung0 xóa
  1. 33 0
      libobs/util/platform-nix.c
  2. 20 0
      libobs/util/platform-windows.c
  3. 1 0
      libobs/util/platform.h

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

@@ -327,3 +327,36 @@ void os_end_high_performance(os_performance_token_t *token)
 }
 #endif
 
+int os_copyfile(const char *file_path_in, const char *file_path_out)
+{
+	FILE *file_out = NULL;
+	FILE *file_in = NULL;
+	uint8_t data[4096];
+	int ret = -1;
+	size_t size;
+
+	if (os_file_exists(file_path_out))
+		return -1;
+
+	file_in = fopen(file_path_in, "rb");
+	if (!file_in)
+		return -1;
+
+	file_out = fopen(file_path_out, "ab+");
+	if (!file_out)
+		goto error;
+
+	do {
+		size = fread(data, 1, sizeof(data), file_in);
+		if (size)
+			size = fwrite(data, 1, size, file_out);
+	} while (size == sizeof(data));
+
+	ret = feof(file_in) ? 0 : -1;
+
+error:
+	if (file_out)
+		fclose(file_out);
+	fclose(file_in);
+	return ret;
+}

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

@@ -496,3 +496,23 @@ void os_end_high_performance(os_performance_token_t *token)
 	UNUSED_PARAMETER(token);
 }
 
+int os_copyfile(const char *file_in, const char *file_out)
+{
+	wchar_t *file_in_utf16 = NULL;
+	wchar_t *file_out_utf16 = NULL;
+	int code = -1;
+
+	if (!os_utf8_to_wcs_ptr(file_in, 0, &file_in_utf16)) {
+		return -1;
+	}
+	if (!os_utf8_to_wcs_ptr(file_out, 0, &file_out_utf16)) {
+		goto error;
+	}
+
+	code = CopyFileW(file_in_utf16, file_out_utf16, true) ? 0 : -1;
+
+error:
+	bfree(file_in_utf16);
+	bfree(file_out_utf16);
+	return code;
+}

+ 1 - 0
libobs/util/platform.h

@@ -136,6 +136,7 @@ EXPORT int os_rmdir(const char *path);
 
 EXPORT int os_mkdir(const char *path);
 EXPORT int os_rename(const char *old_path, const char *new_path);
+EXPORT int os_copyfile(const char *file_in, const char *file_out);
 
 #ifdef _MSC_VER
 #define strtoll _strtoi64