Skip to content

Instantly share code, notes, and snippets.

@firas-jolha
Created November 14, 2023 20:09
Show Gist options
  • Save firas-jolha/69757676ef6817901a567054e829789b to your computer and use it in GitHub Desktop.
Save firas-jolha/69757676ef6817901a567054e829789b to your computer and use it in GitHub Desktop.
/* create a file to act as a disk and format the file system residing on the disk */
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(int argc, char *argv[]){
int i, fd;
char *buf;
if (argc == 1 ){
fprintf(stderr,"usage: %s <diskFileName> \n",argv[0]);
exit(0);
}
printf( "Creating a 128KB file in %s\n",argv[1]);
printf("This file will act as a dummy disk and will hold your filesystem\n");
/* open the disk file for writing */
fd = open(argv[1],O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
printf("Formatting your filesystem...\n");
buf = (char *) calloc(1024,sizeof(char));
/* write super block */
buf[0]=1; /* mark superblock as allocated in the free block list
all other blocks are free, all inodes are zeroed out */
/* write out the super block */
if(write(fd,buf, 1024)<0)
printf("error: write failed \n");
buf[0]=0;
/* write out the remaining 127 data blocks, all zeroed out */
for(i=0;i<127;i++){
if(write(fd,buf,1024)<0)
printf("error: write failed \n");
}
close(fd);
exit(1);
}
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
int create(char name[16], int size){
/*create a file with this name and this size*/
// Step 1: check to see if we have sufficient free space on disk by
// reading in the free block list. To do this:
// move the file pointer to the start of the disk file.
// Read the first 128 bytes (the free/in-use block information)
// Scan the list to make sure you have sufficient free blocks to
// allocate a new file of this size
// Step 2: we look for a free inode on disk
// Read in a inode
// check the "used" field to see if it is free
// If not, repeat the above two steps until you find a free inode
// Set the "used" field to 1
// Copy the filename to the "name" field
// Copy the file size (in units of blocks) to the "size" field
// Step 3: Allocate data blocks to the file
// for(i=0;i<size;i++)
// Scan the block list that you read in Step 1 for a free block
// Once you find a free block, mark it as in-use (Set it to 1)
// Set the blockPointer[i] field in the inode to this block number.
//
// end for
// Step 4: Write out the inode and free block list to disk
// Move the file pointer to the start of the file
// Write out the 128 byte free block list
// Move the file pointer to the position on disk where this inode was stored
// Write out the inode
} // End Create
int detete(char name[16]){
// Delete the file with this name
// Step 1: Locate the inode for this file
// Move the file pointer to the 1st inode (129th byte)
// Read in a inode
// If the inode is free, repeat above step.
// If the inode is in use, check if the "name" field in the
// inode matches the file we want to delete. IF not, read the next
// inode and repeat
// Step 2: free blocks of the file being deleted
// Read in the 128 byte free block list (move file pointer to start of the disk and read in 128 bytes)
// Free each block listed in the blockPointer fields as follows:
// for(i=0;i< inode.size; i++)
// freeblockList[ inode.blockPointer[i] ] = 0;
// Step 3: mark inode as free
// Set the "used" field to 0.
// Step 4: Write out the inode and free block list to disk
// Move the file pointer to the start of the file
// Write out the 128 byte free block list
// Move the file pointer to the position on disk where this inode was stored
// Write out the inode
} // End Delete
int ls(void){
// List names of all files on disk
// Step 1: read in each inode and print!
// Move file pointer to the position of the 1st inode (129th byte)
// for(i=0;i<16;i++)
// REad in a inode
// If the inode is in-use
// print the "name" and "size" fields from the inode
// end for
} // End ls
int read(char name[16], int blockNum, char buf[1024]){
// read this block from this file
// Step 1: locate the inode for this file
// Move file pointer to the position of the 1st inode (129th byte)
// Read in a inode
// If the inode is in use, compare the "name" field with the above file
// IF the file names don't match, repeat
// Step 2: Read in the specified block
// Check that blockNum < inode.size, else flag an error
// Get the disk address of the specified block
// That is, addr = inode.blockPointer[blockNum]
// move the file pointer to the block location (i.e., to byte # addr*1024 in the file)
// Read in the block! => Read in 1024 bytes from this location into the buffer "buf"
} // End read
int write(char name[16], int blockNum, char buf[1024]){
// write this block to this file
// Step 1: locate the inode for this file
// Move file pointer to the position of the 1st inode (129th byte)
// Read in a inode
// If the inode is in use, compare the "name" field with the above file
// IF the file names don't match, repeat
// Step 2: Write to the specified block
// Check that blockNum < inode.size, else flag an error
// Get the disk address of the specified block
// That is, addr = inode.blockPointer[blockNum]
// move the file pointer to the block location (i.e., byte # addr*1024)
// Write the block! => Write 1024 bytes from the buffer "buff" to this location
} // end write
int main(int argc, char *argv[]){
// Todo
}
disk0
C file1.c 3
C file2.c 8
C file3.c 4
C a.out 5
C lab1.c 6
L
W file1.c 0
W file1.c 1
W file1.c 2
W file2.c 3
W file2.c 7
W file2.c 2
W file2.c 4
W file2.c 5
W a.out 0
W a.out 1
W a.out 2
W a.out 3
W a.out 4
D file3.c
R file1.c 2
C file4.c 7
L
R file2.c 4
R file2.c 5
R file2.c 6
D lab1.c
C lab2.c 7
R a.out 1
R a.out 3
R a.out 0
L
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment