Skip to content

Commit

Permalink
ImfsContext supports rmdir
Browse files Browse the repository at this point in the history
  • Loading branch information
ZekeAranyLucas committed Nov 24, 2023
1 parent 4180344 commit f2909cf
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 5 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/imfs/ImfsContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,8 @@ public ImfsContext cd(String string) {
return null;
}

public void rmdir(String string) throws IOException {
Files.delete(this.path.resolve(string));
}

}
4 changes: 4 additions & 0 deletions src/main/java/com/imfs/ImfsFileSystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,8 @@ public boolean contains(String kid) {
public void reset() {
entries = initEntries(key);
}

public void removeEntry(String kid) {
entries.remove(kid);
}
}
8 changes: 5 additions & 3 deletions src/main/java/com/imfs/ImfsProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions src/test/java/com/imfs/ImfsContextTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}
}

0 comments on commit f2909cf

Please sign in to comment.