r48 - in trunk/diswork-fs/src: main/java/org/nuiton/disworkfs main/java/org/nuiton/disworkfs/storage test/java/org/nuiton/disworkfs
Author: bleny Date: 2010-05-21 17:04:11 +0200 (Fri, 21 May 2010) New Revision: 48 Url: http://nuiton.org/repositories/revision/diswork/48 Log: ln, remove with tests Modified: trunk/diswork-fs/src/main/java/org/nuiton/disworkfs/DistributedFileSystem.java trunk/diswork-fs/src/main/java/org/nuiton/disworkfs/DisworkFileSystem.java trunk/diswork-fs/src/main/java/org/nuiton/disworkfs/storage/EntryUtil.java trunk/diswork-fs/src/test/java/org/nuiton/disworkfs/DistributedFileSystemTest.java trunk/diswork-fs/src/test/java/org/nuiton/disworkfs/EntryUtilTest.java Modified: trunk/diswork-fs/src/main/java/org/nuiton/disworkfs/DistributedFileSystem.java =================================================================== --- trunk/diswork-fs/src/main/java/org/nuiton/disworkfs/DistributedFileSystem.java 2010-05-21 13:01:45 UTC (rev 47) +++ trunk/diswork-fs/src/main/java/org/nuiton/disworkfs/DistributedFileSystem.java 2010-05-21 15:04:11 UTC (rev 48) @@ -15,4 +15,8 @@ public void mkdir(String path) throws IOException; + public void ln(String path, String target) throws IOException; + + public void remove(String path) throws IOException; + } Modified: trunk/diswork-fs/src/main/java/org/nuiton/disworkfs/DisworkFileSystem.java =================================================================== --- trunk/diswork-fs/src/main/java/org/nuiton/disworkfs/DisworkFileSystem.java 2010-05-21 13:01:45 UTC (rev 47) +++ trunk/diswork-fs/src/main/java/org/nuiton/disworkfs/DisworkFileSystem.java 2010-05-21 15:04:11 UTC (rev 48) @@ -3,6 +3,7 @@ import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; +import java.util.Map.Entry; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -206,5 +207,88 @@ throw new IOException(parent + " is not a directory"); } } + + @Override + public void ln(String path, String target) throws IOException { + String parent = EntryUtil.getParentFromPath(path); + String name = EntryUtil.getNameFromPath(path); + ln(parent, name, target); + } + public void ln(String parent, String name, String target) throws IOException { + if (exists(target)) { + + String entryParent = walk(parent); + + if (entryParent == null) { + throw new IOException(parent + " directory doesn't exists"); + } + + if (EntryUtil.isDirectory(entryParent)) { + String parentId = EntryUtil.getId(entryParent); + String content = storage.getDirectory(parentId); + String newLinkId = EntryUtil.generateId(); + String newContent = EntryUtil.addEntryToDirectoryContent( + content, EntryUtil.TYPE.L, name, newLinkId); + + // store link before meta info + storage.putLink(newLinkId, target); + // update meta info directory + storage.putDirectory(parentId, newContent); + } else { + throw new IOException(parent + " is not a directory"); + } + + + } else { + throw new IOException(target + " is not a valid target"); + } + } + + @Override + public void remove(String path) throws IOException { + String parent = EntryUtil.getParentFromPath(path); + String name = EntryUtil.getNameFromPath(path); + log.info("trying to remove " + path); + remove(parent, name); + } + + protected void remove(String parent, String name) throws IOException { + String entryParent = walk(parent); + + if (entryParent == null) { + throw new IOException(parent + " directory doesn't exists"); + } + + if (EntryUtil.isDirectory(entryParent)) { + String parentId = EntryUtil.getId(entryParent); + String content = storage.getDirectory(parentId); + + String entry = EntryUtil.findEntryInDirectory(content, name); + String idToRemove = EntryUtil.getId(entry); + + String newContent = EntryUtil.removeEntryFromEntries( + content, name); + + // check if not removing a non-empty directory + if (EntryUtil.isDirectory(entry)) { + String innerDirectoryId = EntryUtil.getId(entry); + String innerDirectoryContent = storage.getDirectory(innerDirectoryId); + if (! innerDirectoryContent.equals(EntryUtil.newEmptyDirectoryContent())) { + // directory is not empty + throw new IOException("trying to remove a non-empty directory"); + } + } + + + // update meta info directory + storage.putDirectory(parentId, newContent); + // store file before meta info + storage.remove(idToRemove); + + } else { + throw new IOException(parent + " is not a directory"); + } + } + } Modified: trunk/diswork-fs/src/main/java/org/nuiton/disworkfs/storage/EntryUtil.java =================================================================== --- trunk/diswork-fs/src/main/java/org/nuiton/disworkfs/storage/EntryUtil.java 2010-05-21 13:01:45 UTC (rev 47) +++ trunk/diswork-fs/src/main/java/org/nuiton/disworkfs/storage/EntryUtil.java 2010-05-21 15:04:11 UTC (rev 48) @@ -212,5 +212,11 @@ public static String getNameFromPath(String path) { return FilenameUtils.getName(path); } + + public static String removeEntryFromEntries(String content, String name) { + String entry = findEntryInDirectory(content, name); + String newContent = content.replaceFirst(entry + ENTRIES_SEPARATOR, ""); + return newContent; + } } Modified: trunk/diswork-fs/src/test/java/org/nuiton/disworkfs/DistributedFileSystemTest.java =================================================================== --- trunk/diswork-fs/src/test/java/org/nuiton/disworkfs/DistributedFileSystemTest.java 2010-05-21 13:01:45 UTC (rev 47) +++ trunk/diswork-fs/src/test/java/org/nuiton/disworkfs/DistributedFileSystemTest.java 2010-05-21 15:04:11 UTC (rev 48) @@ -182,7 +182,7 @@ * doesn't exists */ @Test(expected = IOException.class) - public void testWriteFail() throws Exception { + public void testFailAtWrite() throws Exception { fileSystem.write("/my_folder", "my_file", new FileInputStream(randomFilePath)); } @@ -203,5 +203,46 @@ assertTrue(fileSystem.exists("/my_folder/my_sub_folder/my_file")); } + @Test + public void testln() throws Exception { + fileSystem.mkdir("/my_folder"); + fileSystem.write("/my_folder", "my_file", new FileInputStream(randomFilePath)); + fileSystem.ln("/my_link", "/my_folder/my_file"); + + InputStream source = new FileInputStream(randomFilePath); + InputStream readResult = fileSystem.read("/my_link"); + + boolean actualContentEquality = + IOUtils.contentEquals(source, readResult); + source.close(); + readResult.close(); + + assertTrue(actualContentEquality); + } + + @Test(expected = IOException.class) + public void testFailAtLn() throws Exception { + fileSystem.ln("/my_link", "/wrong_target_path"); + } + + @Test + public void testRemove() throws Exception { + fileSystem.mkdir("/my_folder"); + fileSystem.write("/my_folder", "my_file", new FileInputStream(randomFilePath)); + fileSystem.remove("/my_folder/my_file"); + assertTrue(fileSystem.exists("/my_folder")); + assertFalse(fileSystem.exists("/my_folder/my_file")); + fileSystem.remove("/my_folder"); + assertFalse(fileSystem.exists("/my_folder")); + } + + @Test(expected = IOException.class) + public void testFailAtRemove() throws Exception { + fileSystem.mkdir("/my_folder"); + fileSystem.write("/my_folder", "my_file", new FileInputStream(randomFilePath)); + + // trying to remove a non-empty directory should raise an exception + fileSystem.remove("/my_folder"); + } } Modified: trunk/diswork-fs/src/test/java/org/nuiton/disworkfs/EntryUtilTest.java =================================================================== --- trunk/diswork-fs/src/test/java/org/nuiton/disworkfs/EntryUtilTest.java 2010-05-21 13:01:45 UTC (rev 47) +++ trunk/diswork-fs/src/test/java/org/nuiton/disworkfs/EntryUtilTest.java 2010-05-21 15:04:11 UTC (rev 48) @@ -142,4 +142,20 @@ assertEquals(expectedBlocksIds[1], actualBlocksIds[1]); assertEquals(expectedBlocksIds[2], actualBlocksIds[2]); } + + @Test + public void testRemoveEntryFromEntries() { + + String content = directoryEntry + EntryUtil.ENTRIES_SEPARATOR + + fileEntry + EntryUtil.ENTRIES_SEPARATOR + + linkEntry + EntryUtil.ENTRIES_SEPARATOR; + + String expected = directoryEntry + EntryUtil.ENTRIES_SEPARATOR + + linkEntry + EntryUtil.ENTRIES_SEPARATOR; + + String actual = EntryUtil.removeEntryFromEntries(content, "myfile"); + + assertEquals(expected, actual); + + } }
participants (1)
-
bleny@users.nuiton.org