fatfilesystemtype.java

来自「纯java操作系统jnode,安装简单和操作简单的个人使用的Java操作系统」· Java 代码 · 共 645 行

JAVA
645
字号
/*
 * $Id: FatFileSystemType.java,v 1.4 2004/02/02 00:57:24 gbin Exp $
 */
package org.jnode.fs.fat;

import java.io.IOException;

import org.jnode.driver.ApiNotFoundException;
import org.jnode.driver.Device;
import org.jnode.driver.block.BlockDeviceAPI;
import org.jnode.driver.block.FSBlockDeviceAPI;
import org.jnode.fs.FileSystem;
import org.jnode.fs.FileSystemException;
import org.jnode.fs.FileSystemType;
import org.jnode.fs.partitions.PartitionTableEntry;
import org.jnode.fs.partitions.ibm.IBMPartitionTableEntry;
import org.jnode.fs.partitions.ibm.IBMPartitionTypes;

/**
 * @author epr
 */
public class FatFileSystemType implements FileSystemType {

	/** Name of this filesystem type */
	public static final String NAME = "FAT";
	private static final int NB_HEADS = 255;
	private static final int SECTOR_PER_TRACK = 63;

	/**
	 * Gets the unique name of this file system type.
	 */
	public String getName() {
		return NAME;
	}

	/**
	 * Can this file system type be used on the given first sector of a
	 * blockdevice?
	 * 
	 * @param pte
	 *           The partition table entry, if any. If null, there is no
	 *           partition table entry.
	 * @param firstSector
	 */
	public boolean supports(PartitionTableEntry pte, byte[] firstSector) {
		if (pte != null) {
			if (!pte.isValid()) {
				return false;
			}
			if (!(pte instanceof IBMPartitionTableEntry)) {
				return false;
			}
			final IBMPartitionTableEntry ipte = (IBMPartitionTableEntry)pte;
			final int type = ipte.getSystemIndicator();
			switch (type) {
				case IBMPartitionTypes.PARTTYPE_DOS_FAT12 :
				case IBMPartitionTypes.PARTTYPE_DOS_FAT16_LT32M :
				case IBMPartitionTypes.PARTTYPE_DOS_FAT16_GT32M :
					return true;
				default :
					return false;
			}

		}

		if (!new BootSector(firstSector).isaValidBootSector())
			return false;

		// Very ugly, but good enough for now.
		return true;
	}

	/**
	 * Create a filesystem for a given device.
	 * 
	 * @param device
	 */
	public FileSystem create(Device device) throws FileSystemException {
		return new FatFileSystem(device);
	}

	/**
	 * @param specificOptions
	 *           Integer representing the FatSize
	 * 
	 * @see org.jnode.fs.FileSystemType#format(org.jnode.driver.Device,
	 *      java.lang.Object)
	 */
	public FileSystem format(Device device, Object specificOptions) throws FileSystemException {
		try {
			long numberOfSectors;
			long offset;
			int fatSize = ((Integer)specificOptions).intValue();

			FSBlockDeviceAPI api = (FSBlockDeviceAPI)device.getAPI(BlockDeviceAPI.class);
			int sectorSize = api.getSectorSize();

			PartitionTableEntry entry = api.getPartitionTableEntry();

			// if we can deduce partitiontable/fat dependencies do it otherwise
			// guess it.
			if (entry != null && entry instanceof IBMPartitionTableEntry) {
				numberOfSectors = ((IBMPartitionTableEntry)entry).getNrSectors();
				offset = ((IBMPartitionTableEntry)entry).getStartLba();
			} else {
				numberOfSectors = api.getLength() / sectorSize;
				offset = 0;
			}

			FatFormatter ff =
				FatFormatter.HDFormatter(
					sectorSize,
					(int)numberOfSectors,
					SECTOR_PER_TRACK,
					NB_HEADS,
					fatSize,
					(int)offset,
					1,
					FAT_STANDARD_BS);
			ff.format(api);
			return new FatFileSystem(device);
		} catch (IOException ioe) {
			throw new FileSystemException("Formating problem", ioe);
		} catch (ApiNotFoundException e) {
			throw new FileSystemException("Formating problem", e);
		}
	}

