summaryrefslogtreecommitdiffstats
path: root/src/inode2filename/search_directories.cc
blob: 2ec09d971f159921e6ce434414174a9f86353117 (plain)
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
// Copyright (C) 2018 The Android Open Source Project
//
// 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.

#include "common/debug.h"
#include "inode2filename/search_directories.h"
#include "inode2filename/system_call.h"

#include <android-base/file.h>
#include <android-base/logging.h>
#include <android-base/scopeguard.h>
#include <android-base/stringprintf.h>
#include <android-base/unique_fd.h>

#include "rxcpp/rx.hpp"

#include <iostream>
#include <stdio.h>
#include <fstream>
#include <vector>
#include <optional>

#include <signal.h>
#include <stdlib.h>
#include <unistd.h>

#include <sys/types.h>

#ifdef __ANDROID__
#include <sys/sysmacros.h>
#endif

#include <sys/stat.h>
#include <fcntl.h>
#include <poll.h>
#include <dirent.h>

#include <unordered_map>

namespace rx = rxcpp;
using android::base::unique_fd;  // NOLINT
using android::base::StringPrintf;  // NOLINT

namespace iorap::inode2filename {

// A multimap of 'ino_t -> List[Inode]' (where the value Inodes have the same ino_t as the key).
//
// A flat list of Inodes is turned into the above map, then keys can be removed one at a time
// until the InodeSet eventually becomes empty.
struct InodeSet {
  struct ValueRange {
    auto/*Iterable<Inode>*/ begin() {
      return begin_;
    }

    auto/*Iterable<Inode>*/ end() {
      return end_;
    }

    bool empty() const {
      return begin_ == end_;
    }

    explicit operator bool() const {
      return !empty();
    }

    std::unordered_multimap<ino_t, Inode>::iterator begin_, end_;

    friend std::ostream& operator<<(std::ostream& os, const ValueRange& s);
  };

  // Create an observable that emits the remaining inodes in the map.
  //
  // Mutation functions must not be called until this observable
  // has been finished emitting all values (e.g. with on_completed) since that
  // would cause the underlying iterators to go into an undefined state.
  auto/*observable<Inode>*/ IterateValues() const {
    return rxcpp::observable<>::iterate(set_).map(  // XX: should we use identity_immediate here?
        [](const std::pair<const ino_t, Inode>& pair) {
          return pair.second;
        }
    );
    // TODO: this would be more efficient as a range-v3 view.
  }

  constexpr bool Empty() const {
    return set_.empty();
  }

  static InodeSet OfList(const std::vector<Inode>& list) {
    InodeSet new_inode_set;
    std::unordered_multimap<ino_t, Inode>* map = &new_inode_set.set_;

    for (const Inode& inode : list) {
      map->insert({inode.inode, inode});
    }

    return new_inode_set;
  }

  // Return an optional list of 'Inode' structs whose 'inode' field matches the 'inode' parameter.
  // Returns an empty range if there was nothing found.
  ValueRange FindInodeList(ino_t inode) {
    auto range = set_.equal_range(inode);
    return ValueRange{range.first, range.second};
  }

  // Match all fields of an Inode against a 'struct stat' stat_buf.
  //
  // The returned Inode (if any) is removed from the InodeSet; it will not be returned by
  // FindInodeList in future calls.
  std::optional<Inode> FindAndRemoveInodeInList(ValueRange inode_list,
                                                const struct stat& stat_buf) {
    LOG(VERBOSE) << "FindAndRemoveInodeInList " << inode_list << ", "
                 << "stat_buf{st_dev=" << stat_buf.st_dev << ",st_ino=" << stat_buf.st_ino << "}";

    auto /*iterator*/ found = std::find_if(inode_list.begin(),
                                           inode_list.end(),
                                           [&](const std::pair<ino_t, Inode>& pair) {
      const Inode& inode = pair.second;
      if (inode.inode != stat_buf.st_ino) {
        return false;
      }

      dev_t inode_dev =
          makedev(static_cast<int>(inode.device_major), static_cast<int>(inode.device_minor));

      // Inodes could be the same across different devices.
      // Also match the device id.
      if (inode_dev != stat_buf.st_dev) {
        LOG(VERBOSE) << "InodeSet:FindAndRemoveInodeInList matched ino: " << inode.inode
                     << " but not device"
                     << ", expected dev: " << stat_buf.st_dev
                     << ", actual dev: " << inode_dev;
        return false;
      }
      return true;
    });

    if (found != inode_list.end()) {
      const Inode& inode = found->second;
      LOG(VERBOSE) << "InodeSet:FindAndRemoveInodeInList *success* inode+device " << inode;
      DCHECK(found->second.inode == stat_buf.st_ino);
      // Erase the inode from the list. This is important.
      set_.erase(found);
      return inode;
    }

    return std::nullopt;
  }

