[alt.sources] little superblock editor for ISC

src@scuzzy.in-berlin.de (Heiko Blume) (11/20/90)

well, i got some requests to post this. its was a hack i had to do
to recover some largish filesystem :-) that might save your life...
and don't tell me if it's ugly or something, it just did the job for me.

btw: does anyone know what the s_dinfo is ????
     on my drives its always '1 400 0 0'. what
     does that tell me? nothing!

/* The FSF GPL applies - hacked by Heiko Blume - src@scuzzy.in-berlin.de */
/* BEWARE! you can really fuck up your superblock with this!! */

#include <stdio.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/fs/s5param.h>
#include <sys/fs/s5filsys.h>
#include <unistd.h>

struct filsys bla;

void display_info(special) 
char *special;
{
	int fd;

	if((fd=open(special,O_RDONLY))==-1) {
		perror("wrsup");
		exit(-1);
	}

	if(lseek(fd,512,0)==-1) {
		perror("lseek");
		exit(-2);
	}

	if(read(fd,&bla,sizeof(struct filsys))!=sizeof(struct filsys)) {
		perror("read");
		exit(-3);
	}
	printf("Superblock of %s\n---------------------------------\n",special);
	printf("Size of i-list in blocks (s_isize)   %u\n", bla.s_isize);
	printf("Size of volume in blocks (s_fsize)   %u\n", bla.s_fsize);
	printf("# of addr. in free list  (s_nfree)   %u\n", bla.s_nfree);
	printf("# of inodes in           (s_ninode)  %u\n", bla.s_ninode);
	printf("Free list lock           (s_flock)   %u\n", bla.s_flock);
	printf("Inode list lock          (s_ilock)   %u\n", bla.s_ilock);
	printf("Superblock modified flag (s_fmod)    %u\n", bla.s_fmod);
	printf("Mounted read-only flag   (s_ronly)   %u\n", bla.s_ronly);
	printf("Last superblock update   (s_time)    %s", ctime(&bla.s_time));
	printf("Device information       (s_dinfo)   %u %u %u %u\n",
		bla.s_dinfo[0], bla.s_dinfo[1], bla.s_dinfo[2], bla.s_dinfo[3]);
	printf("Total free blocks        (s_tfree)   %u\n", bla.s_tfree);
	printf("Total free inodes        (s_tinode)  %u\n", bla.s_tinode);
	printf("File system name         (s_fname)   %s\n", bla.s_fname);
	printf("File system pack name    (s_ifpack)  %s\n", bla.s_fpack);
	printf("File system state        (s_state)   %x\n", bla.s_state);
	printf("Magic number             (s_magic)   %x\n", bla.s_magic);
	printf("File system type         (s_type)    %u\n", bla.s_type);
	close(fd);
}

