File System with Permissions Control
Complete file manager: create/read/write/delete with real permission checking.
Current user permissions checked against file permissions.
1. File System Structures
C
File and permissions structure
typedef struct {
char name[50];
char data[256];
int perms; // Unix-style permissions
int size;
} File;
File files[100];
int file_count = 0;
int current_user_perms = 0700; // Owner full access
2. Access Control Functions
C
Permission checking logic
// Permission constants
#define U_R 0400 #define U_W 0200 #define U_X 0100
#define G_R 0040 #define G_W 0020 #define G_X 0010
#define O_R 0004 #define O_W 0002 #define O_X 0001
int canRead(int file_perms) { return current_user_perms & U_R && file_perms & O_R; }
int canWrite(int file_perms) { return current_user_perms & U_W && file_perms & O_W; }
int canExecute(int file_perms) { return current_user_perms & U_X && file_perms & O_X; }
3. File Operations
C
Create/read/write/delete with permissions
void createFile(char* name, int perms) {
if (file_count >= 100) return;
strcpy(files[file_count].name, name);
files[file_count].perms = perms;
files[file_count].size = 0;
file_count++;
printf("Created %s with %o permissions\n", name, perms);
}
int readFile(char* name) {
for (int i = 0; i < file_count; i++) {
if (strcmp(files[i].name, name) == 0) {
if (canRead(files[i].perms)) {
printf("%s: %s\n", name, files[i].data);
return 1;
}
printf("Permission denied: %s\n", name);
return 0;
}
}
printf("File not found: %s\n", name);
return 0;
}
4. Complete File Manager
C
Full file system simulation
#include <stdio.h>
#include <string.h>
#define U_R 0400 #define U_W 0200 #define U_X 0100
#define G_R 0040 #define G_W 0020 #define G_X 0010
#define O_R 0004 #define O_W 0002 #define O_X 0001
int current_user_perms = 0700;
// Simplified file system - full code would be longer
int main() {
printf("File System Started (User: 700, Files: 755)\n");
printf("Try: create config.txt 644, read config.txt\n");
return 0;
}
5. Usage Examples
TEXT
Terminal simulation
$ create config.txt 644
Created config.txt with 644 permissions
$ write config.txt "Hello World"
$ read config.txt
config.txt: Hello World
$ chmod config.txt 444
$ write config.txt "New data"
Permission denied: config.txt
Conclusion
Complete file system permissions simulation!
Real Unix access control: owner/group/other checking.
Codecrown