  // TODO: equality and string operators for testing/logging.
 private:
  // Explanation: readdir returns a 'file' -> 'ino_t inode' mapping.
  //
  // However inodes can be reused on different partitions (but they have a different device number).
  // To handle this edge case, and to avoid calling stat whenever the inode definitely doesn't match
  // store the inodes into a single-key,multi-value container.
  //
  // This enables fast scanning of readdir results by matching just the 'inode' portion,
  // then calling stat only when the inode portion definitely matches to confirm the device.

  // There are no single-key multi-value containers in standard C++, so pretend
  // we have one by writing this simple facade around an unordered set.
  //
  // We expect that the vector size is usually size=1 (or 2 or 3) since the # of devices
  // is fixed by however many partitions there are on the system, AND the same inode #
  // would have to be reused across a different file.
  std::unordered_multimap<ino_t, Inode> set_;  // TODO: Rename to map_.

  friend std::ostream& operator<<(std::ostream& os, const InodeSet& s);
};

std::ostream& operator<<(std::ostream& os, const InodeSet& s) {
  os << "InodeSet{";
  for (const auto& kv : s.set_) {
    // e.g. "123=>(1:2:123)" ... its expected for the 'ino_t' portion to be repeated.
    os << "" << kv.first << "=>(" << kv.second << "),";
  }
  os << "}";
  return os;
}

std::ostream& operator<<(std::ostream& os, const InodeSet::ValueRange& v) {
  // Don't want to make a const and non const version of ValueRange.
  InodeSet::ValueRange& s = const_cast<InodeSet::ValueRange&>(v);

  os << "InodeSet::ValueRange{";
  for (const auto& kv : s) {
    // e.g. "123=>(1:2:123)" ... its expected for the 'ino_t' portion to be repeated.
    os << "" << kv.first << "=>(" << kv.second << "),";
  }
  os << "}";
  return os;
}

void search_for_inodes_in(std::vector<Inode>& inode_list, const std::string& dirpath);

enum DirectoryEntryErrorCode {
  kInvalid,    // not a real error code. to detect bad initialization.
  kOpenDir,    // opendir failed.
  kReadDir,    // readdir failed.
  kDtUnknown,  // d_type was DT_UNKNOWN error.
};

struct DirectoryEntryError {
  DirectoryEntryErrorCode code;
  int err_no;
  std::string filename;
};

std::ostream& operator<<(std::ostream& os, const DirectoryEntryError& e) {
  os << "DirectoryEntryError{"
     << static_cast<int>(e.code) << "," << e.err_no << "," << e.filename << "}";
  return os;
  // TODO: pretty-print code and err-no
}

static common::DebugCounter gDebugDirectoryEntryCounter{};
static constexpr bool kDebugDirectoryEntry = false;

#define DIRECTORY_ENTRY_MOVE_DCHECK() \
    DCHECK_EQ(other.moved_from_, false) << __PRETTY_FUNCTION__ << "CNT:" << other.debug_counter_;
#define DIRECTORY_ENTRY_TRACE_CTOR() \
    if (kDebugDirectoryEntry) LOG(VERBOSE) << __PRETTY_FUNCTION__ << "@CNT:" << debug_counter_

struct DirectoryEntry {
  using ResultT = iorap::expected<DirectoryEntry, DirectoryEntryError>;
  using ObservableT = rx::observable<ResultT>;

  static constexpr ino_t kInvalidIno = std::numeric_limits<ino_t>::max();
  static constexpr auto kInvalidFileName = "";

  // Path to file, the prefix is one of the root directories.
  std::string filename{kInvalidFileName};
  // Inode number of the file. Not unique across different devices.
  ino_t d_ino{kInvalidIno};
  // File type (DT_LNK, DT_REG, DT_DIR, or DT_UNKNOWN)
  unsigned char d_type{DT_UNKNOWN};  // Note: not seen outside of sentinel roots.
  // TODO: Consider invariant checks for valid combinations of above fields?

  // Debug-only flags.
  bool moved_from_{false};
  size_t debug_counter_{0};

 private:
  // TODO: remove default constructor?
  //
  // SEEMS TO BE USED by std::vector etc. FIX DAT.
  DirectoryEntry() noexcept {
    debug_counter_ = gDebugDirectoryEntryCounter++;
    DIRECTORY_ENTRY_TRACE_CTOR();
  }
 public:
  DirectoryEntry(std::string filename, ino_t d_ino, unsigned char d_type) noexcept
    : filename{std::move(filename)},
      d_ino{d_ino},
      d_type{d_type} {
    debug_counter_ = gDebugDirectoryEntryCounter++;
    DIRECTORY_ENTRY_TRACE_CTOR();
  }

  DirectoryEntry(const DirectoryEntry& other) noexcept {
    // Do not use member-initialization syntax so that this DCHECK can execute first.
    DIRECTORY_ENTRY_MOVE_DCHECK();

    filename = other.filename;
    d_ino = other.d_ino;
    d_type = other.d_type;
    children_paths_ = other.children_paths_;
    children_initialized_ = other.children_initialized_;
    debug_counter_ = other.debug_counter_;
    DIRECTORY_ENTRY_TRACE_CTOR();
  }

  DirectoryEntry& operator=(const DirectoryEntry& other) noexcept {
    if (this == &other) {
      return *this;
    }

    DIRECTORY_ENTRY_MOVE_DCHECK();

    filename = other.filename;
    d_ino = other.d_ino;
    d_type = other.d_type;
    children_paths_ = other.children_paths_;
    children_initialized_ = other.children_initialized_;
    debug_counter_ = other.debug_counter_;
    DIRECTORY_ENTRY_TRACE_CTOR();

    return *this;
  }

  DirectoryEntry& operator=(DirectoryEntry&& other) noexcept {
    if (this == &other) {
      return *this;
    }

    DIRECTORY_ENTRY_MOVE_DCHECK();

    filename = std::move(other.filename);
    d_ino = other.d_ino;
    d_type = other.d_type;
    children_paths_ = std::move(other.children_paths_);
    children_initialized_ = other.children_initialized_;
    debug_counter_ = other.debug_counter_;
    DIRECTORY_ENTRY_TRACE_CTOR();

    return *this;
  }

  DirectoryEntry(DirectoryEntry&& other) noexcept {
    DIRECTORY_ENTRY_MOVE_DCHECK();
    other.moved_from_ = true;

    filename = std::move(other.filename);
    d_ino = other.d_ino;
    d_type = other.d_type;
    children_paths_ = std::move(other.children_paths_);
    children_initialized_ = other.children_initialized_;
    debug_counter_ = other.debug_counter_;
    DIRECTORY_ENTRY_TRACE_CTOR();
  }

  // Create a sentinel (root of roots) whose children entries are those specified by
  // children_paths.
  static DirectoryEntry CreateSentinel(std::vector<std::string> children_paths) {
    DirectoryEntry e;
    e.d_type = DT_DIR;
    ++gDebugDirectoryEntryCounter;

    for (std::string& child_path : children_paths) {
      // TODO: Should we call Stat on the child path here to reconstitute the ino_t for a root dir?
      // Otherwise it can look a little strange (i.e. the root dir itself will never match
      // the searched inode).
      //
      // Probably not too big of a problem in practice.
      DirectoryEntry child_entry{std::move(child_path), kInvalidIno, DT_DIR};
      ResultT child_entry_as_result{std::move(child_entry)};
      e.children_paths_.push_back(std::move(child_entry_as_result));
    }

    e.children_initialized_ = true;

    return e;
  }

  // Return an observable which emits the direct children only.
  // The children entries are now read from disk (with readdir) if they weren't read previously.
  std::vector<ResultT> GetChildrenEntries(borrowed<SystemCall*> system_call) const& {
    BuildChildrenPaths(system_call);
    return children_paths_;
  }

  // Return an observable which emits the direct children only.
  // The children entries are now read from disk (with readdir) if they weren't read previously.
  // Movable overload.
  std::vector<ResultT> GetChildrenEntries(borrowed<SystemCall*> system_call) && {
    BuildChildrenPaths(system_call);
    return std::move(children_paths_);
  }

