File: HDF5Archiver.java

package info (click to toggle)
libsis-jhdf5-java 19.04.0%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 8,668 kB
  • sloc: java: 79,644; ansic: 18,986; sh: 309; makefile: 49; xml: 12
file content (966 lines) | stat: -rw-r--r-- 33,576 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
/*
 * Copyright 2007 - 2018 ETH Zuerich, CISD and SIS.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package ch.systemsx.cisd.hdf5.h5ar;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.File;
import java.io.Flushable;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import ch.systemsx.cisd.base.exceptions.IErrorStrategy;
import ch.systemsx.cisd.base.exceptions.IOExceptionUnchecked;
import ch.systemsx.cisd.base.io.AdapterIInputStreamToInputStream;
import ch.systemsx.cisd.base.io.AdapterIOutputStreamToOutputStream;
import ch.systemsx.cisd.base.io.IInputStream;
import ch.systemsx.cisd.base.io.IOutputStream;
import ch.systemsx.cisd.hdf5.HDF5DataBlock;
import ch.systemsx.cisd.hdf5.HDF5FactoryProvider;
import ch.systemsx.cisd.hdf5.IHDF5Reader;
import ch.systemsx.cisd.hdf5.IHDF5Writer;
import ch.systemsx.cisd.hdf5.IHDF5WriterConfigurator;
import ch.systemsx.cisd.hdf5.IHDF5WriterConfigurator.FileFormatVersionBounds;
import ch.systemsx.cisd.hdf5.IHDF5WriterConfigurator.SyncMode;
import ch.systemsx.cisd.hdf5.h5ar.NewArchiveEntry.NewDirectoryArchiveEntry;
import ch.systemsx.cisd.hdf5.h5ar.NewArchiveEntry.NewFileArchiveEntry;
import ch.systemsx.cisd.hdf5.h5ar.NewArchiveEntry.NewSymLinkArchiveEntry;
import ch.systemsx.cisd.hdf5.io.HDF5IOAdapterFactory;

/**
 * An archiver based on HDF5 as archive format for directory with fast random access to particular
 * files in the archive.
 * 
 * @author Bernd Rinn
 */
final class HDF5Archiver implements Closeable, Flushable, IHDF5Archiver, IHDF5ArchiveInfoProvider
{
    private static final String HOUSEKEEPING_SUFFIX = "\1\0";

    public static final int CHUNK_SIZE_AUTO = -1;

    private final static int MB = 1024 * 1024;

    final static int BUFFER_SIZE = 10 * MB;

    private final IHDF5Reader hdf5Reader;

    private final IHDF5Writer hdf5WriterOrNull;

    private final boolean closeReaderOnCloseFile;

    private final IErrorStrategy errorStrategy;

    private final IDirectoryIndexProvider indexProvider;

    private final byte[] buffer;

    private final HDF5ArchiveUpdater updaterOrNull;

    private final HDF5ArchiveDeleter deleterOrNull;

    private final HDF5ArchiveTraverser processor;

    private final IdCache idCache;

    static IHDF5Reader createHDF5Reader(File archiveFile)
    {
        return HDF5FactoryProvider.get().openForReading(archiveFile);
    }

    static IHDF5Writer createHDF5Writer(File archiveFile, FileFormatVersionBounds fileFormat, boolean noSync)
    {
        final IHDF5WriterConfigurator config = HDF5FactoryProvider.get().configure(archiveFile);
        config.fileFormat(fileFormat);
        if (fileFormat.getHighBound().supportsMDCCache())
        {
            config.generateMDCImage();
        }
        config.useUTF8CharacterEncoding();
        config.houseKeepingNameSuffix(HOUSEKEEPING_SUFFIX);
        if (noSync == false)
        {
            config.syncMode(SyncMode.SYNC);
        }
        return config.writer();
    }

    HDF5Archiver(File archiveFile, boolean readOnly)
    {
        this(archiveFile, readOnly, false, FileFormatVersionBounds.V1_8_V1_8, null);
    }

