From 0a12af4a29f01cff79b2036c682ffb12c1f53736 Mon Sep 17 00:00:00 2001 From: Natalia Portillo Date: Mon, 29 Apr 2019 00:33:15 +0100 Subject: [PATCH] Implement FileAttributes() for Linux. --- setter/linux.c | 436 +++++++++++++++++++++++++++++++++++++++++++++++++ setter/linux.h | 1 + setter/unix.c | 4 +- 3 files changed, 440 insertions(+), 1 deletion(-) diff --git a/setter/linux.c b/setter/linux.c index 4da106e..521d045 100644 --- a/setter/linux.c +++ b/setter/linux.c @@ -38,6 +38,7 @@ Copyright (C) 2011-2018 Natalia Portillo #include #include #include +#include #include #include #include @@ -46,6 +47,7 @@ Copyright (C) 2011-2018 Natalia Portillo #include #if((__GLIBC__ == 2) && (__GLIBC_MINOR__ >= 3)) || (__GLIBC__ > 2) +#include #include #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 \ No newline at end of file diff --git a/setter/linux.h b/setter/linux.h index d405f90..108d7d6 100644 --- a/setter/linux.h +++ b/setter/linux.h @@ -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, diff --git a/setter/unix.c b/setter/unix.c index e34d672..599065c 100644 --- a/setter/unix.c +++ b/setter/unix.c @@ -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)