From 4fe02b7ed25a3c3d89745a6b02d46f750d2649eb Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Wed, 31 Aug 2022 06:54:10 +0200 Subject: Complete full `jau::fs::mount()` and `umount()` --- java_jni/org/jau/fs/FileUtil.java | 75 +++++++++++++++++++++--- java_jni/org/jau/fs/MountFlags.java | 91 +++++++++++++++++++++++++++++ java_jni/org/jau/fs/UnmountFlags.java | 91 +++++++++++++++++++++++++++++ java_jni/org/jau/fs/linux/MountFlags.java | 86 +++++++++++++++++++++++++++ java_jni/org/jau/fs/linux/UnmountFlags.java | 65 +++++++++++++++++++++ 5 files changed, 399 insertions(+), 9 deletions(-) create mode 100644 java_jni/org/jau/fs/MountFlags.java create mode 100644 java_jni/org/jau/fs/UnmountFlags.java create mode 100644 java_jni/org/jau/fs/linux/MountFlags.java create mode 100644 java_jni/org/jau/fs/linux/UnmountFlags.java (limited to 'java_jni/org/jau') diff --git a/java_jni/org/jau/fs/FileUtil.java b/java_jni/org/jau/fs/FileUtil.java index f04362f..8b192da 100644 --- a/java_jni/org/jau/fs/FileUtil.java +++ b/java_jni/org/jau/fs/FileUtil.java @@ -353,33 +353,90 @@ public final class FileUtil { public static native void sync(); /** - * Attach the filesystem image named in `image_path` to `target_path`. + * Attach the filesystem image named in `image_path` to `target` + * using an intermediate platform specific filesystem image loop-device. * * This method either requires root permissions
* or the following capabilities: `cap_sys_admin`,`cap_setuid`, `cap_setgid`. * + * Unmounting shall be done via umount() with mount_ctx argument to ensure + * all intermediate resources are released. + * * @param image_path path of image source file - * @param mount_point directory where `image_path` shall be attached to + * @param target directory where `image_path` filesystem shall be attached to * @param fs_type type of filesystem, e.g. `squashfs`, `tmpfs`, `iso9660`, etc. - * @param mountflags mount flags, e.g. `MS_LAZYTIME | MS_NOATIME | MS_RDONLY` for a read-only lazy-time and no-atime filesystem. - * @param fs_options special filesystem options - * @return native mount context if successful, otherwise null + * @param flags filesystem agnostic mount flags, see mountflags_linux. + * @param fs_options comma separated options for the filesystem `fs_type`, see mount(8) for available options for the used filesystem. + * @return mount_ctx structure containing mounted status etc + * + * @see mount() + * @see umount() + */ + public static long mount_image(final String image_path, final String target, final String fs_type, + final MountFlags flags, final String fs_options) { + return mount_image_impl(image_path, target, fs_type, flags.value(), fs_options); + } + private static native long mount_image_impl(final String image_path, final String target, final String fs_type, + final long mountflags, final String fs_options); + + /** + * Attach the filesystem named in `source` to `target` + * using the given filesystem source directly. * + * This method either requires root permissions
+ * or the following capabilities: `cap_sys_admin`,`cap_setuid`, `cap_setgid`. + * + * @param source filesystem path for device, directory, file or dummy-string which shall be attached + * @param target directory where `source` filesystem shall be attached to + * @param fs_type type of filesystem, e.g. `squashfs`, `tmpfs`, `iso9660`, etc. + * @param flags mount flags, e.g. `MS_LAZYTIME | MS_NOATIME | MS_RDONLY` for a read-only lazy-time and no-atime filesystem. + * @param fs_options comma separated options for the filesystem `fs_type`, see mount(8) for available options for the used filesystem. + * @return mount_ctx structure containing mounted status etc + * + * @see mount_image() * @see umount() */ - public static native long mount_image(final String image_path, final String mount_point, final String fs_type, + public static long mount(final String source, final String target, final String fs_type, + final MountFlags flags, final String fs_options) { + return mount_impl(source, target, fs_type, flags.value(), fs_options); + } + private static native long mount_impl(final String source, final String target, final String fs_type, final long mountflags, final String fs_options); /** - * Detach the given mount_ctc `context` + * Detach the given mount_ctx `context` * * This method either requires root permissions
* or the following capabilities: `cap_sys_admin`,`cap_setuid`, `cap_setgid`. * - * @param context native mount context, previously attached via mount_image() + * @param context native mount context, previously attached via mount_image() or mount() + * @param flags optional umount options, if supported by the system * @return true if successful, otherwise false * + * @see mount() * @see mount_image() */ - public static native boolean umount(final long context); + public static boolean umount(final long context, final UnmountFlags flags) { + return umount1_impl(context, flags.value()); + } + private static native boolean umount1_impl(final long context, final int unmountflags); + + /** + * Detach the topmost filesystem mounted on `target` + * optionally using given `umountflags` options if supported. + * + * This method either requires root permissions
+ * or the following capabilities: `cap_sys_admin`,`cap_setuid`, `cap_setgid`. + * + * @param target directory of previously attached filesystem + * @param umountflags optional umount options, if supported by the system + * @return true if successful, otherwise false + * + * @see mount() + * @see mount_image() + */ + public static boolean umount(final String target, final UnmountFlags flags) { + return umount2_impl(target, flags.value()); + } + private static native boolean umount2_impl(final String target, final int unmountflags); } diff --git a/java_jni/org/jau/fs/MountFlags.java b/java_jni/org/jau/fs/MountFlags.java new file mode 100644 index 0000000..b26ad45 --- /dev/null +++ b/java_jni/org/jau/fs/MountFlags.java @@ -0,0 +1,91 @@ +/** + * Author: Sven Gothel + * Copyright (c) 2022 Gothel Software e.K. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package org.jau.fs; + +/** + * Generic flag bit values for mount() `flags`. + * + * See mount(2) for a detailed description. + * + * @see FileUtil#mount_image(String, String, String, long, String) + * @see FileUtil#mount(String, String, String, long, String) + */ +public class MountFlags { + + public static interface Bit { + String name(); + long value(); + } + public static enum Bit0 implements Bit { + none ( 0 ); + + Bit0(final long v) { _value = v; } + private final long _value; + + @Override + public long value() { return _value; } + } + protected Bit[] bit_values() { return Bit0.values(); } + + private long mask; + + public long value() { return mask; } + + protected MountFlags(final long v) { + mask = v; + } + public MountFlags() { + mask = 0; + } + + public boolean isSet(final Bit bit) { return bit.value() == ( mask & bit.value() ); } + + public MountFlags set(final Bit bit) { mask = mask | bit.value(); return this; } + + @Override + public String toString() { + int count = 0; + final StringBuilder out = new StringBuilder(); + for (final Bit dt : bit_values()) { + if( 0 != dt.value() && isSet(dt) ) { + if( 0 < count ) { out.append(", "); } + out.append(dt.name()); count++; + } + } + if( 1 < count ) { + out.insert(0, "["); + out.append("]"); + } + return out.toString(); + } + + @Override + public boolean equals(final Object other) { + if (this == other) { + return true; + } + return (other instanceof MountFlags) && + this.mask == ((MountFlags)other).mask; + } +} diff --git a/java_jni/org/jau/fs/UnmountFlags.java b/java_jni/org/jau/fs/UnmountFlags.java new file mode 100644 index 0000000..8321005 --- /dev/null +++ b/java_jni/org/jau/fs/UnmountFlags.java @@ -0,0 +1,91 @@ +/** + * Author: Sven Gothel + * Copyright (c) 2022 Gothel Software e.K. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package org.jau.fs; + +/** + * Generic flag bit class for umount() `flags` + * + * See umount(2) for a detailed description. + * + * @see FileUtil#umount(long, int) + * @see FileUtil#umount(String, int) + */ +public class UnmountFlags { + + public static interface Bit { + String name(); + int value(); + } + public static enum Bit0 implements Bit { + none ( 0 ); + + Bit0(final int v) { _value = v; } + private int _value; + + @Override + public int value() { return _value; } + } + protected Bit[] bit_values() { return Bit0.values(); } + + private int mask; + + public int value() { return mask; } + + protected UnmountFlags(final int v) { + mask = v; + } + public UnmountFlags() { + mask = 0; + } + + public boolean isSet(final Bit bit) { return bit.value() == ( mask & bit.value() ); } + + public UnmountFlags set(final Bit bit) { mask = mask | bit.value(); return this; } + + @Override + public String toString() { + int count = 0; + final StringBuilder out = new StringBuilder(); + for (final Bit dt : bit_values()) { + if( 0 != dt.value() && isSet(dt) ) { + if( 0 < count ) { out.append(", "); } + out.append(dt.name()); count++; + } + } + if( 1 < count ) { + out.insert(0, "["); + out.append("]"); + } + return out.toString(); + } + + @Override + public boolean equals(final Object other) { + if (this == other) { + return true; + } + return (other instanceof UnmountFlags) && + this.mask == ((UnmountFlags)other).mask; + } +} \ No newline at end of file diff --git a/java_jni/org/jau/fs/linux/MountFlags.java b/java_jni/org/jau/fs/linux/MountFlags.java new file mode 100644 index 0000000..010e3a9 --- /dev/null +++ b/java_jni/org/jau/fs/linux/MountFlags.java @@ -0,0 +1,86 @@ +/** + * Author: Sven Gothel + * Copyright (c) 2022 Gothel Software e.K. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package org.jau.fs.linux; + +import org.jau.fs.FileUtil; + +/** + * Flag bit values for mount() `flags` under GNU/Linux. + * + * See mount(2) for a detailed description. + * + * @see FileUtil#mount_image(String, String, String, long, String) + * @see FileUtil#mount(String, String, String, long, String) + */ +public class MountFlags extends org.jau.fs.MountFlags { + + public static enum Bit implements org.jau.fs.MountFlags.Bit { + none ( 0 ), + MS_RDONLY ( 1 ), + MS_NOSUID ( 2 ), + MS_NODEV ( 4 ), + MS_NOEXEC ( 8 ), + MS_SYNCHRONOUS ( 16 ), + MS_REMOUNT ( 32 ), + MS_MANDLOCK ( 64 ), + MS_DIRSYNC ( 128 ), + MS_NOATIME ( 1024 ), + MS_NODIRATIME ( 2048 ), + MS_BIND ( 4096 ), + MS_MOVE ( 8192 ), + MS_REC ( 16384 ), + MS_SILENT ( 32768 ), + MS_POSIXACL ( 1 << 16 ), + MS_UNBINDABLE ( 1 << 17 ), + MS_PRIVATE ( 1 << 18 ), + MS_SLAVE ( 1 << 19 ), + MS_SHARED ( 1 << 20 ), + MS_RELATIME ( 1 << 21 ), + MS_KERNMOUNT ( 1 << 22 ), + MS_I_VERSION ( 1 << 23 ), + MS_STRICTATIME ( 1 << 24 ), + MS_LAZYTIME ( 1 << 25 ), + MS_ACTIVE ( 1 << 30 ), + MS_NOUSER ( 1 << 31 ); + + Bit(final long v) { _value = v; } + private final long _value; + + @Override + public long value() { return _value; } + } + + @Override + protected Bit[] bit_values() { + return Bit.values(); + } + + public MountFlags(final long v) { + super(v); + } + + public MountFlags() { + super(0); + } +} diff --git a/java_jni/org/jau/fs/linux/UnmountFlags.java b/java_jni/org/jau/fs/linux/UnmountFlags.java new file mode 100644 index 0000000..b5ba6e7 --- /dev/null +++ b/java_jni/org/jau/fs/linux/UnmountFlags.java @@ -0,0 +1,65 @@ +/** + * Author: Sven Gothel + * Copyright (c) 2022 Gothel Software e.K. + * + * Permission is hereby granted ), free of charge ), to any person obtaining + * a copy of this software and associated documentation files (the + * "Software") ), to deal in the Software without restriction ), including + * without limitation the rights to use ), copy ), modify ), merge ), publish ), + * distribute ), sublicense ), and/or sell copies of the Software ), and to + * permit persons to whom the Software is furnished to do so ), subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS" ), WITHOUT WARRANTY OF ANY KIND ), + * EXPRESS OR IMPLIED ), INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY ), FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM ), DAMAGES OR OTHER LIABILITY ), WHETHER IN AN ACTION + * OF CONTRACT ), TORT OR OTHERWISE ), ARISING FROM ), OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package org.jau.fs.linux; + +import org.jau.fs.FileUtil; + +/** + * Flag bit class for umount() `flags` under GNU/Linux + * + * See umount(2) for a detailed description. + * + * @see FileUtil#umount(long, int) + * @see FileUtil#umount(String, int) + */ +public class UnmountFlags extends org.jau.fs.UnmountFlags { + + public static enum Bit implements org.jau.fs.UnmountFlags.Bit { + none ( 0 ), + MNT_FORCE ( 1 ), + MNT_DETACH ( 2 ), + MNT_EXPIRE ( 4 ), + UMOUNT_NOFOLLOW ( 8 ); + + Bit(final int v) { _value = v; } + + private final int _value; + + @Override + public int value() { return _value; } + } + + @Override + protected Bit[] bit_values() { + return Bit.values(); + } + + public UnmountFlags(final int v) { + super(v); + } + + public UnmountFlags() { + super(0); + } +} -- cgit v1.2.3