    HDF5Archiver(File archiveFile, boolean readOnly, boolean noSync, FileFormatVersionBounds fileFormatVersionBounds,
            IErrorStrategy errorStrategyOrNull)
    {
        this.buffer = new byte[BUFFER_SIZE];
        this.closeReaderOnCloseFile = true;
        this.hdf5WriterOrNull = readOnly ? null : createHDF5Writer(archiveFile, fileFormatVersionBounds, noSync);
        this.hdf5Reader =
                (hdf5WriterOrNull != null) ? hdf5WriterOrNull : createHDF5Reader(archiveFile);
        if (errorStrategyOrNull == null)
        {
            this.errorStrategy = IErrorStrategy.DEFAULT_ERROR_STRATEGY;
        } else
        {
            this.errorStrategy = errorStrategyOrNull;
        }
        this.indexProvider = new DirectoryIndexProvider(hdf5Reader, errorStrategy);
        this.idCache = new IdCache();
        this.processor = new HDF5ArchiveTraverser(new HDF5ArchiveTraverser.IDirectoryChecker()
            {
                @Override
                public boolean isDirectoryFollowSymlinks(ArchiveEntry entry)
                {
                    final ArchiveEntry resolvedEntry = tryResolveLink(entry);
                    return (resolvedEntry == null) ? false : resolvedEntry.isDirectory();
                }
            }, hdf5Reader, indexProvider, idCache);
        if (hdf5WriterOrNull == null)
        {
            this.updaterOrNull = null;
            this.deleterOrNull = null;
        } else
        {
            this.updaterOrNull =
                    new HDF5ArchiveUpdater(hdf5WriterOrNull, indexProvider, idCache, buffer);
            this.deleterOrNull = new HDF5ArchiveDeleter(hdf5WriterOrNull, indexProvider, idCache);
        }
    }

    HDF5Archiver(IHDF5Reader reader, boolean enforceReadOnly, IErrorStrategy errorStrategyOrNull)
    {
        this.buffer = new byte[BUFFER_SIZE];
        this.closeReaderOnCloseFile = false;
        this.hdf5WriterOrNull =
                (enforceReadOnly == false && reader instanceof IHDF5Writer) ? (IHDF5Writer) reader
                        : null;
        if (errorStrategyOrNull == null)
        {
            this.errorStrategy = IErrorStrategy.DEFAULT_ERROR_STRATEGY;
        } else
        {
            this.errorStrategy = errorStrategyOrNull;
        }
        this.hdf5Reader = reader;
        this.indexProvider = new DirectoryIndexProvider(hdf5Reader, errorStrategy);
        this.idCache = new IdCache();
        this.processor = new HDF5ArchiveTraverser(new HDF5ArchiveTraverser.IDirectoryChecker()
            {
                @Override
                public boolean isDirectoryFollowSymlinks(ArchiveEntry entry)
                {
                    return tryResolveLink(entry).isDirectory();
                }
            }, hdf5Reader, indexProvider, idCache);
        if (hdf5WriterOrNull == null)
        {
            this.updaterOrNull = null;
            this.deleterOrNull = null;
        } else
        {
            this.updaterOrNull =
                    new HDF5ArchiveUpdater(hdf5WriterOrNull, indexProvider, idCache, buffer);
            this.deleterOrNull = new HDF5ArchiveDeleter(hdf5WriterOrNull, indexProvider, idCache);
        }
    }

    //
    // Closeable
    //

    @Override
    public void close()
    {
        if (isClosed() == false)
        {
            flush();
        }
        if (closeReaderOnCloseFile)
        {
            hdf5Reader.close();
        } else
        {
            indexProvider.close();
        }
    }

    @Override
    public boolean isClosed()
    {
        return hdf5Reader.file().isClosed();
    }

    //
    // Flusheable
    //

    @Override
    public void flush()
    {
        if (hdf5WriterOrNull != null)
        {
            hdf5WriterOrNull.file().flush();
        }
    }

    //
    // IHDF5ArchiveInfo
    //

    @Override
    public boolean exists(String path)
    {
        final String normalizedPath = Utils.normalizePath(path);
        final String parentPath = Utils.getParentPath(normalizedPath);
        final String name = Utils.getName(normalizedPath);
        return indexProvider.get(parentPath, false).exists(name);
    }

    @Override
    public boolean isDirectory(String path)
    {
        final String normalizedPath = Utils.normalizePath(path);
        final String parentPath = Utils.getParentPath(normalizedPath);
        final String name = Utils.getName(normalizedPath);
        return indexProvider.get(parentPath, false).isDirectory(name);
    }

