diff --git a/chrome_browser_proxy_resolver.cc b/chrome_browser_proxy_resolver.cc index cd0e2a7..7696d4b 100644 --- a/chrome_browser_proxy_resolver.cc +++ b/chrome_browser_proxy_resolver.cc @@ -104,10 +104,9 @@ ChromeBrowserProxyResolver::~ChromeBrowserProxyResolver() { } // Kill outstanding timers - for (TimeoutsMap::iterator it = timers_.begin(), e = timers_.end(); it != e; - ++it) { - g_source_destroy(it->second); - it->second = nullptr; + for (auto& timer : timers_) { + g_source_destroy(timer.second); + timer.second = nullptr; } } diff --git a/payload_generator/delta_diff_generator.cc b/payload_generator/delta_diff_generator.cc index 337b1aa..78f66ab 100644 --- a/payload_generator/delta_diff_generator.cc +++ b/payload_generator/delta_diff_generator.cc @@ -346,19 +346,18 @@ bool ReadUnwrittenBlocks(const vector& blocks, // We use the temporary buffer 'buf' to hold the data, which may be // smaller than the extent, so in that case we have to loop to get // the extent's data (that's the inner while loop). - for (vector::const_iterator it = extents.begin(); - it != extents.end(); ++it) { + for (const Extent& extent : extents) { vector::size_type blocks_read = 0; float printed_progress = -1; - while (blocks_read < it->num_blocks()) { + while (blocks_read < extent.num_blocks()) { const int copy_block_cnt = min(buf.size() / kBlockSize, static_cast::size_type>( - it->num_blocks() - blocks_read)); + extent.num_blocks() - blocks_read)); ssize_t rc = pread(image_fd, &buf[0], copy_block_cnt * kBlockSize, - (it->start_block() + blocks_read) * kBlockSize); + (extent.start_block() + blocks_read) * kBlockSize); TEST_AND_RETURN_FALSE_ERRNO(rc >= 0); TEST_AND_RETURN_FALSE(static_cast(rc) == copy_block_cnt * kBlockSize); @@ -421,9 +420,8 @@ void InstallOperationsToManifest( const vector& kernel_ops, DeltaArchiveManifest* out_manifest, OperationNameMap* out_op_name_map) { - for (vector::const_iterator it = order.begin(); - it != order.end(); ++it) { - const Vertex& vertex = graph[*it]; + for (Vertex::Index vertex_index : order) { + const Vertex& vertex = graph[vertex_index]; const DeltaArchiveManifest_InstallOperation& add_op = vertex.op; if (DeltaDiffGenerator::IsNoopOperation(add_op)) { continue; @@ -457,8 +455,8 @@ void InstallOperationsToManifest( } void CheckGraph(const Graph& graph) { - for (Graph::const_iterator it = graph.begin(); it != graph.end(); ++it) { - CHECK(it->op.has_type()); + for (const Vertex& v : graph) { + CHECK(v.op.has_type()); } } @@ -563,9 +561,7 @@ void ReportPayloadUsage(const DeltaArchiveManifest& manifest, std::sort(objects.begin(), objects.end()); static const char kFormatString[] = "%6.2f%% %10jd %-10s %s\n"; - for (vector::const_iterator it = objects.begin(); - it != objects.end(); ++it) { - const DeltaObject& object = *it; + for (const DeltaObject& object : objects) { fprintf(stderr, kFormatString, object.size * 100.0 / total_size, static_cast(object.size), @@ -896,9 +892,8 @@ vector ExpandExtents(const T& extents) { // objects. vector CompressExtents(const vector& blocks) { vector new_extents; - for (vector::const_iterator it = blocks.begin(), e = blocks.end(); - it != e; ++it) { - graph_utils::AppendBlockToExtents(&new_extents, *it); + for (uint64_t block : blocks) { + graph_utils::AppendBlockToExtents(&new_extents, block); } return new_extents; } @@ -924,12 +919,12 @@ void DeltaDiffGenerator::SubstituteBlocks( conversion[remove_extents_expanded[i]] = replace_extents_expanded[i]; } utils::ApplyMap(&read_blocks, conversion); - for (Vertex::EdgeMap::iterator it = vertex->out_edges.begin(), - e = vertex->out_edges.end(); it != e; ++it) { + for (auto& edge_prop_pair : vertex->out_edges) { vector write_before_deps_expanded = - ExpandExtents(it->second.write_extents); + ExpandExtents(edge_prop_pair.second.write_extents); utils::ApplyMap(&write_before_deps_expanded, conversion); - it->second.write_extents = CompressExtents(write_before_deps_expanded); + edge_prop_pair.second.write_extents = + CompressExtents(write_before_deps_expanded); } } // Convert read_blocks back to extents @@ -947,23 +942,22 @@ bool DeltaDiffGenerator::CutEdges(Graph* graph, cuts.reserve(edges.size()); uint64_t scratch_blocks_used = 0; - for (set::const_iterator it = edges.begin(); - it != edges.end(); ++it) { + for (const Edge& edge : edges) { cuts.resize(cuts.size() + 1); vector old_extents = - (*graph)[it->first].out_edges[it->second].extents; + (*graph)[edge.first].out_edges[edge.second].extents; // Choose some scratch space - scratch_blocks_used += graph_utils::EdgeWeight(*graph, *it); + scratch_blocks_used += graph_utils::EdgeWeight(*graph, edge); cuts.back().tmp_extents = - scratch_allocator.Allocate(graph_utils::EdgeWeight(*graph, *it)); + scratch_allocator.Allocate(graph_utils::EdgeWeight(*graph, edge)); // create vertex to copy original->scratch cuts.back().new_vertex = graph->size(); graph->resize(graph->size() + 1); - cuts.back().old_src = it->first; - cuts.back().old_dst = it->second; + cuts.back().old_src = edge.first; + cuts.back().old_dst = edge.second; EdgeProperties& cut_edge_properties = - (*graph)[it->first].out_edges.find(it->second)->second; + (*graph)[edge.first].out_edges.find(edge.second)->second; // This should never happen, as we should only be cutting edges between // real file nodes, and write-before relationships are created from @@ -972,7 +966,7 @@ bool DeltaDiffGenerator::CutEdges(Graph* graph, << "Can't cut edge that has write-before relationship."; // make node depend on the copy operation - (*graph)[it->first].out_edges.insert(make_pair(graph->size() - 1, + (*graph)[edge.first].out_edges.insert(make_pair(graph->size() - 1, cut_edge_properties)); // Set src/dst extents and other proto variables for copy operation @@ -983,23 +977,23 @@ bool DeltaDiffGenerator::CutEdges(Graph* graph, DeltaDiffGenerator::StoreExtents(cuts.back().tmp_extents, graph->back().op.mutable_dst_extents()); graph->back().op.set_src_length( - graph_utils::EdgeWeight(*graph, *it) * kBlockSize); + graph_utils::EdgeWeight(*graph, edge) * kBlockSize); graph->back().op.set_dst_length(graph->back().op.src_length()); // make the dest node read from the scratch space DeltaDiffGenerator::SubstituteBlocks( - &((*graph)[it->second]), - (*graph)[it->first].out_edges[it->second].extents, + &((*graph)[edge.second]), + (*graph)[edge.first].out_edges[edge.second].extents, cuts.back().tmp_extents); // delete the old edge CHECK_EQ(static_cast(1), - (*graph)[it->first].out_edges.erase(it->second)); + (*graph)[edge.first].out_edges.erase(edge.second)); // Add an edge from dst to copy operation EdgeProperties write_before_edge_properties; write_before_edge_properties.write_extents = cuts.back().tmp_extents; - (*graph)[it->second].out_edges.insert( + (*graph)[edge.second].out_edges.insert( make_pair(graph->size() - 1, write_before_edge_properties)); } out_cuts->swap(cuts); @@ -1010,10 +1004,9 @@ bool DeltaDiffGenerator::CutEdges(Graph* graph, void DeltaDiffGenerator::StoreExtents( const vector& extents, google::protobuf::RepeatedPtrField* out) { - for (vector::const_iterator it = extents.begin(); - it != extents.end(); ++it) { + for (const Extent& extent : extents) { Extent* new_extent = out->Add(); - *new_extent = *it; + *new_extent = extent; } } @@ -1145,25 +1138,23 @@ bool ConvertCutsToFull( const vector& cuts) { CHECK(!cuts.empty()); set deleted_nodes; - for (vector::const_iterator it = cuts.begin(), - e = cuts.end(); it != e; ++it) { + for (const CutEdgeVertexes& cut : cuts) { TEST_AND_RETURN_FALSE(DeltaDiffGenerator::ConvertCutToFullOp( graph, - *it, + cut, new_root, data_fd, data_file_size)); - deleted_nodes.insert(it->new_vertex); + deleted_nodes.insert(cut.new_vertex); } deleted_nodes.insert(cuts[0].old_dst); vector new_op_indexes; new_op_indexes.reserve(op_indexes->size()); - for (vector::iterator it = op_indexes->begin(), - e = op_indexes->end(); it != e; ++it) { - if (utils::SetContainsKey(deleted_nodes, *it)) + for (Vertex::Index vertex_index : *op_indexes) { + if (utils::SetContainsKey(deleted_nodes, vertex_index)) continue; - new_op_indexes.push_back(*it); + new_op_indexes.push_back(vertex_index); } new_op_indexes.push_back(cuts[0].old_dst); op_indexes->swap(new_op_indexes); @@ -1189,16 +1180,14 @@ bool AssignBlockForAdjoiningCuts( const Vertex::Index old_dst = cuts[0].old_dst; // Calculate # of blocks needed uint64_t blocks_needed = 0; - map cuts_blocks_needed; - for (vector::const_iterator it = cuts.begin(), - e = cuts.end(); it != e; ++it) { + vector cuts_blocks_needed(cuts.size()); + for (vector::size_type i = 0; i < cuts.size(); ++i) { uint64_t cut_blocks_needed = 0; - for (vector::const_iterator jt = it->tmp_extents.begin(), - je = it->tmp_extents.end(); jt != je; ++jt) { - cut_blocks_needed += jt->num_blocks(); + for (const Extent& extent : cuts[i].tmp_extents) { + cut_blocks_needed += extent.num_blocks(); } blocks_needed += cut_blocks_needed; - cuts_blocks_needed[&*it] = cut_blocks_needed; + cuts_blocks_needed[i] = cut_blocks_needed; } // Find enough blocks @@ -1257,31 +1246,30 @@ bool AssignBlockForAdjoiningCuts( TEST_AND_RETURN_FALSE(scratch_ranges.blocks() == scratch_blocks_found); // Make all the suppliers depend on this node - for (SupplierVector::iterator it = block_suppliers.begin(), - e = block_suppliers.end(); it != e; ++it) { + for (const auto& index_range_pair : block_suppliers) { graph_utils::AddReadBeforeDepExtents( - &(*graph)[it->first], + &(*graph)[index_range_pair.first], old_dst, - it->second.GetExtentsForBlockCount(it->second.blocks())); + index_range_pair.second.GetExtentsForBlockCount( + index_range_pair.second.blocks())); } // Replace temp blocks in each cut - for (vector::const_iterator it = cuts.begin(), - e = cuts.end(); it != e; ++it) { + for (vector::size_type i = 0; i < cuts.size(); ++i) { + const CutEdgeVertexes& cut = cuts[i]; vector real_extents = - scratch_ranges.GetExtentsForBlockCount(cuts_blocks_needed[&*it]); + scratch_ranges.GetExtentsForBlockCount(cuts_blocks_needed[i]); scratch_ranges.SubtractExtents(real_extents); // Fix the old dest node w/ the real blocks DeltaDiffGenerator::SubstituteBlocks(&(*graph)[old_dst], - it->tmp_extents, + cut.tmp_extents, real_extents); // Fix the new node w/ the real blocks. Since the new node is just a // copy operation, we can replace all the dest extents w/ the real // blocks. - DeltaArchiveManifest_InstallOperation *op = - &(*graph)[it->new_vertex].op; + DeltaArchiveManifest_InstallOperation *op = &(*graph)[cut.new_vertex].op; op->clear_dst_extents(); DeltaDiffGenerator::StoreExtents(real_extents, op->mutable_dst_extents()); } @@ -1364,10 +1352,9 @@ bool DeltaDiffGenerator::NoTempBlocksRemain(const Graph& graph) { } // Check out-edges: - for (Vertex::EdgeMap::const_iterator jt = it->out_edges.begin(), - je = it->out_edges.end(); jt != je; ++jt) { - if (TempBlocksExistInExtents(jt->second.extents) || - TempBlocksExistInExtents(jt->second.write_extents)) { + for (const auto& edge_prop_pair : it->out_edges) { + if (TempBlocksExistInExtents(edge_prop_pair.second.extents) || + TempBlocksExistInExtents(edge_prop_pair.second.write_extents)) { LOG(INFO) << "bad out edge in node " << idx; LOG(INFO) << "so yeah"; return false; diff --git a/payload_generator/generate_delta_main.cc b/payload_generator/generate_delta_main.cc index b08e47d..bcd8738 100644 --- a/payload_generator/generate_delta_main.cc +++ b/payload_generator/generate_delta_main.cc @@ -46,13 +46,11 @@ void ParseSignatureSizes(const string& signature_sizes_flag, vector split_strings; base::SplitString(signature_sizes_flag, ':', &split_strings); - for (vector::iterator i = split_strings.begin(); - i < split_strings.end(); - i++) { + for (const string& str : split_strings) { int size = 0; - bool parsing_successful = base::StringToInt(*i, &size); + bool parsing_successful = base::StringToInt(str, &size); LOG_IF(FATAL, !parsing_successful) - << "Invalid signature size: " << *i; + << "Invalid signature size: " << str; LOG_IF(FATAL, size != (2048 / 8)) << "Only signature sizes of 256 bytes are supported."; @@ -148,10 +146,9 @@ void SignPayload(const string& in_file, vector> signatures; vector signature_files; base::SplitString(signature_file, ':', &signature_files); - for (vector::iterator it = signature_files.begin(), - e = signature_files.end(); it != e; ++it) { + for (const string& signature_file : signature_files) { vector signature; - CHECK(utils::ReadFile(*it, &signature)); + CHECK(utils::ReadFile(signature_file, &signature)); signatures.push_back(signature); } uint64_t final_metadata_size; diff --git a/payload_generator/metadata.cc b/payload_generator/metadata.cc index 460aa46..0b4e428 100644 --- a/payload_generator/metadata.cc +++ b/payload_generator/metadata.cc @@ -38,9 +38,8 @@ bool ReadExtentsData(const ext2_filsys fs, vector* data) { // Resize the data buffer to hold all data in the extents size_t num_data_blocks = 0; - for (vector::const_iterator it = extents.begin(); - it != extents.end(); it++) { - num_data_blocks += it->num_blocks(); + for (const Extent& extent : extents) { + num_data_blocks += extent.num_blocks(); } data->resize(num_data_blocks * kBlockSize); @@ -48,17 +47,16 @@ bool ReadExtentsData(const ext2_filsys fs, // Read in the data blocks const size_t kMaxReadBlocks = 256; vector::size_type blocks_copied_count = 0; - for (vector::const_iterator it = extents.begin(); - it != extents.end(); it++) { + for (const Extent& extent : extents) { vector::size_type blocks_read = 0; - while (blocks_read < it->num_blocks()) { + while (blocks_read < extent.num_blocks()) { const int copy_block_cnt = min(kMaxReadBlocks, static_cast( - it->num_blocks() - blocks_read)); + extent.num_blocks() - blocks_read)); TEST_AND_RETURN_FALSE_ERRCODE( io_channel_read_blk(fs->io, - it->start_block() + blocks_read, + extent.start_block() + blocks_read, copy_block_cnt, &(*data)[blocks_copied_count * kBlockSize])); blocks_read += copy_block_cnt; diff --git a/payload_generator/payload_signer.cc b/payload_generator/payload_signer.cc index d3afb86..a33c24f 100644 --- a/payload_generator/payload_signer.cc +++ b/payload_generator/payload_signer.cc @@ -36,9 +36,7 @@ bool ConvertSignatureToProtobufBlob(const vector>& signatures, << kSignatureMessageOriginalVersion << ", " << kSignatureMessageCurrentVersion << "] inclusive, but you only " << "provided " << signatures.size() << " signatures."; - for (vector>::const_iterator it = signatures.begin(), - e = signatures.end(); it != e; ++it) { - const vector& signature = *it; + for (const vector& signature : signatures) { Signatures_Signature* sig_message = out_message.add_signatures(); sig_message->set_version(version++); sig_message->set_data(signature.data(), signature.size()); @@ -177,10 +175,9 @@ bool PayloadSigner::SignPayload(const string& unsigned_payload_path, utils::FileSize(unsigned_payload_path)); vector> signatures; - for (vector::const_iterator it = private_key_paths.begin(), - e = private_key_paths.end(); it != e; ++it) { + for (const string& path : private_key_paths) { vector signature; - TEST_AND_RETURN_FALSE(SignHash(hash_data, *it, &signature)); + TEST_AND_RETURN_FALSE(SignHash(hash_data, path, &signature)); signatures.push_back(signature); } TEST_AND_RETURN_FALSE(ConvertSignatureToProtobufBlob(signatures, @@ -216,10 +213,8 @@ bool PayloadSigner::PrepPayloadForHashing( // Loads the payload and adds the signature op to it. vector> signatures; - for (vector::const_iterator it = signature_sizes.begin(), - e = signature_sizes.end(); it != e; ++it) { - vector signature(*it, 0); - signatures.push_back(signature); + for (int signature_size : signature_sizes) { + signatures.emplace_back(signature_size, 0); } vector signature_blob; TEST_AND_RETURN_FALSE(ConvertSignatureToProtobufBlob(signatures, diff --git a/test_utils.cc b/test_utils.cc index 251ed78..4a49fb2 100644 --- a/test_utils.cc +++ b/test_utils.cc @@ -190,9 +190,8 @@ void VerifyAllPaths(const string& parent, set expected_paths) { EXPECT_FALSE(iter.IsErr()); EXPECT_TRUE(expected_paths.empty()); if (!expected_paths.empty()) { - for (set::const_iterator it = expected_paths.begin(); - it != expected_paths.end(); ++it) { - LOG(INFO) << "extra path: " << *it; + for (const string& path : expected_paths) { + LOG(INFO) << "extra path: " << path; } } } diff --git a/update_attempter.cc b/update_attempter.cc index 200493d..32c3728 100644 --- a/update_attempter.cc +++ b/update_attempter.cc @@ -673,9 +673,8 @@ void UpdateAttempter::BuildUpdateActions(bool interactive) { actions_.push_back(shared_ptr(update_complete_action)); // Enqueue the actions - for (vector>::iterator it = actions_.begin(); - it != actions_.end(); ++it) { - processor_->EnqueueAction(it->get()); + for (const shared_ptr& action : actions_) { + processor_->EnqueueAction(action.get()); } } @@ -727,9 +726,8 @@ bool UpdateAttempter::Rollback(bool powerwash) { BuildPostInstallActions(install_plan_action.get()); // Enqueue the actions - for (vector>::iterator it = actions_.begin(); - it != actions_.end(); ++it) { - processor_->EnqueueAction(it->get()); + for (const shared_ptr& action : actions_) { + processor_->EnqueueAction(action.get()); } // Update the payload state for Rollback. diff --git a/utils.cc b/utils.cc index 33df1b5..99a02d3 100644 --- a/utils.cc +++ b/utils.cc @@ -83,13 +83,12 @@ string ParseECVersion(string input_line) { // a vector of key value pairs. vector> kv_pairs; if (base::SplitStringIntoKeyValuePairs(input_line, '=', ' ', &kv_pairs)) { - for (vector>::iterator it = kv_pairs.begin(); - it != kv_pairs.end(); ++it) { + for (const pair& kv_pair : kv_pairs) { // Finally match against the fw_verion which may have quotes. - if (it->first == "fw_version") { + if (kv_pair.first == "fw_version") { string output; // Trim any quotes. - base::TrimString(it->second, "\"", &output); + base::TrimString(kv_pair.second, "\"", &output); return output; } } @@ -536,25 +535,12 @@ string ErrnoNumberAsString(int err) { string NormalizePath(const string& path, bool strip_trailing_slash) { string ret; - bool last_insert_was_slash = false; - for (string::const_iterator it = path.begin(); it != path.end(); ++it) { - if (*it == '/') { - if (last_insert_was_slash) - continue; - last_insert_was_slash = true; - } else { - last_insert_was_slash = false; - } - ret.push_back(*it); - } - if (strip_trailing_slash && last_insert_was_slash) { - string::size_type last_non_slash = ret.find_last_not_of('/'); - if (last_non_slash != string::npos) { - ret.resize(last_non_slash + 1); - } else { - ret = ""; - } - } + std::unique_copy(path.begin(), path.end(), std::back_inserter(ret), + [](char c1, char c2) { return c1 == c2 && c1 == '/'; }); + // The above code ensures no "//" is present in the string, so at most one + // '/' is present at the end of the line. + if (strip_trailing_slash && !ret.empty() && ret.back() == '/') + ret.pop_back(); return ret; } @@ -1436,9 +1422,8 @@ Time TimeFromStructTimespec(struct timespec *ts) { gchar** StringVectorToGStrv(const vector &vec_str) { GPtrArray *p = g_ptr_array_new(); - for (vector::const_iterator i = vec_str.begin(); - i != vec_str.end(); ++i) { - g_ptr_array_add(p, g_strdup(i->c_str())); + for (const string& str : vec_str) { + g_ptr_array_add(p, g_strdup(str.c_str())); } g_ptr_array_add(p, nullptr); return reinterpret_cast(g_ptr_array_free(p, FALSE)); diff --git a/utils_unittest.cc b/utils_unittest.cc index 44d1b87..0e29113 100644 --- a/utils_unittest.cc +++ b/utils_unittest.cc @@ -309,9 +309,8 @@ TEST(UtilsTest, ApplyMapTest) { utils::ApplyMap(&collection, value_map); size_t index = 0; - for (vector::iterator it = collection.begin(), e = collection.end(); - it != e; ++it) { - EXPECT_EQ(expected_values[index++], *it); + for (const int value : collection) { + EXPECT_EQ(expected_values[index++], value); } }