aboutsummaryrefslogtreecommitdiffstats
path: root/util/folder/iterator.h
blob: 35c6538e3105df0af4859973742eb0387bd4d310 (plain) (blame)
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
#pragma once
 
#include "fts.h" 
 
#include <util/system/error.h> 
#include <util/generic/ptr.h> 
#include <util/generic/iterator.h> 
#include <util/generic/yexception.h> 
 
/// Note this magic API traverses directory hierarchy

class TDirIterator: public TInputRangeAdaptor<TDirIterator> {
    struct TFtsDestroy { 
        static inline void Destroy(FTS* f) noexcept {
            yfts_close(f); 
        } 
    }; 
 
public: 
    class TError: public TSystemError { 
    public: 
        inline TError(int err) 
            : TSystemError(err) 
        { 
        } 
    }; 
 
    using TCompare = int (*)(const FTSENT**, const FTSENT**);
 
    struct TOptions { 
        inline TOptions() { 
            Init(FTS_PHYSICAL); 
        } 
 
        inline TOptions(int opts) { 
            Init(opts); 
        } 
 
        inline TOptions& SetMaxLevel(size_t level) noexcept {
            MaxLevel = level; 
 
            return *this; 
        } 
 
        inline TOptions& SetSortFunctor(TCompare cmp) noexcept {
            Cmp = cmp; 
 
            return *this; 
        } 
 
        TOptions& SetSortByName() noexcept;
 
        int FtsOptions; 
        size_t MaxLevel; 
        TCompare Cmp; 
 
    private: 
        inline void Init(int opts) noexcept {
            FtsOptions = opts | FTS_NOCHDIR; 
            MaxLevel = Max<size_t>(); 
            Cmp = nullptr;
        } 
    }; 
 
    inline TDirIterator(const TString& path, const TOptions& options = TOptions())
        : Options_(options) 
        , Path_(path) 
    { 
        Trees_[0] = Path_.begin(); 
        Trees_[1] = nullptr;
 
        ClearLastSystemError(); 
        FileTree_.Reset(yfts_open(Trees_, Options_.FtsOptions, Options_.Cmp)); 
 
        const int err = LastSystemError(); 
 
        if (err) { 
            ythrow TError(err) << "can not open '" << Path_ << "'"; 
        } 
    } 
 
    inline FTSENT* Next() { 
        FTSENT* ret = yfts_read(FileTree_.Get()); 
 
        if (ret) { 
            if ((size_t)(ret->fts_level + 1) > Options_.MaxLevel) { 
                yfts_set(FileTree_.Get(), ret, FTS_SKIP); 
            } 
        } else { 
            const int err = LastSystemError(); 
 
            if (err) { 
                ythrow TError(err) << "error while iterating " << Path_; 
            } 
        } 
 
        return ret; 
    } 
 
    inline void Skip(FTSENT* ent) {
        yfts_set(FileTree_.Get(), ent, FTS_SKIP);
    }

private: 
    TOptions Options_; 
    TString Path_;
    char* Trees_[2]; 
    THolder<FTS, TFtsDestroy> FileTree_; 
};