    @Override
    public boolean isRegularFile(String path)
    {
        return isRegularFile(tryGetLink(path, false));
    }

    @Override
    public boolean isSymLink(String path)
    {
        return isSymLink(tryGetLink(path, false));
    }

    @Override
    public ArchiveEntry tryGetEntry(String path, boolean readLinkTarget)
    {
        final String normalizedPath = Utils.normalizePath(path);
        if ("/".equals(normalizedPath))
        {
            return new ArchiveEntry("", "/", LinkRecord.getLinkRecordForArchiveRoot(hdf5Reader
                    .file().getFile()), idCache);
        }
        final String parentPath = Utils.getParentPath(normalizedPath);
        final String name = Utils.getName(normalizedPath);
        return Utils.tryToArchiveEntry(parentPath, normalizedPath,
                indexProvider.get(parentPath, readLinkTarget).tryGetLink(name), idCache);
    }

    private LinkRecord tryGetLink(String path, boolean readLinkTargets)
    {
        final String normalizedPath = Utils.normalizePath(path);
        final String parentPath = Utils.getParentPath(normalizedPath);
        final String name = Utils.getName(normalizedPath);
        return indexProvider.get(parentPath, readLinkTargets).tryGetLink(name);
    }

    @Override
    public ArchiveEntry tryResolveLink(ArchiveEntry entry)
    {
        if (entry == null)
        {
            return null;
        }
        ArchiveEntry workEntry = entry;
        String firstPath = null;
        if (entry.isSymLink())
        {
            Set<String> workPathSet = null;
            while (workEntry != null && workEntry.isSymLink())
            {
                if (firstPath == null)
                {
                    firstPath = workEntry.getPath();
                } else
                {
                    if (workPathSet == null)
                    {
                        workPathSet = new HashSet<String>();
                        workPathSet.add(firstPath);
                    }
                    if (workPathSet.contains(workEntry.getPath()))
                    {
                        // The link targets form a loop, resolve to null.
                        return null;
                    }
                    workPathSet.add(workEntry.getPath());
                }
                String linkTarget;
                if (workEntry.hasLinkTarget())
                {
                    linkTarget = workEntry.getLinkTarget();
                } else
                {
                    workEntry = tryGetEntry(workEntry.getPath(), true);
                    linkTarget = workEntry.getLinkTarget();
                }
                if (linkTarget.startsWith("/") == false)
                {
                    linkTarget = Utils.concatLink(workEntry.getParentPath(), linkTarget);
                }
                linkTarget = Utils.normalizePath(linkTarget);
                if (linkTarget == null) // impossible link target like '/..'
                {
                    return null;
                }
                workEntry = tryGetEntry(linkTarget, true);
            }
        }
        return workEntry;
    }

    @Override
    public ArchiveEntry tryGetResolvedEntry(String path, boolean keepPath)
    {
        final ArchiveEntry entry = tryGetEntry(path, true);
        ArchiveEntry resolvedEntry = tryResolveLink(entry);
        if (resolvedEntry == null)
        {
            return null;
        }
        if (entry != resolvedEntry && keepPath)
        {
            resolvedEntry = new ArchiveEntry(entry, resolvedEntry);
        }
        return resolvedEntry;
    }

    private static boolean isRegularFile(LinkRecord linkOrNull)
    {
        return linkOrNull != null && linkOrNull.isRegularFile();
    }

    private static boolean isSymLink(LinkRecord linkOrNull)
    {
        return linkOrNull != null && linkOrNull.isSymLink();
    }

    //
    // IHDF5ArchiveReader
    //

    @Override
    public List<ArchiveEntry> list()
    {
        return list("/", ListParameters.DEFAULT);
    }

    @Override
    public List<ArchiveEntry> list(final String fileOrDir)
    {
        return list(fileOrDir, ListParameters.DEFAULT);
    }

    @Override
    public List<ArchiveEntry> list(final String fileOrDir, final ListParameters params)
    {
        final List<ArchiveEntry> result = new ArrayList<ArchiveEntry>(100);
        list(fileOrDir, new IArchiveEntryVisitor()
            {
                @Override
                public void visit(ArchiveEntry entry)
                {
                    result.add(entry);
                }
            }, params);
        return result;
    }