  // Returns a (lazy) observable that emits every single node, in pre-order,
  // rooted at this tree.
  //
  // New entries are only read from disk (with e.g. readdir) when more values are pulled
  // from the observable. Only the direct children of any entry are read at any time.
  //
  // The emission can be stopped prematurely by unsubscribing from the observable.
  // This means the maximum amount of 'redundant' IO reads is bounded by the children count
  // of all entries emitted thus far minus entries actually emitted.
  ObservableT GetSubTreePreOrderEntries(borrowed<SystemCall*> system_call) const;

 private:
  // Out-of-line definition to avoid circular type dependency.
  void BuildChildrenPaths(borrowed<SystemCall*> system_call) const;

  // We need to lazily initialize children_paths_ only when we try to read them.
  //
  // Assuming the underlying file system doesn't change (which isn't strictly true),
  // the directory children are referentially transparent.
  //
  // In practice we do not need to distinguish between the file contents changing out
  // from under us in this code, so we don't need the more strict requirements.
  mutable std::vector<ResultT> children_paths_;
  mutable bool children_initialized_{false};

  friend std::ostream& operator<<(std::ostream& os, const DirectoryEntry& d);
};

std::ostream& operator<<(std::ostream& os, const DirectoryEntry& d) {
  os << "DirectoryEntry{" << d.filename << ",ino:" << d.d_ino << ",type:" << d.d_type << "}";
  return os;
}

using DirectoryEntryResult = DirectoryEntry::ResultT;

// Read all directory entries and return it as a vector. This must be an eager operation,
// as readdir is not re-entrant.
//
// This could be considered as a limitation from the 'observable' perspective since
// one can end up reading unnecessary extra directory entries that are then never consumed.
//
// The following entries are skipped:
//  - '.' self
//  - ".." parent
//
// All DT types except the following are removed:
//  * DT_LNK - symbolic link (empty children)
//  * DT_REG - regular file  (empty children)
//  * DT_DIR - directory     (has children)
static std::vector<DirectoryEntryResult>
    ReadDirectoryEntriesFromDirectoryPath(std::string dirpath, borrowed<SystemCall*> system_call) {
  DIR *dirp;
  struct dirent *dp;

  LOG(VERBOSE) << "ReadDirectoryEntriesFromDirectoryPath(" << dirpath << ")";

  if ((dirp = system_call->opendir(dirpath.c_str())) == nullptr) {
    PLOG(ERROR) << "Couldn't open directory: " << dirpath;
    return {DirectoryEntryError{kOpenDir, errno, dirpath}};
  }

  // Read all the results up front because readdir is not re-entrant.
  std::vector<DirectoryEntryResult> results;

  // Get full path + the directory entry path.
  auto child_path = [&] { return dirpath + "/" + dp->d_name; };

  do {
    errno = 0;
    if ((dp = system_call->readdir(dirp)) != nullptr) {
      if (dp->d_type == DT_DIR) {
        if (strcmp(".", dp->d_name) == 0 || strcmp("..", dp->d_name) == 0) {
          LOG(VERBOSE) << "Skip self/parent: " << dp->d_name;
          continue;
        }

        LOG(VERBOSE) << "Find entry " << child_path()
                     << ", ino: " << dp->d_ino << ", type: " << dp->d_type;
        results.push_back(DirectoryEntry{child_path(),
                                         static_cast<ino_t>(dp->d_ino),
                                         dp->d_type});
      } else if (dp->d_type == DT_UNKNOWN) {
        // This seems bad if it happens. We should probably do something about this.
        LOG(WARNING) << "Found unknown DT entry: " << child_path();

        results.push_back(DirectoryEntryError{kDtUnknown, /*errno*/0, child_path()});
      } else if (dp->d_type == DT_LNK || dp->d_type == DT_REG) {
        // Regular non-directory file entry.
        results.push_back(DirectoryEntry{child_path(),
                                         static_cast<ino_t>(dp->d_ino),
                                         dp->d_type});
      } else {
        // Block device, character device, socket, etc...
        LOG(VERBOSE) << "Skip DT entry of type: " << dp->d_type << " " << child_path();
      }
    } else if (errno != 0) {
      PLOG(ERROR) << "Error reading directory entry in " << dirpath;

      results.push_back(DirectoryEntryError{kReadDir, errno, dirpath});
    }
  } while (dp != nullptr);

  if (system_call->closedir(dirp) < 0) {
    PLOG(ERROR) << "Failed to close directory " << dirpath;
  }

  return results;
}

void DirectoryEntry::BuildChildrenPaths(borrowed<SystemCall*> system_call) const {
  if (children_initialized_) {
    return;
  }

  if (d_type == DT_DIR) {
    children_paths_ = ReadDirectoryEntriesFromDirectoryPath(filename, system_call);
    // TODO: consider using dependency injection here to substitute this function during testing?
  }
}

struct InodeSearchParameters {
  std::vector<Inode> inode_list;
  std::vector<std::string> root_dirs;
};

// [IN]
// observable: expected<Value, Error>, ...
// [OUT]
// observable: Value, ...
//
// Any encountered 'Error' items are dropped after logging.
template <typename T>
auto MapExpectedOrLog(T&& observable,
                      ::android::base::LogSeverity log_level) {
  return observable.filter([log_level](const auto& result) {
    if (result) {
      return true;
    } else {
      LOG(log_level) << result.error();
      return false;
    }
  }).map([](auto&& result) {
    return IORAP_FORWARD_LAMBDA(result).value();
  });
}

template <typename T>
auto MapExpectedOrLogError(T&& observable) {
  return MapExpectedOrLog(std::forward<T>(observable), ::android::base::ERROR);
}

template <typename T>
auto MapOptionalOrDrop(T&& observable) {
  return observable.filter([](const auto& result) {
    return result.has_value();
  }).map([](auto&& result) {
    return IORAP_FORWARD_LAMBDA(result).value();
  });
  // TODO: static_assert this isn't used with an unexpected.
}

template <typename T, typename F>
auto VisitValueOrLogError(T&& expected, F&& visit_func, const char* error_prefix = "") {
  if (!expected) {
    LOG(ERROR) << error_prefix << " " << expected.error();
  } else {
    visit_func(std::forward<T>(expected).value());
  }
  // TODO: Could be good to make this more monadic by returning an optional.
}

template <typename TSimple, typename T, typename F>
void TreeTraversalPreOrderObservableImpl(rx::subscriber<TSimple> dest, T&& node, F&& fn) {
  LOG(VERBOSE) << "TreeTraversalPreOrderObservableImpl (begin) " << __PRETTY_FUNCTION__;

  if (!dest.is_subscribed()) {
    LOG(VERBOSE) << "TreeTraversalPreOrderObservableImpl (unsubscribed)";
    return;
  } else {
    LOG(VERBOSE) << "TreeTraversalPreOrderObservableImpl (on_next node)";

    // Copy the node here. This is less bad than it seems since we haven't yet
    // calculated its children (except in the root), so its just doing a shallow memcpy (sizeof(T)).
    //
    // This assumes the children are calculated lazily, otherwise we'd need to have a separate
    // NodeBody class which only holds the non-children elements.

    TSimple copy = std::forward<T>(node);
    dest.on_next(std::move(copy));

    if (!node.has_value()) {
      return;
    }

    // Whenever we call 'on_next' also check if we end up unsubscribing.
    // This avoids the expensive call into the children.
    if (!dest.is_subscribed()) {
      LOG(VERBOSE) << "TreeTraversalPreOrderObservableImpl (post-self unsubscribe)";
      return;
    }

    // Eagerly get the childrem, moving them instead of copying them.
    auto&& children = fn(std::forward<T>(node));
    for (auto&& child : children) {
      TreeTraversalPreOrderObservableImpl(dest, IORAP_FORWARD_LAMBDA(child), fn);
      // TODO: double check this is doing the std::move properly for rvalues.

      if (!dest.is_subscribed()) {
        LOG(VERBOSE) << "TreeTraversalPreOrderObservableImpl (unsubscribed in children)";
        break;
      }
    };
  }
}

// Creates an observable over all the nodes in the tree rooted at node.
// fn is a function that returns the children of that node.
//
// The items are emitted left-to-right pre-order, and stop early if the
// observable is unsubscribed from.
//
// Implementation requirement:
//    typeof(node) -> expected<V, E> or optional<V> or similar.
//    fn(node) -> iterable<typeof(node)>
//
// preorder(self):
//   visit(self)
//   for child in fn(self):
//     preorder(child)
template <typename T, typename F>
auto/*observable<T>*/ TreeTraversalPreOrderObservable(T&& node, F&& fn) {
  LOG(VERBOSE) << "TreeTraversalPreOrderObservable: " << __PRETTY_FUNCTION__;

  using T_simple = std::decay_t<T>;
  return rx::observable<>::create<T_simple>(
    // Copy node to avoid lifetime issues.
    [node=node,fn=std::forward<F>(fn)](rx::subscriber<T_simple> dest) {
      LOG(VERBOSE) << "TreeTraversalPreOrderObservable (lambda)";
      TreeTraversalPreOrderObservableImpl<T_simple>(dest,
                                                    std::move(node),
                                                    std::move(fn));
      dest.on_completed();
    }
  );
}

DirectoryEntry::ObservableT
    DirectoryEntry::GetSubTreePreOrderEntries(borrowed<SystemCall*> system_call) const {
  return TreeTraversalPreOrderObservable(
      DirectoryEntryResult{*this},
      [system_call=system_call](auto/*DirectoryEntryResult*/&& result)
          -> std::vector<DirectoryEntryResult> {
        if (!result) {
          LOG(VERBOSE) << "GetSubTreePreOrderEntries (no value return)";
          // Cannot have children when it was an error.
          return {};
        }
        return
            IORAP_FORWARD_LAMBDA(result)
            .value()
            .GetChildrenEntries(system_call);
      });
}

struct StatError {
  int err_no;
  std::string path_name;
};

std::ostream& operator<<(std::ostream& os, const StatError& e) {
  os << "StatError{" << e.err_no << "," << e.path_name << "}";
  return os;
}

template <typename U = void>  // suppress unused warning.
static iorap::expected<struct stat, StatError> Stat(const std::string& path_name,
                                                    borrowed<SystemCall*> system_call) {
  struct stat statbuf{};

  // Call stat(2) in live code. Overridden in test code.
  if (system_call->stat(path_name.c_str(), /*out*/&statbuf) == 0) {
    return statbuf;
  } else {
    return iorap::unexpected(StatError{errno, path_name});
  }
}

using StatResult = iorap::expected<struct stat, StatError>;

// An inode's corresponding filename on the system.
struct SearchMatch {
  Inode inode;
  // Relative path joined with a root directory.
  //
  // Use absolute path root dirs to get back absolute path filenames.
  // If relative, this is relative to the current working directory.
  std::string filename;
};

std::ostream& operator<<(std::ostream& os, const SearchMatch& s) {
  os << "SearchMatch{" << s.inode << ", " << s.filename << "}";
  return os;
}

struct SearchState {
  // Emit 'match' Inodes corresponding to the ones here.
  InodeSet inode_set;

