From e3a2431fb9d1f620e11771c89aac8c7b94407bee Mon Sep 17 00:00:00 2001 From: Natalia Portillo Date: Tue, 9 Mar 2021 12:51:20 +0000 Subject: [PATCH] Set Linux attributes using a for loop instead of repeating code. --- setter/src/linux/attr.c | 415 +++------------------------------------- setter/src/linux/attr.h | 105 ++++++++++ 2 files changed, 134 insertions(+), 386 deletions(-) create mode 100644 setter/src/linux/attr.h diff --git a/setter/src/linux/attr.c b/setter/src/linux/attr.c index dbceae2..ad0980e 100644 --- a/setter/src/linux/attr.c +++ b/setter/src/linux/attr.c @@ -37,6 +37,8 @@ Copyright (C) 2011-2021 Natalia Portillo #include #include +#include "attr.h" + #include "linux.h" void LinuxFileAttributes(const char* path) @@ -49,6 +51,7 @@ void LinuxFileAttributes(const char* path) int sRc; int cRc; int attr; + int i; ret = chdir(path); @@ -76,401 +79,41 @@ void LinuxFileAttributes(const char* path) 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 + for(i = 0; i < KNOWN_LINUX_ATTRS; i++) { - attr |= FS_APPEND_FL; - fd = fileno(h); - ret = ioctl(fd, FS_IOC_SETFLAGS, &attr); - - if(ret) - { - sRc = errno; - unlink("APPEND"); - } + h = fopen(linux_attrs[i].filename, "w+"); + rc = 0; + wRc = 0; + sRc = 0; + cRc = 0; + attr = 0; + if(h == NULL) { rc = errno; } else { - ret = fprintf(h, "This file is now append only"); - if(ret < 1) + attr |= linux_attrs[i].attr; + fd = fileno(h); + ret = ioctl(fd, FS_IOC_SETFLAGS, &attr); + + if(ret) { - wRc = errno; - unlink("APPEND"); + sRc = errno; + unlink(linux_attrs[i].filename); } - } - - 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) + else { - wRc = errno; - unlink("COMPRESS"); + ret = fprintf(h, "%s", linux_attrs[i].contents); + if(ret < 1) + { + wRc = errno; + unlink(linux_attrs[i].filename); + } } - } - ret = fclose(h); - if(ret) { cRc = errno; } + ret = fclose(h); + if(ret) { cRc = errno; } + } + printf("\t%s, rc = %d, wRc = %d, sRc = %d, cRc = %d\n", linux_attrs[i].description, rc, wRc, sRc, cRc); } - 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/src/linux/attr.h b/setter/src/linux/attr.h new file mode 100644 index 0000000..7389c82 --- /dev/null +++ b/setter/src/linux/attr.h @@ -0,0 +1,105 @@ +/**************************************************************************** +Aaru Data Preservation Suite +----------------------------------------------------------------------------- + +Filename : attr.h +Author(s) : Natalia Portillo + +--[ Description ] ----------------------------------------------------------- + +Contains common implementations for UNIX family and compatibles + +--[ 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 . + +----------------------------------------------------------------------------- +Copyright (C) 2011-2021 Natalia Portillo +*****************************************************************************/ + +#ifndef SETTER_SRC_LINUX_ATTR_H_ +#define SETTER_SRC_LINUX_ATTR_H_ + +typedef struct +{ + char filename[16]; + char contents[256]; + char description[256]; + int attr; +} linux_attr_tests_t; + +#define KNOWN_LINUX_ATTRS 12 + +#ifndef FS_SECRM_FL +#define FS_SECRM_FL 0x00000001 /* Secure deletion */ +#endif +#ifndef FS_UNRM_FL +#define FS_UNRM_FL 0x00000002 /* Undelete */ +#endif + +#ifndef FS_COMPR_FL +#define FS_COMPR_FL 0x00000004 /* Compress file */ +#endif + +#ifndef FS_SYNC_FL +#define FS_SYNC_FL 0x00000008 /* Synchronous updates */ +#endif + +#ifndef FS_IMMUTABLE_FL +#define FS_IMMUTABLE_FL 0x00000010 /* Immutable file */ +#endif + +#ifndef FS_APPEND_FL +#define FS_APPEND_FL 0x00000020 /* writes to file may only append */ +#endif + +#ifndef FS_NODUMP_FL +#define FS_NODUMP_FL 0x00000040 /* do not dump file */ +#endif + +#ifndef FS_NOATIME_FL +#define FS_NOATIME_FL 0x00000080 /* do not update atime */ +#endif + +#ifndef FS_JOURNAL_DATA_FL +#define FS_JOURNAL_DATA_FL 0x00004000 /* Reserved for ext3 */ +#endif + +#ifndef FS_NOTAIL_FL +#define FS_NOTAIL_FL 0x00008000 /* file tail should not be merged */ +#endif + +#ifndef FS_NOCOW_FL +#define FS_NOCOW_FL 0x00800000 /* Do not cow file */ +#endif + +#ifndef FS_PROJINHERIT_FL +#define FS_PROJINHERIT_FL 0x20000000 /* Create with parents projid */ +#endif + +static const linux_attr_tests_t linux_attrs[KNOWN_LINUX_ATTRS] = { + {"APPEND", "This file is now append only", "File with append only flag", FS_APPEND_FL}, + {"COMPRESS", "This file is now compressed", "File with compressed flag", FS_COMPR_FL}, + {"IMMUTABLE", "This file is now immutable", "File with immutable flag", FS_IMMUTABLE_FL}, + {"JOURNALED", "This file is now journaled", "File with journaled flag", FS_JOURNAL_DATA_FL}, + {"NOATIME", "This file is now noatime", "File with noatime flag", FS_NOATIME_FL}, + {"NOCOW", "This file is now not copy on write", "File with no copy on write flag", FS_NOCOW_FL}, + {"NODUMP", "This file is now not dumpable", "File with no dump flag", FS_NODUMP_FL}, + {"NOTAIL", "This file is now not tailed", "File with no tail flag", FS_NOTAIL_FL}, + {"PROJECTINHERIT", "This file is now inheriting project id", "File with project inherit flag", FS_PROJINHERIT_FL}, + {"SECUREDELETION", "This file is now secure deletable", "File with secure delete flag", FS_SECRM_FL}, + {"SYNC", "This file is now synchronous", "File with synchronous flag", FS_SYNC_FL}, + {"UNREMOVABLE", "This file is now marked for undeletion", "File with undeletion flag", FS_UNRM_FL}, +}; + +#endif // SETTER_SRC_LINUX_ATTR_H_