    @Override
    public List<ArchiveEntry> test()
    {
        final List<ArchiveEntry> result = new ArrayList<ArchiveEntry>(100);
        list("/", new IArchiveEntryVisitor()
            {
                @Override
                public void visit(ArchiveEntry entry)
                {
                    if (entry.isOK() == false)
                    {
                        result.add(entry);
                    }
                }
            }, ListParameters.TEST);
        return result;
    }

    @Override
    public IHDF5Archiver list(String fileOrDir, IArchiveEntryVisitor visitor)
    {
        return list(fileOrDir, visitor, ListParameters.DEFAULT);
    }

    @Override
    public IHDF5Archiver list(final String fileOrDir, final IArchiveEntryVisitor visitor,
            final ListParameters params)
    {
        final String normalizedPath = Utils.normalizePath(fileOrDir);
        final IArchiveEntryVisitor decoratedVisitor = new IArchiveEntryVisitor()
            {
                @Override
                public void visit(ArchiveEntry entry)
                {
                    if (params.isIncludeTopLevelDirectoryEntry() == false
                            && normalizedPath.equals(entry.getPath()))
                    {
                        return;
                    }
                    ArchiveEntry workEntry = entry;
                    if (workEntry.isSymLink() && params.isResolveSymbolicLinks())
                    {
                        workEntry = tryResolveLink(workEntry);
                        if (workEntry == null)
                        {
                            return;
                        }
                        if (workEntry != entry)
                        {
                            workEntry = new ArchiveEntry(entry, workEntry);
                        }
                    }
                    if (params.isSuppressDirectoryEntries() == false
                            || workEntry.isDirectory() == false)
                    {
                        visitor.visit(workEntry);
                    }
                }
            };
        final ArchiveEntryListProcessor listProcessor =
                new ArchiveEntryListProcessor(decoratedVisitor, buffer, params.isTestArchive());
        processor.process(normalizedPath, params.isRecursive(), params.isReadLinkTargets(),
                params.isFollowSymbolicLinks(), listProcessor);
        return this;
    }

    @Override
    public List<ArchiveEntry> verifyAgainstFilesystem(File rootDirectoryOnFS)
    {
        return verifyAgainstFilesystem("/", rootDirectoryOnFS, VerifyParameters.DEFAULT);
    }

    @Override
    public List<ArchiveEntry> verifyAgainstFilesystem(File rootDirectoryOnFS, VerifyParameters params)
    {
        return verifyAgainstFilesystem("/", rootDirectoryOnFS, params);
    }

    @Override
    public List<ArchiveEntry> verifyAgainstFilesystem(String fileOrDir, File rootDirectoryOnFS)
    {
        return verifyAgainstFilesystem(fileOrDir, rootDirectoryOnFS, VerifyParameters.DEFAULT);
    }

    @Override
    public IHDF5Archiver verifyAgainstFilesystem(String fileOrDir, File rootDirectoryOnFS,
            IArchiveEntryVisitor visitor)
    {
        return verifyAgainstFilesystem(fileOrDir, rootDirectoryOnFS, visitor,
                VerifyParameters.DEFAULT);
    }

    @Override
    public List<ArchiveEntry> verifyAgainstFilesystem(String fileOrDir, File rootDirectoryOnFS,
            VerifyParameters params)
    {
        final List<ArchiveEntry> verifyErrors = new ArrayList<ArchiveEntry>();
        verifyAgainstFilesystem(fileOrDir, rootDirectoryOnFS, new IArchiveEntryVisitor()
            {
                @Override
                public void visit(ArchiveEntry entry)
                {
                    if (entry.isOK() == false)
                        verifyErrors.add(entry);
                }
            }, params);
        return verifyErrors;
    }