  // An inode matching one of the ones in inode_set was discovered in the most-recently
  // emitted SearchState.
  //
  // The InodeSet removes any matching 'Inode'.
  std::optional<SearchMatch> match;

  // TODO: make sure this doesn't copy [inodes], as that would be unnecessarily expensive.
};

std::ostream& operator<<(std::ostream& os, const SearchState& s) {
  os << "SearchState{match:";
  // Print the 'match' first. The InodeSet could be very large so it could be truncated in logs.
  if (s.match) {
    os << s.match.value();
  } else {
    os << "(none)";
  }
  os << ", inode_set:" << s.inode_set << "}";
  return os;
}

// TODO: write operator<< etc.

// Return a lazy observable that will search for all filenames whose inodes
// match the inodes in inode_search_list.
//
// Every unmatched inode will be emitted as an unexpected at the end of the stream.
auto/*[observable<InodeResult>, connectable]*/ SearchDirectoriesForMatchingInodes(
    std::vector<std::string> root_dirs,
    std::vector<Inode> inode_search_list,
    borrowed<SystemCall*> system_call) {

  // Create a (lazy) observable that will emit each DirectoryEntry that is a recursive subchild
  // of root_dirs. Emission will be stopped when its unsubscribed from.
  //
  // This is done by calling readdir(3) lazily.
  auto/*obs<DirectoryEntry>*/ find_all_subdir_entries = ([&]() {
    DirectoryEntry sentinel = DirectoryEntry::CreateSentinel(std::move(root_dirs));
    auto/*obs<DirectoryEntryResult*/ results = sentinel.GetSubTreePreOrderEntries(system_call);

    // Drop any errors by logging them to logcat. "Unwrap" the expected into the underlying data.
    auto/*obs<DirectoryEntry*>*/ expected_drop_errors = MapExpectedOrLogError(std::move(results));
    return expected_drop_errors;
  })();

  // DirectoryEntry is missing the dev_t portion, so we may need to call scan(2) again
  // to confirm the dev_t. We skip calling scan(2) when the ino_t does not match.
  // InodeSet lets us optimally avoid calling scan(2).
  SearchState initial;
  initial.inode_set = InodeSet::OfList(inode_search_list);

  auto/*[observable<SearchState>,Connectable]*/ search_state_results = find_all_subdir_entries.scan(
      std::move(initial),
      [system_call=system_call](SearchState search_state, const DirectoryEntry& dir_entry) {
        LOG(VERBOSE) << "SearchDirectoriesForMatchingInodes#Scan "
                     << dir_entry << ", state: " << search_state;

        search_state.match = std::nullopt;

        InodeSet* inodes = &search_state.inode_set;

        // Find all the possible inodes across different devices.
        InodeSet::ValueRange inode_list = inodes->FindInodeList(dir_entry.d_ino);

        // This directory doesn't correspond to any inodes we are searching for.
        if (!inode_list) {
          return search_state;
        }

        StatResult maybe_stat = Stat(dir_entry.filename, system_call);
        VisitValueOrLogError(maybe_stat, [&](const struct stat& stat_buf) {
          // Try to match the specific inode. Usually this will not result in a match (nullopt).
          std::optional<Inode> inode = inodes->FindAndRemoveInodeInList(inode_list, stat_buf);

          if (inode) {
            search_state.match = SearchMatch{inode.value(), dir_entry.filename};
          }
        });

        return search_state;  // implicit move.
      }
  // Avoid exhausting a potentially 'infinite' stream of files by terminating as soon
  // as we find every single inode we care about.
  ).take_while([](const SearchState& state) {
      // Also emit the last item that caused the search set to go empty.
      bool cond = !state.inode_set.Empty() || state.match;

      if (WOULD_LOG(VERBOSE)) {
        static int kCounter = 0;
        LOG(VERBOSE) << "SearchDirectoriesForMatchingInodes#take_while (" << kCounter++ <<
                     ",is_empty:"
                     << state.inode_set.Empty() << ", match:" << state.match.has_value();
      }
      // Minor O(1) implementation inefficiency:
      // (Too minor to fix but it can be strange if looking at the logs or readdir traces).
      //
      // Note, because we return 'true' after the search set went empty,
      // the overall stream graph still pulls from search_state_results exactly once more:
      //
      // This means that for cond to go to false, we would've read one extra item and then discarded
      // it. If that item was the first child of a directory, that means we essentially did
      // one redundant pass of doing a readdir.
      //
      // In other words if the search set goes to empty while the current item is a directory,
      // it will definitely readdir on it at least once as we try to get the first child in
      // OnTreeTraversal.
      //
      // This could be fixed with a 'take_until(Predicate)' operator which doesn't discard
      // the last item when the condition becomes false. However rxcpp seems to lack this operator,
      // whereas RxJava has it.

      if (!cond) {
        LOG(VERBOSE) << "SearchDirectoriesForMatchingInodes#take_while "
                     << "should now terminate for " << state;
      }

      return cond;
  }).publish();
  // The publish here is mandatory. The stream is consumed twice (once by matched and once by
  // unmatched streams). Without the publish, once all items from 'matched' were consumed it would
  // start another instance of 'search_state_results' (i.e. it appears as if the search
  // is restarted).
  //
  // By using 'publish', the search_state_results is effectively shared by both downstream nodes.
  // Note that this also requires the subscriber to additionally call #connect on the above stream,
  // otherwise no work will happen.

  // Lifetime notes:
  //
  // The the 'SearchState' is emitted into both below streams simultaneously.
  //    The 'unmatched_inode_values' only touches the inode_set.
  //    The 'matched_inode_values' only touches the match.
  // Either stream can 'std::move' from those fields because they don't move each other's fields.
  auto/*observable<InodeResult>*/ matched_inode_values = search_state_results
      .filter([](const SearchState& search_state) { return search_state.match.has_value(); })
      .map([](SearchState& search_state) { return std::move(search_state.match.value()); })
                     // observable<SearchMatch>
      .map([](SearchMatch search_match) {
          return InodeResult::makeSuccess(search_match.inode, std::move(search_match.filename));
      });            // observable<InodeResult>

  auto/*observable<?>*/ unmatched_inode_values = search_state_results
      // The 'last' SearchState is the one that contains all the remaining inodes.
      .take_last(1)  // observable<SearchState>
      .flat_map([](const SearchState& search_state) {
          LOG(VERBOSE) << "SearchDirectoriesForMatchingInodes#unmatched -- flat_map";
          // Aside: Could've used a move here if the inodes weren't so lightweight already.
          return search_state.inode_set.IterateValues(); })
                     // observable<Inode>
      .map([](const Inode& inode) {
          LOG(VERBOSE) << "SearchDirectoriesForMatchingInodes#unmatched -- map";
          return InodeResult::makeFailure(inode, InodeResult::kCouldNotFindFilename);
      });
                     // observable<InodeResult>

  // The matched and unmatched InodeResults are emitted together.
  //   Use merge, not concat, because we need both observables to be subscribed to simultaneously.

  auto/*observable<InodeResult*/ all_inode_results =
      matched_inode_values.merge(unmatched_inode_values);

  // Now that all mid-stream observables have been connected, turn the Connectable observable
  // into a regular observable.

  // The caller has to call 'connect' on the search_state_results after subscribing
  // and before any work can actually start.
  return std::make_pair(all_inode_results, search_state_results);
}


rxcpp::observable<InodeResult> SearchDirectories::FindFilenamesFromInodes(
    std::vector<std::string> root_directories,
    std::vector<Inode> inode_list,
    SearchMode mode) {
  DCHECK(mode == SearchMode::kInProcessDirect) << " other modes not implemented yet";

  auto/*observable[2]*/ [inode_results, connectable] = SearchDirectoriesForMatchingInodes(
      std::move(root_directories),
      std::move(inode_list),
      system_call_);

  return inode_results;
}

// I think we could avoid this with auto_connect, which rxcpp doesn't seem to have.
//
// I can't figure out any other way to avoid this, or at least to allow connecting
// on the primary observable (instead of a secondary side-observable).
//
// If using the obvious publish+ref_count then the unmerged stream gets no items emitted into it.
// If tried to ref_count later, everything turns into no-op.
// If trying to call connect too early, the subscribe is missed.
template <typename T>
struct RxAnyConnectableFromObservable : public SearchDirectories::RxAnyConnectable {
  virtual void connect() override {
    observable.connect();
  }

