Skip to content

Commit 93dd9b7

Browse files
Added umount command and unmount() API
Co-authored-by: LSantha <LSantha@users.noreply.github.com>
1 parent 601f1f4 commit 93dd9b7

5 files changed

Lines changed: 130 additions & 3 deletions

File tree

fs/descriptors/org.jnode.fs.command.xml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
<alias name="eject" class="org.jnode.fs.command.EjectCommand"/>
4444
<alias name="mount" class="org.jnode.fs.command.MountCommand"/>
4545
<alias name="sync" class="org.jnode.fs.command.SyncCommand"/>
46+
<alias name="umount" class="org.jnode.fs.command.UnmountCommand"/>
4647
</extension>
4748

4849
<extension point="org.jnode.shell.syntaxes">
@@ -61,11 +62,16 @@
6162
</sequence>
6263
</syntax>
6364
<syntax alias="sync">
64-
<empty description="Flush all mounted filesystems to disk"/>
65+
<empty description="Flush all mounted filesystems"/>
6566
<sequence description="Flush the filesystem at a specific path">
6667
<argument argLabel="path" description="the path of the filesystem to flush"/>
6768
</sequence>
6869
</syntax>
70+
<syntax alias="umount">
71+
<sequence description="Unmount the filesystem at the given path">
72+
<argument argLabel="directory"/>
73+
</sequence>
74+
</syntax>
6975
</extension>
7076

7177
<extension point="org.jnode.security.permissions">
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* $Id$
3+
*
4+
* Copyright (C) 2003-2015 JNode.org
5+
*
6+
* This library is free software; you can redistribute it and/or modify it
7+
* under the terms of the GNU Lesser General Public License as published
8+
* by the Free Software Foundation; either version 2.1 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* This library is distributed in the hope that it will be useful, but
12+
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13+
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14+
* License for more details.
15+
*
16+
* You should have received a copy of the GNU Lesser General Public License
17+
* along with this library; If not, write to the Free Software Foundation, Inc.,
18+
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19+
*/
20+
21+
package org.jnode.fs.command;
22+
23+
import java.io.IOException;
24+
import java.io.PrintWriter;
25+
import org.jnode.fs.service.FileSystemService;
26+
import org.jnode.naming.InitialNaming;
27+
import org.jnode.shell.AbstractCommand;
28+
import org.jnode.shell.syntax.Argument;
29+
import org.jnode.shell.syntax.FileArgument;
30+
31+
/**
32+
* Unmount a filesystem.
33+
*/
34+
public class UnmountCommand extends AbstractCommand {
35+
36+
private final FileArgument argDir =
37+
new FileArgument("directory", Argument.MANDATORY,
38+
"the mount point to unmount");
39+
40+
public UnmountCommand() {
41+
super("Unmount a filesystem");
42+
registerArguments(argDir);
43+
}
44+
45+
public static void main(String[] args) throws Exception {
46+
new UnmountCommand().execute(args);
47+
}
48+
49+
public void execute() throws Exception {
50+
FileSystemService fss = InitialNaming.lookup(FileSystemService.NAME);
51+
PrintWriter out = getOutput().getPrintWriter();
52+
PrintWriter err = getError().getPrintWriter();
53+
String path = argDir.getValue().getCanonicalPath();
54+
55+
if (!fss.isMount(path)) {
56+
err.println("Not a mount point: " + path);
57+
err.println("Use 'mount' to list mounted filesystems.");
58+
exit(1);
59+
}
60+
61+
try {
62+
fss.unmount(path);
63+
out.println("Unmounted " + path);
64+
} catch (IOException ex) {
65+
err.println("Failed to unmount " + path + ": " + ex.getMessage());
66+
exit(1);
67+
}
68+
}
69+
}

fs/src/fs/org/jnode/fs/service/FileSystemService.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,16 @@ public <T extends FileSystemType<?>> T getFileSystemType(Class<T> name)
115115
*/
116116
public boolean isMount(String fullPath);
117117

118+
/**
119+
* Unmount the filesystem at the given path.
120+
* Flushes the filesystem, closes it, and removes the mount point.
121+
*
122+
* @param fullPath the mount point path
123+
* @throws IOException if flush or close fails
124+
* @throws IllegalArgumentException if path is not a mount point
125+
*/
126+
public void unmount(String fullPath) throws IOException;
127+
118128
/**
119129
* Gets the filesystem API.
120130
*/

fs/src/fs/org/jnode/fs/service/def/FileSystemAPIImpl.java

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import org.jnode.fs.FSDirectory;
3838
import org.jnode.fs.FSEntry;
3939
import org.jnode.fs.FileSystem;
40+
import org.jnode.fs.spi.AbstractFileSystem;
4041
import org.jnode.java.io.VMFileHandle;
4142

4243
/**
@@ -460,8 +461,37 @@ void mount(String fullPath, FileSystem<?> fs, String fsPath) throws IOException
460461
fullPath = File.separatorChar + fullPath;
461462
}
462463

463-
mountPoints.put(fullPath, fs); // TODO handle removal (+ add unmount
464-
// method) of filesystems
464+
mountPoints.put(fullPath, fs);
465+
}
466+
467+
/**
468+
* Unmount the filesystem at the given path.
469+
* Flushes the filesystem, closes it, and removes the mount point.
470+
*
471+
* @param fullPath the mount point path
472+
* @throws IOException if flush or close fails
473+
* @throws IllegalArgumentException if path is not a mount point
474+
*/
475+
void unmount(String fullPath) throws IOException {
476+
if (fullPath.charAt(0) != File.separatorChar) {
477+
fullPath = File.separatorChar + fullPath;
478+
}
479+
480+
FileSystem<?> fs = mountPoints.remove(fullPath);
481+
if (fs == null) {
482+
throw new IllegalArgumentException("Not a mount point: " + fullPath);
483+
}
484+
485+
try {
486+
if (!fs.isReadOnly() && fs instanceof AbstractFileSystem) {
487+
((AbstractFileSystem<?>) fs).flush();
488+
}
489+
vfs.unregisterFileSystem(fs.getDevice());
490+
fs.close();
491+
} catch (IOException ex) {
492+
mountPoints.put(fullPath, fs);
493+
throw ex;
494+
}
465495
}
466496

467497
/**

fs/src/fs/org/jnode/fs/service/def/FileSystemPlugin.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,18 @@ public void mount(String fullPath, FileSystem<?> fs, String fsPath) throws IOExc
147147
api.mount(VMFile.getNormalizedPath(fullPath), fs, fsPath);
148148
}
149149

150+
/**
151+
* Unmount the filesystem at the given path.
152+
* Flushes the filesystem, closes it, and removes the mount point.
153+
*
154+
* @param fullPath the mount point path
155+
* @throws IOException if flush or close fails
156+
* @throws IllegalArgumentException if path is not a mount point
157+
*/
158+
public void unmount(String fullPath) throws IOException {
159+
api.unmount(VMFile.getNormalizedPath(fullPath));
160+
}
161+
150162
/**
151163
* Return a map (fullPath -> FileSystem) of mount points
152164
* @return a copy of the internal map, sorted by fullPath

0 commit comments

Comments
 (0)