/* * Copyright (C) Tristan Franssen, . * * This software is licensed under the Apache License, Version 2.0 (the "License"); * You may not use this file except in compliance with the License. * You may obtain a copy of the License in the file LICENSE or at * http://www.apache.org/licenses/LICENSE-2.0 */ #include "tool-file.h" #include #include #include #define COSMS_CORE_FILE_TOOL_LARGE_FILE_SIZE 5242880000ULL #define CHUNK 4096 #define LARGE_FILE_PATH "tests/data/large-file.bin" #define SMALL_DELETE_FILE_PATH "tests/data/small-delete-file.bin" #define LARGE_DELETE_FILE_PATH "tests/data/large-delete-file.bin" /** * @brief Generates a large nearly 5GB file for testing. */ int cosms_core_tool_file_generate_large_file(void) { FILE *file = fopen(LARGE_FILE_PATH, "wb"); if (file == NULL) { return -1; } static char buffer[CHUNK]; for (unsigned int index = 0; index < CHUNK; index += 1) { buffer[index] = 'a'; } unsigned long long written_bytes = 0; while (written_bytes < COSMS_CORE_FILE_TOOL_LARGE_FILE_SIZE) { unsigned int bytes_to_write = CHUNK; if (written_bytes + CHUNK > COSMS_CORE_FILE_TOOL_LARGE_FILE_SIZE) { bytes_to_write = (unsigned int)(COSMS_CORE_FILE_TOOL_LARGE_FILE_SIZE - written_bytes); } fwrite(buffer, 1, bytes_to_write, file); written_bytes += bytes_to_write; } fclose(file); return 0; } /** * @brief Generates files to be deleted. */ int cosms_core_tool_file_generate_delete_file(void) { FILE *file = fopen(SMALL_DELETE_FILE_PATH, "wb"); if (file == NULL) { return -1; } char *small_buffer = "Hello, World!"; fwrite(small_buffer, 1, strlen(small_buffer), file); fclose(file); file = fopen(LARGE_DELETE_FILE_PATH, "wb"); if (file == NULL) { return -1; } static char large_buffer[CHUNK]; for (unsigned int index = 0; index < CHUNK; index += 1) { large_buffer[index] = 'a'; } unsigned long long written_bytes = 0; while (written_bytes < COSMS_CORE_FILE_TOOL_LARGE_FILE_SIZE) { unsigned int bytes_to_write = CHUNK; if (written_bytes + CHUNK > COSMS_CORE_FILE_TOOL_LARGE_FILE_SIZE) { bytes_to_write = (unsigned int)(COSMS_CORE_FILE_TOOL_LARGE_FILE_SIZE - written_bytes); } fwrite(large_buffer, 1, bytes_to_write, file); written_bytes += bytes_to_write; } fclose(file); return 0; }