summaryrefslogtreecommitdiffstats
path: root/chrome/utility/safe_browsing/mac/dmg_iterator.cc
blob: 834e9310324117389b4a0c72dd72ef3e39d816bc (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
// 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));
    }
  }

  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