From 3827aae82786554e7e84a578f89106382b811b33 Mon Sep 17 00:00:00 2001 From: Natalia Portillo Date: Tue, 30 Mar 2021 02:29:00 +0100 Subject: [PATCH] Create sparse files on UNIX and when Linux does not have fallocate available. --- setter/src/linux/CMakeLists.txt | 2 +- setter/src/linux/sparse.c | 21 ++---- setter/src/unix/sparse.c | 124 +++++++++++++++++++++++++++++++- 3 files changed, 128 insertions(+), 19 deletions(-) diff --git a/setter/src/linux/CMakeLists.txt b/setter/src/linux/CMakeLists.txt index cdb8b18..556d3ff 100644 --- a/setter/src/linux/CMakeLists.txt +++ b/setter/src/linux/CMakeLists.txt @@ -37,4 +37,4 @@ set(EXECUTABLE_NAME "fssetter-${CMAKE_SYSTEM_NAME}-${CMAKE_SYSTEM_PROCESSOR}") add_executable(${EXECUTABLE_NAME} ${PLATFORM_SOURCES}) -target_link_libraries(${EXECUTABLE_NAME} core unix) \ No newline at end of file +target_link_libraries(${EXECUTABLE_NAME} core unix dl) \ No newline at end of file diff --git a/setter/src/linux/sparse.c b/setter/src/linux/sparse.c index e8dffed..03cbace 100644 --- a/setter/src/linux/sparse.c +++ b/setter/src/linux/sparse.c @@ -37,12 +37,11 @@ Copyright (C) 2011-2021 Natalia Portillo void LinuxSparse(const char* path) { - int ret; - int rc, wRc, cRc, zRc; - FILE* h; - unsigned char* buffer; - int i; - int fd; + int ret; + int rc, wRc, cRc, zRc; + FILE* h; + int i; + int fd; ret = chdir(path); @@ -78,9 +77,6 @@ void LinuxSparse(const char* path) if(h == NULL) { rc = errno; } else { - buffer = malloc(4096 * 3); - memset(buffer, 0, 4096 * 3); - for(i = 0; i < 4096 * 3; i += CLAUNIA_SIZE) { ret = fwrite(clauniaBytes, CLAUNIA_SIZE, 1, h); @@ -97,12 +93,11 @@ void LinuxSparse(const char* path) ret = fclose(h); if(ret) { cRc = errno; } - free(buffer); } log_write("\tFile name = \"%s\", size = %d, rc = %d, wRc = %d, cRc = %d, sRc = %d\n", "SMALL", - 4096 * 3, + 4096 * 4, rc, wRc, cRc, @@ -116,9 +111,6 @@ void LinuxSparse(const char* path) if(h == NULL) { rc = errno; } else { - buffer = malloc(4096 * 30); - memset(buffer, 0, 4096 * 30); - for(i = 0; i < 4096 * 30; i += CLAUNIA_SIZE) { ret = fwrite(clauniaBytes, CLAUNIA_SIZE, 1, h); @@ -135,7 +127,6 @@ void LinuxSparse(const char* path) ret = fclose(h); if(ret) { cRc = errno; } - free(buffer); } log_write("\tFile name = \"%s\", size = %d, rc = %d, wRc = %d, cRc = %d, sRc = %d\n", diff --git a/setter/src/unix/sparse.c b/setter/src/unix/sparse.c index fb2559f..6908991 100644 --- a/setter/src/unix/sparse.c +++ b/setter/src/unix/sparse.c @@ -22,15 +22,133 @@ Aaru Data Preservation Suite Copyright (C) 2011-2021 Natalia Portillo *****************************************************************************/ -#include "../include/defs.h" - #if defined(__linux__) || defined(__LINUX__) || defined(__gnu_linux) +#define _GNU_SOURCE + +#include + #include "../linux/linux.h" #endif +#include +#include +#include +#include + +#include "../include/consts.h" +#include "../include/defs.h" +#include "../log.h" + void Sparse(const char* path) { + int ret; + int rc, wRc, cRc; + FILE* h; + int i; + #if defined(__linux__) || defined(__LINUX__) || defined(__gnu_linux) - LinuxSparse(path); + void* linux_fallocate; + + linux_fallocate = dlsym(RTLD_DEFAULT, "fallocate"); + + if(linux_fallocate) + { + LinuxSparse(path); + + return; + } #endif + + ret = chdir(path); + + if(ret) + { + log_write("Error %d changing to specified path.\n", errno); + return; + } + + ret = mkdir("SPARSE", 0755); + + if(ret) + { + log_write("Error %d creating working directory.\n", errno); + return; + } + + ret = chdir("SPARSE"); + + if(ret) + { + log_write("Error %d changing to working directory.\n", errno); + return; + } + + log_write("Creating sparse files.\n"); + + h = fopen("SMALL", "w+"); + rc = 0; + wRc = 0; + cRc = 0; + if(h == NULL) { rc = errno; } + else + { + for(i = 0; i < 4096; i += CLAUNIA_SIZE) + { + ret = fwrite(clauniaBytes, CLAUNIA_SIZE, 1, h); + if(ret < 0) + { + wRc = errno; + break; + } + } + + fseek(h, 8192, SEEK_CUR); + + for(i = 4096 + 8192; i < 4096 * 4; i += CLAUNIA_SIZE) + { + ret = fwrite(clauniaBytes, CLAUNIA_SIZE, 1, h); + if(ret < 0) + { + wRc = errno; + break; + } + } + + ret = fclose(h); + if(ret) { cRc = errno; } + } + + log_write("\tFile name = \"%s\", size = %d, rc = %d, wRc = %d, cRc = %d\n", "SMALL", 4096 * 4, rc, wRc, cRc); + + h = fopen("BIG", "w+"); + rc = 0; + wRc = 0; + cRc = 0; + if(h == NULL) { rc = errno; } + else + { + for(i = 0; i < 4096 * 8; i += CLAUNIA_SIZE) + { + ret = fwrite(clauniaBytes, CLAUNIA_SIZE, 1, h); + if(ret < 0) + { + wRc = errno; + break; + } + } + + fseek(h, 81920, SEEK_CUR); + + for(i = 32768 + 81920; i < 4096 * 30; i += CLAUNIA_SIZE) + { + ret = fwrite(clauniaBytes, CLAUNIA_SIZE, 1, h); + if(ret < 0) + { + wRc = errno; + break; + } + } + } + + log_write("\tFile name = \"%s\", size = %d, rc = %d, wRc = %d, cRc = %d\n", "BIG", 4096 * 30, rc, wRc, cRc); }