in src/utils/system_utils/src/system_utils.c [471:558]
int ADUC_SystemUtils_CopyFileToDir(const char* filePath, const char* dirPath, const bool overwriteExistingFile)
{
int result = -1;
STRING_HANDLE destFilePath = NULL;
FILE* sourceFile = NULL;
FILE* destFile = NULL;
unsigned char readBuff[1024];
const size_t readMaxBuffSize = ARRAY_SIZE(readBuff);
memset(readBuff, 0, readMaxBuffSize);
if (filePath == NULL || dirPath == NULL)
{
goto done;
}
if (!ADUC_SystemUtils_FormatFilePathHelper(&destFilePath, filePath, dirPath))
{
goto done;
}
sourceFile = fopen(filePath, "rb");
if (sourceFile == NULL)
{
goto done;
}
if (overwriteExistingFile)
{
destFile = fopen(STRING_c_str(destFilePath), "wb+");
}
else
{
destFile = fopen(STRING_c_str(destFilePath), "wb");
}
if (destFile == NULL)
{
goto done;
}
size_t readBytes = fread(readBuff, readMaxBuffSize, sizeof(readBuff[0]), sourceFile);
while (readBytes != 0 && feof(sourceFile) != 0)
{
const size_t writtenBytes = fwrite(readBuff, readBytes, sizeof(readBuff[0]), destFile);
result = ferror(destFile);
if (writtenBytes != readBytes || result != 0)
{
goto done;
}
readBytes = fread(readBuff, readMaxBuffSize, sizeof(readBuff[0]), sourceFile);
result = ferror(sourceFile);
if (result != 0)
{
goto done;
}
}
struct stat buff;
if (stat(filePath, &buff) != 0)
{
goto done;
}
if (ADUCPAL_chmod(STRING_c_str(destFilePath), buff.st_mode) != 0)
{
goto done;
}
result = 0;
done:
fclose(sourceFile);
fclose(destFile);
if (result != 0)
{
remove(STRING_c_str(destFilePath));
}
STRING_delete(destFilePath);
return result;
}