blob: 74e207df1d31fca3c91ae9133e62e54f7519e27c [file] [log] [blame]
// Copyright 2018 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef SMBPROVIDER_ITERATOR_CACHING_ITERATOR_H_
#define SMBPROVIDER_ITERATOR_CACHING_ITERATOR_H_
#include <string>
#include <utility>
#include <base/compiler_specific.h>
#include <base/macros.h>
#include "smbprovider/iterator/directory_iterator.h"
#include "smbprovider/metadata_cache.h"
namespace smbprovider {
// Iterator class that wraps a directory iterator and stores each
// entry in a cache as it is iterated over.
class CachingIterator {
public:
CachingIterator(DirectoryIterator it, MetadataCache* cache)
: inner_it_(std::move(it)), cache_(cache) {}
CachingIterator(const std::string& full_path,
SambaInterface* samba_interface,
MetadataCache* cache)
: CachingIterator(DirectoryIterator(full_path, samba_interface), cache) {}
CachingIterator(CachingIterator&& other) = default;
~CachingIterator() = default;
// Initializes the iterator, setting the first value of current. Returns 0 on
// success, error on failure. Must be called before any other operation.
int32_t Init() WARN_UNUSED_RESULT { return inner_it_.Init(); }
// Advances current to the next entry. Returns 0 on success,
// error on failure.
int32_t Next() WARN_UNUSED_RESULT { return inner_it_.Next(); }
// Returns the current DirectoryEntry.
const DirectoryEntry& Get() {
const DirectoryEntry& entry = inner_it_.Get();
cache_->AddEntry(entry);
return entry;
}
// Returns true if there is nothing left to iterate over.
bool IsDone() WARN_UNUSED_RESULT { return inner_it_.IsDone(); }
private:
DirectoryIterator inner_it_;
MetadataCache* cache_ = nullptr; // Not owned.
DISALLOW_COPY_AND_ASSIGN(CachingIterator);
};
} // namespace smbprovider
#endif // SMBPROVIDER_ITERATOR_CACHING_ITERATOR_H_