// Path: src/jext2/exceptions/InvalidArgument.java
// public class InvalidArgument extends JExt2Exception {
// 	static final long serialVersionUID = 42;
// 	protected final static int ERRNO = Errno.EINVAL;
// 
// 	public int getErrno() {
// 		return ERRNO;
// 	}
// }
// 
// Path: src/jext2/exceptions/IoError.java
// public class IoError extends JExt2Exception {
// 	static final long serialVersionUID = 42;
// 	protected static final int ERRNO=Errno.EIO;
// 	public IoError(String msg) {
// 		super(msg);
// 	}
// 	public IoError() {
// 		super();
// 	}
// 	public int getErrno() {
// 		return ERRNO;
// 	}
// }
// 
// Path: src/jext2/exceptions/JExt2Exception.java
// public class JExt2Exception extends Exception {
// 	protected static final int ERRNO = -1;
// 
// 	private static final long serialVersionUID = -7429088074385678308L;
// 
// 	Logger logger = Filesystem.getLogger();
// 
// 	public JExt2Exception() {
// 		log("");
// 	}
// 
// 	public JExt2Exception(String msg) {
// 		log(msg);
// 	}
// 
// 	private void log(String msg) {
// 		if (logger.isLoggable(Level.FINE)) {
// 			StackTraceElement[] stack = getStackTrace();
// 
// 			StringBuilder log = new StringBuilder();
// 			log.append("JEXT2 exception was raised: ");
// 			log.append(this.getClass().getSimpleName());
// 			log.append(" source=");
// 			log.append(stack[0].getClassName());
// 			log.append("->");
// 			log.append(stack[0].getMethodName());
// 			log.append(" msg=");
// 			log.append(msg);
// 			log.append(" errno=");
// 			log.append(getErrno());
// 
// 			logger.fine(log.toString());
// 		}
// 	}
// 
// 	public int getErrno() {
// 		throw new RuntimeException("This method should not be executed. " +
// 				"- The errno value defined here is meaningless");
// 	}
// }
// 
// Path: src/jext2/exceptions/NoSuchFileOrDirectory.java
// public class NoSuchFileOrDirectory extends JExt2Exception {
// 	static final long serialVersionUID = 42;
// 	protected static final int ERRNO = Errno.ENOENT;
// 
// 	public int getErrno() {
// 		return ERRNO;
// 	}
// }


import java.nio.ByteBuffer;
import java.util.logging.Level;
import jext2.exceptions.InvalidArgument;
import jext2.exceptions.IoError;
import jext2.exceptions.JExt2Exception;
import jext2.exceptions.NoSuchFileOrDirectory;
/*
 * Copyright (c) 2011 Marcel Lauhoff.
 * 
 * This file is part of jext2.
 * 
 * jext2 is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * jext2 is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with jext2.  If not, see <http://www.gnu.org/licenses/>.
 */

package jext2;


public class InodeAccess extends DataStructureAccessProvider<Long, Inode>{
	private static InodeAccess _instance = new InodeAccess();

	private static Superblock superblock = Superblock.getInstance();
	private static BlockAccess blocks = BlockAccess.getInstance();
	private static BlockGroupAccess blockGroups = BlockGroupAccess.getInstance();

	private InodeAccess() {
		super(1000);
	}

	public static Inode readFromByteBuffer(ByteBuffer buf) throws IoError {
		Mode mode = Mode.createWithNumericValue(Ext2fsDataTypes.getLE16(buf, 0)); /* NOTE: mode is first field in inode */

		if (mode.isDirectory()) {
			return DirectoryInode.fromByteBuffer(buf, 0);
		} else if (mode.isRegular()) {
			return RegularInode.fromByteBuffer(buf, 0);
		} else if (mode.isSymlink()) {
			return SymlinkInode.fromByteBuffer(buf, 0);
		} else {
			return Inode.fromByteBuffer(buf, 0);
		}
	}