    @Override
    public IHDF5Archiver verifyAgainstFilesystem(String fileOrDir, File rootDirectoryOnFS,
            IArchiveEntryVisitor visitor, IArchiveEntryVisitor missingArchiveEntryVisitorOrNull,
            VerifyParameters params)
    {
        final Set<File> filesOnFSOrNull =
                (missingArchiveEntryVisitorOrNull != null)
                    ? getFiles(Paths.get(rootDirectoryOnFS.toString(), fileOrDir).toFile(), params.isRecursive())
                    : null;
        final ArchiveEntryVerifyProcessor verifyProcessor =
                new ArchiveEntryVerifyProcessor(visitor, rootDirectoryOnFS, filesOnFSOrNull,
                        buffer, params.isVerifyAttributes(), params.isNumeric());
        processor.process(fileOrDir, params.isRecursive(), true, false, verifyProcessor);
        if (filesOnFSOrNull != null && filesOnFSOrNull.isEmpty() == false)
        {
            for (File f : filesOnFSOrNull)
            {
                missingArchiveEntryVisitorOrNull.visit(new ArchiveEntry(HDF5ArchiveUpdater
                        .getRelativePath(rootDirectoryOnFS, f.getParentFile()), HDF5ArchiveUpdater
                        .getRelativePath(rootDirectoryOnFS, f), LinkRecord.getLinkRecordForLink(f),
                        idCache));
            }
        }
        return this;
    }

    @Override
    public IHDF5Archiver verifyAgainstFilesystem(String fileOrDir, File rootDirectoryOnFS,
            IArchiveEntryVisitor visitor, VerifyParameters params)
    {
        return verifyAgainstFilesystem(fileOrDir, rootDirectoryOnFS, visitor, null, params);
    }

    @Override
    public IHDF5Archiver verifyAgainstFilesystem(String fileOrDir, File rootDirectoryOnFS,
            String rootDirectoryInArchive, IArchiveEntryVisitor visitor,
            IArchiveEntryVisitor missingArchiveEntryVisitorOrNull, VerifyParameters params)
    {
        final Set<File> filesOnFSOrNull =
                (missingArchiveEntryVisitorOrNull != null)
                    ? getFiles(Paths.get(rootDirectoryOnFS.toString(), fileOrDir).toFile(), params.isRecursive())
                    : null;
        final ArchiveEntryVerifyProcessor verifyProcessor =
                new ArchiveEntryVerifyProcessor(visitor, rootDirectoryOnFS, filesOnFSOrNull,
                        rootDirectoryInArchive, buffer, params.isVerifyAttributes(),
                        params.isNumeric());
        processor.process(fileOrDir, params.isRecursive(), true, false, verifyProcessor);
        if (filesOnFSOrNull != null && filesOnFSOrNull.isEmpty() == false)
        {
            for (File f : filesOnFSOrNull)
            {
                missingArchiveEntryVisitorOrNull.visit(new ArchiveEntry(f.getParent(), f.getPath(),
                        LinkRecord.getLinkRecordForLink(f), idCache));
            }
        }
        return this;
    }

    @Override
    public IHDF5Archiver verifyAgainstFilesystem(String fileOrDir, File rootDirectoryOnFS,
            String rootDirectoryInArchive, IArchiveEntryVisitor visitor, VerifyParameters params)
    {
        return verifyAgainstFilesystem(fileOrDir, rootDirectoryOnFS, rootDirectoryInArchive,
                visitor, null, params);
    }

    @Override
    public IHDF5Archiver verifyAgainstFilesystem(String fileOrDir, File rootDirectoryOnFS,
            IArchiveEntryVisitor visitor, IArchiveEntryVisitor missingArchiveEntryVisitor)
    {
        return verifyAgainstFilesystem(fileOrDir, rootDirectoryOnFS, visitor,
                missingArchiveEntryVisitor, VerifyParameters.DEFAULT);
    }

    @Override
    public List<ArchiveEntry> verifyAgainstFilesystem(String fileOrDir, File rootDirectoryOnFS,
            String rootDirectoryInArchive, VerifyParameters params)
    {
        final List<ArchiveEntry> verifyErrors = new ArrayList<ArchiveEntry>();
        verifyAgainstFilesystem(fileOrDir, rootDirectoryOnFS, rootDirectoryInArchive,
                new IArchiveEntryVisitor()
                    {
                        @Override
                        public void visit(ArchiveEntry entry)
                        {
                            if (entry.isOK() == false)
                            {
                                verifyErrors.add(entry);
                            }
                        }
                    }, params);
        return verifyErrors;
    }

