jaulib v1.3.6
Jau Support Library (C++, Java, ..)
Loading...
Searching...
No Matches
TestsudoFileUtils02.java
Go to the documentation of this file.
1/*
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright (c) 2021 Gothel Software e.K.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25package jau.test.fs;
26
27import java.util.List;
28
29import org.jau.fs.CopyOptions;
30import org.jau.fs.DirItem;
31import org.jau.fs.FMode;
32import org.jau.fs.FileStats;
33import org.jau.fs.FileUtil;
34import org.jau.fs.MountFlags;
35import org.jau.fs.TraverseOptions;
36import org.jau.fs.UnmountFlags;
37import org.jau.io.PrintUtil;
38import org.jau.sys.PlatformProps;
39import org.jau.sys.PlatformTypes;
40import org.junit.Assert;
41import org.junit.FixMethodOrder;
42import org.junit.Test;
43import org.junit.runners.MethodSorters;
44
45import jau.pkg.PlatformRuntime;
46
47@FixMethodOrder(MethodSorters.NAME_ASCENDING)
49 static final boolean DEBUG = false;
50
51 private static boolean isFolderEmpty(final String path) {
52 final List<DirItem> files = FileUtil.get_dir_content(path);
53 return null == files || 0 == files.size();
54 }
55
56 @Test(timeout = 10000)
57 public void test50_mount_copy_r_p() {
59 PrintUtil.println(System.err, "test50_mount_copy_r_p\n");
60
61 final FileStats root_orig_stats = getTestDataDirStats();
62 Assert.assertTrue( true == root_orig_stats.exists() );
63 Assert.assertTrue( true == root_orig_stats.is_dir() );
64
65 {
66
67 final FileStats image_stats = getTestDataImageFile();
68 Assert.assertTrue( true == image_stats.exists() );
69
70 final String mount_point = root+"_mount";
71 FileUtil.remove(mount_point, TraverseOptions.recursive); // start fresh
72 Assert.assertTrue( true == FileUtil.mkdir(mount_point, FMode.def_dir) );
73
74 final MountFlags mount_flags;
75 if( PlatformProps.OS == PlatformTypes.OSType.LINUX ) {
76 mount_flags = new org.jau.fs.linux.MountFlags();
77 mount_flags.set(org.jau.fs.linux.MountFlags.Bit.rdonly);
78 } else {
79 mount_flags = new MountFlags();
80 }
81 PrintUtil.println(System.err, "MountFlags for "+PlatformProps.OS+": "+mount_flags);
82 final long mctx = FileUtil.mount_image(image_stats.path(), mount_point, "squashfs", mount_flags, "");
83 Assert.assertTrue( 0 != mctx );
84 Assert.assertFalse(isFolderEmpty(mount_point));
85
86 final CopyOptions copts = new CopyOptions();
89 copts.set(CopyOptions.Bit.sync);
91
92 final String root_copy = root+"_copy_test50";
94 testxx_copy_r_p("test50_mount_copy_r_p", new FileStats(mount_point), 1 /* source_added_dead_links */, root_copy, copts, false /* dest_is_vfat */);
95 Assert.assertTrue( true == FileUtil.remove(root_copy, TraverseOptions.recursive) );
96
97 final UnmountFlags umount_flags;
99 umount_flags = new org.jau.fs.linux.UnmountFlags();
100 umount_flags.set(org.jau.fs.linux.UnmountFlags.Bit.detach); // lazy
101 } else {
102 umount_flags = new UnmountFlags();
103 }
104 PrintUtil.println(System.err, "UnmountFlags for "+PlatformProps.OS+": "+umount_flags);
105 final boolean umount_ok = FileUtil.umount(mctx, umount_flags);
106 Assert.assertTrue( true == umount_ok );
107 Assert.assertTrue(isFolderEmpty(mount_point));
108
109 Assert.assertTrue( true == FileUtil.remove(mount_point, TraverseOptions.recursive) );
110 }
111 }
112
113 /* pp */ static String test51_devname = null;
114 /* pp */ static boolean test51_umount = true;
115
116 @Test(timeout = 10000)
117 public void test51_mount_device() {
119 PrintUtil.println(System.err, "test51_mount_device\n");
120 if( null == test51_devname ) {
121 return;
122 }
123 {
124 final String device_file = test51_devname;
125 final FileStats device_stats = new FileStats(device_file);
126 Assert.assertTrue( true == device_stats.exists() );
127 PrintUtil.println(System.err, "Mount "+device_stats);
128
129 final String mount_point = root+"_mount";
130 Assert.assertTrue(isFolderEmpty(mount_point));
131 FileUtil.remove(mount_point, TraverseOptions.recursive); // start fresh
132 Assert.assertTrue( true == FileUtil.mkdir(mount_point, FMode.def_dir) );
133
134 final MountFlags mount_flags;
135 if( PlatformProps.OS == PlatformTypes.OSType.LINUX ) {
136 mount_flags = new org.jau.fs.linux.MountFlags();
137 mount_flags.set(org.jau.fs.linux.MountFlags.Bit.rdonly); // failed: MS_PRIVATE, MS_UNBINDABLE
138 } else {
139 mount_flags = new MountFlags();
140 }
141 PrintUtil.println(System.err, "MountFlags for "+PlatformProps.OS+": "+mount_flags);
142 final long mctx = FileUtil.mount(device_stats.path(), mount_point, "vfat", mount_flags, "errors=continue,rodir,sys_immutable,usefree");
143 Assert.assertTrue( 0 != mctx );
144 Assert.assertFalse(isFolderEmpty(mount_point));
145
146 final List<DirItem> files = FileUtil.get_dir_content(mount_point);
147 Assert.assertNotNull(files);
148 Assert.assertTrue(files.size() > 0 );
149 int count=0;
150 for(final DirItem item : files) {
151 ++count;
152 PrintUtil.fprintf_td(System.err, "%,4d: %s\n", count, item);
153 }
154
155 if( test51_umount ) {
156 final UnmountFlags umount_flags;
157 if( PlatformProps.OS == PlatformTypes.OSType.LINUX ) {
158 umount_flags = new org.jau.fs.linux.UnmountFlags();
159 umount_flags.set(org.jau.fs.linux.UnmountFlags.Bit.detach); // lazy
160 } else {
161 umount_flags = new UnmountFlags();
162 }
163 PrintUtil.println(System.err, "UnmountFlags for "+PlatformProps.OS+": "+umount_flags);
164 final boolean umount_ok = FileUtil.umount(mount_point, umount_flags);
165 Assert.assertTrue( true == umount_ok );
166 Assert.assertTrue(isFolderEmpty(mount_point));
167
168 Assert.assertTrue( true == FileUtil.remove(mount_point, TraverseOptions.recursive) );
169 }
170 }
171 }
172
173 public static void main(final String args[]) {
174 final int argc = args.length;
175 for(int i=0; i< argc; i++) {
176 final String arg = args[i];
177 if( arg.equals("-mount_device") && args.length > (i+1) ) {
178 test51_devname = args[++i];
179 } else if( arg.equals("-no_umount") ) {
180 test51_umount = false;
181 }
182 }
183 org.junit.runner.JUnitCore.main(TestsudoFileUtils02.class.getName());
184 }
185}
Initialized by org.jau.sys.RuntimeProps.
static final FileStats getTestDataDirStats()
void testxx_copy_r_p(final String title, final FileStats source, final int source_added_dead_links, final String dest, final CopyOptions copts, final boolean dest_is_vfat)
static void main(final String args[])
Filesystem copy options used to copy() path elements.
CopyOptions set(final Bit bit)
Sets the given bit and returns this instance for chaining.
Representing a directory item split into dirname() and basename().
Definition DirItem.java:29
Platform agnostic representation of POSIX ::lstat() and ::stat() for a given pathname.
Native file types and functionality.
Definition FileUtil.java:33
static long mount_image(final String image_path, final String target, final String fs_type, final MountFlags flags, final String fs_options)
Attach the filesystem image named in image_path to target using an intermediate platform specific fil...
static boolean umount(final long context, final UnmountFlags flags)
Detach the given mount_ctx context
static boolean remove(final String path, final TraverseOptions topts)
Remove the given path.
static native List< DirItem > get_dir_content(final String path)
Returns a list of directory elements excluding .
static long mount(final String source, final String target, final String fs_type, final MountFlags flags, final String fs_options)
Attach the filesystem named in source to target using the given filesystem source directly.
Filesystem traverse options used to visit() path elements.
static final TraverseOptions recursive
Generic flag bit class for umount() flags
UnmountFlags set(final Bit bit)
static void println(final PrintStream out, final String msg)
Convenient PrintStream#println(String) invocation, prepending the elapsedTimeMillis() timestamp.
Platform Properties derived from Java properties.
Exposing types describing the underlying platform.
sync
Ensure data and meta-data file synchronization is performed via ::fsync() after asynchronous copy ope...
recursive
Traverse through directories, i.e.
preserve_all
Preserve uid and gid if allowed and access- and modification-timestamps, i.e.
verbose
Enable verbosity mode, show error messages on stderr.