diff options
author | rsesek <rsesek@chromium.org> | 2015-08-20 01:05:09 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-08-20 08:05:40 +0000 |
commit | 655fdb286bc0aa1c4be463fcb7675a2377a9d4f2 (patch) | |
tree | 72444cb8f2058520dc7cff313bd1b601dcd6893d /chrome/utility | |
parent | ca486e5e194f692b730d886d4e6df5be1a47ded3 (diff) | |
download | chromium_src-655fdb286bc0aa1c4be463fcb7675a2377a9d4f2.zip chromium_src-655fdb286bc0aa1c4be463fcb7675a2377a9d4f2.tar.gz chromium_src-655fdb286bc0aa1c4be463fcb7675a2377a9d4f2.tar.bz2 |
Add DMGIterator, which composes the UDIFParser and HFSIterator.
No new tests at this level, since the components are both tested.
BUG=496898
Review URL: https://codereview.chromium.org/1292753008
Cr-Commit-Position: refs/heads/master@{#344447}
Diffstat (limited to 'chrome/utility')
-rw-r--r-- | chrome/utility/safe_browsing/mac/dmg_iterator.cc | 83 | ||||
-rw-r--r-- | chrome/utility/safe_browsing/mac/dmg_iterator.h | 59 |
2 files changed, 142 insertions, 0 deletions
diff --git a/chrome/utility/safe_browsing/mac/dmg_iterator.cc b/chrome/utility/safe_browsing/mac/dmg_iterator.cc new file mode 100644 index 0000000..270bef7 --- /dev/null +++ b/chrome/utility/safe_browsing/mac/dmg_iterator.cc @@ -0,0 +1,83 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/utility/safe_browsing/mac/dmg_iterator.h" + +#include "chrome/utility/safe_browsing/mac/hfs.h" +#include "chrome/utility/safe_browsing/mac/read_stream.h" + +namespace safe_browsing { +namespace dmg { + +DMGIterator::DMGIterator(ReadStream* stream) + : udif_(stream), + partitions_(), + current_partition_(0), + hfs_() { +} + +DMGIterator::~DMGIterator() {} + +bool DMGIterator::Open() { + if (!udif_.Parse()) + return false; + + // Collect all the HFS partitions up-front. The data are accessed lazily, so + // this is relatively inexpensive. + for (size_t i = 0; i < udif_.GetNumberOfPartitions(); ++i) { + if (udif_.GetPartitionType(i) == "Apple_HFS" || + udif_.GetPartitionType(i) == "Apple_HFSX") { + partitions_.push_back(udif_.GetPartitionReadStream(i).Pass()); + } + } + + return partitions_.size() > 0; +} + +bool DMGIterator::Next() { + // Iterate through all the HFS partitions in the DMG file. + for (; current_partition_ < partitions_.size(); ++current_partition_) { + if (!hfs_) { + hfs_.reset(new HFSIterator(partitions_[current_partition_])); + if (!hfs_->Open()) + continue; + } + + // Iterate through the HFS filesystem until a concrete file is found. + while (true) { + if (!hfs_->Next()) + break; + + // Skip directories and symlinks. + if (hfs_->IsDirectory() || hfs_->IsSymbolicLink()) + continue; + + // Hard links are not supported by the HFSIterator. + if (hfs_->IsHardLink()) + continue; + + // Decmpfs compression is not supported by the HFSIterator. + if (hfs_->IsDecmpfsCompressed()) + continue; + + // This must be a normal file! + return true; + } + + hfs_.reset(); + } + + return false; +} + +base::string16 DMGIterator::GetPath() { + return hfs_->GetPath(); +} + +scoped_ptr<ReadStream> DMGIterator::GetReadStream() { + return hfs_->GetReadStream(); +} + +} // namespace dmg +} // namespace safe_browsing diff --git a/chrome/utility/safe_browsing/mac/dmg_iterator.h b/chrome/utility/safe_browsing/mac/dmg_iterator.h new file mode 100644 index 0000000..763285c --- /dev/null +++ b/chrome/utility/safe_browsing/mac/dmg_iterator.h @@ -0,0 +1,59 @@ +// Copyright 2015 The Chromium 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 CHROME_UTILITY_SAFE_BROWSING_MAC_DMG_ITERATOR_H_ +#define CHROME_UTILITY_SAFE_BROWSING_MAC_DMG_ITERATOR_H_ + +#include "base/macros.h" +#include "base/memory/scoped_ptr.h" +#include "base/memory/scoped_vector.h" +#include "base/strings/string16.h" +#include "chrome/utility/safe_browsing/mac/udif.h" + +namespace safe_browsing { +namespace dmg { + +class HFSIterator; +class ReadStream; + +// DMGIterator provides iterator access over all of the concrete files located +// on HFS+ and HFSX volumes in a UDIF/DMG file. This class maintains the +// limitations of its composed components. +class DMGIterator { + public: + // Creates a DMGIterator from a ReadStream. In most cases, this will be a + // FileReadStream opened from a DMG file. This does not take ownership + // of the stream. + explicit DMGIterator(ReadStream* stream); + ~DMGIterator(); + + // Opens the DMG file for iteration. This must be called before any other + // method. If this returns false, it is illegal to call any other methods + // on this class. If this returns true, the iterator is advanced to an + // invalid element before the first item. + bool Open(); + + // Advances the iterator to the next file item. Returns true on success + // and false on end-of-iterator. + bool Next(); + + // Returns the full path in a DMG filesystem to the current file item. + base::string16 GetPath(); + + // Returns a ReadStream for the current file item. + scoped_ptr<ReadStream> GetReadStream(); + + private: + UDIFParser udif_; // The UDIF parser that accesses the partitions. + ScopedVector<ReadStream> partitions_; // Streams for all the HFS partitions. + size_t current_partition_; // The index in |partitions_| of the current one. + scoped_ptr<HFSIterator> hfs_; // The HFSIterator for |current_partition_|. + + DISALLOW_COPY_AND_ASSIGN(DMGIterator); +}; + +} // namespace dmg +} // namespace safe_browsing + +#endif // CHROME_UTILITY_SAFE_BROWSING_MAC_DMG_ITERATOR_H_ |