Indexed File Allocation
Indexed File Allocation
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_FILES 10
#define MAX_BLOCKS 100 // Maximum number of blocks in the disk
typedef struct {
char filename[20];
int numBlocks;
int* blockIndexes; // Array to hold indexes of blocks allocated to this file
int indexBlock; // Index block to store the starting block for the file's data blocks
} DirectoryEntry;
int bitVector[MAX_BLOCKS]; // Bit Vector to track free (0) or allocated (1) blocks
DirectoryEntry directory[MAX_FILES]; // Directory to store file details
int fileCount = 0;
// Function to display the directory (List of files and their block allocations)
void showDirectory() {
if (fileCount == 0) {
printf("No files in the directory.\n");
} else {
printf("Directory List:\n");
printf("Filename\tNumber of Blocks\tIndex Block\tAllocated Blocks\n");
for (int i = 0; i < fileCount; i++) {
printf("%s\t\t%d\t\t%d\t\t", directory[i].filename, directory[i].numBlocks,
directory[i].indexBlock);
for (int j = 0; j < directory[i].numBlocks; j++) {
printf("%d ", directory[i].blockIndexes[j]);
}
printf("\n");
}
}
}
char filename[20];
int numBlocks;
int blocksAllocated = 0;
for (int i = 0; i < n && blocksAllocated < numBlocks; i++) {
if (bitVector[i] == 0) { // If the block is free
bitVector[i] = 1; // Mark the block as allocated
directory[fileCount].blockIndexes[blocksAllocated++] = i; // Store the block index
}
}
// Set the index block (the first allocated block will be the index block)
directory[fileCount].indexBlock = directory[fileCount].blockIndexes[0];
fileCount++;
printf("File '%s' created successfully with %d blocks.\n", filename, numBlocks);
}
char filename[20];
printf("Enter filename to delete: ");
scanf("%s", filename);
if (index == -1) {
printf("File not found in the directory.\n");
return;
}
fileCount--;
printf("File '%s' deleted successfully.\n", filename);
}
// Main function to drive the program with a menu
int main() {
int n;
printf("Enter the number of blocks on disk (max %d): ", MAX_BLOCKS);
scanf("%d", &n);
if (n > MAX_BLOCKS) {
printf("Exceeded maximum block size. Exiting program.\n");
return 1;
}
initializeBitVector(n);
int choice;
while (1) {
printf("\nMenu:\n");
printf("1. Show Bit Vector\n");
printf("2. Create New File\n");
printf("3. Show Directory\n");
printf("4. Delete File\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
showBitVector(n);
break;
case 2:
createNewFile(n);
break;
case 3:
showDirectory();
break;
case 4:
deleteFile();
break;
case 5:
printf("Exiting program.\n");
exit(0);
default:
printf("Invalid choice. Please try again.\n");
}
}
return 0;
}