	public static Inode readByIno(long ino) throws IoError, InvalidArgument {
		if (ino == 0 || ino > superblock.getInodesCount()) {
			throw new InvalidArgument();
		}

		int group = Calculations.groupOfIno(ino);
// Path: src/jext2/exceptions/InvalidArgument.java
// public class InvalidArgument extends JExt2Exception {
// 	static final long serialVersionUID = 42;
// 	protected final static int ERRNO = Errno.EINVAL;
// 
// 	public int getErrno() {
// 		return ERRNO;
// 	}
// }
// 
// Path: src/jext2/exceptions/IoError.java
// public class IoError extends JExt2Exception {
// 	static final long serialVersionUID = 42;
// 	protected static final int ERRNO=Errno.EIO;
// 	public IoError(String msg) {
// 		super(msg);
// 	}
// 	public IoError() {
// 		super();
// 	}
// 	public int getErrno() {
// 		return ERRNO;
// 	}
// }
// 
// Path: src/jext2/exceptions/JExt2Exception.java
// public class JExt2Exception extends Exception {
// 	protected static final int ERRNO = -1;
// 
// 	private static final long serialVersionUID = -7429088074385678308L;
// 
// 	Logger logger = Filesystem.getLogger();
// 
// 	public JExt2Exception() {
// 		log("");
// 	}
// 
// 	public JExt2Exception(String msg) {
// 		log(msg);
// 	}
// 
// 	private void log(String msg) {
// 		if (logger.isLoggable(Level.FINE)) {
// 			StackTraceElement[] stack = getStackTrace();
// 
// 			StringBuilder log = new StringBuilder();
// 			log.append("JEXT2 exception was raised: ");
// 			log.append(this.getClass().getSimpleName());
// 			log.append(" source=");
// 			log.append(stack[0].getClassName());
// 			log.append("->");
// 			log.append(stack[0].getMethodName());
// 			log.append(" msg=");
// 			log.append(msg);
// 			log.append(" errno=");
// 			log.append(getErrno());
// 
// 			logger.fine(log.toString());
// 		}
// 	}
// 
// 	public int getErrno() {
// 		throw new RuntimeException("This method should not be executed. " +
// 				"- The errno value defined here is meaningless");
// 	}
// }
// 
// Path: src/jext2/exceptions/NoSuchFileOrDirectory.java
// public class NoSuchFileOrDirectory extends JExt2Exception {
// 	static final long serialVersionUID = 42;
// 	protected static final int ERRNO = Errno.ENOENT;
// 
// 	public int getErrno() {
// 		return ERRNO;
// 	}
// }


// Path: src/jext2/InodeAccess.java
import java.nio.ByteBuffer;
import java.util.logging.Level;
import jext2.exceptions.InvalidArgument;
import jext2.exceptions.IoError;
import jext2.exceptions.JExt2Exception;
import jext2.exceptions.NoSuchFileOrDirectory;

/*
 * Copyright (c) 2011 Marcel Lauhoff.
 * 
 * This file is part of jext2.
 * 
 * jext2 is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * jext2 is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with jext2.  If not, see <http://www.gnu.org/licenses/>.
 */

package jext2;


public class InodeAccess extends DataStructureAccessProvider<Long, Inode>{
	private static InodeAccess _instance = new InodeAccess();

	private static Superblock superblock = Superblock.getInstance();
	private static BlockAccess blocks = BlockAccess.getInstance();
	private static BlockGroupAccess blockGroups = BlockGroupAccess.getInstance();

	private InodeAccess() {
		super(1000);
	}

	public static Inode readFromByteBuffer(ByteBuffer buf) throws IoError {
		Mode mode = Mode.createWithNumericValue(Ext2fsDataTypes.getLE16(buf, 0)); /* NOTE: mode is first field in inode */

		if (mode.isDirectory()) {
			return DirectoryInode.fromByteBuffer(buf, 0);
		} else if (mode.isRegular()) {
			return RegularInode.fromByteBuffer(buf, 0);
		} else if (mode.isSymlink()) {
			return SymlinkInode.fromByteBuffer(buf, 0);
		} else {
			return Inode.fromByteBuffer(buf, 0);
		}
	}

	public static Inode readByIno(long ino) throws IoError, InvalidArgument {
		if (ino == 0 || ino > superblock.getInodesCount()) {
			throw new InvalidArgument();
		}

		int group = Calculations.groupOfIno(ino);
		int offset = Calculations.localInodeOffset(ino);