    @Override
    public List<ArchiveEntry> verifyAgainstFilesystem(String fileOrDir, File rootDirectoryOnFS,
            String rootDirectoryInArchive)
    {
        return verifyAgainstFilesystem(fileOrDir, rootDirectoryOnFS, rootDirectoryInArchive,
                VerifyParameters.DEFAULT);
    }

    private static Set<File> getFiles(File fsRoot, boolean recursive)
    {
        final Set<File> result = new HashSet<File>();
        if (fsRoot.isDirectory())
        {
            addFilesInDir(fsRoot, result, recursive);
        }
        return result;
    }

    private static void addFilesInDir(File dir, Set<File> files, boolean recursive)
    {
        for (File f : dir.listFiles())
        {
            files.add(f);
            if (recursive && f.isDirectory())
            {
                addFilesInDir(f, files, recursive);
            }
        }
    }

    @Override
    public IHDF5Archiver extractFile(String path, OutputStream out) throws IOExceptionUnchecked
    {
        if (hdf5Reader.object().isDataSet(path) == false)
        {
            errorStrategy.dealWithError(new UnarchivingException(path, "not found in archive"));
            return this;
        }
        try
        {
            for (HDF5DataBlock<byte[]> block : hdf5Reader.opaque().getArrayNaturalBlocks(path))
            {
                out.write(block.getData());
            }
        } catch (IOException ex)
        {
            errorStrategy.dealWithError(new UnarchivingException(new File("stdout"), ex));
        }
        return this;
    }

    @Override
    public byte[] extractFileAsByteArray(String path) throws IOExceptionUnchecked
    {
        final ByteArrayOutputStream out = new ByteArrayOutputStream();
        extractFile(path, out);
        return out.toByteArray();
    }

    @Override
    public IInputStream extractFileAsIInputStream(String path)
    {
        if (hdf5Reader.object().isDataSet(path) == false)
        {
            errorStrategy.dealWithError(new UnarchivingException(path, "not found in archive"));
            return null;
        }
        return HDF5IOAdapterFactory.asIInputStream(hdf5Reader, path);
    }

    @Override
    public InputStream extractFileAsInputStream(String path)
    {
        return new AdapterIInputStreamToInputStream(extractFileAsIInputStream(path));
    }

    @Override
    public IHDF5Archiver extractToFilesystem(File rootDirectory) throws IllegalStateException
    {
        return extractToFilesystemBelowDirectory(rootDirectory, "", "/", ArchivingStrategy.DEFAULT,
                null);
    }

    @Override
    public IHDF5Archiver extractToFilesystem(File rootDirectory, String path)
            throws IllegalStateException
    {
        return extractToFilesystemBelowDirectory(rootDirectory, "", path,
                ArchivingStrategy.DEFAULT, null);
    }

    @Override
    public IHDF5Archiver extractToFilesystem(File rootDirectory, String path,
            IArchiveEntryVisitor visitorOrNull) throws IllegalStateException
    {
        return extractToFilesystemBelowDirectory(rootDirectory, "", path,
                ArchivingStrategy.DEFAULT, visitorOrNull);
    }

    @Override
    public IHDF5Archiver extractToFilesystem(File rootDirectory, String path,
            ArchivingStrategy strategy, IArchiveEntryVisitor visitorOrNull)
            throws IllegalStateException
    {
        return extractToFilesystemBelowDirectory(rootDirectory, "", path, strategy, visitorOrNull);
    }

    @Override
    public IHDF5Archiver extractToFilesystemBelowDirectory(File rootDirectory,
            String rootPathInArchive)
    {
        return extractToFilesystemBelowDirectory(rootDirectory, rootPathInArchive, "",
                ArchivingStrategy.DEFAULT, null);
    }

    @Override
    public IHDF5Archiver extractToFilesystemBelowDirectory(File rootDirectory,
            String rootPathInArchive, IArchiveEntryVisitor visitorOrNull)
    {
        return extractToFilesystemBelowDirectory(rootDirectory, rootPathInArchive, "",
                ArchivingStrategy.DEFAULT, visitorOrNull);

    }

