From f2909cfe5c53d06910bde2295662ecce27c49797 Mon Sep 17 00:00:00 2001 From: ZekeAranyLucas Date: Thu, 23 Nov 2023 21:44:59 -0800 Subject: [PATCH] ImfsContext supports rmdir --- README.md | 4 ++-- src/main/java/com/imfs/ImfsContext.java | 4 ++++ src/main/java/com/imfs/ImfsFileSystem.java | 4 ++++ src/main/java/com/imfs/ImfsProvider.java | 8 +++++--- src/test/java/com/imfs/ImfsContextTest.java | 15 +++++++++++++++ 5 files changed, 30 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index d5f227c..a4fd5fc 100644 --- a/README.md +++ b/README.md @@ -25,8 +25,8 @@ - [x] Create a new directory. The current working directory is the parent. - [x] Get the directory contents: Returns the children of the current working directory. Example: [‘math’, ‘history’, ‘spanish’] -- [] Remove a directory. The target directory must be among the current working directory’s - children. +- [x] Remove a directory. The target directory must be among the current working directory’s + children. - [] Create a new file: Creates a new empty file in the current working directory. - [] Write file contents: Writes the specified contents to a file in the current working directory. All file contents will fit into memory. diff --git a/src/main/java/com/imfs/ImfsContext.java b/src/main/java/com/imfs/ImfsContext.java index 6f6cdca..4f6c794 100644 --- a/src/main/java/com/imfs/ImfsContext.java +++ b/src/main/java/com/imfs/ImfsContext.java @@ -59,4 +59,8 @@ public ImfsContext cd(String string) { return null; } + public void rmdir(String string) throws IOException { + Files.delete(this.path.resolve(string)); + } + } diff --git a/src/main/java/com/imfs/ImfsFileSystem.java b/src/main/java/com/imfs/ImfsFileSystem.java index eed92ed..621ef61 100644 --- a/src/main/java/com/imfs/ImfsFileSystem.java +++ b/src/main/java/com/imfs/ImfsFileSystem.java @@ -126,4 +126,8 @@ public boolean contains(String kid) { public void reset() { entries = initEntries(key); } + + public void removeEntry(String kid) { + entries.remove(kid); + } } diff --git a/src/main/java/com/imfs/ImfsProvider.java b/src/main/java/com/imfs/ImfsProvider.java index 1e90c69..3079962 100644 --- a/src/main/java/com/imfs/ImfsProvider.java +++ b/src/main/java/com/imfs/ImfsProvider.java @@ -57,9 +57,11 @@ public void createDirectory(Path path, FileAttribute... arg1) throws IOExcept } @Override - public void delete(Path arg0) throws IOException { - // TODO Auto-generated method stub - throw new UnsupportedOperationException("Unimplemented method 'delete'"); + public void delete(Path path) throws IOException { + var imfsPath = checkPath(path); + var fileSystem = (ImfsFileSystem) imfsPath.getFileSystem(); + var kid = imfsPath.getMaterializedPath(); + fileSystem.removeEntry(kid); } @Override diff --git a/src/test/java/com/imfs/ImfsContextTest.java b/src/test/java/com/imfs/ImfsContextTest.java index 4f70672..1c5ffce 100644 --- a/src/test/java/com/imfs/ImfsContextTest.java +++ b/src/test/java/com/imfs/ImfsContextTest.java @@ -5,6 +5,7 @@ import java.io.IOException; import java.net.URI; +import java.nio.file.Files; import java.nio.file.Paths; import org.junit.Before; @@ -86,4 +87,18 @@ public void testCdLevels() throws IOException { assertEquals(0, foo.ls().size()); } + @Test + public void testRmdir() throws IOException { + var context = new ImfsContext("imfs://ImfsContextTest/"); + var kids = context.ls(); + assertArrayEquals(new String[] { "math", "history", "spanish" }, kids.toArray()); + + var history = Paths.get(URI.create("imfs://ImfsContextTest/history")); + assertEquals(true, Files.isDirectory(history)); + + context.rmdir("history"); + kids = context.ls(); + assertArrayEquals(new String[] { "math", "spanish" }, kids.toArray()); + assertEquals(false, Files.isDirectory(history)); + } }