26import org.jau.fs.CopyOptions;
27import org.jau.fs.FileStats;
28import org.jau.fs.FileUtil;
29import org.jau.fs.TraverseEvent;
30import org.jau.fs.TraverseOptions;
31import org.jau.io.PrintUtil;
32import org.junit.Assert;
34import jau.test.junit.util.JunitTracer;
37 public static final String
image_file =
"test_data.sqfs";
38 public static final String
root =
"test_data";
46 public static final String
project_root2b =
"../../../../../jaulib/test_data";
54 if( path_stats.exists() ) {
58 if( path_stats.exists() ) {
62 if( path_stats.exists() ) {
66 if( path_stats.exists() ) {
69 return new FileStats();
73 if( path_stats.exists() ) {
77 if( path_stats.exists() ) {
81 if( path_stats.exists() ) {
85 if( path_stats.exists() ) {
91 FileStats path_stats =
new FileStats(
"../"+
image_file);
92 if( path_stats.exists() ) {
95 path_stats =
new FileStats(
"../../"+
image_file);
96 if( path_stats.exists() ) {
99 return new FileStats();
129 public void add(
final FileStats element_stats) {
130 if( element_stats.is_link() ) {
131 if( element_stats.exists() ) {
139 if( !element_stats.has_access() ) {
142 if( !element_stats.exists() ) {
145 if( element_stats.is_file() ) {
146 if( element_stats.is_link() ) {
148 if(
topts.isSet(TraverseOptions.Bit.follow_symlinks) ) {
155 }
else if( element_stats.is_dir() ) {
156 if( element_stats.is_link() ) {
165 public boolean equals(
final Object other) {
166 if(
this == other ) {
187 final StringBuilder res =
new StringBuilder();
188 res.append(
"- traverse_options ").append(
topts).append(
"\n");
189 res.append(
"- total_real ").append(
total_real).append(
"\n");
192 res.append(
"- total_no_access ").append(
total_no_access).append(
"\n");
194 res.append(
"- total_file_bytes ").append(String.format(
"%,d",
total_file_bytes)).append(
"\n");
195 res.append(
"- files_real ").append(
files_real).append(
"\n");
196 res.append(
"- files_sym_link ").append(
files_sym_link).append(
"\n");
197 res.append(
"- dirs_real ").append(
dirs_real).append(
"\n");
198 res.append(
"- dirs_sym_link ").append(
dirs_sym_link).append(
"\n");
199 return res.toString();
211 public boolean visit(
final TraverseEvent tevt,
final FileStats item_stats,
final long depth) {
213 stats.
add(item_stats);
218 static class source_visitor_params {
220 public String source_folder_path;
221 public FileStats dest;
222 public boolean dest_is_vfat;
223 public boolean opt_drop_dest_links;
225 public source_visitor_params(
final String t,
final String sfp,
final FileStats d,
final boolean dest_is_vfat_,
final boolean opt_drop_dest_links_) {
227 source_folder_path = sfp;
229 dest_is_vfat = dest_is_vfat_;
230 opt_drop_dest_links = opt_drop_dest_links_;
234 static class dest_visitor_params {
236 public String source_folder_path;
237 public String dest_folder_path;
238 public String source_basename;
239 public FileStats stats;
240 public boolean dest_is_vfat;
241 public boolean match;
242 public dest_visitor_params(
final String t,
final String sfp,
final String dfp,
final String sb,
final FileStats s,
final boolean dest_is_vfat_) {
244 source_folder_path = sfp;
245 dest_folder_path = dfp;
246 source_basename = sb;
248 dest_is_vfat = dest_is_vfat_;
253 public void testxx_copy_r_p(
final String title,
final FileStats source,
final int source_added_dead_links,
255 final CopyOptions copts,
256 final boolean dest_is_vfat) {
257 Assert.assertTrue( source.exists() );
258 Assert.assertTrue( source.is_dir() );
260 final boolean dest_is_parent;
261 final String dest_root;
263 final FileStats dest_stats =
new FileStats(dest);
264 if( dest_stats.exists() ) {
267 Assert.assertTrue( dest_stats.is_dir() );
268 if( copts.isSet(CopyOptions.Bit.into_existing_dir) ) {
269 dest_is_parent =
false;
272 dest_is_parent =
true;
273 dest_root = dest +
"/" + source.item().basename();
277 dest_is_parent =
false;
281 PrintUtil.fprintf_td(System.err,
"%s: source %s, dest[arg %s, is_parent %b, dest_root %s], copts %s, dest_is_vfat %b\n",
282 title, source, dest, dest_is_parent, dest_root, copts, dest_is_vfat);
284 final boolean opt_follow_links = copts.isSet(CopyOptions.Bit.follow_symlinks);
285 final boolean opt_drop_dest_links = !opt_follow_links && copts.isSet(CopyOptions.Bit.ignore_symlink_errors);
287 Assert.assertTrue(
true == FileUtil.copy(source.path(), dest, copts) );
289 final FileStats dest_stats =
new FileStats(dest_root);
290 Assert.assertTrue(
true == dest_stats.exists() );
291 Assert.assertTrue(
true == dest_stats.ok() );
292 Assert.assertTrue(
true == dest_stats.is_dir() );
295 final TraverseOptions topts =
new TraverseOptions();
296 topts.set(TraverseOptions.Bit.recursive);
297 topts.set(TraverseOptions.Bit.dir_entry);
305 Assert.assertTrue(
true == FileUtil.visit(source, topts, pv_orig) );
306 Assert.assertTrue(
true == FileUtil.visit(dest_stats, topts, pv_copy) );
308 PrintUtil.fprintf_td(System.err,
"%s: copy %s, traverse %s\n", title, copts, topts);
309 PrintUtil.fprintf_td(System.err,
"%s: source visitor stats\n%s\n", title, stats);
310 PrintUtil.fprintf_td(System.err,
"%s: destination visitor stats\n%s\n", title, stats_copy);
319 Assert.assertTrue( 9 - source_added_dead_links == stats.
files_sym_link );
320 Assert.assertTrue( 3 == stats.
dirs_real );
323 if( ( !opt_follow_links && !opt_drop_dest_links ) ||
328 Assert.assertTrue( 7 == stats_copy.
total_real );
334 Assert.assertTrue( 4 == stats_copy.
files_real );
336 Assert.assertTrue( 3 == stats_copy.
dirs_real );
338 }
else if( opt_drop_dest_links ) {
340 Assert.assertTrue( 7 == stats_copy.
total_real );
346 Assert.assertTrue( 4 == stats_copy.
files_real );
348 Assert.assertTrue( 3 == stats_copy.
dirs_real );
350 }
else if( opt_follow_links ) {
352 Assert.assertTrue( 20 == stats_copy.
total_real );
358 Assert.assertTrue( 16 == stats_copy.
files_real );
360 Assert.assertTrue( 4 == stats_copy.
dirs_real );
366 final TraverseOptions topts =
new TraverseOptions();
367 topts.set(TraverseOptions.Bit.recursive);
368 topts.set(TraverseOptions.Bit.dir_entry);
370 final source_visitor_params svp =
new source_visitor_params(title, source.path(), dest_stats, dest_is_vfat, opt_drop_dest_links);
371 final FileUtil.PathVisitor pv1 =
new FileUtil.PathVisitor() {
373 public boolean visit(
final TraverseEvent tevt1,
final FileStats element_stats1,
final long depth) {
374 final dest_visitor_params dvp =
new dest_visitor_params(svp.title, svp.source_folder_path, svp.dest.path(), FileUtil.basename(element_stats1.path() ), element_stats1, svp.dest_is_vfat);
375 final FileUtil.PathVisitor pv2 =
new FileUtil.PathVisitor() {
377 public boolean visit(
final TraverseEvent tevt2,
final FileStats element_stats2,
final long depth2) {
378 final String path2 = element_stats2.path();
379 final String basename2 = FileUtil.basename( path2 );
380 final String source_folder_basename = FileUtil.basename( dvp.source_folder_path );
381 if( basename2.equals( dvp.source_basename ) ||
382 ( source_folder_basename.equals( dvp.source_basename ) && dvp.dest_folder_path.equals( path2 ) )
385 boolean attr_equal, bit_equal;
386 if(
"README_slink08_relext.txt".equals(basename2) || 0 == basename2.indexOf(
"dead_link") ) {
389 attr_equal = element_stats2.is_link() &&
390 !element_stats2.exists();
394 if( !dvp.dest_is_vfat ) {
397 element_stats2.mode().equals( dvp.stats.mode() ) &&
399 element_stats2.mtime().equals( dvp.stats.mtime() ) &&
400 element_stats2.uid() == dvp.stats.uid() &&
401 element_stats2.gid() == dvp.stats.gid() &&
402 element_stats2.size() == dvp.stats.size();
406 final long td_ms = 5000;
411 Math.abs( element_stats2.mtime().toEpochMilli() - dvp.stats.mtime().toEpochMilli() ) <= td_ms &&
412 element_stats2.uid() == dvp.stats.uid() &&
414 element_stats2.size() == dvp.stats.size();
416 if( dvp.stats.is_file() ) {
417 bit_equal = FileUtil.compare(dvp.stats.path(), element_stats2.path(),
true);
422 dvp.match = attr_equal && bit_equal;
423 PrintUtil.fprintf_td(System.err,
"%s.check: '%s', match [attr %b, bit %b -> %b]\n\t source %s\n\t dest__ %s\n\n",
424 dvp.title, basename2, attr_equal, bit_equal, dvp.match,
432 if( FileUtil.visit(svp.dest, topts, pv2) ) {
434 final boolean ignore = element_stats1.is_link() && svp.opt_drop_dest_links;
435 PrintUtil.fprintf_td(System.err,
"%s.check: %s: '%s', not found!\n\t source %s\n\n",
436 svp.title, ignore ?
"Ignored" :
"Error", dvp.source_basename, element_stats1);
447 Assert.assertTrue(
true == FileUtil.visit(source, topts, pv1) );
PathStatsVisitor(final VisitorStats stats_)
boolean visit(final TraverseEvent tevt, final FileStats item_stats, final long depth)
void add(final FileStats element_stats)
VisitorStats(final TraverseOptions topts_)
boolean equals(final Object other)
int total_sym_links_existing
int total_sym_links_not_existing
static final FileStats getTestDataImageFile()
static String getTestDataRelDir()
static final String project_root_ext
static final String project_root1a
static final String project_root2b
static final String project_root1b
static final String project_root2a
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 final String image_file
static final String dest_fs_vfat