    @Override
    public IHDF5Archiver extractToFilesystemBelowDirectory(File rootDirectory,
            String rootPathInArchive, ArchivingStrategy strategy, IArchiveEntryVisitor visitorOrNull)
    {
        return extractToFilesystemBelowDirectory(rootDirectory, rootPathInArchive, "", strategy,
                visitorOrNull);
    }

    @Override
    public IHDF5Archiver extractToFilesystemBelowDirectory(File rootDirectory,
            String rootPathInArchive, String path, ArchivingStrategy strategy,
            IArchiveEntryVisitor visitorOrNull) throws IllegalStateException
    {
        final IArchiveEntryProcessor extractor =
                new ArchiveEntryExtractProcessor(visitorOrNull, strategy, rootDirectory,
                        rootPathInArchive, buffer);
        processor.process(Utils.concatLink(rootPathInArchive, path), true, true, false, extractor);
        return this;
    }

    //
    // IHDF5Archiver
    //

    @Override
    public IHDF5Archiver archiveFromFilesystem(File path) throws IllegalStateException
    {
        return archiveFromFilesystem(path, ArchivingStrategy.DEFAULT, false,
                (IArchiveEntryVisitor) null);
    }

    @Override
    public IHDF5Archiver archiveFromFilesystem(File path, ArchivingStrategy strategy)
            throws IllegalStateException
    {
        return archiveFromFilesystem(path, strategy, false, (IArchiveEntryVisitor) null);
    }

    @Override
    public IHDF5Archiver archiveFromFilesystem(File path, IArchiveEntryVisitor entryVisitorOrNull)
            throws IllegalStateException
    {
        return archiveFromFilesystem(path, ArchivingStrategy.DEFAULT, false, entryVisitorOrNull);
    }

    @Override
    public IHDF5Archiver archiveFromFilesystem(File path, ArchivingStrategy strategy,
            boolean keepNameFromPath, IArchiveEntryVisitor entryVisitorOrNull)
            throws IllegalStateException
    {
        checkReadWrite();
        updaterOrNull
                .archive(path, strategy, CHUNK_SIZE_AUTO, keepNameFromPath, entryVisitorOrNull);
        return this;
    }

    @Override
    public IHDF5Archiver archiveFromFilesystem(File parentDirToStrip, File path)
            throws IllegalStateException
    {
        return archiveFromFilesystem(parentDirToStrip, path, ArchivingStrategy.DEFAULT);
    }

    @Override
    public IHDF5Archiver archiveFromFilesystem(File parentDirToStrip, File path,
            ArchivingStrategy strategy) throws IllegalStateException
    {
        return archiveFromFilesystem(parentDirToStrip, path, strategy, null);
    }

    @Override
    public IHDF5Archiver archiveFromFilesystem(File parentDirToStrip, File path,
            ArchivingStrategy strategy, IArchiveEntryVisitor entryVisitorOrNull)
            throws IllegalStateException
    {
        checkReadWrite();
        updaterOrNull
                .archive(parentDirToStrip, path, strategy, CHUNK_SIZE_AUTO, entryVisitorOrNull);
        return this;
    }

    @Override
    public IHDF5Archiver archiveFromFilesystem(String rootInArchive, File path)
    {
        return archiveFromFilesystem(rootInArchive, path, ArchivingStrategy.DEFAULT, null);
    }

    @Override
    public IHDF5Archiver archiveFromFilesystem(String rootInArchive, File path,
            ArchivingStrategy strategy)
    {
        return archiveFromFilesystem(rootInArchive, path, strategy, null);
    }

    @Override
    public IHDF5Archiver archiveFromFilesystem(String rootInArchive, File path,
            ArchivingStrategy strategy, IArchiveEntryVisitor entryVisitorOrNull)
    {
        checkReadWrite();
        updaterOrNull.archive(rootInArchive, path, strategy, CHUNK_SIZE_AUTO, entryVisitorOrNull);
        return this;
    }

    @Override
    public IHDF5Archiver archiveFromFilesystemBelowDirectory(String rootInArchive, File directory)
    {
        return archiveFromFilesystemBelowDirectory(rootInArchive, directory,
                ArchivingStrategy.DEFAULT, null);
    }

    @Override
    public IHDF5Archiver archiveFromFilesystemBelowDirectory(String rootInArchive, File directory,
            ArchivingStrategy strategy)
    {
        return archiveFromFilesystemBelowDirectory(rootInArchive, directory, strategy, null);
    }

