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.
This commit is contained in:
jp9000
2015-06-29 00:39:56 -07:00
parent 216baaf799
commit 09b78b12de
3 changed files with 54 additions and 0 deletions

View File

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

View File

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

View File

@@ -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