Use dynamic calling for fallocate to allow sparse to compile in old Linux.

This commit is contained in:
2021-05-11 21:56:18 +01:00
parent 4e60409954
commit 056af1e1fc
3 changed files with 57 additions and 8 deletions

View File

@@ -26,6 +26,6 @@ if(NOT ${CMAKE_SYSTEM_NAME} MATCHES "Linux")
return()
endif()
add_sources(attr.c sparse.c xattr.c xattr.h volume.c volume.h)
add_sources(attr.c sparse.c sparse.h xattr.c xattr.h volume.c volume.h)
add_sub_libraries(dl)

View File

@@ -24,6 +24,7 @@ Copyright (C) 2011-2021 Natalia Portillo
#include "../../include/consts.h"
#define _GNU_SOURCE
#include <dlfcn.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
@@ -34,14 +35,24 @@ Copyright (C) 2011-2021 Natalia Portillo
#include "../../log.h"
#include "linux.h"
#include "sparse.h"
void LinuxSparse(const char* path)
{
int ret;
int rc, wRc, cRc, zRc;
FILE* h;
int i;
int fd;
int ret;
int rc, wRc, cRc, zRc;
FILE* h;
int i;
int fd;
_linux_fallocate linux_fallocate;
linux_fallocate = (_linux_fallocate)dlsym(RTLD_DEFAULT, "fallocate");
if(!linux_fallocate)
{
log_write("Error loading fallocate(2) from libc: %s\n", dlerror());
return;
}
ret = chdir(path);
@@ -88,7 +99,7 @@ void LinuxSparse(const char* path)
}
fd = fileno(h);
ret = fallocate(fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, 4096, 8192);
ret = linux_fallocate(fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, 4096, 8192);
if(ret) { zRc = errno; }
ret = fclose(h);
@@ -122,7 +133,7 @@ void LinuxSparse(const char* path)
}
fd = fileno(h);
ret = fallocate(fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, 32768, 81920);
ret = linux_fallocate(fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, 32768, 81920);
if(ret) { zRc = errno; }
ret = fclose(h);

View File

@@ -0,0 +1,38 @@
/****************************************************************************
Aaru Data Preservation Suite
-----------------------------------------------------------------------------
Author(s) : Natalia Portillo
--[ License ] ---------------------------------------------------------------
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
-----------------------------------------------------------------------------
Copyright (C) 2011-2021 Natalia Portillo
*****************************************************************************/
#ifndef AARU_FSTESTER_SETTER_SRC_LINUX_SPARSE_H_
#define AARU_FSTESTER_SETTER_SRC_LINUX_SPARSE_H_
#ifndef FALLOC_FL_KEEP_SIZE
#define FALLOC_FL_KEEP_SIZE 0x01
#endif
#ifndef FALLOC_FL_PUNCH_HOLE
#define FALLOC_FL_PUNCH_HOLE 0x02
#endif
typedef int (*_linux_fallocate)(int fd, int mode, off_t offset, off_t len);
#endif // AARU_FSTESTER_SETTER_SRC_LINUX_SPARSE_H_