|
| 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 | +} |
0 commit comments