xref: /aosp_15_r20/external/cronet/net/disk_cache/simple/simple_file_enumerator.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2022 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef NET_DISK_CACHE_SIMPLE_SIMPLE_FILE_ENUMERATOR_H_
6 #define NET_DISK_CACHE_SIMPLE_SIMPLE_FILE_ENUMERATOR_H_
7 
8 #include <memory>
9 #include <optional>
10 
11 #include "base/files/file_path.h"
12 #include "base/time/time.h"
13 #include "build/build_config.h"
14 #include "net/base/net_export.h"
15 #include "net/disk_cache/disk_cache.h"
16 
17 #if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
18 #include <dirent.h>
19 #include <sys/types.h>
20 #else
21 #include "base/files/file_enumerator.h"
22 #endif
23 
24 namespace disk_cache {
25 
26 // This is similar to base::SimpleFileEnumerator, but the implementation is
27 // optimized for the big directory use-case on POSIX. See
28 // https://crbug.com/270762 and https://codereview.chromium.org/22927018.
29 class NET_EXPORT SimpleFileEnumerator final {
30  public:
31   using Entry = BackendFileOperations::FileEnumerationEntry;
32 
33   explicit SimpleFileEnumerator(const base::FilePath& root_path);
34   ~SimpleFileEnumerator();
35 
36   // Returns true if we've found an error during enumeration.
37   bool HasError() const;
38 
39   // Returns the next item, or nullopt if there are no more results (including
40   // the error case).
41   std::optional<Entry> Next();
42 
43  private:
44 #if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
45   struct DirCloser {
operatorDirCloser46     void operator()(DIR* dir) { closedir(dir); }
47   };
48   const base::FilePath path_;
49   std::unique_ptr<DIR, DirCloser> dir_;
50   bool has_error_ = false;
51 #else
52   base::FileEnumerator enumerator_;
53 #endif
54 };
55 
56 }  // namespace disk_cache
57 
58 #endif  // NET_DISK_CACHE_SIMPLE_SIMPLE_FILE_ENUMERATOR_H_
59