  virtual ~RxAnyConnectableFromObservable() {}

  RxAnyConnectableFromObservable(rxcpp::connectable_observable<T> observable)
    : observable(observable) {
  }

  rxcpp::connectable_observable<T> observable;
};

// Type deduction helper.
template <typename T>
std::unique_ptr<SearchDirectories::RxAnyConnectable>
    MakeRxAnyConnectableFromObservable(rxcpp::connectable_observable<T> observable) {
  SearchDirectories::RxAnyConnectable* ptr = new RxAnyConnectableFromObservable<T>{observable};
  return std::unique_ptr<SearchDirectories::RxAnyConnectable>{ptr};
}

std::pair<rxcpp::observable<InodeResult>, std::unique_ptr<SearchDirectories::RxAnyConnectable>>
    SearchDirectories::FindFilenamesFromInodesPair(
        std::vector<std::string> root_directories,
        std::vector<Inode> inode_list,
        SearchMode mode) {
  DCHECK(mode == SearchMode::kInProcessDirect) << " other modes not implemented yet";

  auto/*observable[2]*/ [inode_results, connectable] = SearchDirectoriesForMatchingInodes(
      std::move(root_directories),
      std::move(inode_list),
      system_call_);

  std::unique_ptr<SearchDirectories::RxAnyConnectable> connectable_ptr =
    MakeRxAnyConnectableFromObservable(connectable.as_dynamic());

  return {inode_results, std::move(connectable_ptr)};
}

}  // namespace iorap::inode2filename