jaulib v1.3.0
Jau Support Library (C++, Java, ..)
TestFileUtils01.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.time.Instant;
28import java.time.ZoneOffset;
29import java.time.temporal.ChronoUnit;
30
31import org.jau.fs.CopyOptions;
32import org.jau.fs.DirItem;
33import org.jau.fs.FMode;
34import org.jau.fs.FileStats;
35import org.jau.fs.FileUtil;
36import org.jau.fs.TraverseEvent;
37import org.jau.fs.TraverseOptions;
38import org.jau.io.ByteOutStream_File;
39import org.jau.io.PrintUtil;
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)
48public class TestFileUtils01 extends FileUtilBaseTest {
49 static final boolean DEBUG = false;
50
51 @Test(timeout = 10000)
52 public final void test01_cwd() {
54 final String cwd = FileUtil.get_cwd();
55 PrintUtil.println(System.err, "test01_cwd: cwd "+cwd);
56 Assert.assertTrue( 0 < cwd.length() );
57 final int idx = cwd.indexOf("/jaulib/");
58 Assert.assertTrue( 0 < idx );
59 Assert.assertTrue( idx < cwd.length() );
60 Assert.assertTrue( idx > 0 );
61 }
62
63 /**
64 *
65 */
66 @Test(timeout = 10000)
67 public final void test02_dirname() {
69 {
70 final String pathname0 = "/";
71 final String pathname1 = FileUtil.dirname(pathname0);
72 PrintUtil.println(System.err, "test02_dirname: cwd "+pathname0+" -> "+pathname1);
73 Assert.assertTrue( 0 < pathname1.length() );
74 Assert.assertTrue( pathname1.equals( "/" ) );
75 }
76 {
77 {
78 final String pathname0 = "lala.txt";
79 final String pathname1 = FileUtil.dirname(pathname0);
80 PrintUtil.println(System.err, "test02_dirname: cwd "+pathname0+" -> "+pathname1);
81 Assert.assertTrue( 0 < pathname1.length() );
82 Assert.assertTrue( pathname1.equals( "." ) );
83 }
84 {
85 final String pathname0 = "lala";
86 final String pathname1 = FileUtil.dirname(pathname0);
87 PrintUtil.println(System.err, "test02_dirname: cwd "+pathname0+" -> "+pathname1);
88 Assert.assertTrue( 0 < pathname1.length() );
89 Assert.assertTrue( pathname1.equals( "." ) );
90 }
91 {
92 final String pathname0 = "lala/";
93 final String pathname1 = FileUtil.dirname(pathname0);
94 PrintUtil.println(System.err, "test02_dirname: cwd "+pathname0+" -> "+pathname1);
95 Assert.assertTrue( 0 < pathname1.length() );
96 Assert.assertTrue( pathname1.equals( "." ) );
97 }
98 }
99 {
100 final String pathname0 = "/lala.txt";
101 final String pathname1 = FileUtil.dirname(pathname0);
102 PrintUtil.println(System.err, "test02_dirname: cwd "+pathname0+" -> "+pathname1);
103 Assert.assertTrue( 0 < pathname1.length() );
104 Assert.assertTrue( pathname1.equals( "/" ) );
105 }
106 {
107 final String pathname0 = "blabla/jaulib/test/sub.txt";
108 final String pathname1 = FileUtil.dirname(pathname0);
109 PrintUtil.println(System.err, "test02_dirname: cwd "+pathname0+" -> "+pathname1);
110 Assert.assertTrue( 0 < pathname1.length() );
111 Assert.assertTrue( pathname1.equals( "blabla/jaulib/test" ) );
112 }
113 {
114 final String pathname0 = "blabla/jaulib/test/sub";
115 final String pathname1 = FileUtil.dirname(pathname0);
116 PrintUtil.println(System.err, "test02_dirname: cwd "+pathname0+" -> "+pathname1);
117 Assert.assertTrue( 0 < pathname1.length() );
118 Assert.assertTrue( pathname1.equals( "blabla/jaulib/test" ) );
119 }
120 {
121 final String pathname0 = "blabla/jaulib/test/";
122 final String pathname1 = FileUtil.dirname(pathname0);
123 PrintUtil.println(System.err, "test02_dirname: cwd "+pathname0+" -> "+pathname1);
124 Assert.assertTrue( 0 < pathname1.length() );
125 Assert.assertTrue( pathname1.equals( "blabla/jaulib" ) );
126 }
127 {
128 final String pathname0 = "blabla/jaulib/test";
129 final String pathname1 = FileUtil.dirname(pathname0);
130 PrintUtil.println(System.err, "test02_dirname: cwd "+pathname0+" -> "+pathname1);
131 Assert.assertTrue( 0 < pathname1.length() );
132 Assert.assertTrue( pathname1.equals( "blabla/jaulib" ) );
133 }
134 }
135
136 @Test(timeout = 10000)
137 public final void test03_basename() {
139 {
140 final String pathname0 = "/";
141 final String pathname1 = FileUtil.basename(pathname0);
142 PrintUtil.println(System.err, "test03_basename: cwd "+pathname0+" -> "+pathname1);
143 Assert.assertTrue( 0 < pathname1.length() );
144 Assert.assertTrue( pathname1.equals( "/" ) );
145 }
146 {
147 {
148 final String pathname0 = "lala.txt";
149 final String pathname1 = FileUtil.basename(pathname0);
150 PrintUtil.println(System.err, "test03_basename: cwd "+pathname0+" -> "+pathname1);
151 Assert.assertTrue( 0 < pathname1.length() );
152 Assert.assertTrue( pathname1.equals( "lala.txt" ) );
153 }
154 {
155 final String pathname0 = "lala";
156 final String pathname1 = FileUtil.basename(pathname0);
157 PrintUtil.println(System.err, "test03_basename: cwd "+pathname0+" -> "+pathname1);
158 Assert.assertTrue( 0 < pathname1.length() );
159 Assert.assertTrue( pathname1.equals( "lala" ) );
160 }
161 {
162 final String pathname0 = "lala/";
163 final String pathname1 = FileUtil.basename(pathname0);
164 PrintUtil.println(System.err, "test03_basename: cwd "+pathname0+" -> "+pathname1);
165 Assert.assertTrue( 0 < pathname1.length() );
166 Assert.assertTrue( pathname1.equals( "lala" ) );
167 }
168 }
169 {
170 final String pathname0 = "/lala.txt";
171 final String pathname1 = FileUtil.basename(pathname0);
172 PrintUtil.println(System.err, "test03_basename: cwd "+pathname0+" -> "+pathname1);
173 Assert.assertTrue( 0 < pathname1.length() );
174 Assert.assertTrue( pathname1.equals( "lala.txt" ) );
175 }
176 {
177 final String pathname0 = "blabla/jaulib/test/sub.txt";
178 final String pathname1 = FileUtil.basename(pathname0);
179 PrintUtil.println(System.err, "test03_basename: cwd "+pathname0+" -> "+pathname1);
180 Assert.assertTrue( 0 < pathname1.length() );
181 Assert.assertTrue( pathname1.equals( "sub.txt" ) );
182 }
183
184 {
185 final String pathname0 = "blabla/jaulib/test/";
186 final String pathname1 = FileUtil.basename(pathname0);
187 PrintUtil.println(System.err, "test03_basename: cwd "+pathname0+" -> "+pathname1);
188 Assert.assertTrue( 0 < pathname1.length() );
189 Assert.assertTrue( pathname1.equals( "test" ) );
190 }
191
192 {
193 final String pathname0 = "blabla/jaulib/test";
194 final String pathname1 = FileUtil.basename(pathname0);
195 PrintUtil.println(System.err, "test03_basename: cwd "+pathname0+" -> "+pathname1);
196 Assert.assertTrue( 0 < pathname1.length() );
197 Assert.assertTrue( pathname1.equals( "test" ) );
198 }
199 }
200
201 @Test(timeout = 10000)
202 public final void test04_dir_item() {
204 {
205 final String dirname_ = "";
206 final DirItem di = new DirItem(dirname_);
207 PrintUtil.println(System.err, "test04_dir_item: 01 '"+dirname_+"' -> "+di.toString()+" -> '"+di.path()+"'\n");
208 Assert.assertTrue( ".".equals( di.dirname() ) );
209 Assert.assertTrue( ".".equals( di.basename() ) );
210 Assert.assertTrue( ".".equals( di.path() ) );
211 }
212 {
213 final String dirname_ =".";
214 final DirItem di = new DirItem(dirname_);
215 PrintUtil.println(System.err, "test04_dir_item: 02 '"+dirname_+"' -> "+di.toString()+" -> '"+di.path()+"'\n");
216 Assert.assertTrue( ".".equals( di.dirname() ) );
217 Assert.assertTrue( ".".equals( di.basename() ) );
218 Assert.assertTrue( ".".equals( di.path() ) );
219 }
220 {
221 final String dirname_ ="/";
222 final DirItem di = new DirItem(dirname_);
223 PrintUtil.println(System.err, "test04_dir_item: 03 '"+dirname_+"' -> "+di.toString()+" -> '"+di.path()+"'\n");
224 Assert.assertTrue( "/".equals( di.dirname() ) );
225 Assert.assertTrue( ".".equals( di.basename() ) );
226 Assert.assertTrue( "/".equals( di.path() ) );
227 }
228
229 {
230 final String path1_ = "lala";
231 final DirItem di = new DirItem(path1_);
232 PrintUtil.println(System.err, "test04_dir_item: 10 '"+path1_+" -> "+di.toString()+" -> '"+di.path()+"'\n");
233 Assert.assertTrue( ".".equals( di.dirname() ) );
234 Assert.assertTrue( "lala".equals( di.basename() ) );
235 Assert.assertTrue( "lala".equals( di.path() ) );
236 }
237 {
238 final String path1_ = "lala/";
239 final DirItem di = new DirItem(path1_);
240 PrintUtil.println(System.err, "test04_dir_item: 11 '"+path1_+" -> "+di.toString()+" -> '"+di.path()+"'\n");
241 Assert.assertTrue( ".".equals( di.dirname() ) );
242 Assert.assertTrue( "lala".equals( di.basename() ) );
243 Assert.assertTrue( "lala".equals( di.path() ) );
244 }
245
246 {
247 final String path1_ = "/lala";
248 final DirItem di = new DirItem(path1_);
249 PrintUtil.println(System.err, "test04_dir_item: 12 '"+path1_+" -> "+di.toString()+" -> '"+di.path()+"'\n");
250 Assert.assertTrue( "/".equals( di.dirname() ) );
251 Assert.assertTrue( "lala".equals( di.basename() ) );
252 Assert.assertTrue( "/lala".equals( di.path() ) );
253 }
254
255 {
256 final String path1_ = "dir0/lala";
257 final DirItem di = new DirItem(path1_);
258 PrintUtil.println(System.err, "test04_dir_item: 20 '"+path1_+" -> "+di.toString()+" -> '"+di.path()+"'\n");
259 Assert.assertTrue( "dir0".equals( di.dirname() ) );
260 Assert.assertTrue( "lala".equals( di.basename() ) );
261 Assert.assertTrue( "dir0/lala".equals( di.path() ) );
262 }
263 {
264 final String path1_ = "dir0/lala/";
265 final DirItem di = new DirItem(path1_);
266 PrintUtil.println(System.err, "test04_dir_item: 21 '"+path1_+" -> "+di.toString()+" -> '"+di.path()+"'\n");
267 Assert.assertTrue( "dir0".equals( di.dirname() ) );
268 Assert.assertTrue( "lala".equals( di.basename() ) );
269 Assert.assertTrue( "dir0/lala".equals( di.path() ) );
270 }
271 {
272 final String path1_ = "/dir0/lala";
273 final DirItem di = new DirItem(path1_);
274 PrintUtil.println(System.err, "test04_dir_item: 22 '"+path1_+" -> "+di.toString()+" -> '"+di.path()+"'\n");
275 Assert.assertTrue( "/dir0".equals( di.dirname() ) );
276 Assert.assertTrue( "lala".equals( di.basename() ) );
277 Assert.assertTrue( "/dir0/lala".equals( di.path() ) );
278 }
279 {
280 final String path1_ = "/dir0/lala/";
281 final DirItem di = new DirItem(path1_);
282 PrintUtil.println(System.err, "test04_dir_item: 23 '"+path1_+" -> "+di.toString()+" -> '"+di.path()+"'\n");
283 Assert.assertTrue( "/dir0".equals( di.dirname() ) );
284 Assert.assertTrue( "lala".equals( di.basename() ) );
285 Assert.assertTrue( "/dir0/lala".equals( di.path() ) );
286 }
287
288
289 {
290 final String path1_ = "/dir0/../lala";
291 final DirItem di = new DirItem(path1_);
292 PrintUtil.println(System.err, "test04_dir_item: 30 '"+path1_+" -> "+di.toString()+" -> '"+di.path()+"'\n");
293 Assert.assertTrue( "/".equals( di.dirname() ) );
294 Assert.assertTrue( "lala".equals( di.basename() ) );
295 Assert.assertTrue( "/lala".equals( di.path() ) );
296 }
297 {
298 final String path1_ = "dir0/../lala";
299 final DirItem di = new DirItem(path1_);
300 PrintUtil.println(System.err, "test04_dir_item: 31 '"+path1_+" -> "+di.toString()+" -> '"+di.path()+"'\n");
301 Assert.assertTrue( ".".equals( di.dirname() ) );
302 Assert.assertTrue( "lala".equals( di.basename() ) );
303 Assert.assertTrue( "lala".equals( di.path() ) );
304 }
305 {
306 final String path1_ = "../../lala";
307 final DirItem di = new DirItem(path1_);
308 PrintUtil.println(System.err, "test04_dir_item: 32 '"+path1_+" -> "+di.toString()+" -> '"+di.path()+"'\n");
309 Assert.assertTrue( "../..".equals( di.dirname() ) );
310 Assert.assertTrue( "lala".equals( di.basename() ) );
311 Assert.assertTrue( "../../lala".equals( di.path() ) );
312 }
313 {
314 final String path1_ = "./../lala";
315 final DirItem di = new DirItem(path1_);
316 PrintUtil.println(System.err, "test04_dir_item: 33 '"+path1_+" -> "+di.toString()+" -> '"+di.path()+"'\n");
317 Assert.assertTrue( "..".equals( di.dirname() ) );
318 Assert.assertTrue( "lala".equals( di.basename() ) );
319 Assert.assertTrue( "../lala".equals( di.path() ) );
320 }
321 {
322 final String path1_ = "dir0/../../lala";
323 final DirItem di = new DirItem(path1_);
324 PrintUtil.println(System.err, "test04_dir_item: 34 '"+path1_+" -> "+di.toString()+" -> '"+di.path()+"'\n");
325 Assert.assertTrue( "..".equals( di.dirname() ) );
326 Assert.assertTrue( "lala".equals( di.basename() ) );
327 Assert.assertTrue( "../lala".equals( di.path() ) );
328 }
329
330 {
331 final String path1_ = "dir0/dir1/../lala";
332 final DirItem di = new DirItem(path1_);
333 PrintUtil.println(System.err, "test04_dir_item: 40 '"+path1_+" -> "+di.toString()+" -> '"+di.path()+"'\n");
334 Assert.assertTrue( "dir0".equals( di.dirname() ) );
335 Assert.assertTrue( "lala".equals( di.basename() ) );
336 Assert.assertTrue( "dir0/lala".equals( di.path() ) );
337 }
338 {
339 final String path1_ = "/dir0/dir1/../lala/";
340 final DirItem di = new DirItem(path1_);
341 PrintUtil.println(System.err, "test04_dir_item: 41 '"+path1_+" -> "+di.toString()+" -> '"+di.path()+"'\n");
342 Assert.assertTrue( "/dir0".equals( di.dirname() ) );
343 Assert.assertTrue( "lala".equals( di.basename() ) );
344 Assert.assertTrue( "/dir0/lala".equals( di.path() ) );
345 }
346 {
347 final String path1_ = "dir0/dir1/../bbb/ccc/../lala";
348 final DirItem di = new DirItem(path1_);
349 PrintUtil.println(System.err, "test04_dir_item: 42 '"+path1_+" -> "+di.toString()+" -> '"+di.path()+"'\n");
350 Assert.assertTrue( "dir0/bbb".equals( di.dirname() ) );
351 Assert.assertTrue( "lala".equals( di.basename() ) );
352 Assert.assertTrue( "dir0/bbb/lala".equals( di.path() ) );
353 }
354 {
355 final String path1_ = "dir0/dir1/bbb/../../lala";
356 final DirItem di = new DirItem(path1_);
357 PrintUtil.println(System.err, "test04_dir_item: 43 '"+path1_+" -> "+di.toString()+" -> '"+di.path()+"'\n");
358 Assert.assertTrue( "dir0".equals( di.dirname() ) );
359 Assert.assertTrue( "lala".equals( di.basename() ) );
360 Assert.assertTrue( "dir0/lala".equals( di.path() ) );
361 }
362 {
363 final String path1_ = "dir0/dir1/bbb/../../../lala";
364 final DirItem di = new DirItem(path1_);
365 PrintUtil.println(System.err, "test04_dir_item: 44 '"+path1_+" -> "+di.toString()+" -> '"+di.path()+"'\n");
366 Assert.assertTrue( ".".equals( di.dirname() ) );
367 Assert.assertTrue( "lala".equals( di.basename() ) );
368 Assert.assertTrue( "lala".equals( di.path() ) );
369 }
370 {
371 final String path1_ = "dir0/dir1/bbb/../../../../lala";
372 final DirItem di = new DirItem(path1_);
373 PrintUtil.println(System.err, "test04_dir_item: 45 '"+path1_+" -> "+di.toString()+" -> '"+di.path()+"'\n");
374 Assert.assertTrue( "..".equals( di.dirname() ) );
375 Assert.assertTrue( "lala".equals( di.basename() ) );
376 Assert.assertTrue( "../lala".equals( di.path() ) );
377 }
378 {
379 final String path1_ = "dir0/dir1/bbb/../../lala/..";
380 final DirItem di = new DirItem(path1_);
381 PrintUtil.println(System.err, "test04_dir_item: 46 '"+path1_+" -> "+di.toString()+" -> '"+di.path()+"'\n");
382 Assert.assertTrue( ".".equals( di.dirname() ) );
383 Assert.assertTrue( "dir0".equals( di.basename() ) );
384 Assert.assertTrue( "dir0".equals( di.path() ) );
385 }
386 {
387 final String path1_ = "dir0/./dir1/./bbb/../.././lala";
388 final DirItem di = new DirItem(path1_);
389 PrintUtil.println(System.err, "test04_dir_item: 50 '"+path1_+" -> "+di.toString()+" -> '"+di.path()+"'\n");
390 Assert.assertTrue( "dir0".equals( di.dirname() ) );
391 Assert.assertTrue( "lala".equals( di.basename() ) );
392 Assert.assertTrue( "dir0/lala".equals( di.path() ) );
393 }
394 {
395 final String path1_ = "dir0/./dir1/./bbb/../.././lala/.";
396 final DirItem di = new DirItem(path1_);
397 PrintUtil.println(System.err, "test04_dir_item: 51 '"+path1_+" -> "+di.toString()+" -> '"+di.path()+"'\n");
398 Assert.assertTrue( "dir0".equals( di.dirname() ) );
399 Assert.assertTrue( "lala".equals( di.basename() ) );
400 Assert.assertTrue( "dir0/lala".equals( di.path() ) );
401 }
402 {
403 final String path1_ = "./dir0/./dir1/./bbb/../.././lala/.";
404 final DirItem di = new DirItem(path1_);
405 PrintUtil.println(System.err, "test04_dir_item: 51 '"+path1_+" -> "+di.toString()+" -> '"+di.path()+"'\n");
406 Assert.assertTrue( "dir0".equals( di.dirname() ) );
407 Assert.assertTrue( "lala".equals( di.basename() ) );
408 Assert.assertTrue( "dir0/lala".equals( di.path() ) );
409 }
410 {
411 final String path1_ = "/./dir0/./dir1/./bbb/../.././lala/.";
412 final DirItem di = new DirItem(path1_);
413 PrintUtil.println(System.err, "test04_dir_item: 52 '"+path1_+" -> "+di.toString()+" -> '"+di.path()+"'\n");
414 Assert.assertTrue( "/dir0".equals( di.dirname() ) );
415 Assert.assertTrue( "lala".equals( di.basename() ) );
416 Assert.assertTrue( "/dir0/lala".equals( di.path() ) );
417 }
418
419 {
420 final String path1_ = "../../test_data/file_01_slink09R1.txt";
421 final DirItem di = new DirItem(path1_);
422 PrintUtil.println(System.err, "test04_dir_item: 60 '"+path1_+" -> "+di.toString()+" -> '"+di.path()+"'\n");
423 Assert.assertTrue( "../../test_data".equals( di.dirname() ) );
424 Assert.assertTrue( "file_01_slink09R1.txt".equals( di.basename() ) );
425 Assert.assertTrue( path1_.equals( di.path() ) );
426 }
427
428 {
429 final String path1_ = "../../../jaulib/test_data";
430 final DirItem di = new DirItem(path1_);
431 PrintUtil.println(System.err, "test04_dir_item: 61 '"+path1_+" -> "+di.toString()+" -> '"+di.path()+"'\n");
432 Assert.assertTrue( "../../../jaulib".equals( di.dirname() ) );
433 Assert.assertTrue( "test_data".equals( di.basename() ) );
434 Assert.assertTrue( path1_.equals( di.path() ) );
435 }
436
437 {
438 final String path1_ = "../../../../jaulib/test_data";
439 final DirItem di = new DirItem(path1_);
440 PrintUtil.println(System.err, "test04_dir_item: 62 '"+path1_+" -> "+di.toString()+" -> '"+di.path()+"'\n");
441 Assert.assertTrue( "../../../../jaulib".equals( di.dirname() ) );
442 Assert.assertTrue( "test_data".equals( di.basename() ) );
443 Assert.assertTrue( path1_.equals( di.path() ) );
444 }
445
446 {
447 final String path1_ = "././././jaulib/test_data";
448 final DirItem di = new DirItem(path1_);
449 PrintUtil.println(System.err, "test04_dir_item: 63 '"+path1_+" -> "+di.toString()+" -> '"+di.path()+"'\n");
450 Assert.assertTrue( "jaulib".equals( di.dirname() ) );
451 Assert.assertTrue( "test_data".equals( di.basename() ) );
452 Assert.assertTrue( "jaulib/test_data".equals( di.path() ) );
453 }
454
455 {
456 final String path1_ = "a/././././jaulib/test_data";
457 final DirItem di = new DirItem(path1_);
458 PrintUtil.println(System.err, "test04_dir_item: 64 '"+path1_+" -> "+di.toString()+" -> '"+di.path()+"'\n");
459 Assert.assertTrue( "a/jaulib".equals( di.dirname() ) );
460 Assert.assertTrue( "test_data".equals( di.basename() ) );
461 Assert.assertTrue( "a/jaulib/test_data".equals( di.path() ) );
462 }
463
464 {
465 // Error
466 final String path1_ = "/../lala";
467 final DirItem di = new DirItem(path1_);
468 PrintUtil.println(System.err, "test04_dir_item: 99 '"+path1_+" -> "+di.toString()+" -> '"+di.path()+"'\n");
469 Assert.assertTrue( "/..".equals( di.dirname() ) );
470 Assert.assertTrue( "lala".equals( di.basename() ) );
471 Assert.assertTrue( "/../lala".equals( di.path() ) );
472 }
473 }
474
475 @Test(timeout = 10000)
476 public final void test05_file_stat() {
478 PrintUtil.println(System.err, "test05_file_stat\n");
479
480 {
481 final FileStats stats = new FileStats(project_root_ext+"/file_01.txt");
482 PrintUtil.fprintf_td(System.err, "test05_file_stat: 01: %s\n", stats);
483 PrintUtil.fprintf_td(System.err, "test05_file_stat: 01: fields %s\n", stats.fields());
484 if( stats.exists() ) {
485 Assert.assertTrue( stats.has_access() );
486 Assert.assertTrue( !stats.is_dir() );
487 Assert.assertTrue( stats.is_file() );
488 Assert.assertTrue( !stats.is_link() );
489 Assert.assertTrue( 15 == stats.size() );
490 }
491 }
492
493 final FileStats proot_stats = getTestDataDirStats();
494 PrintUtil.fprintf_td(System.err, "test05_file_stat: 11: %s\n", proot_stats);
495 PrintUtil.fprintf_td(System.err, "test05_file_stat: 11: fields %s\n", proot_stats.fields());
496 Assert.assertTrue( true == proot_stats.exists() );
497 Assert.assertTrue( true == proot_stats.is_dir() );
498
499 {
500 final FileStats stats = new FileStats(proot_stats.path()+"/file_01.txt");
501 PrintUtil.fprintf_td(System.err, "test05_file_stat: 12: %s\n", stats);
502 PrintUtil.fprintf_td(System.err, "test05_file_stat: 12: fields %s\n", stats.fields());
503 Assert.assertTrue( stats.exists() );
504 Assert.assertTrue( stats.has_access() );
505 Assert.assertTrue( !stats.is_dir() );
506 Assert.assertTrue( stats.is_file() );
507 Assert.assertTrue( !stats.is_link() );
508 Assert.assertTrue( 15 == stats.size() );
509
510 final long link_count[] = { 0 };
511 final FileStats final_target = stats.final_target(link_count);
512 PrintUtil.fprintf_td(System.err, "test05_file_stat: 12: final_target (%d link count): %s\n", link_count[0], final_target);
513 Assert.assertTrue( 0 == link_count[0] );
514 Assert.assertTrue( final_target.equals( stats ) );
515
516 {
517 final FileStats stats2 = new FileStats(proot_stats.path()+"/file_01.txt");
518 Assert.assertTrue( stats2.exists() );
519 Assert.assertTrue( stats2.has_access() );
520 Assert.assertTrue( !stats2.is_dir() );
521 Assert.assertTrue( stats2.is_file() );
522 Assert.assertTrue( !stats2.is_link() );
523 Assert.assertTrue( 15 == stats2.size() );
524 Assert.assertEquals( stats, stats2 );
525 }
526 {
527 final FileStats stats2 = new FileStats(proot_stats.path()+"/dir_01/file_02.txt");
528 Assert.assertTrue( stats2.exists() );
529 Assert.assertTrue( stats2.has_access() );
530 Assert.assertTrue( !stats2.is_dir() );
531 Assert.assertTrue( stats2.is_file() );
532 Assert.assertTrue( !stats2.is_link() );
533 Assert.assertNotEquals( stats, stats2 );
534 }
535 }
536 {
537 final FileStats stats = new FileStats(proot_stats.path()+"/dir_01");
538 PrintUtil.fprintf_td(System.err, "test05_file_stat: 13: %s\n", stats);
539 PrintUtil.fprintf_td(System.err, "test05_file_stat: 13: fields %s\n", stats.fields());
540 Assert.assertTrue( stats.exists() );
541 Assert.assertTrue( stats.has_access() );
542 Assert.assertTrue( stats.is_dir() );
543 Assert.assertTrue( !stats.is_file() );
544 Assert.assertTrue( !stats.is_link() );
545 Assert.assertTrue( 0 == stats.size() );
546
547 final long link_count[] = { 0 };
548 final FileStats final_target = stats.final_target(link_count);
549 PrintUtil.fprintf_td(System.err, "test05_file_stat: 13: final_target (%d link count): %s\n", link_count[0], final_target);
550 Assert.assertTrue( 0 == link_count[0] );
551 Assert.assertTrue( final_target.equals( stats ) );
552 }
553 {
554 final FileStats stats = new FileStats(proot_stats.path()+"/does_not_exist");
555 PrintUtil.fprintf_td(System.err, "test05_file_stat: 14: %s\n", stats);
556 PrintUtil.fprintf_td(System.err, "test05_file_stat: 14: fields %s\n", stats.fields());
557 Assert.assertTrue( !stats.exists() );
558 Assert.assertTrue( stats.has_access() );
559 Assert.assertTrue( !stats.is_dir() );
560 Assert.assertTrue( !stats.is_file() );
561 Assert.assertTrue( !stats.is_link() );
562 Assert.assertTrue( 0 == stats.size() );
563
564 final long link_count[] = { 0 };
565 final FileStats final_target = stats.final_target(link_count);
566 PrintUtil.fprintf_td(System.err, "test05_file_stat: 14: final_target (%d link count): %s\n", link_count[0], final_target);
567 Assert.assertTrue( 0 == link_count[0] );
568 Assert.assertTrue( final_target.equals( stats ) );
569 }
570 }
571
572 @Test(timeout = 10000)
573 public final void test06_file_stat_symlinks() {
575 PrintUtil.println(System.err, "test06_file_stat_symlinks\n");
576
577 final FileStats proot_stats = getTestDataDirStats();
578 Assert.assertTrue( true == proot_stats.exists() );
579 Assert.assertTrue( true == proot_stats.is_dir() );
580
581 {
582 final FileStats stats = new FileStats(proot_stats.path()+"/file_01_slink01.txt");
583 PrintUtil.fprintf_td(System.err, "test06_file_stat_symlinks: 13: %s\n", stats);
584 PrintUtil.fprintf_td(System.err, "test06_file_stat_symlinks: 13: fields %s\n", stats.fields());
585 Assert.assertTrue( stats.exists() );
586 Assert.assertTrue( stats.has_access() );
587 Assert.assertTrue( !stats.is_dir() );
588 Assert.assertTrue( stats.is_file() );
589 Assert.assertTrue( stats.is_link() );
590 Assert.assertTrue( 15 == stats.size() );
591 Assert.assertTrue( null != stats.link_target_path() );
592 Assert.assertTrue( "file_01.txt".equals( stats.link_target_path() ) );
593
594 final long link_count[] = { 0 };
595 final FileStats final_target = stats.final_target(link_count);
596 PrintUtil.fprintf_td(System.err, "- final_target (%d link count): %s\n", link_count[0], final_target);
597 Assert.assertTrue( 1 == link_count[0] );
598 Assert.assertTrue( final_target != stats );
599 Assert.assertTrue( (proot_stats.path()+"/file_01.txt").equals( final_target.path() ) );
600
601 Assert.assertTrue( null != stats.link_target() );
602 final FileStats link_target = stats.link_target();
603 Assert.assertTrue( null != link_target );
604 PrintUtil.fprintf_td(System.err, "- link_target %s\n", link_target);
605 Assert.assertTrue( final_target.equals( link_target ) );
606 Assert.assertTrue( !link_target.is_dir() );
607 Assert.assertTrue( link_target.is_file() );
608 Assert.assertTrue( !link_target.is_link() );
609 Assert.assertTrue( null == link_target.link_target_path() );
610 Assert.assertTrue( null == link_target.link_target() );
611 }
612 {
613 final FileStats stats = new FileStats(proot_stats.path()+"/fstab_slink07_absolute");
614 PrintUtil.fprintf_td(System.err, "test06_file_stat_symlinks: 14: %s\n", stats);
615 PrintUtil.fprintf_td(System.err, "test06_file_stat_symlinks: 14: fields %s\n", stats.fields());
616 Assert.assertTrue( stats.exists() );
617 Assert.assertTrue( stats.has_access() );
618 Assert.assertTrue( !stats.is_dir() );
619 Assert.assertTrue( stats.is_file() );
620 Assert.assertTrue( stats.is_link() );
621 Assert.assertTrue( 20 < stats.size() ); // greater than basename
622 Assert.assertTrue( null != stats.link_target_path() );
623 Assert.assertTrue( "/etc/fstab".equals( stats.link_target_path() ) );
624
625 final long link_count[] = { 0 };
626 final FileStats final_target = stats.final_target(link_count);
627 PrintUtil.fprintf_td(System.err, "- final_target (%d link count): %s\n", link_count[0], final_target);
628 Assert.assertTrue( 1 == link_count[0] );
629 Assert.assertTrue( final_target != stats );
630 Assert.assertTrue( "/etc/fstab".equals( final_target.path() ) );
631
632 Assert.assertTrue( null != stats.link_target() );
633 final FileStats link_target = stats.link_target();
634 Assert.assertTrue( null != link_target );
635 PrintUtil.fprintf_td(System.err, "- link_target %s\n", link_target);
636 Assert.assertTrue( final_target.equals( link_target ) );
637 Assert.assertTrue( !link_target.is_dir() );
638 Assert.assertTrue( link_target.is_file() );
639 Assert.assertTrue( !link_target.is_link() );
640 Assert.assertTrue( null == link_target.link_target_path() );
641 Assert.assertTrue( null == link_target.link_target() );
642 }
643 {
644 final FileStats stats = new FileStats(proot_stats.path()+"/file_01_slink10R2.txt"); // -> file_01_slink09R1.txt -> file_01_slink01.txt -> file_01.txt
645 PrintUtil.fprintf_td(System.err, "test06_file_stat_symlinks: 20: %s\n", stats);
646 PrintUtil.fprintf_td(System.err, "test06_file_stat_symlinks: 20: fields %s\n", stats.fields());
647 Assert.assertTrue( stats.exists() );
648 Assert.assertTrue( stats.has_access() );
649 Assert.assertTrue( !stats.is_dir() );
650 Assert.assertTrue( stats.is_file() );
651 Assert.assertTrue( stats.is_link() );
652 Assert.assertTrue( 15 == stats.size() );
653 Assert.assertTrue( null != stats.link_target_path() );
654 Assert.assertTrue( "file_01_slink09R1.txt".equals( stats.link_target_path() ) );
655
656 final long link_count[] = { 0 };
657 final FileStats final_target = stats.final_target(link_count);
658 PrintUtil.fprintf_td(System.err, "- final_target (%d link count): %s\n", link_count[0], final_target);
659 Assert.assertTrue( 3 == link_count[0] );
660 Assert.assertTrue( final_target != stats );
661 Assert.assertTrue( (proot_stats.path()+"/file_01.txt").equals( final_target.path() ) );
662
663 Assert.assertTrue( null != stats.link_target() );
664 final FileStats link_target1 = stats.link_target();
665 PrintUtil.fprintf_td(System.err, "- link_target1 %s\n", link_target1);
666 Assert.assertTrue( final_target != link_target1 );
667 Assert.assertTrue( (proot_stats.path()+"/file_01_slink09R1.txt").equals( link_target1.path() ) );
668 Assert.assertTrue( 15 == link_target1.size() );
669 Assert.assertTrue( !link_target1.is_dir() );
670 Assert.assertTrue( link_target1.is_file() );
671 Assert.assertTrue( link_target1.is_link() );
672 Assert.assertTrue( null != link_target1.link_target_path() );
673 Assert.assertTrue( "file_01_slink01.txt".equals( link_target1.link_target_path() ) );
674 {
675 final FileStats link_target2 = link_target1.link_target();
676 Assert.assertTrue( null != link_target2 );
677 PrintUtil.fprintf_td(System.err, " - link_target2 %s\n", link_target2);
678 Assert.assertTrue( final_target != link_target2 );
679 Assert.assertTrue( link_target1 != link_target2 );
680 Assert.assertTrue( (proot_stats.path()+"/file_01_slink01.txt").equals( link_target2.path() ) );
681 Assert.assertTrue( 15 == link_target2.size() );
682 Assert.assertTrue( !link_target2.is_dir() );
683 Assert.assertTrue( link_target2.is_file() );
684 Assert.assertTrue( link_target2.is_link() );
685 Assert.assertTrue( null != link_target2.link_target_path() );
686 Assert.assertTrue( "file_01.txt".equals( link_target2.link_target_path() ) );
687
688 final FileStats link_target3 = link_target2.link_target();
689 Assert.assertTrue( null != link_target3 );
690 PrintUtil.fprintf_td(System.err, " - link_target3 %s\n", link_target3);
691 Assert.assertTrue( final_target.equals( link_target3 ) );
692 Assert.assertTrue( link_target1 != link_target3 );
693 Assert.assertTrue( link_target2 != link_target3 );
694 Assert.assertTrue( 15 == link_target3.size() );
695 Assert.assertTrue( !link_target3.is_dir() );
696 Assert.assertTrue( link_target3.is_file() );
697 Assert.assertTrue( !link_target3.is_link() );
698 Assert.assertTrue( null == link_target3.link_target_path() );
699 Assert.assertTrue( null == link_target3.link_target() );
700 }
701 }
702 {
703 final FileStats stats = new FileStats(proot_stats.path()+"/dead_link23"); // -> not_existing_file
704 PrintUtil.fprintf_td(System.err, "test06_file_stat_symlinks: 30: %s\n", stats);
705 PrintUtil.fprintf_td(System.err, "test06_file_stat_symlinks: 30: fields %s\n", stats.fields());
706 Assert.assertTrue( !stats.exists() );
707 Assert.assertTrue( stats.has_access() );
708 Assert.assertTrue( !stats.is_dir() );
709 Assert.assertTrue( !stats.is_file() );
710 Assert.assertTrue( stats.is_link() );
711 Assert.assertTrue( 0 == stats.size() );
712 Assert.assertTrue( null != stats.link_target_path() );
713 Assert.assertTrue( "not_existing_file".equals( stats.link_target_path() ) );
714 Assert.assertTrue( null == stats.link_target() );
715
716 final long link_count[] = { 0 };
717 final FileStats final_target = stats.final_target(link_count);
718 PrintUtil.fprintf_td(System.err, "- final_target (%d link count): %s\n", link_count[0], final_target);
719 Assert.assertTrue( 0 == link_count[0] );
720 Assert.assertTrue( final_target.equals( stats ) );
721 }
722 {
723 final FileStats stats = new FileStats(proot_stats.path()+"/dead_link22"); // LOOP: dead_link22 -> dead_link21 -> dead_link20 -> dead_link22 ...
724 PrintUtil.fprintf_td(System.err, "test06_file_stat_symlinks: 31: %s\n", stats);
725 PrintUtil.fprintf_td(System.err, "test06_file_stat_symlinks: 31: fields %s\n", stats.fields());
726 Assert.assertTrue( !stats.exists() );
727 Assert.assertTrue( stats.has_access() );
728 Assert.assertTrue( !stats.is_dir() );
729 Assert.assertTrue( !stats.is_file() );
730 Assert.assertTrue( stats.is_link() );
731 Assert.assertTrue( 0 == stats.size() );
732 Assert.assertTrue( null != stats.link_target_path() );
733 Assert.assertTrue( "dead_link21".equals( stats.link_target_path() ) );
734 Assert.assertTrue( null == stats.link_target() );
735
736 final long link_count[] = { 0 };
737 final FileStats final_target = stats.final_target(link_count);
738 PrintUtil.fprintf_td(System.err, "- final_target (%d link count): %s\n", link_count[0], final_target);
739 Assert.assertTrue( 0 == link_count[0] );
740 Assert.assertTrue( final_target.equals( stats ) );
741 }
742 }
743
744 static void test_file_stat_fd_item(final FMode.Bit exp_type, final int fd, final String named_fd1, final String named_fd_link) {
745 PrintUtil.fprintf_td(System.err, "test_file_stat_fd_item: expect '%s', fd %d, name_fd1 '%s', make_fd_link '%s'\n", exp_type, fd, named_fd1, named_fd_link);
746 {
747 final FileStats stats = new FileStats(fd);
748 PrintUtil.fprintf_td(System.err, "fd: %s\n", stats);
749 Assert.assertTrue( stats.exists() );
750 Assert.assertTrue( stats.has_access() );
751 Assert.assertTrue( !stats.is_socket() );
752 Assert.assertTrue( !stats.is_block() );
753 Assert.assertTrue( !stats.is_dir() );
754 if( !stats.type_mode().isSet(exp_type) ) {
755 PrintUtil.fprintf_td(System.err, "INFO: Not matching expected type '%s': fd: %s\n", exp_type, stats);
756 }
757 Assert.assertTrue( stats.has_fd() );
758 Assert.assertEquals( fd, stats.fd() );
759 if( !stats.is_file() ) {
760 Assert.assertEquals( 0, stats.size() );
761 }
762 }
763 if( !named_fd1.isEmpty() ) {
764 final FileStats stats = new FileStats(named_fd1);
765 PrintUtil.fprintf_td(System.err, "fd_1: %s\n", stats);
766 Assert.assertTrue( stats.exists() );
767 Assert.assertTrue( stats.has_access() );
768 Assert.assertTrue( !stats.is_socket() );
769 Assert.assertTrue( !stats.is_block() );
770 Assert.assertTrue( !stats.is_dir() );
771 if( !stats.type_mode().isSet(exp_type) ) {
772 PrintUtil.fprintf_td(System.err, "INFO: Not matching expected type '%s': fd: %s\n", exp_type, stats);
773 }
774 Assert.assertTrue( stats.has_fd() );
775 Assert.assertEquals( fd, stats.fd() );
776 if( !stats.is_file() ) {
777 Assert.assertEquals( 0, stats.size() );
778 }
779 }
780 if( !named_fd_link.isEmpty() ) {
781 final FileStats stats = new FileStats(named_fd_link);
782 PrintUtil.fprintf_td(System.err, "fd_link: %s\n", stats);
783 Assert.assertTrue( stats.exists() );
784 Assert.assertTrue( stats.has_access() );
785 Assert.assertTrue( !stats.is_socket() );
786 Assert.assertTrue( !stats.is_block() );
787 Assert.assertTrue( !stats.is_dir() );
788 if( !stats.type_mode().isSet(exp_type) ) {
789 PrintUtil.fprintf_td(System.err, "INFO: Not matching expected type '%s': fd: %s\n", exp_type, stats);
790 }
791 Assert.assertTrue( stats.has_fd() );
792 Assert.assertEquals( fd, stats.fd() );
793 if( !stats.is_file() ) {
794 Assert.assertEquals( 0, stats.size() );
795 }
796
797 final long link_count[] = { 0 };
798 final FileStats final_target = stats.final_target(link_count);
799 Assert.assertNotNull( final_target );
800 PrintUtil.fprintf_td(System.err, "- final_target (%d link count): %s\n", link_count[0], final_target);
801 Assert.assertTrue( 1 <= link_count[0] );
802 Assert.assertTrue( 2 >= link_count[0] );
803 }
804 }
805
806 @Test(timeout = 10000)
807 public void test07_file_stat_fd() {
809 PrintUtil.println(System.err, "test07_file_stat_fd\n");
810
811 final String fd_stdin_1 = "/dev/fd/0";
812 final String fd_stdout_1 = "/dev/fd/1";
813 final String fd_stderr_1 = "/dev/fd/2";
814
815 final String fd_stdin_l = "/dev/stdin";
816 final String fd_stdout_l = "/dev/stdout";
817 final String fd_stderr_l = "/dev/stderr";
818
819 test_file_stat_fd_item(FMode.Bit.chr, 0, fd_stdin_1, fd_stdin_l);
820 test_file_stat_fd_item(FMode.Bit.chr, 1, fd_stdout_1, fd_stdout_l);
821 test_file_stat_fd_item(FMode.Bit.chr, 2, fd_stderr_1, fd_stderr_l);
822 try {
823 final ByteOutStream_File fos = new ByteOutStream_File("test07_file_stat_fd_tmp", FMode.def_file);
824 final int fd = fos.fd();
825 final String named_fd = FileUtil.to_named_fd(fd);
826 PrintUtil.fprintf_td(System.err, "XXXX.0: %s -> fd %d, named_fd '%s'\n", fos.toString(), fd, named_fd);
827 Assert.assertTrue( 0 <= fd );
828 test_file_stat_fd_item(FMode.Bit.file, fd, FileUtil.to_named_fd(fd), "");
829 fos.close();
830 } catch( final Exception e ) {
831 e.printStackTrace();
832 }
833 }
834
835 @Test(timeout = 10000)
836 public final void test10_mkdir() {
838 PrintUtil.println(System.err, "test10_mkdir\n");
839
840 FileUtil.remove(root, TraverseOptions.recursive); // start fresh
841 {
842 final FileStats root_stats = new FileStats(root);
843 PrintUtil.println(System.err, "root_stats.pre: "+root_stats);
844 Assert.assertTrue( !root_stats.exists() );
845 Assert.assertTrue( root_stats.has_access() );
846 Assert.assertTrue( !root_stats.is_dir() );
847 Assert.assertTrue( !root_stats.is_file() );
848 Assert.assertTrue( !root_stats.is_link() );
849 }
850 final FMode mode_def_dir = new FMode(FMode.Bit.def_dir_prot.value);
851 Assert.assertTrue( true == FileUtil.mkdir(root, mode_def_dir) );
852 {
853 final FileStats root_stats = new FileStats(root);
854 PrintUtil.println(System.err, "root_stats.post: "+root_stats);
855 Assert.assertTrue( root_stats.exists() );
856 Assert.assertTrue( root_stats.has_access() );
857 Assert.assertTrue( root_stats.is_dir() );
858 Assert.assertTrue( !root_stats.is_file() );
859 Assert.assertTrue( !root_stats.is_link() );
860 }
861 Assert.assertTrue( false == FileUtil.remove(root, TraverseOptions.none) );
862 Assert.assertTrue( true == FileUtil.remove(root, TraverseOptions.recursive) );
863 }
864
865 @Test(timeout = 10000)
866 public void test11_touch() {
868 PrintUtil.println(System.err, "test11_touch\n");
869 final String file_01 = root+"/data01.txt";
870 final String file_02 = root+"/data02.txt";
871 Assert.assertTrue( true == FileUtil.mkdir(root, FMode.def_dir) );
872 {
873 final FileStats root_stats = new FileStats(root);
874 PrintUtil.println(System.err, "root_stats1.post: "+root_stats);
875 Assert.assertTrue( root_stats.exists() );
876 Assert.assertTrue( root_stats.has_access() );
877 Assert.assertTrue( root_stats.is_dir() );
878 Assert.assertTrue( !root_stats.is_file() );
879 Assert.assertTrue( !root_stats.is_link() );
880 }
881
882 Assert.assertTrue( true == FileUtil.touch(file_01, FMode.def_dir) );
883 {
884 final Instant now = Instant.now();
885 final FileStats file_stats = new FileStats(file_01);
886 PrintUtil.println(System.err, "file_stats2.post: "+file_stats);
887 final Instant btime = file_stats.btime();
888 final Instant atime = file_stats.atime();
889 final long atime_td_ms = atime.until(now, ChronoUnit.MILLIS);
890 final Instant mtime = file_stats.mtime();
891 final long mtime_td_ms = mtime.until(now, ChronoUnit.MILLIS);
892 PrintUtil.println(System.err, "now: "+now.atZone(ZoneOffset.UTC));
893 PrintUtil.println(System.err, "btime: "+btime.atZone(ZoneOffset.UTC));
894 PrintUtil.println(System.err, "atime: "+atime.atZone(ZoneOffset.UTC)+", td_now "+atime_td_ms+"ms");
895 PrintUtil.println(System.err, "mtime: "+mtime.atZone(ZoneOffset.UTC)+", td_now "+mtime_td_ms+"ms");
896 Assert.assertTrue( file_stats.exists() );
897 Assert.assertTrue( file_stats.has_access() );
898 Assert.assertTrue( !file_stats.is_dir() );
899 Assert.assertTrue( file_stats.is_file() );
900 Assert.assertTrue( !file_stats.is_link() );
901 if( file_stats.has( FileStats.Field.Type.atime ) ) {
902 Assert.assertTrue( 1000 >= atime_td_ms );
903 }
904 if( file_stats.has( FileStats.Field.Type.mtime ) ) {
905 Assert.assertTrue( 1000 >= mtime_td_ms );
906 }
907 }
908
909 Assert.assertTrue( true == FileUtil.touch(file_02, FMode.def_dir ) );
910 {
911 final Instant now = Instant.now();
912 final FileStats file_stats_pre = new FileStats(file_02);
913 final Instant btime_pre = file_stats_pre.btime();
914 final Instant atime_pre = file_stats_pre.atime();
915 final long atime_td_ms = atime_pre.until(now, ChronoUnit.MILLIS);
916 final Instant mtime_pre = file_stats_pre.mtime();
917 final long mtime_td_ms = mtime_pre.until(now, ChronoUnit.MILLIS);
918 PrintUtil.println(System.err, "now : "+now.atZone(ZoneOffset.UTC));
919 PrintUtil.println(System.err, "btime.pre: "+btime_pre.atZone(ZoneOffset.UTC));
920 PrintUtil.println(System.err, "atime.pre: "+atime_pre.atZone(ZoneOffset.UTC)+", td_now "+atime_td_ms+"ms");
921 PrintUtil.println(System.err, "mtime.pre: "+mtime_pre.atZone(ZoneOffset.UTC)+", td_now "+mtime_td_ms+"ms");
922 if( file_stats_pre.has( FileStats.Field.Type.atime ) ) {
923 Assert.assertTrue( 1000 >= atime_td_ms );
924 }
925 if( file_stats_pre.has( FileStats.Field.Type.mtime ) ) {
926 Assert.assertTrue( 1000 >= mtime_td_ms );
927 }
928
929 // final jau::fraction_timespec ts_20200101( 1577836800_s + 0_h); // 2020-01-01 00:00:00
930 final Instant ts_20200101 = Instant.ofEpochSecond(1577836800); // 2020-01-01 00:00:00
931 final Instant atime_set = ts_20200101.plus( 1, ChronoUnit.DAYS).plus(10, ChronoUnit.HOURS);
932 final Instant mtime_set = ts_20200101.plus( 31, ChronoUnit.DAYS).plus(10, ChronoUnit.HOURS);
933 PrintUtil.println(System.err, "atime.set: "+atime_set.atZone(ZoneOffset.UTC)+", "+atime_set.atZone(ZoneOffset.UTC));
934 PrintUtil.println(System.err, "mtime.set: "+mtime_set.atZone(ZoneOffset.UTC)+", "+mtime_set.atZone(ZoneOffset.UTC));
935 Assert.assertTrue( true == FileUtil.touch(file_02, atime_set, mtime_set, FMode.def_file) );
936
937 final FileStats file_stats_post = new FileStats(file_02);
938 final Instant atime_post = file_stats_post.atime();
939 final Instant mtime_post = file_stats_post.mtime();
940 PrintUtil.println(System.err, "atime.post: "+atime_post.atZone(ZoneOffset.UTC)+", "+atime_post.atZone(ZoneOffset.UTC));
941 PrintUtil.println(System.err, "mtime.post: "+mtime_post.atZone(ZoneOffset.UTC)+", "+mtime_post.atZone(ZoneOffset.UTC));
942 PrintUtil.fprintf_td(System.err, "test11_touch: 03: %s\n", file_stats_post);
943 {
944 Assert.assertTrue( file_stats_post.exists() );
945 Assert.assertTrue( file_stats_post.has_access() );
946 Assert.assertTrue( !file_stats_post.is_dir() );
947 Assert.assertTrue( file_stats_post.is_file() );
948 Assert.assertTrue( !file_stats_post.is_link() );
949 if( file_stats_post.has( FileStats.Field.Type.atime ) ) {
950 Assert.assertTrue( atime_set.equals( file_stats_post.atime() ) );
951 }
952 if( file_stats_post.has( FileStats.Field.Type.mtime ) ) {
953 Assert.assertTrue( mtime_set.equals( file_stats_post.mtime() ) );
954 }
955 }
956 }
957
958 Assert.assertTrue( true == FileUtil.remove(root, TraverseOptions.recursive) );
959 }
960
961 @Test(timeout = 10000)
962 public void test20_visit() {
964 PrintUtil.println(System.err, "test20_visit\n");
965
966 final String sub_dir1 = root+"/sub1";
967 final String sub_dir2 = root+"/sub2";
968 final String sub_dir3 = root+"/sub1/sub3";
969
970 Assert.assertTrue( true == FileUtil.mkdir(root, FMode.def_dir) );
971 Assert.assertTrue( true == FileUtil.touch(root+"/data01.txt", FMode.def_file) );
972 Assert.assertTrue( true == FileUtil.touch(root+"/data02.txt", FMode.def_file) );
973 Assert.assertTrue( true == FileUtil.mkdir(sub_dir1, FMode.def_dir) );
974 Assert.assertTrue( true == FileUtil.mkdir(sub_dir2, FMode.def_dir) );
975 Assert.assertTrue( true == FileUtil.mkdir(sub_dir3, FMode.def_dir) );
976 Assert.assertTrue( true == FileUtil.touch(sub_dir1+"/data03.txt", FMode.def_file) );
977 Assert.assertTrue( true == FileUtil.touch(sub_dir1+"/data04.txt", FMode.def_file) );
978 Assert.assertTrue( true == FileUtil.touch(sub_dir2+"/data05.txt", FMode.def_file) );
979 Assert.assertTrue( true == FileUtil.touch(sub_dir2+"/data06.txt", FMode.def_file) );
980 Assert.assertTrue( true == FileUtil.touch(sub_dir3+"/data07.txt", FMode.def_file) );
981 Assert.assertTrue( true == FileUtil.touch(sub_dir3+"/data08.txt", FMode.def_file) );
982
983 final TraverseOptions topts_R_FSL_PDL = new TraverseOptions();
984 topts_R_FSL_PDL.set(TraverseOptions.Bit.recursive);
985 topts_R_FSL_PDL.set(TraverseOptions.Bit.follow_symlinks);
986 topts_R_FSL_PDL.set(TraverseOptions.Bit.dir_exit);
987 final VisitorStats stats_R_FSL_PDL = new VisitorStats(topts_R_FSL_PDL);
988 {
989 final PathStatsVisitor pv = new PathStatsVisitor(stats_R_FSL_PDL);
990 Assert.assertTrue( true == FileUtil.visit(root, topts_R_FSL_PDL, pv) );
991 PrintUtil.fprintf_td(System.err, "test20_visit[R, FSL, PDL]: %s\n%s\n", topts_R_FSL_PDL, stats_R_FSL_PDL);
992 Assert.assertTrue( 12 == stats_R_FSL_PDL.total_real );
993 Assert.assertTrue( 0 == stats_R_FSL_PDL.total_sym_links_existing );
994 Assert.assertTrue( 0 == stats_R_FSL_PDL.total_sym_links_not_existing );
995 Assert.assertTrue( 0 == stats_R_FSL_PDL.total_no_access );
996 Assert.assertTrue( 0 == stats_R_FSL_PDL.total_not_existing );
997 Assert.assertTrue( 0 == stats_R_FSL_PDL.total_file_bytes );
998 Assert.assertTrue( 8 == stats_R_FSL_PDL.files_real );
999 Assert.assertTrue( 0 == stats_R_FSL_PDL.files_sym_link );
1000 Assert.assertTrue( 4 == stats_R_FSL_PDL.dirs_real );
1001 Assert.assertTrue( 0 == stats_R_FSL_PDL.dirs_sym_link );
1002 }
1003 final TraverseOptions topts_R_FSL = new TraverseOptions();
1004 topts_R_FSL.set(TraverseOptions.Bit.recursive);
1006 topts_R_FSL.set(TraverseOptions.Bit.dir_entry);
1007 final VisitorStats stats_R_FSL = new VisitorStats(topts_R_FSL);
1008 {
1009 final PathStatsVisitor pv = new PathStatsVisitor(stats_R_FSL);
1010 Assert.assertTrue( true == FileUtil.visit(root, topts_R_FSL, pv) );
1011 PrintUtil.fprintf_td(System.err, "test20_visit[R, FSL]: %s\n%s\n", topts_R_FSL, stats_R_FSL);
1012 Assert.assertTrue( stats_R_FSL_PDL.equals( stats_R_FSL ) );
1013 }
1014 Assert.assertTrue( true == FileUtil.remove(root, TraverseOptions.recursive) );
1015 }
1016
1017 @Test(timeout = 10000)
1018 public void test22_visit_symlinks() {
1020 PrintUtil.println(System.err, "test22_visit_symlinks\n");
1021
1022 final FileStats proot_stats = getTestDataDirStats();
1023 Assert.assertTrue( true == proot_stats.exists() );
1024 Assert.assertTrue( true == proot_stats.is_dir() );
1025
1026 // recursive without symlinks
1027 {
1028 final TraverseOptions topts = new TraverseOptions();
1031 final VisitorStats stats = new VisitorStats(topts);
1032
1033 final PathStatsVisitor pv = new PathStatsVisitor(stats);
1034 Assert.assertTrue( true == FileUtil.visit(proot_stats, topts, pv) );
1035 PrintUtil.fprintf_td(System.err, "test22_visit[R]: %s\n%s\n", topts, stats);
1036 Assert.assertTrue( 7 == stats.total_real );
1037 Assert.assertTrue( 10 == stats.total_sym_links_existing );
1038 Assert.assertTrue( 4 == stats.total_sym_links_not_existing );
1039 Assert.assertTrue( 0 == stats.total_no_access );
1040 Assert.assertTrue( 4 == stats.total_not_existing );
1041 Assert.assertTrue( 60 == stats.total_file_bytes );
1042 Assert.assertTrue( 4 == stats.files_real );
1043 Assert.assertTrue( 9 == stats.files_sym_link );
1044 Assert.assertTrue( 3 == stats.dirs_real );
1045 Assert.assertTrue( 1 == stats.dirs_sym_link );
1046 }
1047
1048 // recursive with symlinks
1049 {
1050 final TraverseOptions topts = new TraverseOptions();
1054 final VisitorStats stats = new VisitorStats(topts);
1055
1056 final PathStatsVisitor pv = new PathStatsVisitor(stats);
1057 Assert.assertTrue( true == FileUtil.visit(proot_stats, topts, pv) );
1058 PrintUtil.fprintf_td(System.err, "test22_visit[R, FSL]: %s\n%s\n", topts, stats);
1059 Assert.assertTrue( 9 == stats.total_real );
1060 Assert.assertTrue( 11 == stats.total_sym_links_existing );
1061 Assert.assertTrue( 4 == stats.total_sym_links_not_existing );
1062 Assert.assertTrue( 0 == stats.total_no_access );
1063 Assert.assertTrue( 4 == stats.total_not_existing );
1064 Assert.assertTrue( 60 < stats.total_file_bytes ); // some followed symlink files are of unknown size, e.g. /etc/fstab
1065 Assert.assertTrue( 6 == stats.files_real );
1066 Assert.assertTrue( 10 == stats.files_sym_link );
1067 Assert.assertTrue( 3 == stats.dirs_real );
1068 Assert.assertTrue( 1 == stats.dirs_sym_link );
1069 }
1070 // flat with symlinks
1071 {
1072 final TraverseOptions topts = new TraverseOptions();
1077 final VisitorStats stats = new VisitorStats(topts);
1078
1079 final PathStatsVisitor pv_flat = new PathStatsVisitor(stats) {
1080 @Override
1081 public boolean visit(final TraverseEvent tevt, final FileStats item_stats, final long depth) {
1082 // PrintUtil.fprintf_td(System.err, "add: item_stats "+item_stats+", tevt "+tevt+"\n");
1083 if( TraverseEvent.dir_check_entry == tevt && depth > 1 ) {
1084 return false;
1085 }
1086 stats.add(item_stats);
1087 return true;
1088 }
1089 };
1090
1091 Assert.assertTrue( true == FileUtil.visit(proot_stats, topts, pv_flat) );
1092 PrintUtil.fprintf_td(System.err, "test22_visit[F, FSL]: %s\n%s\n", topts, stats);
1093 Assert.assertTrue( 3 == stats.total_real );
1094 Assert.assertTrue( 5 == stats.total_sym_links_existing );
1095 Assert.assertTrue( 4 == stats.total_sym_links_not_existing );
1096 Assert.assertTrue( 0 == stats.total_no_access );
1097 Assert.assertTrue( 4 == stats.total_not_existing );
1098 Assert.assertTrue( 60 < stats.total_file_bytes ); // some followed symlink files are of unknown size, e.g. /etc/fstab
1099 Assert.assertTrue( 1 == stats.files_real );
1100 Assert.assertTrue( 5 == stats.files_sym_link );
1101 Assert.assertTrue( 2 == stats.dirs_real );
1102 Assert.assertTrue( 0 == stats.dirs_sym_link );
1103 }
1104 }
1105
1106 @Test(timeout = 10000)
1107 public void test30_copy_file2dir() {
1109 PrintUtil.println(System.err, "test30_copy_file2dir\n");
1110
1111 final FileStats root_orig_stats = getTestDataDirStats();
1112 Assert.assertTrue( true == root_orig_stats.exists() );
1113 Assert.assertTrue( true == root_orig_stats.is_dir() );
1114
1115 final String root_copy = root+"_copy_test30";
1116 {
1117 // Fresh target folder
1119
1120 Assert.assertTrue( true == FileUtil.mkdir(root_copy, FMode.def_dir) );
1121 {
1122 final FileStats stats = new FileStats(root_copy);
1123 Assert.assertTrue( true == stats.exists() );
1124 Assert.assertTrue( true == stats.ok() );
1125 Assert.assertTrue( true == stats.is_dir() );
1126 }
1127 }
1128 final FileStats source1_stats = new FileStats(root_orig_stats.path()+"/file_01.txt");
1129 PrintUtil.fprintf_td(System.err, "test30_copy_file2dir: source1: %s\n", source1_stats);
1130 {
1131 Assert.assertTrue( true == source1_stats.exists() );
1132 Assert.assertTrue( true == source1_stats.ok() );
1133 Assert.assertTrue( true == source1_stats.is_file() );
1134 }
1135 {
1136 // Copy file to folder
1137 final CopyOptions copts = new CopyOptions();
1139 copts.set(CopyOptions.Bit.verbose);
1140 {
1141 final FileStats dest_stats = new FileStats(root_copy+"/file_01.txt");
1142 PrintUtil.fprintf_td(System.err, "test30_copy_file2dir: 01: dest.pre: %s\n", dest_stats);
1143 Assert.assertTrue( false == dest_stats.exists() );
1144 }
1145 Assert.assertTrue( true == FileUtil.copy(source1_stats.path(), root_copy, copts) );
1146 {
1147 final FileStats dest_stats = new FileStats(root_copy+"/file_01.txt");
1148 PrintUtil.fprintf_td(System.err, "test30_copy_file2dir: 01: dest.post: %s\n", dest_stats);
1149 Assert.assertTrue( true == dest_stats.exists() );
1150 Assert.assertTrue( true == dest_stats.ok() );
1151 Assert.assertTrue( true == dest_stats.is_file() );
1152 Assert.assertTrue( source1_stats.size() == dest_stats.size() );
1153 Assert.assertTrue( source1_stats.mode().equals( dest_stats.mode() ) );
1154 }
1155 }
1156 {
1157 // Error: already exists of 'Copy file to folder'
1158 final CopyOptions copts = new CopyOptions();
1160 copts.set(CopyOptions.Bit.verbose);
1161 {
1162 final FileStats dest_stats = new FileStats(root_copy+"/file_01.txt");
1163 PrintUtil.fprintf_td(System.err, "test30_copy_file2dir: 02: dest.pre: %s\n", dest_stats);
1164 Assert.assertTrue( true == dest_stats.exists() );
1165 Assert.assertTrue( true == dest_stats.ok() );
1166 Assert.assertTrue( true == dest_stats.is_file() );
1167 }
1168 Assert.assertTrue( false == FileUtil.copy(source1_stats.path(), root_copy, copts) );
1169 }
1170 {
1171 // Overwrite copy file to folder
1172 final CopyOptions copts = new CopyOptions();
1175 copts.set(CopyOptions.Bit.verbose);
1176
1177 PrintUtil.fprintf_td(System.err, "test30_copy_file2dir: 03: source: %s\n", source1_stats);
1178 {
1179 final FileStats dest_stats = new FileStats(root_copy+"/file_01.txt");
1180 PrintUtil.fprintf_td(System.err, "test30_copy_file2dir: 03: dest.pre: %s\n", dest_stats);
1181 Assert.assertTrue( true == dest_stats.exists() );
1182 Assert.assertTrue( true == dest_stats.ok() );
1183 Assert.assertTrue( true == dest_stats.is_file() );
1184 Assert.assertTrue( source1_stats.size() == dest_stats.size() );
1185 Assert.assertTrue( source1_stats.mode().equals( dest_stats.mode() ) );
1186 }
1187 Assert.assertTrue( true == FileUtil.copy(source1_stats.path(), root_copy, copts) );
1188 {
1189 final FileStats dest_stats = new FileStats(root_copy+"/file_01.txt");
1190 PrintUtil.fprintf_td(System.err, "test30_copy_file2dir: 03: dest.post: %s\n", dest_stats);
1191 Assert.assertTrue( true == dest_stats.exists() );
1192 Assert.assertTrue( true == dest_stats.ok() );
1193 Assert.assertTrue( true == dest_stats.is_file() );
1194 Assert.assertTrue( source1_stats.size() == dest_stats.size() );
1195 Assert.assertTrue( source1_stats.mode().equals( dest_stats.mode() ) );
1196 }
1197 }
1198 Assert.assertTrue( true == FileUtil.remove(root_copy, TraverseOptions.recursive) );
1199 }
1200
1201 @Test(timeout = 10000)
1202 public void test31_copy_file2file() {
1204 PrintUtil.println(System.err, "test31_copy_file2file\n");
1205
1206 final FileStats root_orig_stats = getTestDataDirStats();
1207 Assert.assertTrue( true == root_orig_stats.exists() );
1208 Assert.assertTrue( true == root_orig_stats.is_dir() );
1209
1210 final String root_copy = root+"_copy_test31";
1211 {
1212 // Fresh target folder
1214
1215 Assert.assertTrue( true == FileUtil.mkdir(root_copy, FMode.def_dir) );
1216 {
1217 final FileStats stats = new FileStats(root_copy);
1218 Assert.assertTrue( true == stats.exists() );
1219 Assert.assertTrue( true == stats.ok() );
1220 Assert.assertTrue( true == stats.is_dir() );
1221 }
1222 }
1223 final FileStats source1_stats = new FileStats(root_orig_stats.path()+"/file_01.txt");
1224 PrintUtil.fprintf_td(System.err, "test31_copy_file2file: source1: %s\n", source1_stats);
1225 {
1226 Assert.assertTrue( true == source1_stats.exists() );
1227 Assert.assertTrue( true == source1_stats.ok() );
1228 Assert.assertTrue( true == source1_stats.is_file() );
1229 }
1230 final FileStats source2_stats = new FileStats(root_orig_stats.path()+"/README_slink08_relext.txt");
1231 PrintUtil.fprintf_td(System.err, "test31_copy_file2file: source2: %s\n", source2_stats);
1232 {
1233 Assert.assertTrue( true == source2_stats.exists() );
1234 Assert.assertTrue( true == source2_stats.ok() );
1235 Assert.assertTrue( true == source2_stats.is_file() );
1236 Assert.assertTrue( true == source2_stats.is_link() );
1237 }
1238 {
1239 // Copy file to new file-name
1240 final CopyOptions copts = new CopyOptions();
1243 {
1244 final FileStats dest_stats = new FileStats(root_copy+"/file_10.txt");
1245 PrintUtil.fprintf_td(System.err, "test31_copy_file2file: 10: dest.pre: %s\n", dest_stats);
1246 Assert.assertTrue( false == dest_stats.exists() );
1247 }
1248 Assert.assertTrue( true == FileUtil.copy(source1_stats.path(), root_copy+"/file_10.txt", copts) );
1249 FileUtil.sync(); // just check API
1250 {
1251 final FileStats dest_stats = new FileStats(root_copy+"/file_10.txt");
1252 PrintUtil.fprintf_td(System.err, "test31_copy_file2file: 10: dest.post: %s\n", dest_stats);
1253 Assert.assertTrue( true == dest_stats.exists() );
1254 Assert.assertTrue( true == dest_stats.ok() );
1255 Assert.assertTrue( true == dest_stats.is_file() );
1256 Assert.assertTrue( source1_stats.size() == dest_stats.size() );
1257 Assert.assertTrue( source1_stats.mode().equals( dest_stats.mode() ) );
1258 }
1259 }
1260 {
1261 // Error: already exists of 'Copy file to file'
1262 final CopyOptions copts = new CopyOptions();
1265 {
1266 final FileStats dest_stats = new FileStats(root_copy+"/file_10.txt");
1267 PrintUtil.fprintf_td(System.err, "test31_copy_file2file: 11: dest.pre: %s\n", dest_stats);
1268 Assert.assertTrue( true == dest_stats.exists() );
1269 Assert.assertTrue( true == dest_stats.ok() );
1270 Assert.assertTrue( true == dest_stats.is_file() );
1271 }
1272 Assert.assertTrue( false == FileUtil.copy(source1_stats.path(), root_copy+"/file_10.txt", copts) );
1273 FileUtil.sync(); // just check API
1274 }
1275 {
1276 // Overwrite copy file to file
1277 final CopyOptions copts = new CopyOptions();
1282 {
1283 final FileStats dest_stats = new FileStats(root_copy+"/file_10.txt");
1284 PrintUtil.fprintf_td(System.err, "test31_copy_file2file: 12: dest.pre: %s\n", dest_stats);
1285 Assert.assertTrue( true == dest_stats.exists() );
1286 Assert.assertTrue( true == dest_stats.ok() );
1287 Assert.assertTrue( true == dest_stats.is_file() );
1288 Assert.assertTrue( source1_stats.size() == dest_stats.size() );
1289 Assert.assertTrue( source1_stats.mode().equals( dest_stats.mode() ) );
1290 }
1291 Assert.assertTrue( true == FileUtil.copy(source2_stats.path(), root_copy+"/file_10.txt", copts) );
1292 FileUtil.sync(); // just check API
1293 {
1294 final FileStats dest_stats = new FileStats(root_copy+"/file_10.txt");
1295 PrintUtil.fprintf_td(System.err, "test31_copy_file2file: 12: dest.post: %s\n", dest_stats);
1296 Assert.assertTrue( true == dest_stats.exists() );
1297 Assert.assertTrue( true == dest_stats.ok() );
1298 Assert.assertTrue( true == dest_stats.is_file() );
1299 Assert.assertTrue( false == dest_stats.is_link() );
1300 Assert.assertTrue( source2_stats.size() == dest_stats.size() );
1301 Assert.assertTrue( source2_stats.link_target().prot_mode().equals( dest_stats.prot_mode() ) );
1302 }
1303 }
1304 Assert.assertTrue( true == FileUtil.remove(root_copy, TraverseOptions.recursive) );
1305 }
1306
1307 @Test(timeout = 10000)
1308 public void test40_copy_ext_r_p() {
1310 PrintUtil.println(System.err, "test40_copy_ext_r_p\n");
1311
1312 final FileStats root_orig_stats = getTestDataDirStats();
1313 Assert.assertTrue( true == root_orig_stats.exists() );
1314 Assert.assertTrue( true == root_orig_stats.is_dir() );
1315
1316 final CopyOptions copts = new CopyOptions();
1321
1322 final String root_copy = root+"_copy_test40";
1324 testxx_copy_r_p("test40_copy_ext_r_p", root_orig_stats, 0 /* source_added_dead_links */, root_copy, copts, false /* dest_is_vfat */);
1325 Assert.assertTrue( true == FileUtil.remove(root_copy, TraverseOptions.recursive) );
1326 }
1327
1328 @Test(timeout = 10000)
1329 public void test41_copy_ext_r_p_below() {
1331 PrintUtil.println(System.err, "test41_copy_ext_r_p_below\n");
1332
1333 final FileStats root_orig_stats = getTestDataDirStats();
1334 Assert.assertTrue( true == root_orig_stats.exists() );
1335 Assert.assertTrue( true == root_orig_stats.is_dir() );
1336
1337 final CopyOptions copts = new CopyOptions();
1342
1343 final String root_copy_parent = root+"_copy_test41_parent";
1344 FileUtil.remove(root_copy_parent, TraverseOptions.recursive);
1345 Assert.assertTrue( FileUtil.mkdir(root_copy_parent) );
1346 testxx_copy_r_p("test41_copy_ext_r_p_below", root_orig_stats, 0 /* source_added_dead_links */, root_copy_parent, copts, false /* dest_is_vfat */);
1347 Assert.assertTrue( true == FileUtil.remove(root_copy_parent, TraverseOptions.recursive) );
1348 }
1349
1350 @Test(timeout = 10000)
1351 public void test42_copy_ext_r_p_into() {
1353 PrintUtil.println(System.err, "test42_copy_ext_r_p_into\n");
1354
1355 final FileStats root_orig_stats = getTestDataDirStats();
1356 Assert.assertTrue( true == root_orig_stats.exists() );
1357 Assert.assertTrue( true == root_orig_stats.is_dir() );
1358
1359 final CopyOptions copts = new CopyOptions();
1365
1366 final String root_copy = root+"_copy_test42_into";
1368 Assert.assertTrue( FileUtil.mkdir(root_copy) );
1369 testxx_copy_r_p("test42_copy_ext_r_p_into", root_orig_stats, 0 /* source_added_dead_links */, root_copy, copts, false /* dest_is_vfat */);
1370 Assert.assertTrue( true == FileUtil.remove(root_copy, TraverseOptions.recursive) );
1371 }
1372
1373 @Test(timeout = 10000)
1374 public void test43_copy_ext_r_p_over() {
1376 PrintUtil.println(System.err, "test43_copy_ext_r_p_over\n");
1377
1378 final FileStats root_orig_stats = getTestDataDirStats();
1379 Assert.assertTrue( true == root_orig_stats.exists() );
1380 Assert.assertTrue( true == root_orig_stats.is_dir() );
1381
1382 final CopyOptions copts = new CopyOptions();
1387
1388 final String root_copy = root+"_copy_test43_over";
1390 Assert.assertTrue( FileUtil.mkdir(root_copy) );
1391 final String root_copy_sub = root_copy+"/"+root_orig_stats.item().basename();
1392 Assert.assertTrue( FileUtil.mkdir(root_copy_sub) );
1393 testxx_copy_r_p("test43_copy_ext_r_p_over", root_orig_stats, 0 /* source_added_dead_links */, root_copy, copts, false /* dest_is_vfat */);
1394 Assert.assertTrue( true == FileUtil.remove(root_copy, TraverseOptions.recursive) );
1395 }
1396
1397 @Test(timeout = 10000)
1398 public void test49_copy_ext_r_p_vfat() {
1400 PrintUtil.println(System.err, "test49_copy_ext_r_p_vfat\n");
1401
1402 // Query and prepare vfat data sink
1403 final FileStats dest_fs_vfat_stats = new FileStats(dest_fs_vfat);
1404 if( !dest_fs_vfat_stats.is_dir() ) {
1405 PrintUtil.fprintf_td(System.err, "test49_copy_ext_r_p_vfat: Skipped, no vfat dest-dir %s\n", dest_fs_vfat_stats);
1406 return;
1407 }
1408 final String dest_vfat_parent = dest_fs_vfat+"/test49_data_sink";
1409 FileUtil.remove(dest_vfat_parent, TraverseOptions.recursive);
1410 if( !FileUtil.mkdir(dest_vfat_parent) ) {
1411 PrintUtil.fprintf_td(System.err, "test49_copy_ext_r_p_vfat: Skipped, couldn't create vfat dest folder %s\n", dest_vfat_parent);
1412 return;
1413 }
1414
1415 // Source
1416 final FileStats root_orig_stats = getTestDataDirStats();
1417 Assert.assertTrue( true == root_orig_stats.exists() );
1418 Assert.assertTrue( true == root_orig_stats.is_dir() );
1419
1420 final CopyOptions copts = new CopyOptions();
1426
1427 final String dest_vfat_dir = dest_vfat_parent+"/"+root;
1428 testxx_copy_r_p("test49_copy_ext_r_p_vfat", root_orig_stats, 0 /* source_added_dead_links */, dest_vfat_dir, copts, true /* dest_is_vfat */);
1429 Assert.assertTrue( true == FileUtil.remove(dest_vfat_parent, TraverseOptions.recursive) );
1430 }
1431
1432 @Test(timeout = 10000)
1433 public void test50_copy_ext_r_p_fsl() {
1435 PrintUtil.println(System.err, "test50_copy_ext_r_p_fsl\n");
1436
1437 final FileStats root_orig_stats = getTestDataDirStats();
1438 Assert.assertTrue( true == root_orig_stats.exists() );
1439 Assert.assertTrue( true == root_orig_stats.is_dir() );
1440
1441 final String root_copy = root+"_copy_test50";
1442 final CopyOptions copts = new CopyOptions();
1447 copts.set(CopyOptions.Bit.verbose);
1448 {
1450
1451 Assert.assertTrue( true == FileUtil.copy(root_orig_stats.path(), root_copy, copts) );
1452 }
1453 final FileStats root_copy_stats = new FileStats(root_copy);
1454 Assert.assertTrue( true == root_copy_stats.exists() );
1455 Assert.assertTrue( true == root_copy_stats.ok() );
1456 Assert.assertTrue( true == root_copy_stats.is_dir() );
1457
1458 {
1459 final TraverseOptions topts_orig = new TraverseOptions();
1460 topts_orig.set(TraverseOptions.Bit.recursive);
1461 topts_orig.set(TraverseOptions.Bit.dir_entry);
1463
1464 final TraverseOptions topts_copy = new TraverseOptions();
1465 topts_copy.set(TraverseOptions.Bit.recursive);
1466 topts_copy.set(TraverseOptions.Bit.dir_entry);
1467
1468 final VisitorStats stats = new VisitorStats(topts_orig);
1469 final VisitorStats stats_copy = new VisitorStats(topts_copy);
1470
1471 final PathStatsVisitor pv_orig = new PathStatsVisitor(stats);
1472 final PathStatsVisitor pv_copy = new PathStatsVisitor(stats_copy);
1473
1474 Assert.assertTrue( true == FileUtil.visit(root_orig_stats, topts_orig, pv_orig) );
1475 Assert.assertTrue( true == FileUtil.visit(root_copy_stats, topts_copy, pv_copy) );
1476
1477 PrintUtil.fprintf_td(System.err, "test50_copy_ext_r_p_fsl: copy %s, traverse_orig %s, traverse_copy %s\n", copts, topts_orig, topts_copy);
1478
1479 PrintUtil.fprintf_td(System.err, "test50_copy_ext_r_p_fsl: source visitor stats\n%s\n", stats);
1480 PrintUtil.fprintf_td(System.err, "test50_copy_ext_r_p_fsl: destination visitor stats\n%s\n", stats_copy);
1481
1482 Assert.assertTrue( 9 == stats.total_real );
1483 Assert.assertTrue( 11 == stats.total_sym_links_existing );
1484 Assert.assertTrue( 4 == stats.total_sym_links_not_existing );
1485 Assert.assertTrue( 0 == stats.total_no_access );
1486 Assert.assertTrue( 4 == stats.total_not_existing );
1487 Assert.assertTrue( 60 < stats.total_file_bytes ); // some followed symlink files are of unknown size, e.g. /etc/fstab
1488 Assert.assertTrue( 6 == stats.files_real );
1489 Assert.assertTrue( 10 == stats.files_sym_link );
1490 Assert.assertTrue( 3 == stats.dirs_real );
1491 Assert.assertTrue( 1 == stats.dirs_sym_link );
1492
1493 Assert.assertTrue( 20 == stats_copy.total_real );
1494 Assert.assertTrue( 0 == stats_copy.total_sym_links_existing );
1495 Assert.assertTrue( 0 == stats_copy.total_sym_links_not_existing );
1496 Assert.assertTrue( 0 == stats_copy.total_no_access );
1497 Assert.assertTrue( 0 == stats_copy.total_not_existing );
1498 Assert.assertTrue( 60 < stats_copy.total_file_bytes ); // some followed symlink files are of unknown size, e.g. /etc/fstab
1499 Assert.assertTrue( 16 == stats_copy.files_real );
1500 Assert.assertTrue( 0 == stats_copy.files_sym_link );
1501 Assert.assertTrue( 4 == stats_copy.dirs_real );
1502 Assert.assertTrue( 0 == stats_copy.dirs_sym_link );
1503 }
1504
1505 final String root_copy_renamed = root+"_copy_test50_renamed";
1506 {
1507 Assert.assertTrue( true == FileUtil.rename(root_copy, root_copy_renamed) );
1508 }
1509 final FileStats root_copy_stats2 = new FileStats(root_copy);
1510 Assert.assertTrue( false == root_copy_stats2.exists() );
1511
1512 final FileStats root_copy_renamed_stats = new FileStats(root_copy_renamed);
1513 Assert.assertTrue( true == root_copy_renamed_stats.exists() );
1514 Assert.assertTrue( true == root_copy_renamed_stats.ok() );
1515 Assert.assertTrue( true == root_copy_renamed_stats.is_dir() );
1516 {
1517 final TraverseOptions topts_copy = new TraverseOptions();
1518 topts_copy.set(TraverseOptions.Bit.recursive);
1519 topts_copy.set(TraverseOptions.Bit.dir_entry);
1520
1521 final VisitorStats stats_copy = new VisitorStats(topts_copy);
1522
1523 final PathStatsVisitor pv_copy = new PathStatsVisitor(stats_copy);
1524
1525 Assert.assertTrue( true == FileUtil.visit(root_copy_renamed_stats, topts_copy, pv_copy) );
1526
1527 PrintUtil.fprintf_td(System.err, "test50_copy_ext_r_p_fsl: renamed: traverse_copy %s\n", topts_copy);
1528
1529 PrintUtil.fprintf_td(System.err, "test50_copy_ext_r_p_fsl: renamed: visitor stats\n%s\n", stats_copy);
1530
1531 Assert.assertTrue( 20 == stats_copy.total_real );
1532 Assert.assertTrue( 0 == stats_copy.total_sym_links_existing );
1533 Assert.assertTrue( 0 == stats_copy.total_sym_links_not_existing );
1534 Assert.assertTrue( 0 == stats_copy.total_no_access );
1535 Assert.assertTrue( 0 == stats_copy.total_not_existing );
1536 Assert.assertTrue( 60 < stats_copy.total_file_bytes ); // some followed symlink files are of unknown size, e.g. /etc/fstab
1537 Assert.assertTrue( 16 == stats_copy.files_real );
1538 Assert.assertTrue( 0 == stats_copy.files_sym_link );
1539 Assert.assertTrue( 4 == stats_copy.dirs_real );
1540 Assert.assertTrue( 0 == stats_copy.dirs_sym_link );
1541 }
1542
1543 Assert.assertTrue( true == FileUtil.remove(root_copy_renamed, TraverseOptions.recursive) );
1544 }
1545
1546 public static void main(final String args[]) {
1547 org.junit.runner.JUnitCore.main(TestFileUtils01.class.getName());
1548 }
1549
1550}
Initialized by org.jau.sys.RuntimeProps.
void add(final FileStats element_stats)
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
String basename()
Return the basename, shall not be empty nor contain a dirname.
Definition: DirItem.java:63
String toString()
Definition: DirItem.java:82
String dirname()
Returns the dirname, shall not be empty and denotes .
Definition: DirItem.java:60
String path()
Returns a full unix path representation combining dirname() and basename().
Definition: DirItem.java:68
Generic file type and POSIX protection mode bits as used in file_stats, touch(), mkdir() etc.
Definition: FMode.java:37
static final FMode def_file
Default file protection bit: Safe default: POSIX S_IRUSR | S_IWUSR | S_IRGRP or read_usr | write_usr ...
Definition: FMode.java:161
boolean isSet(final Bit bit)
Definition: FMode.java:123
boolean equals(final Object other)
Definition: FMode.java:149
static final FMode def_dir
Default directory protection bit: Safe default: POSIX S_IRWXU | S_IRGRP | S_IXGRP or rwx_usr | read_g...
Definition: FMode.java:158
Platform agnostic representation of POSIX ::lstat() and ::stat() for a given pathname.
Definition: FileStats.java:41
boolean equals(final Object other)
Definition: FileStats.java:145
int fd()
Returns the file descriptor if has_fd(), otherwise -1 for no file descriptor.
Definition: FileStats.java:328
String path()
Returns the unix path representation.
Definition: FileStats.java:261
Field fields()
Returns the retrieved field_t fields.
Definition: FileStats.java:312
boolean is_file()
Returns true if entity is a file, might be in combination with is_link().
Definition: FileStats.java:386
Instant mtime()
Returns the last modification time of this element since Unix Epoch.
Definition: FileStats.java:350
boolean ok()
Returns true if no error occurred.
Definition: FileStats.java:356
boolean exists()
Returns true if entity does not exist, exclusive bit.
Definition: FileStats.java:395
FileStats final_target(final long link_count[])
Returns the final target element, either a pointer to this instance if not a symbolic-link or the fin...
Definition: FileStats.java:293
boolean has_access()
Returns true if entity gives no access to user, exclusive bit.
Definition: FileStats.java:392
FMode prot_mode()
Returns the POSIX protection bit portion of fmode_t, i.e.
Definition: FileStats.java:318
boolean is_link()
Returns true if entity is a symbolic link, might be in combination with is_file(),...
Definition: FileStats.java:389
String link_target_path()
Returns the stored link-target path this symbolic-link points to if instance is a symbolic-link,...
Definition: FileStats.java:270
DirItem item()
Returns the dir_item.
Definition: FileStats.java:250
FileStats link_target()
Returns the link-target this symbolic-link points to if instance is a symbolic-link,...
Definition: FileStats.java:281
FMode mode()
Returns the FMode, file type and mode.
Definition: FileStats.java:315
boolean is_socket()
Returns true if entity is a socket, might be in combination with is_link().
Definition: FileStats.java:371
boolean has_fd()
Returns true if entity has a file descriptor.
Definition: FileStats.java:368
Instant btime()
Returns the birth time of this element since Unix Epoch, i.e.
Definition: FileStats.java:344
boolean is_block()
Returns true if entity is a block device, might be in combination with is_link().
Definition: FileStats.java:374
boolean is_dir()
Returns true if entity is a directory, might be in combination with is_link().
Definition: FileStats.java:383
Instant atime()
Returns the last access time of this element since Unix Epoch.
Definition: FileStats.java:346
boolean has(final Field.Type bit)
Returns true if the given field_t fields were retrieved, otherwise false.
Definition: FileStats.java:309
FMode type_mode()
Returns the type bit portion of fmode_t, i.e.
Definition: FileStats.java:321
long size()
Returns the size in bytes of this element if is_file(), otherwise zero.
Definition: FileStats.java:341
Native file types and functionality.
Definition: FileUtil.java:33
static boolean touch(final String path, final Instant atime, final Instant mtime, final FMode mode)
Touch the file with given atime and mtime and create file if not existing yet.
Definition: FileUtil.java:131
static boolean copy(final String source_path, final String dest_path, final CopyOptions copts)
Copy the given source_path to dest_path using copy_options.
Definition: FileUtil.java:356
static native String dirname(final String path)
Return stripped last component from given path separated by /, excluding the trailing separator /.
static native String basename(final String path)
Return stripped leading directory components from given path separated by /.
static boolean mkdir(final String path, final FMode mode)
Create directory.
Definition: FileUtil.java:110
static boolean remove(final String path, final TraverseOptions topts)
Remove the given path.
Definition: FileUtil.java:304
static native void sync()
Synchronizes filesystems, i.e.
static native boolean rename(final String oldpath, final String newpath)
Rename oldpath to newpath using POSIX ::rename(), with the following combinations.
static native String to_named_fd(final int fd)
Returns platform dependent named file descriptor of given file descriptor, if supported.
static native String get_cwd()
Return the current working directory or empty on failure.
static boolean visit(final String path, final TraverseOptions topts, final PathVisitor visitor)
Visit element(s) of a given path, see traverse_options for detailed settings.
Definition: FileUtil.java:201
Filesystem traverse options used to visit() path elements.
TraverseOptions set(final Bit bit)
Sets the given bit and returns this instance for chaining.
static final TraverseOptions recursive
static final TraverseOptions none
File based byte output stream, including named file descriptor.
native int fd()
Returns the file descriptor if is_open(), otherwise -1 for no file descriptor.
void close()
Close the stream if supported by the underlying mechanism and dispose the native instance.
static void fprintf_td(final PrintStream out, final String format, final Object ... args)
Convenient PrintStream#printf(String, Object...) invocation, prepending the elapsedTimeMillis() times...
Definition: PrintUtil.java:37
static void println(final PrintStream out, final String msg)
Convenient PrintStream#println(String) invocation, prepending the elapsedTimeMillis() timestamp.
Definition: PrintUtil.java:46
follow_symlinks
Copy referenced symbolic linked files or directories instead of just the symbolic link with property ...
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.
ignore_symlink_errors
Ignore errors from erroneous symlinks, e.g.
overwrite
Overwrite existing destination files.
into_existing_dir
Copy source dir content into an already existing destination directory as if destination directory di...
file
Type: Entity is a file ), might be in combination with link.
Definition: FMode.java:101
def_dir_prot
Default directory protection bit: Safe default: POSIX S_IRWXU | S_IRGRP | S_IXGRP or rwx_usr | read_g...
Definition: FMode.java:82
chr
Type: Entity is a character device ), might be in combination with link.
Definition: FMode.java:95
Filesystem traverse event used to call path_visitor for path elements from visit().
dir_check_entry
Visiting a directory on entry, see traverse_options::dir_check_entry.
follow_symlinks
Traverse through symbolic linked directories if traverse_options::recursive is set,...
dir_entry
Visit the content's parent directory at entry.
dir_check_entry
Call path_visitor at directory entry, allowing path_visitor to skip traversal of this directory if re...
recursive
Traverse through directories, i.e.
dir_exit
Visit the content's parent directory at exit.