Implement FileAttributes() for Linux.

This commit is contained in:
2019-04-29 00:33:15 +01:00
parent 2540c4cc2a
commit 0a12af4a29
3 changed files with 440 additions and 1 deletions

View File

@@ -38,6 +38,7 @@ Copyright (C) 2011-2018 Natalia Portillo
#include <errno.h>
#include <fcntl.h>
#include <features.h>
#include <linux/fs.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -46,6 +47,7 @@ Copyright (C) 2011-2018 Natalia Portillo
#include <unistd.h>
#if((__GLIBC__ == 2) && (__GLIBC_MINOR__ >= 3)) || (__GLIBC__ > 2)
#include <sys/ioctl.h>
#include <sys/xattr.h>
#endif
@@ -227,4 +229,438 @@ void LinuxSparse(const char *path)
zRc);
}
void LinuxFileAttributes(const char *path)
{
int ret;
int fd;
FILE *h;
int rc;
int wRc;
int sRc;
int cRc;
int attr;
ret = chdir(path);
if(ret)
{
printf("Error %d changing to specified path.\n", errno);
return;
}
ret = mkdir("ATTRS", 0755);
if(ret)
{
printf("Error %d creating working directory.\n", errno);
return;
}
ret = chdir("ATTRS");
if(ret)
{
printf("Error %d changing to working directory.\n", errno);
return;
}
printf("Creating files with different flags (attributes).\n");
h = fopen("APPEND", "w+");
rc = 0;
wRc = 0;
sRc = 0;
cRc = 0;
attr = 0;
if(h == NULL) { rc = errno; }
else
{
attr |= FS_APPEND_FL;
fd = fileno(h);
ret = ioctl(fd, FS_IOC_SETFLAGS, &attr);
if(ret)
{
sRc = errno;
unlink("APPEND");
}
else
{
ret = fprintf(h, "This file is now append only");
if(ret < 1)
{
wRc = errno;
unlink("APPEND");
}
}
ret = fclose(h);
if(ret) { cRc = errno; }
}
printf("\tFile with append only flag, rc = %d, wRc = %d, sRc = %d, cRc = %d\n", rc, wRc, sRc, cRc);
h = fopen("COMPRESS", "w+");
rc = 0;
wRc = 0;
sRc = 0;
cRc = 0;
attr = 0;
if(h == NULL) { rc = errno; }
else
{
attr |= FS_COMPR_FL;
fd = fileno(h);
ret = ioctl(fd, FS_IOC_SETFLAGS, &attr);
if(ret)
{
sRc = errno;
unlink("COMPRESS");
}
else
{
ret = fprintf(h, "This file is now compressed");
if(ret < 1)
{
wRc = errno;
unlink("COMPRESS");
}
}
ret = fclose(h);
if(ret) { cRc = errno; }
}
printf("\tFile with compressed flag, rc = %d, wRc = %d, sRc = %d, cRc = %d\n", rc, wRc, sRc, cRc);
h = fopen("IMMUTABLE", "w+");
rc = 0;
wRc = 0;
sRc = 0;
cRc = 0;
attr = 0;
if(h == NULL) { rc = errno; }
else
{
attr |= FS_IMMUTABLE_FL;
fd = fileno(h);
ret = ioctl(fd, FS_IOC_SETFLAGS, &attr);
if(ret)
{
sRc = errno;
unlink("IMMUTABLE");
}
else
{
ret = fprintf(h, "This file is now immutable");
if(ret < 1)
{
wRc = errno;
unlink("IMMUTABLE");
}
}
ret = fclose(h);
if(ret) { cRc = errno; }
}
printf("\tFile with immutable flag, rc = %d, wRc = %d, sRc = %d, cRc = %d\n", rc, wRc, sRc, cRc);
h = fopen("JOURNALED", "w+");
rc = 0;
wRc = 0;
sRc = 0;
cRc = 0;
attr = 0;
if(h == NULL) { rc = errno; }
else
{
attr |= FS_JOURNAL_DATA_FL;
fd = fileno(h);
ret = ioctl(fd, FS_IOC_SETFLAGS, &attr);
if(ret)
{
sRc = errno;
unlink("JOURNALED");
}
else
{
ret = fprintf(h, "This file is now journaled");
if(ret < 1)
{
wRc = errno;
unlink("JOURNALED");
}
}
ret = fclose(h);
if(ret) { cRc = errno; }
}
printf("\tFile with journaled flag, rc = %d, wRc = %d, sRc = %d, cRc = %d\n", rc, wRc, sRc, cRc);
h = fopen("NOATIME", "w+");
rc = 0;
wRc = 0;
sRc = 0;
cRc = 0;
attr = 0;
if(h == NULL) { rc = errno; }
else
{
attr |= FS_NOATIME_FL;
fd = fileno(h);
ret = ioctl(fd, FS_IOC_SETFLAGS, &attr);
if(ret)
{
sRc = errno;
unlink("NOATIME");
}
else
{
ret = fprintf(h, "This file is now noatime");
if(ret < 1)
{
wRc = errno;
unlink("NOATIME");
}
}
ret = fclose(h);
if(ret) { cRc = errno; }
}
printf("\tFile with noatime flag, rc = %d, wRc = %d, sRc = %d, cRc = %d\n", rc, wRc, sRc, cRc);
h = fopen("NOCOW", "w+");
rc = 0;
wRc = 0;
sRc = 0;
cRc = 0;
attr = 0;
if(h == NULL) { rc = errno; }
else
{
attr |= FS_NOCOW_FL;
fd = fileno(h);
ret = ioctl(fd, FS_IOC_SETFLAGS, &attr);
if(ret)
{
sRc = errno;
unlink("NOCOW");
}
else
{
ret = fprintf(h, "This file is now not copy on write");
if(ret < 1)
{
wRc = errno;
unlink("NOCOW");
}
}
ret = fclose(h);
if(ret) { cRc = errno; }
}
printf("\tFile with no copy on write flag, rc = %d, wRc = %d, sRc = %d, cRc = %d\n", rc, wRc, sRc, cRc);
h = fopen("NODUMP", "w+");
rc = 0;
wRc = 0;
sRc = 0;
cRc = 0;
attr = 0;
if(h == NULL) { rc = errno; }
else
{
attr |= FS_NODUMP_FL;
fd = fileno(h);
ret = ioctl(fd, FS_IOC_SETFLAGS, &attr);
if(ret)
{
sRc = errno;
unlink("NODUMP");
}
else
{
ret = fprintf(h, "This file is now not dumpable");
if(ret < 1)
{
wRc = errno;
unlink("NODUMP");
}
}
ret = fclose(h);
if(ret) { cRc = errno; }
}
printf("\tFile with no dump flag, rc = %d, wRc = %d, sRc = %d, cRc = %d\n", rc, wRc, sRc, cRc);
h = fopen("NOTAIL", "w+");
rc = 0;
wRc = 0;
sRc = 0;
cRc = 0;
attr = 0;
if(h == NULL) { rc = errno; }
else
{
attr |= FS_NOTAIL_FL;
fd = fileno(h);
ret = ioctl(fd, FS_IOC_SETFLAGS, &attr);
if(ret)
{
sRc = errno;
unlink("NOTAIL");
}
else
{
ret = fprintf(h, "This file is now not tailed");
if(ret < 1)
{
wRc = errno;
unlink("NOTAIL");
}
}
ret = fclose(h);
if(ret) { cRc = errno; }
}
printf("\tFile with no tail flag, rc = %d, wRc = %d, sRc = %d, cRc = %d\n", rc, wRc, sRc, cRc);
h = fopen("PROJECTINHERIT", "w+");
rc = 0;
wRc = 0;
sRc = 0;
cRc = 0;
attr = 0;
if(h == NULL) { rc = errno; }
else
{
attr |= FS_PROJINHERIT_FL;
fd = fileno(h);
ret = ioctl(fd, FS_IOC_SETFLAGS, &attr);
if(ret)
{
sRc = errno;
unlink("PROJECTINHERIT");
}
else
{
ret = fprintf(h, "This file is now inheriting project id");
if(ret < 1)
{
wRc = errno;
unlink("PROJECTINHERIT");
}
}
ret = fclose(h);
if(ret) { cRc = errno; }
}
printf("\tFile with project inherit flag, rc = %d, wRc = %d, sRc = %d, cRc = %d\n", rc, wRc, sRc, cRc);
h = fopen("SECUREDELETION", "w+");
rc = 0;
wRc = 0;
sRc = 0;
cRc = 0;
attr = 0;
if(h == NULL) { rc = errno; }
else
{
attr |= FS_SECRM_FL;
fd = fileno(h);
ret = ioctl(fd, FS_IOC_SETFLAGS, &attr);
if(ret)
{
sRc = errno;
unlink("SECUREDELETION");
}
else
{
ret = fprintf(h, "This file is now secure deletable");
if(ret < 1)
{
wRc = errno;
unlink("SECUREDELETION");
}
}
ret = fclose(h);
if(ret) { cRc = errno; }
}
printf("\tFile with secure delete flag, rc = %d, wRc = %d, sRc = %d, cRc = %d\n", rc, wRc, sRc, cRc);
h = fopen("SYNC", "w+");
rc = 0;
wRc = 0;
sRc = 0;
cRc = 0;
attr = 0;
if(h == NULL) { rc = errno; }
else
{
attr |= FS_SYNC_FL;
fd = fileno(h);
ret = ioctl(fd, FS_IOC_SETFLAGS, &attr);
if(ret)
{
sRc = errno;
unlink("SYNC");
}
else
{
ret = fprintf(h, "This file is now synchronous");
if(ret < 1)
{
wRc = errno;
unlink("SYNC");
}
}
ret = fclose(h);
if(ret) { cRc = errno; }
}
printf("\tFile with synchronous flag, rc = %d, wRc = %d, sRc = %d, cRc = %d\n", rc, wRc, sRc, cRc);
h = fopen("UNREMOVABLE", "w+");
rc = 0;
wRc = 0;
sRc = 0;
cRc = 0;
attr = 0;
if(h == NULL) { rc = errno; }
else
{
attr |= FS_UNRM_FL;
fd = fileno(h);
ret = ioctl(fd, FS_IOC_SETFLAGS, &attr);
if(ret)
{
sRc = errno;
unlink("UNREMOVABLE");
}
else
{
ret = fprintf(h, "This file is now marked for undeletion");
if(ret < 1)
{
wRc = errno;
unlink("UNREMOVABLE");
}
}
ret = fclose(h);
if(ret) { cRc = errno; }
}
printf("\tFile with undeletion flag, rc = %d, wRc = %d, sRc = %d, cRc = %d\n", rc, wRc, sRc, cRc);
}
#endif

View File

@@ -36,6 +36,7 @@ Copyright (C) 2011-2018 Natalia Portillo
void LinuxExtendedAttributes(const char *path);
void LinuxSparse(const char *path);
void LinuxFileAttributes(const char *path);
static unsigned char CommentsEA[72] = {
0x45, 0x00, 0x00, 0x00, 0x00, 0x09, 0x33, 0x00, 0x2E, 0x43, 0x4F, 0x4D, 0x4D, 0x45, 0x4E, 0x54, 0x53, 0x00,

View File

@@ -258,7 +258,9 @@ void GetVolumeInfo(const char *path, size_t *clusterSize)
void FileAttributes(const char *path)
{
// Operating system dependent
#if defined(__linux__) || defined(__LINUX__) || defined(__gnu_linux)
LinuxFileAttributes(path);
#endif
}
void FilePermissions(const char *path)