	private static final BootSector FAT_STANDARD_BS =
		new BootSector(
			new byte[] {
				(byte)0xEB,
				(byte)0x48,
				(byte)0x90,
				(byte)0x4A,
				(byte)0x4E,
				(byte)0x6F,
				(byte)0x64,
				(byte)0x65,
				(byte)0x31,
				(byte)0x2E,
				(byte)0x30,
				(byte)0x00,
				(byte)0x02,
				(byte)0x01,
				(byte)0xED,
				(byte)0x00,
				(byte)0x02,
				(byte)0x00,
				(byte)0x02,
				(byte)0x00,
				(byte)0x80,
				(byte)0xF8,
				(byte)0x81,
				(byte)0x00,
				(byte)0x20,
				(byte)0x00,
				(byte)0x40,
				(byte)0x00,
				(byte)0x00,
				(byte)0x00,
				(byte)0x00,
				(byte)0x00,
				(byte)0x00,
				(byte)0x00,
				(byte)0x00,
				(byte)0x00,
				(byte)0x00,
				(byte)0x00,
				(byte)0x00,
				(byte)0x00,
				(byte)0x00,
				(byte)0x00,
				(byte)0x00,
				(byte)0x00,
				(byte)0x00,
				(byte)0x00,
				(byte)0x00,
				(byte)0x00,
				(byte)0x00,
				(byte)0x00,
				(byte)0x00,
				(byte)0x00,
				(byte)0x00,
				(byte)0x00,
				(byte)0x00,
				(byte)0x00,
				(byte)0x00,
				(byte)0x00,
				(byte)0x00,
				(byte)0x00,
				(byte)0x00,
				(byte)0x00,
				(byte)0x03,
				(byte)0x02,
				(byte)0xFF,
				(byte)0x00,
				(byte)0x00,
				(byte)0x80,
				(byte)0x01,
				(byte)0x00,
				(byte)0x00,
				(byte)0x00,
				(byte)0x00,
				(byte)0x08,
				(byte)0xFA,
				(byte)0xEA,
				(byte)0x50,
				(byte)0x7C,
				(byte)0x00,
				(byte)0x00,
				(byte)0x31,
				(byte)0xC0,
				(byte)0x8E,
				(byte)0xD8,
				(byte)0x8E,
				(byte)0xD0,
				(byte)0xBC,
				(byte)0x00,
				(byte)0x20,
				(byte)0xFB,
				(byte)0xA0,
				(byte)0x40,
				(byte)0x7C,
				(byte)0x3C,
				(byte)0xFF,
				(byte)0x74,
				(byte)0x02,
				(byte)0x88,
				(byte)0xC2,
				(byte)0x52,
				(byte)0xBE,
				(byte)0x76,
				(byte)0x7D,
				(byte)0xE8,
				(byte)0x34,
				(byte)0x01,
				(byte)0xF6,
				(byte)0xC2,
				(byte)0x80,
				(byte)0x74,
				(byte)0x54,
				(byte)0xB4,
				(byte)0x41,
				(byte)0xBB,
				(byte)0xAA,
				(byte)0x55,
				(byte)0xCD,
				(byte)0x13,
				(byte)0x5A,
				(byte)0x52,
				(byte)0x72,
				(byte)0x49,
				(byte)0x81,
				(byte)0xFB,
				(byte)0x55,
				(byte)0xAA,
				(byte)0x75,
				(byte)0x43,
				(byte)0xA0,
				(byte)0x41,
				(byte)0x7C,
				(byte)0x84,
				(byte)0xC0,
				(byte)0x75,
				(byte)0x05,
				(byte)0x83,
				(byte)0xE1,
				(byte)0x01,
				(byte)0x74,
				(byte)0x37,
				(byte)0x66,
				(byte)0x8B,
				(byte)0x4C,
				(byte)0x10,
				(byte)0xBE,
				(byte)0x05,
				(byte)0x7C,
				(byte)0xC6,
				(byte)0x44,
				(byte)0xFF,
				(byte)0x01,
				(byte)0x66,
				(byte)0x8B,
				(byte)0x1E,
				(byte)0x44,
				(byte)0x7C,
				(byte)0xC7,
				(byte)0x04,
				(byte)0x10,
				(byte)0x00,
				(byte)0xC7,
				(byte)0x44,
				(byte)0x02,
				(byte)0x01,
				(byte)0x00,
				(byte)0x66,
				(byte)0x89,
				(byte)0x5C,
				(byte)0x08,
				(byte)0xC7,
				(byte)0x44,
				(byte)0x06,
				(byte)0x00,
				(byte)0x70,
				(byte)0x66,
				(byte)0x31,
				(byte)0xC0,
				(byte)0x89,
				(byte)0x44,
				(byte)0x04,
				(byte)0x66,
				(byte)0x89,
				(byte)0x44,
				(byte)0x0C,
				(byte)0xB4,
				(byte)0x42,
				(byte)0xCD,
				(byte)0x13,
				(byte)0x72,
				(byte)0x05,
				(byte)0xBB,
				(byte)0x00,
				(byte)0x70,
				(byte)0xEB,
				(byte)0x7D,
				(byte)0xB4,
				(byte)0x08,
				(byte)0xCD,
				(byte)0x13,
				(byte)0x73,
				(byte)0x0A,
				(byte)0xF6,
				(byte)0xC2,
				(byte)0x80,
				(byte)0x0F,
				(byte)0x84,
				(byte)0xF3,
				(byte)0x00,
				(byte)0xE9,
				(byte)0x8D,
				(byte)0x00,
				(byte)0xBE,
				(byte)0x05,
				(byte)0x7C,
				(byte)0xC6,
				(byte)0x44,
				(byte)0xFF,
				(byte)0x00,
				(byte)0x66,
				(byte)0x31,
				(byte)0xC0,
				(byte)0x88,
				(byte)0xF0,
				(byte)0x40,
				(byte)0x66,
				(byte)0x89,
				(byte)0x44,
				(byte)0x04,
				(byte)0x31,
				(byte)0xD2,
				(byte)0x88,
				(byte)0xCA,
				(byte)0xC1,
				(byte)0xE2,
				(byte)0x02,
				(byte)0x88,
				(byte)0xE8,
				(byte)0x88,
				(byte)0xF4,
				(byte)0x40,
				(byte)0x89,
				(byte)0x44,
				(byte)0x08,
				(byte)0x31,
				(byte)0xC0,
				(byte)0x88,
				(byte)0xD0,
				(byte)0xC0,
				(byte)0xE8,
				(byte)0x02,
				(byte)0x66,
				(byte)0x89,
				(byte)0x04,
				(byte)0x66,
				(byte)0xA1,
				(byte)0x44,
				(byte)0x7C,
				(byte)0x66,
				(byte)0x31,
				(byte)0xD2,
				(byte)0x66,
				(byte)0xF7,
				(byte)0x34,
				(byte)0x88,
				(byte)0x54,
				(byte)0x0A,
				(byte)0x66,
				(byte)0x31,
				(byte)0xD2,
				(byte)0x66,
				(byte)0xF7,
				(byte)0x74,
				(byte)0x04,
				(byte)0x88,
				(byte)0x54,
				(byte)0x0B,
				(byte)0x89,
				(byte)0x44,
				(byte)0x0C,
				(byte)0x3B,
				(byte)0x44,
				(byte)0x08,
				(byte)0x7D,
				(byte)0x3C,
				(byte)0x8A,
				(byte)0x54,
				(byte)0x0D,
				(byte)0xC0,
				(byte)0xE2,
				(byte)0x06,
				(byte)0x8A,
				(byte)0x4C,
				(byte)0x0A,
				(byte)0xFE,
				(byte)0xC1,
				(byte)0x08,
				(byte)0xD1,
				(byte)0x8A,
				(byte)0x6C,
				(byte)0x0C,
				(byte)0x5A,
				(byte)0x8A,
				(byte)0x74,
				(byte)0x0B,
				(byte)0xBB,
				(byte)0x00,
				(byte)0x70,
				(byte)0x8E,
				(byte)0xC3,
				(byte)0x31,
				(byte)0xDB,
				(byte)0xB8,
				(byte)0x01,
				(byte)0x02,
				(byte)0xCD,
				(byte)0x13,
				(byte)0x72,
				(byte)0x2A,
				(byte)0x8C,
				(byte)0xC3,
				(byte)0x8E,
				(byte)0x06,
				(byte)0x48,
				(byte)0x7C,
				(byte)0x60,
				(byte)0x1E,
				(byte)0xB9,
				(byte)0x00,
				(byte)0x01,
				(byte)0x8E,
				(byte)0xDB,
				(byte)0x31,
				(byte)0xF6,
				(byte)0x31,
				(byte)0xFF,
				(byte)0xFC,
				(byte)0xF3,
				(byte)0xA5,
				(byte)0x1F,
				(byte)0x61,
				(byte)0xFF,
				(byte)0x26,
				(byte)0x42,
				(byte)0x7C,
				(byte)0xBE,
				(byte)0x7C,
				(byte)0x7D,
				(byte)0xE8,
				(byte)0x40,
				(byte)0x00,
				(byte)0xEB,
				(byte)0x0E,
				(byte)0xBE,
				(byte)0x81,
				(byte)0x7D,
				(byte)0xE8,
				(byte)0x38,
				(byte)0x00,
				(byte)0xEB,
				(byte)0x06,
				(byte)0xBE,
				(byte)0x8B,
				(byte)0x7D,
				(byte)0xE8,
				(byte)0x30,
				(byte)0x00,
				(byte)0xBE,
				(byte)0x90,
				(byte)0x7D,
				(byte)0xE8,
				(byte)0x2A,
				(byte)0x00,
				(byte)0xEB,
				(byte)0xFE,
				(byte)0x47,
				(byte)0x52,
				(byte)0x55,
				(byte)0x42,
				(byte)0x20,
				(byte)0x00,
				(byte)0x47,
				(byte)0x65,
				(byte)0x6F,
				(byte)0x6D,
				(byte)0x00,
				(byte)0x48,
				(byte)0x61,
				(byte)0x72,
				(byte)0x64,
				(byte)0x20,
				(byte)0x44,
				(byte)0x69,
				(byte)0x73,
				(byte)0x6B,
				(byte)0x00,
				(byte)0x52,
				(byte)0x65,
				(byte)0x61,
				(byte)0x64,
				(byte)0x00,
				(byte)0x20,
				(byte)0x45,
				(byte)0x72,
				(byte)0x72,
				(byte)0x6F,
				(byte)0x72,
				(byte)0x00,
				(byte)0xBB,
				(byte)0x01,
				(byte)0x00,
				(byte)0xB4,
				(byte)0x0E,
				(byte)0xCD,
				(byte)0x10,
				(byte)0xAC,
				(byte)0x3C,
				(byte)0x00,
				(byte)0x75,
				(byte)0xF4,
				(byte)0xC3,
				(byte)0x00,
				(byte)0x00,
				(byte)0x00,
				(byte)0x00,
				(byte)0x00,
				(byte)0x00,
				(byte)0x00,
				(byte)0x00,
				(byte)0x00,
				(byte)0x00,
				(byte)0x00,
				(byte)0x00,
				(byte)0x00,
				(byte)0x00,
				(byte)0x00,
				(byte)0x00,
				(byte)0x00,
				(byte)0x00,
				(byte)0x00,
				(byte)0x00,
				(byte)0x00,
				(byte)0x00,
				(byte)0x00,
				(byte)0x00,
				(byte)0x00,
				(byte)0x00,
				(byte)0x24,
				(byte)0x12,
				(byte)0x0F,
				(byte)0x09,
				(byte)0x00,
				(byte)0xBE,
				(byte)0xBD,
				(byte)0x7D,
				(byte)0x31,
				(byte)0xC0,
				(byte)0xCD,
				(byte)0x13,
				(byte)0x46,
				(byte)0x8A,
				(byte)0x0C,
				(byte)0x80,
				(byte)0xF9,
				(byte)0x00,
				(byte)0x75,
				(byte)0x0F,
				(byte)0xBE,
				(byte)0xDA,
				(byte)0x7D,
				(byte)0xE8,
				(byte)0xC6,
				(byte)0xFF,
				(byte)0xEB,
				(byte)0x94,
				(byte)0x46,
				(byte)0x6C,
				(byte)0x6F,
				(byte)0x70,
				(byte)0x70,
				(byte)0x79,
				(byte)0x00,
				(byte)0xBB,
				(byte)0x00,
				(byte)0x70,
				(byte)0xB8,
				(byte)0x01,
				(byte)0x02,
				(byte)0xB5,
				(byte)0x00,
				(byte)0xB6,
				(byte)0x00,
				(byte)0xCD,
				(byte)0x13,
				(byte)0x72,
				(byte)0xD7,
				(byte)0xB6,
				(byte)0x01,
				(byte)0xB5,
				(byte)0x4F,
				(byte)0xE9,
				(byte)0xDD,
				(byte)0xFE,
				(byte)0x00,
				(byte)0x00,
				(byte)0x00,
				(byte)0x00,
				(byte)0x00,
				(byte)0x00,
				(byte)0x00,
				(byte)0x00,
				(byte)0x55,
				(byte)0xAA });
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?