    @Override
    public IHDF5Archiver archiveFromFilesystemBelowDirectory(String rootInArchive, File directory,
            IArchiveEntryVisitor visitor)
    {
        return archiveFromFilesystemBelowDirectory(rootInArchive, directory,
                ArchivingStrategy.DEFAULT, visitor);
    }

    @Override
    public IHDF5Archiver archiveFromFilesystemBelowDirectory(String rootInArchive, File directory,
            ArchivingStrategy strategy, IArchiveEntryVisitor entryVisitorOrNull)
    {
        checkReadWrite();
        updaterOrNull.archiveBelow(rootInArchive, directory, strategy, CHUNK_SIZE_AUTO,
                entryVisitorOrNull);
        return this;
    }

    @Override
    public IHDF5Archiver archiveFile(String path, byte[] data) throws IllegalStateException
    {
        return archiveFile(NewArchiveEntry.file(path), new ByteArrayInputStream(data));
    }

    @Override
    public IHDF5Archiver archiveFile(String path, InputStream input)
    {
        return archiveFile(NewArchiveEntry.file(path), input);
    }

    @Override
    public IHDF5Archiver archiveFile(NewFileArchiveEntry entry, byte[] data)
    {
        return archiveFile(entry, new ByteArrayInputStream(data));
    }

    @Override
    public OutputStream archiveFileAsOutputStream(NewFileArchiveEntry entry)
    {
        return new AdapterIOutputStreamToOutputStream(archiveFileAsIOutputStream(entry));
    }

    @Override
    public IOutputStream archiveFileAsIOutputStream(NewFileArchiveEntry entry)
    {
        checkReadWrite();
        final LinkRecord link = new LinkRecord(entry);
        final IOutputStream stream =
                updaterOrNull.archiveFile(entry.getParentPath(), link, entry.isCompress(),
                        entry.getChunkSize());
        return stream;
    }

    @Override
    public IHDF5Archiver archiveFile(NewFileArchiveEntry entry, InputStream input)
    {
        checkReadWrite();
        final LinkRecord link = new LinkRecord(entry);
        updaterOrNull.archive(entry.getParentPath(), link, input, entry.isCompress(),
                entry.getChunkSize());
        entry.setCrc32(link.getCrc32());
        return this;
    }

    @Override
    public IHDF5Archiver archiveSymlink(String path, String linkTarget)
    {
        return archiveSymlink(NewArchiveEntry.symlink(path, linkTarget));
    }

    @Override
    public IHDF5Archiver archiveSymlink(NewSymLinkArchiveEntry entry)
    {
        checkReadWrite();
        final LinkRecord link = new LinkRecord(entry);
        updaterOrNull.archive(entry.getParentPath(), link, null, false, CHUNK_SIZE_AUTO);
        return this;
    }

    @Override
    public IHDF5Archiver archiveDirectory(String path)
    {
        return archiveDirectory(NewArchiveEntry.directory(path));
    }

    @Override
    public IHDF5Archiver archiveDirectory(NewDirectoryArchiveEntry entry)
            throws IllegalStateException, IllegalArgumentException
    {
        checkReadWrite();
        final LinkRecord link = new LinkRecord(entry);
        updaterOrNull.archive(entry.getParentPath(), link, null, false, CHUNK_SIZE_AUTO);
        return this;
    }

    @Override
    public IHDF5Archiver delete(String hdf5ObjectPath)
    {
        return delete(Collections.singletonList(hdf5ObjectPath), null);
    }

    @Override
    public IHDF5Archiver delete(List<String> hdf5ObjectPaths)
    {
        return delete(hdf5ObjectPaths, null);
    }

    @Override
    public IHDF5Archiver delete(List<String> hdf5ObjectPaths,
            IArchiveEntryVisitor entryVisitorOrNull)
    {
        checkReadWrite();
        deleterOrNull.delete(hdf5ObjectPaths, entryVisitorOrNull);
        return this;
    }

    private void checkReadWrite()
    {
        if (updaterOrNull == null)
        {
            throw new IllegalStateException("Cannot update archive in read-only mode.");
        }
    }

}