Implement Sparse() for Linux.

This commit is contained in:
2019-04-29 00:07:02 +01:00
parent 840f6738e9
commit 2540c4cc2a
6 changed files with 318 additions and 235 deletions

View File

@@ -31,18 +31,21 @@ Copyright (C) 2011-2018 Natalia Portillo
#if defined(__linux__) || defined(__LINUX__) || defined(__gnu_linux)
#include "consts.h"
#define _GNU_SOURCE
#include "linux.h"
#include <errno.h>
#include <fcntl.h>
#include <features.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#if((__GLIBC__ == 2) && (__GLIBC_MINOR__ >= 3)) || (__GLIBC__ > 2)
#include "xattrs.h"
#include <sys/xattr.h>
#endif
@@ -112,4 +115,116 @@ void LinuxExtendedAttributes(const char *path)
#endif
}
void LinuxSparse(const char *path)
{
int ret;
int rc, wRc, cRc, zRc;
FILE * h;
unsigned char *buffer;
int i;
int fd;
ret = chdir(path);
if(ret)
{
printf("Error %d changing to specified path.\n", errno);
return;
}
ret = mkdir("SPARSE", 0755);
if(ret)
{
printf("Error %d creating working directory.\n", errno);
return;
}
ret = chdir("SPARSE");
if(ret)
{
printf("Error %d changing to working directory.\n", errno);
return;
}
printf("Creating sparse files.\n");
h = fopen("SMALL", "w+");
rc = 0;
wRc = 0;
cRc = 0;
zRc = 0;
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);
if(ret < 0)
{
wRc = errno;
break;
}
}
fd = fileno(h);
ret = fallocate(fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, 4096, 8192);
if(ret) { zRc = errno; }
ret = fclose(h);
if(ret) { cRc = errno; }
free(buffer);
}
printf("\tFile name = \"%s\", size = %d, rc = %d, wRc = %d, cRc = %d, sRc = %d\n",
"SMALL",
4096 * 3,
rc,
wRc,
cRc,
zRc);
h = fopen("BIG", "w+");
rc = 0;
wRc = 0;
cRc = 0;
zRc = 0;
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);
if(ret < 0)
{
wRc = errno;
break;
}
}
fd = fileno(h);
ret = fallocate(fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, 32768, 81920);
if(ret) { zRc = errno; }
ret = fclose(h);
if(ret) { cRc = errno; }
free(buffer);
}
printf("\tFile name = \"%s\", size = %d, rc = %d, wRc = %d, cRc = %d, sRc = %d\n",
"BIG",
4096 * 30,
rc,
wRc,
cRc,
zRc);
}
#endif