Set UNIX perms using a for loop instead of repeating code.

This commit is contained in:
2021-03-09 05:24:56 +00:00
parent 801383796d
commit 73d4ee607a
3 changed files with 74 additions and 152 deletions

View File

@@ -34,6 +34,8 @@ Copyright (C) 2011-2021 Natalia Portillo
#include <sys/stat.h>
#include <unistd.h>
#include "perms.h"
#include "../include/defs.h"
void FilePermissions(const char* path)
@@ -42,6 +44,7 @@ void FilePermissions(const char* path)
FILE* file;
int rc;
int cRc;
int i;
ret = chdir(path);
@@ -69,161 +72,22 @@ void FilePermissions(const char* path)
printf("Creating attributes files.\n");
file = fopen("NONE", "w+");
rc = 0;
cRc = 0;
if(!file) { rc = errno; }
else
for(i = 0; i < KNOWN_UNIX_PERMS; i++)
{
fprintf(file, "File with no permissions.\n");
fclose(file);
cRc = chmod("NONE", 0);
}
printf("\tFile with no permissions: name = \"%s\", rc = %d, cRc = %d\n", "NONE", rc, cRc);
file = fopen(unix_perms[i].filename, "w+");
rc = 0;
cRc = 0;
file = fopen("04000", "w+");
rc = 0;
cRc = 0;
if(!file) { rc = errno; }
else
{
fprintf(file, "File with set-user-ID.\n");
fclose(file);
cRc = chmod("04000", 04000);
}
printf("\tFile with set-user-ID: name = \"%s\", rc = %d, cRc = %d\n", "04000", rc, cRc);
if(!file) rc = errno;
else
{
fprintf(file, "%s.\n", unix_perms[i].description);
fclose(file);
cRc = chmod(unix_perms[i].filename, unix_perms[i].mode);
}
file = fopen("02000", "w+");
rc = 0;
cRc = 0;
if(!file) { rc = errno; }
else
{
fprintf(file, "File with set-group-ID.\n");
fclose(file);
cRc = chmod("02000", 02000);
printf("\t%s: name = \"%s\", rc = %d, cRc = %d\n", unix_perms[i].description, unix_perms[i].filename, rc, cRc);
}
printf("\tFile with set-group-ID: name = \"%s\", rc = %d, cRc = %d\n", "02000", rc, cRc);
file = fopen("01000", "w+");
rc = 0;
cRc = 0;
if(!file) { rc = errno; }
else
{
fprintf(file, "File with sticky bit.\n");
fclose(file);
cRc = chmod("01000", 01000);
}
printf("\tFile with sticky bit: name = \"%s\", rc = %d, cRc = %d\n", "01000", rc, cRc);
file = fopen("00400", "w+");
rc = 0;
cRc = 0;
if(!file) { rc = errno; }
else
{
fprintf(file, "File with read by owner.\n");
fclose(file);
cRc = chmod("00400", 00400);
}
printf("\tFile with read by owner: name = \"%s\", rc = %d, cRc = %d\n", "00400", rc, cRc);
file = fopen("00200", "w+");
rc = 0;
cRc = 0;
if(!file) { rc = errno; }
else
{
fprintf(file, "File with write by owner.\n");
fclose(file);
cRc = chmod("00200", 00200);
}
printf("\tFile with write by owner: name = \"%s\", rc = %d, cRc = %d\n", "00200", rc, cRc);
file = fopen("00100", "w+");
rc = 0;
cRc = 0;
if(!file) { rc = errno; }
else
{
fprintf(file, "File with execute by owner.\n");
fclose(file);
cRc = chmod("00100", 00100);
}
printf("\tFile with execute by owner: name = \"%s\", rc = %d, cRc = %d\n", "00100", rc, cRc);
file = fopen("00040", "w+");
rc = 0;
cRc = 0;
if(!file) { rc = errno; }
else
{
fprintf(file, "File with read by group.\n");
fclose(file);
cRc = chmod("00040", 00040);
}
printf("\tFile with read by group: name = \"%s\", rc = %d, cRc = %d\n", "00040", rc, cRc);
file = fopen("00020", "w+");
rc = 0;
cRc = 0;
if(!file) { rc = errno; }
else
{
fprintf(file, "File with write by group.\n");
fclose(file);
cRc = chmod("00020", 00020);
}
printf("\tFile with write by group: name = \"%s\", rc = %d, cRc = %d\n", "00020", rc, cRc);
file = fopen("00010", "w+");
rc = 0;
cRc = 0;
if(!file) { rc = errno; }
else
{
fprintf(file, "File with execute by group.\n");
fclose(file);
cRc = chmod("00010", 00010);
}
printf("\tFile with execute by group: name = \"%s\", rc = %d, cRc = %d\n", "00010", rc, cRc);
file = fopen("00004", "w+");
rc = 0;
cRc = 0;
if(!file) { rc = errno; }
else
{
fprintf(file, "File with read by others.\n");
fclose(file);
cRc = chmod("00004", 00004);
}
printf("\tFile with read by others: name = \"%s\", rc = %d, cRc = %d\n", "00004", rc, cRc);
file = fopen("00002", "w+");
rc = 0;
cRc = 0;
if(!file) { rc = errno; }
else
{
fprintf(file, "File with write by others.\n");
fclose(file);
cRc = chmod("00002", 00002);
}
printf("\tFile with write by others: name = \"%s\", rc = %d, cRc = %d\n", "00002", rc, cRc);
file = fopen("00001", "w+");
rc = 0;
cRc = 0;
if(!file) { rc = errno; }
else
{
fprintf(file, "File with execute by others.\n");
fclose(file);
cRc = chmod("00001", 00001);
}
printf("\tFile with execute by others: name = \"%s\", rc = %d, cRc = %d\n", "00001", rc, cRc);
}
#endif

58
setter/src/unix/perms.h Normal file
View File

@@ -0,0 +1,58 @@
/****************************************************************************
Aaru Data Preservation Suite
-----------------------------------------------------------------------------
Filename : perms.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 <http://www.gnu.org/licenses/>.
-----------------------------------------------------------------------------
Copyright (C) 2011-2021 Natalia Portillo
*****************************************************************************/
#ifndef SETTER_SRC_UNIX_PERMS_H_
#define SETTER_SRC_UNIX_PERMS_H_
typedef struct
{
char filename[256];
char description[256];
mode_t mode;
} unix_perms_tests_t;
#define KNOWN_UNIX_PERMS 14
static const unix_perms_tests_t unix_perms[KNOWN_UNIX_PERMS] = {
{"NONE", "File with no permissions", 0},
{"04000", "File with set-user-ID", 04000},
{"02000", "File with set-group-ID", 02000},
{"01000", "File with sticky bit", 01000},
{"00400", "File with read by owner", 00400},
{"00200", "File with write by owner", 00200},
{"00100", "File with execute by owner", 00100},
{"00040", "File with read by group", 00040},
{"00020", "File with write by group", 00020},
{"00010", "File with execute by group", 00010},
{"00004", "File with read by others", 00004},
{"00002", "File with write by others", 00002},
{"00001", "File with execute by others", 00001},
};
#endif // SETTER_SRC_UNIX_PERMS_H_