void change_info(special)
char *special;
{
	int fd;
	unsigned long val;
	char option[20];
	char tmp[20];

	if((fd=open(special,O_RDWR))==-1) {
		perror("wrsup");
		exit(-1);
	}

	if(lseek(fd,512,0)==-1) {
		perror("lseek");
		exit(-2);
	}

	if(read(fd,&bla,sizeof(struct filsys))!=sizeof(struct filsys)) {
		perror("read");
		exit(-3);
	}

	printf("Well! which info do you want to change ?\n");
	printf("1  - isize	2  - fsize	3  - nfree	4  - ninode\n");
	printf("5  - flock	6  - ilock	7  - fmod	8  - ronly\n");
	printf("9  - time	10 - dinfo	11 - tfree	12 - tinode\n");
	printf("13 - fname	14 - fpack	15 - state	16 - magic\n");
	printf("17 - type\n");
	printf("99 - exit\n");

	gets(option);
	if(strcmp(option,"99")==0) return;

	if(strcmp(option,"1")==0) {
		printf("Current value of isize is %u\n", bla.s_isize);
		printf("Enter new value ->");
		fflush(stdout);
		scanf("%u",&val);
		bla.s_isize = (ushort) val;
		printf("New value is %u\n",bla.s_isize);
	}
	if(strcmp(option,"2")==0) {
		printf("Current value of fsize is %u\n", bla.s_fsize);
		printf("Enter new value ->");
		fflush(stdout);
		scanf("%u",&val);
		bla.s_fsize=(daddr_t) val;
		printf("New value is %u\n",bla.s_fsize);
	}
	if(strcmp(option,"3")==0) {
		printf("Current value of nfree is %u\n", bla.s_nfree);
		printf("Enter new value ->");
		fflush(stdout);
		scanf("%u",&val);
		bla.s_nfree=(short) val;
		printf("New value is %u\n",bla.s_nfree);
	}
	if(strcmp(option,"4")==0) {
		printf("Current value of ninode is %u\n", bla.s_ninode);
		printf("Enter new value ->");
		fflush(stdout);
		scanf("%u",&val);
		bla.s_ninode=(short)val;
		printf("New value is %u\n",bla.s_ninode);
	}
	if(strcmp(option,"5")==0) {
		printf("Current value of flock is %u\n", bla.s_flock);
		printf("Enter new value ->");
		fflush(stdout);
		scanf("%u",&val);
		bla.s_flock=(char)val;
		printf("New value is %u\n",bla.s_flock);
	}
	if(strcmp(option,"6")==0) {
		printf("Current value of ilock is %u\n", bla.s_ilock);
		printf("Enter new value ->");
		fflush(stdout);
		scanf("%u",&val);
		bla.s_ilock=(char)val;
		printf("New value is %u\n",bla.s_ilock);
	}
	if(strcmp(option,"7")==0) {
		printf("Current value of fmod is %u\n", bla.s_fmod);
		printf("Enter new value ->");
		fflush(stdout);
		scanf("%u",&val);
		bla.s_fmod=(char)val;
		printf("New value is %u\n",bla.s_fmod);
	}
	if(strcmp(option,"8")==0) {
		printf("Current value of ronly is %u\n", bla.s_ronly);
		printf("Enter new value ->");
		fflush(stdout);
		scanf("%u",&val);
		bla.s_ronly=(char)val;
		printf("New value is %u\n",bla.s_ronly);
	}
	if(strcmp(option,"9")==0) {
		char Time[40];
		printf("Current value of time is %u\n", bla.s_time);
		printf("Enter new value ->");
		fflush(stdout);
		scanf("%s",&Time);
	}
	if(strcmp(option,"10")==0) {
		int i;
		printf("Current values of dinfo is %u %u %u %u\n",
		 bla.s_dinfo[0], bla.s_dinfo[1], bla.s_dinfo[2], bla.s_dinfo[3]);
		for(i=0;i<4;i++) {
			printf("Enter new value for dinfo[%d]->",i);
			fflush(stdout);
			scanf("%u",&val);
			bla.s_dinfo[i]=(short)val;
		}
		printf("New values of dinfo is %u %u %u %u\n",
		 bla.s_dinfo[0], bla.s_dinfo[1], bla.s_dinfo[2], bla.s_dinfo[3]);
	}
	if(strcmp(option,"11")==0) {
		printf("Current value of tfree is %u\n", bla.s_tfree);
		printf("Enter new value ->");
		fflush(stdout);
		scanf("%u",&val);
		bla.s_tfree=(daddr_t)val;
		printf("New value is %u\n",bla.s_tfree);
	}
	if(strcmp(option,"12")==0) {
		printf("Current value of tinode is %u\n", bla.s_tinode);
		printf("Enter new value ->");
		fflush(stdout);
		scanf("%u",&val);
		bla.s_tinode=(ushort)val;
		printf("New value is %u\n",bla.s_tinode);
	}
	if(strcmp(option,"13")==0) {
		printf("Current value of fname is %s\n", bla.s_fname);
		printf("Enter new value (6 chars max!) ->");
		fflush(stdout);
		scanf("%s",&tmp);
		strncpy(bla.s_fname,tmp,6);
		printf("New value is %s\n",bla.s_fname);
	}
	if(strcmp(option,"14")==0) {
		printf("Current value of fpack is %s\n", bla.s_fpack);
		printf("Enter new value (6 chars max!) ->");
		fflush(stdout);
		scanf("%s",&tmp);
		strncpy(bla.s_fpack,tmp,6);
		printf("New value is %s\n",bla.s_fpack);
	}
	if(strcmp(option,"15")==0) {
		printf("Current value of state is %x\n", bla.s_state);
		printf("Enter new value ->");
		fflush(stdout);
		scanf("%lx",&bla.s_state);
		printf("New value is %x\n",bla.s_state);
	}
	if(strcmp(option,"16")==0) {
		printf("Current value of magic is %x\n", bla.s_magic);
		printf("Enter new value ->");
		fflush(stdout);
		scanf("%lx",&bla.s_magic);
		printf("New value is %x\n",bla.s_magic);
	}
	if(strcmp(option,"17")==0) {
		printf("Current value of type is %u\n", bla.s_type);
		printf("Enter new value ->");
		fflush(stdout);
		scanf("%lu",&bla.s_type);
		printf("New value is %u\n",bla.s_type);
	}

	printf("OK, do you really want to write the modified superblock ???\n");
	printf("Enter 'YES' if you want to do it! ->");
	fflush(stdout);
	scanf("%s",&tmp);
	if(strcmp(tmp,"YES")==0) {
		if(lseek(fd,512,0)==-1) {
			perror("wrsup");
			exit(-4);
		}
		if(write(fd,&bla,sizeof(struct filsys))
			!=sizeof(struct filsys)) {
			perror("wrsup (PANIC!)");
			exit(-5);
		}
	printf("Done.\n");
	}
	else printf("Superblock not written...\n");

	close(fd);
}

void menu() {
	printf("\nd - display superblock info\n");
	printf("c - change superblock info\n");
	printf("e - exit\n");
}

main(argc,argv)
int argc;
char **argv;
{
	char option[20];

	if(argc != 2) {
		printf("usage: wrsup <special of fs>\n");
		exit(1);
	}

	menu();
	for(;;) {
		gets(option);
		switch(option[0]) {
			case 'd': display_info(argv[1]); break;
			case 'c': change_info(argv[1]); break;
			case 'e': exit(); break;
			default: menu();
		}	
			
	}
}
-- 
      Heiko Blume <-+-> src@scuzzy.in-berlin.de <-+-> (+49 30) 691 88 93
                    public source archive [HST V.42bis]:
        scuzzy Any ACU,f 38400 6919520 gin:--gin: nuucp sword: nuucp
                     uucp scuzzy!/src/README /your/home