All pastes #365276 Raw Edit

Stuff

public text v1 · immutable
#365276 ·published 2007-02-20 20:59 UTC
rendered paste body
/**
 *	SmallFS.java - template for a file-system driver for the "small file-system"
 *
 *	@author Gurtac Yemiscioglu
 */


public class SmallFS implements FSIF
{
	private DiskScheduler dsched;
	private byte sblk[];
	private int nblocks;

	public static final int SFS_NTABLES = 16;


	/**
	 * called to initialise the file-system driver
	 *
	 * @param dsched disk scheduler used to interface with the underlying device
	 *
	 * @return 0 on success, non-zero on failure
	 */
	public int initFS (DiskScheduler dsched)
	{
		int i;
		int itotal, iused;
		int imagic;

		this.dsched = dsched;

		sblk = dsched.readBlock (0);
		if (sblk == null) {
			System.err.println ("failed to read superblock!");
			return -1;
		}

		nblocks = (int)BM.read_uint32 (sblk, 12);

		imagic = (int)BM.read_uint32 (sblk, 0);
		if (imagic != 0xefbeadde) {
			System.err.println ("bad file-system magic!  expected [deadbeef], found [" + BM.tohexbuf (sblk, 0, 4) + "]");
			return -1;
		}

		System.out.println ("File-system magic   : " + BM.tohexbuf (sblk, 0, 4));
		System.out.println ("File-system version : " + BM.read_uint16 (sblk, 4) + "." + BM.read_uint16 (sblk, 6));
		System.out.println ("Superblock age      : " + BM.read_uint32 (sblk, 8));
		System.out.println ("Total blocks        : " + nblocks);
		System.out.println ("Block bitmap at     : " + BM.read_uint32 (sblk, 16));
		System.out.print ("Inode tables at     : ");
		for (i=0; i<SFS_NTABLES; i++) {
			int itblk = (int)BM.read_uint32 (sblk, 20 + (i * 4));
			int itlen = (int)BM.read_uint32 (sblk, 20 + (SFS_NTABLES * 4) + (i * 4));

			System.out.print ("(" + itblk + "-" + (itblk + (itlen - 1)) + ") ");
		}
		System.out.println ("");
		itotal = (int)BM.read_uint32 (sblk, 20 + (SFS_NTABLES * 8));
		iused = (int)BM.read_uint32 (sblk, 24 + (SFS_NTABLES * 8));
		System.out.println ("Total inodes        : " + itotal);
		System.out.println ("Used inodes         : " + iused);
		System.out.println ("Free inodes         : " + (itotal - iused));
		System.out.println ("Root inode          : " + BM.read_uint32 (sblk, 28 + (SFS_NTABLES * 8)));

		return 0;
	}

	/**
	 * prints out a block summary for the small file-system by examining
	 * the contents of the block bitmap and counting up the 4 states.  Only
	 * examines physically present blocks (i.e. not those in the bitmap which
	 * are past the end of the file-system).  Assumes that the file-system has
	 * already been initialised (by calling initFS() above)
	 *
	 * @return 0 on success, non-zero on failure
	 */
	public int fsBlockSummary ()
	{
		//variables declared
		int unused = 0;
		int reserved = 0;
		int used = 0;
		int valid = 0;
		
		//reading block
		byte gblk = dsched.readBlock(BM.read_uint32(sblk, 16));
		//for(int i=0; i < nblocks*2; i+2)//
		int i = 0;
		while(i < nblocks*2)
		{
			//read bits from gblk
			int map = read_ubits(gblk,i,2);
			switch(map)
			{
				case 0: unused++;
				break;
				case 1: reserved++;
				break;
				case 2: used++;
				break;
				case 3: valid++;
				break;
				
				default:System.out.println("Wrong block\n"); 
				return -1;
				break;
			}
			i= i+2;
		}
		//prints Block summary
		System.out.println("Block summary:" +unused+ "unused, " +reserved+ "reserved," +used+ "used," +valid+ "valid");
	return 0;
	}

}