From: <ga...@us...> - 2009-03-05 16:35:21
|
Revision: 5088 http://jnode.svn.sourceforge.net/jnode/?rev=5088&view=rev Author: galatnm Date: 2009-03-05 16:31:25 +0000 (Thu, 05 Mar 2009) Log Message: ----------- Don't use AbstractFS classes anymore. Modified Paths: -------------- trunk/fs/src/fs/org/jnode/fs/hfsplus/HFSPlusDirectory.java trunk/fs/src/fs/org/jnode/fs/hfsplus/HFSPlusEntry.java trunk/fs/src/fs/org/jnode/fs/hfsplus/HFSPlusFile.java trunk/fs/src/fs/org/jnode/fs/hfsplus/HfsPlusFileSystem.java Modified: trunk/fs/src/fs/org/jnode/fs/hfsplus/HFSPlusDirectory.java =================================================================== --- trunk/fs/src/fs/org/jnode/fs/hfsplus/HFSPlusDirectory.java 2009-03-05 11:33:43 UTC (rev 5087) +++ trunk/fs/src/fs/org/jnode/fs/hfsplus/HFSPlusDirectory.java 2009-03-05 16:31:25 UTC (rev 5088) @@ -17,16 +17,18 @@ * along with this library; If not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ - + package org.jnode.fs.hfsplus; import java.io.IOException; import java.util.Calendar; import java.util.Date; +import java.util.Iterator; import java.util.LinkedList; import java.util.List; import org.apache.log4j.Logger; +import org.jnode.fs.FSDirectory; import org.jnode.fs.FSEntry; import org.jnode.fs.ReadOnlyFileSystemException; import org.jnode.fs.hfsplus.catalog.CatalogFolder; @@ -34,93 +36,117 @@ import org.jnode.fs.hfsplus.catalog.CatalogNodeId; import org.jnode.fs.hfsplus.catalog.CatalogThread; import org.jnode.fs.hfsplus.tree.LeafRecord; -import org.jnode.fs.spi.AbstractFSDirectory; import org.jnode.fs.spi.FSEntryTable; -public class HFSPlusDirectory extends AbstractFSDirectory { - private final Logger log = Logger.getLogger(getClass()); +public class HFSPlusDirectory extends HFSPlusEntry implements FSDirectory { - private LeafRecord record; + private static final Logger log = Logger.getLogger(HFSPlusDirectory.class); + /** Table of entries of our parent */ + private FSEntryTable entries; + private CatalogFolder folder; - public HFSPlusDirectory(final HFSPlusEntry e) { - super((HfsPlusFileSystem) e.getFileSystem()); - this.record = e.getRecord(); + public HFSPlusDirectory(HfsPlusFileSystem fs, HFSPlusDirectory parent, String name, + LeafRecord record) { + super(fs, parent, name, record); this.folder = new CatalogFolder(record.getData()); - log.debug("Associated record:" + record.toString()); - if (record.getType() == HfsPlusConstants.RECORD_TYPE_FOLDER) { - log.debug("Associated folder : " + folder.toString()); - } } + public FSEntryTable getTable() { + return entries; + } + @Override - protected final FSEntry createDirectoryEntry(final String name) - throws IOException { - if (!canWrite()) { - throw new ReadOnlyFileSystemException(); - } - Superblock volumeHeader = ((HfsPlusFileSystem) getFileSystem()) - .getVolumeHeader(); + public FSEntry addDirectory(String name) throws IOException { + // TODO Auto-generated method stub + return null; + } - Calendar now = Calendar.getInstance(); - now.setTime(new Date()); - int macDate = (int) HFSUtils.getDate(now.getTimeInMillis() / 1000, true); + @Override + public FSEntry addFile(String name) throws IOException { + // TODO Auto-generated method stub + return null; + } - HFSUnicodeString dirName = new HFSUnicodeString(name); - CatalogThread thread = new CatalogThread( - HfsPlusConstants.RECORD_TYPE_FOLDER_THREAD, this.folder - .getFolderId(), dirName); + @Override + public void flush() throws IOException { + // TODO Auto-generated method stub - CatalogFolder newFolder = new CatalogFolder(); - newFolder - .setFolderId(new CatalogNodeId(volumeHeader.getNextCatalogId())); - newFolder.setCreateDate(macDate); - newFolder.setContentModDate(macDate); - newFolder.setAttrModDate(macDate); - log.debug("New catalog folder :\n" + newFolder.toString()); + } - CatalogKey key = new CatalogKey(this.folder.getFolderId(), dirName); - log.debug("New catalog key :\n" + key.toString()); + @Override + public FSEntry getEntry(String name) throws IOException { + // TODO Auto-generated method stub + return null; + } - LeafRecord folderRecord = new LeafRecord(key, newFolder.getBytes()); - log.debug("New record folder :\n" + folderRecord.toString()); - - HFSPlusEntry newEntry = new HFSPlusEntry( - (HfsPlusFileSystem) getFileSystem(), null, this, name, - folderRecord); - volumeHeader.setFolderCount(volumeHeader.getFolderCount() + 1); - log.debug("New volume header :\n" + volumeHeader.toString()); - - return newEntry; + @Override + public Iterator<? extends FSEntry> iterator() throws IOException { + // TODO Auto-generated method stub + return null; } @Override - protected final FSEntry createFileEntry(final String name) - throws IOException { - throw new ReadOnlyFileSystemException(); + public void remove(String name) throws IOException { + // TODO Auto-generated method stub + } - public synchronized void remove(String name) throws IOException { - if (!canWrite()) { - throw new ReadOnlyFileSystemException(); + // Helper methods + + /** + * BE CAREFULL : don't call this method from the constructor of this class + * because it call the method readEntries of the child classes that are not + * yet initialized (constructed). + */ + protected final void checkEntriesLoaded() { + if (!isEntriesLoaded()) { + log.debug("checkEntriesLoaded : loading"); + try { + if (rights.canRead()) { + entries = readEntries(); + } else { + // the next time, we will call checkEntriesLoaded() + // we will retry to load entries + entries = FSEntryTable.EMPTY_TABLE; + log.debug("checkEntriesLoaded : can't read, using EMPTY_TABLE"); + } + resetDirty(); + } catch (IOException e) { + log.fatal("unable to read directory entries", e); + // the next time, we will call checkEntriesLoaded() + // we will retry to load entries + entries = FSEntryTable.EMPTY_TABLE; + } } + log.debug("<<< END checkEntriesLoaded >>>"); } - @Override - protected final FSEntryTable readEntries() throws IOException { + /** + * Have we already loaded our entries from device ? + * + * @return if the entries are already loaded from the device + */ + private final boolean isEntriesLoaded() { + return (entries != FSEntryTable.EMPTY_TABLE); + } + + /** + * + * @return + * @throws IOException + */ + private final FSEntryTable readEntries() throws IOException { List<FSEntry> pathList = new LinkedList<FSEntry>(); HfsPlusFileSystem fs = (HfsPlusFileSystem) getFileSystem(); if (fs.getVolumeHeader().getFolderCount() > 0) { LeafRecord[] records = fs.getCatalog().getRecords(folder.getFolderId()); for (LeafRecord rec : records) { - if (rec.getType() == HfsPlusConstants.RECORD_TYPE_FOLDER - || rec.getType() == HfsPlusConstants.RECORD_TYPE_FILE) { - String name = ((CatalogKey) rec.getKey()).getNodeName() - .getUnicodeString(); - HFSPlusEntry e = new HFSPlusEntry( - (HfsPlusFileSystem) getFileSystem(), null, this, name, - rec); + if (rec.getType() == HfsPlusConstants.RECORD_TYPE_FOLDER || + rec.getType() == HfsPlusConstants.RECORD_TYPE_FILE) { + String name = ((CatalogKey) rec.getKey()).getNodeName().getUnicodeString(); + HFSPlusEntry e = new HFSPlusEntry(fs, this, name, rec); pathList.add(e); } } @@ -128,10 +154,46 @@ return new FSEntryTable(((HfsPlusFileSystem) getFileSystem()), pathList); } - @Override - protected void writeEntries(final FSEntryTable entries) throws IOException { - // TODO Auto-generated method stub + /** + * + * @param name + * @return + * @throws IOException + */ + private final FSEntry createDirectoryEntry(final String name) throws IOException { + if (fs.isReadOnly()) { + throw new ReadOnlyFileSystemException(); + } + Superblock volumeHeader = ((HfsPlusFileSystem) getFileSystem()).getVolumeHeader(); + + Calendar now = Calendar.getInstance(); + now.setTime(new Date()); + int macDate = (int) HFSUtils.getDate(now.getTimeInMillis() / 1000, true); + + HFSUnicodeString dirName = new HFSUnicodeString(name); + CatalogThread thread = + new CatalogThread(HfsPlusConstants.RECORD_TYPE_FOLDER_THREAD, this.folder + .getFolderId(), dirName); + + CatalogFolder newFolder = new CatalogFolder(); + newFolder.setFolderId(new CatalogNodeId(volumeHeader.getNextCatalogId())); + newFolder.setCreateDate(macDate); + newFolder.setContentModDate(macDate); + newFolder.setAttrModDate(macDate); + log.debug("New catalog folder :\n" + newFolder.toString()); + + CatalogKey key = new CatalogKey(this.folder.getFolderId(), dirName); + log.debug("New catalog key :\n" + key.toString()); + + LeafRecord folderRecord = new LeafRecord(key, newFolder.getBytes()); + log.debug("New record folder :\n" + folderRecord.toString()); + + HFSPlusEntry newEntry = new HFSPlusEntry(fs, this, name, folderRecord); + volumeHeader.setFolderCount(volumeHeader.getFolderCount() + 1); + log.debug("New volume header :\n" + volumeHeader.toString()); + + return newEntry; } } Modified: trunk/fs/src/fs/org/jnode/fs/hfsplus/HFSPlusEntry.java =================================================================== --- trunk/fs/src/fs/org/jnode/fs/hfsplus/HFSPlusEntry.java 2009-03-05 11:33:43 UTC (rev 5087) +++ trunk/fs/src/fs/org/jnode/fs/hfsplus/HFSPlusEntry.java 2009-03-05 16:31:25 UTC (rev 5088) @@ -17,24 +17,52 @@ * along with this library; If not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ - + package org.jnode.fs.hfsplus; +import java.io.IOException; + +import org.jnode.fs.FSAccessRights; +import org.jnode.fs.FSDirectory; +import org.jnode.fs.FSEntry; +import org.jnode.fs.FSFile; +import org.jnode.fs.FileSystem; import org.jnode.fs.hfsplus.tree.LeafRecord; import org.jnode.fs.spi.AbstractFSEntry; -import org.jnode.fs.spi.FSEntryTable; +import org.jnode.fs.spi.UnixFSAccessRights; -public class HFSPlusEntry extends AbstractFSEntry { +public class HFSPlusEntry implements FSEntry { - private LeafRecord record; + protected HfsPlusFileSystem fs; + protected HFSPlusDirectory parent; + protected String name; + protected LeafRecord record; + private int type; - public HFSPlusEntry(final HfsPlusFileSystem fs, final FSEntryTable table, final HFSPlusDirectory parent, - final String name, final LeafRecord record) { - super(fs, table, parent, name, getFSEntryType(name, record)); + protected boolean valid; + protected boolean dirty; + protected FSAccessRights rights; + private long lastModified; + + /** + * + * @param fs + * @param parent + * @param name + * @param record + */ + public HFSPlusEntry(HfsPlusFileSystem fs, HFSPlusDirectory parent, String name, + LeafRecord record) { + this.fs = fs; + this.parent = parent; + this.name = name; this.record = record; + this.type = getFSEntryType(); + this.rights = new UnixFSAccessRights(fs); + this.lastModified = System.currentTimeMillis(); } - private static int getFSEntryType(final String name, final LeafRecord record) { + private int getFSEntryType() { int mode = record.getType(); if ("/".equals(name)) { return AbstractFSEntry.ROOT_ENTRY; @@ -47,7 +75,91 @@ } } - public final LeafRecord getRecord() { - return record; + @Override + public FSAccessRights getAccessRights() throws IOException { + return rights; } + + @Override + public FSDirectory getDirectory() throws IOException { + if (!isFile()) { + throw new IOException("It is not a Directory"); + } + return (HFSPlusDirectory) this; + } + + @Override + public FSFile getFile() throws IOException { + if (!isFile()) { + throw new IOException("It is not a file"); + } + return (HFSPlusFile) this; + } + + @Override + public long getLastModified() throws IOException { + // TODO Auto-generated method stub + return lastModified; + } + + @Override + public String getName() { + return name; + } + + @Override + public FSDirectory getParent() { + return parent; + } + + @Override + public boolean isDirectory() { + return (type == AbstractFSEntry.DIR_ENTRY || type == AbstractFSEntry.ROOT_ENTRY); + } + + @Override + public boolean isDirty() throws IOException { + return dirty; + } + + public void setDirty() { + dirty = true; + } + + public void resetDirty() { + dirty = false; + } + + @Override + public boolean isFile() { + return (type == AbstractFSEntry.FILE_ENTRY); + } + + @Override + public void setLastModified(long lastModified) throws IOException { + this.lastModified = lastModified; + } + + @Override + public void setName(String newName) throws IOException { + if (type == AbstractFSEntry.ROOT_ENTRY) { + throw new IOException("Cannot change name of root directory"); + } + if (parent.getTable().rename(name, newName) < 0) { + throw new IOException("Cannot change name"); + } + + this.name = newName; + } + + @Override + public FileSystem<?> getFileSystem() { + return fs; + } + + @Override + public boolean isValid() { + return valid; + } + } Modified: trunk/fs/src/fs/org/jnode/fs/hfsplus/HFSPlusFile.java =================================================================== --- trunk/fs/src/fs/org/jnode/fs/hfsplus/HFSPlusFile.java 2009-03-05 11:33:43 UTC (rev 5087) +++ trunk/fs/src/fs/org/jnode/fs/hfsplus/HFSPlusFile.java 2009-03-05 16:31:25 UTC (rev 5088) @@ -17,24 +17,23 @@ * along with this library; If not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ - + package org.jnode.fs.hfsplus; import java.io.IOException; import java.nio.ByteBuffer; +import org.jnode.fs.FSFile; import org.jnode.fs.hfsplus.catalog.CatalogFile; import org.jnode.fs.hfsplus.extent.ExtentDescriptor; import org.jnode.fs.hfsplus.tree.LeafRecord; -import org.jnode.fs.spi.AbstractFSFile; -public class HFSPlusFile extends AbstractFSFile { - private LeafRecord record; +public class HFSPlusFile extends HFSPlusEntry implements FSFile { + private CatalogFile file; - public HFSPlusFile(final HFSPlusEntry e) { - super((HfsPlusFileSystem) e.getFileSystem()); - this.record = e.getRecord(); + public HFSPlusFile(HfsPlusFileSystem fs, HFSPlusDirectory parent, String name, LeafRecord record) { + super(fs, parent, name, record); this.file = new CatalogFile(record.getData()); } Modified: trunk/fs/src/fs/org/jnode/fs/hfsplus/HfsPlusFileSystem.java =================================================================== --- trunk/fs/src/fs/org/jnode/fs/hfsplus/HfsPlusFileSystem.java 2009-03-05 11:33:43 UTC (rev 5087) +++ trunk/fs/src/fs/org/jnode/fs/hfsplus/HfsPlusFileSystem.java 2009-03-05 16:31:25 UTC (rev 5088) @@ -90,21 +90,19 @@ @Override protected final FSDirectory createDirectory(final FSEntry entry) throws IOException { - HFSPlusEntry e = (HFSPlusEntry) entry; - return new HFSPlusDirectory(e); + return entry.getDirectory(); } @Override protected final FSFile createFile(final FSEntry entry) throws IOException { - HFSPlusEntry e = (HFSPlusEntry) entry; - return new HFSPlusFile(e); + return entry.getFile(); } @Override protected final HFSPlusEntry createRootEntry() throws IOException { LeafRecord record = catalog.getRecord(CatalogNodeId.HFSPLUS_POR_CNID); if (record != null) { - return new HFSPlusEntry(this, null, null, "/", record); + return new HFSPlusEntry(this, null, "/", record); } log.debug("Root entry : No record found."); return null; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |