We're working on a tricky project and C is not my forte but we are trying to update the number of free inodes and blocks in an ext2 filesystem. These values are contained in the Super and Group Descriptor Blocks. The function that is causing problems gets the super block from an image of a floppy disk filesystem and creates a super block struct out of the buffer it is read into. That struct has an element called s_free_inodes_count that we are trying to update.
When ever I run the code on my ubuntu (ext4) machine the program segfaults but on my teammates' machines it runs fine. There is not a lot of information online about this subject I'm not sure why it would be segfaulting, any advice would be greatly appreciated.
The function that segfaults is below.
Code:
void decFreeInodes(int device) {
SUPER *superBlockPtr;
char *buffer;
getBlock(device, 1, buffer); //get super block from disk
superBlockPtr = (ext2_super_block *) buffer;
superBlockPtr->s_free_inodes_count--; //SEGFAULTS HERE
putBlock(device, 1, buffer); //write super block back to disk
}//decFreeInodes
Where getBlock and putBlock simply search to a block on the filesystem and read the block into a buffer as follows:
Code:
void getBlock(int device, int blockNum, char *buf) {
printf("getting block: %d from device: %d\n", blockNum, dev);
lseek( device, (long)(blockNum * BLOCK_SIZE), 0 );
read( device, buf, BLOCK_SIZE);
}//get block
void putBlock(int device, int blockNum, char *buf) {
printf("putting block %d to device %d\n", blockNum, dev);
lseek(device, (long)(blockNum * BLOCK_SIZE), 0);
write(device, buf, BLOCK_SIZE);
}//put block