Skip to content

Commit 947ed1e

Browse files
committed
DepsLog: Use DiskInterface instance.
1 parent a3f5c70 commit 947ed1e

File tree

6 files changed

+94
-62
lines changed

6 files changed

+94
-62
lines changed

src/build_test.cc

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,7 @@ void BuildTest::RebuildTarget(const string& target, const char* manifest,
605605
pbuild_log = &build_log;
606606
}
607607

608-
DepsLog deps_log, *pdeps_log = NULL;
608+
DepsLog deps_log(disk_interface), *pdeps_log = NULL;
609609
if (deps_path) {
610610
ASSERT_TRUE(deps_log.Load(deps_path, pstate, &err));
611611
ASSERT_TRUE(deps_log.OpenForWrite(deps_path, &err));
@@ -2330,7 +2330,8 @@ struct BuildWithQueryDepsLogTest : public BuildTest {
23302330
ScopedTempDir temp_dir_;
23312331

23322332
ScopedFilePath deps_log_file_;
2333-
DepsLog log_;
2333+
SystemDiskInterface disk_interface_;
2334+
DepsLog log_{ disk_interface_ };
23342335
};
23352336

23362337
/// Test a MSVC-style deps log with multiple outputs.
@@ -2553,13 +2554,15 @@ TEST_F(BuildWithDepsLogTest, Straightforward) {
25532554
" deps = gcc\n"
25542555
" depfile = in1.d\n";
25552556

2557+
SystemDiskInterface disk_interface;
2558+
25562559
{
25572560
State state;
25582561
ASSERT_NO_FATAL_FAILURE(AddCatRule(&state));
25592562
ASSERT_NO_FATAL_FAILURE(AssertParse(&state, manifest));
25602563

25612564
// Run the build once, everything should be ok.
2562-
DepsLog deps_log;
2565+
DepsLog deps_log(disk_interface);
25632566
ASSERT_TRUE(deps_log.OpenForWrite(deps_log_file_.path(), &err));
25642567
ASSERT_EQ("", err);
25652568

@@ -2589,7 +2592,7 @@ TEST_F(BuildWithDepsLogTest, Straightforward) {
25892592
fs_.Create("in2", "");
25902593

25912594
// Run the build again.
2592-
DepsLog deps_log;
2595+
DepsLog deps_log(disk_interface);
25932596
ASSERT_TRUE(deps_log.Load(deps_log_file_.path(), &state, &err));
25942597
ASSERT_TRUE(deps_log.OpenForWrite(deps_log_file_.path(), &err));
25952598

@@ -2620,6 +2623,9 @@ TEST_F(BuildWithDepsLogTest, ObsoleteDeps) {
26202623
"build out: cat in1\n"
26212624
" deps = gcc\n"
26222625
" depfile = in1.d\n";
2626+
2627+
SystemDiskInterface disk_interface;
2628+
26232629
{
26242630
// Run an ordinary build that gathers dependencies.
26252631
fs_.Create("in1", "");
@@ -2630,7 +2636,7 @@ TEST_F(BuildWithDepsLogTest, ObsoleteDeps) {
26302636
ASSERT_NO_FATAL_FAILURE(AssertParse(&state, manifest));
26312637

26322638
// Run the build once, everything should be ok.
2633-
DepsLog deps_log;
2639+
DepsLog deps_log(disk_interface);
26342640
ASSERT_TRUE(deps_log.OpenForWrite(deps_log_file_.path(), &err));
26352641
ASSERT_EQ("", err);
26362642

@@ -2659,7 +2665,7 @@ TEST_F(BuildWithDepsLogTest, ObsoleteDeps) {
26592665
ASSERT_NO_FATAL_FAILURE(AddCatRule(&state));
26602666
ASSERT_NO_FATAL_FAILURE(AssertParse(&state, manifest));
26612667

2662-
DepsLog deps_log;
2668+
DepsLog deps_log(disk_interface);
26632669
ASSERT_TRUE(deps_log.Load(deps_log_file_.path(), &state, &err));
26642670
ASSERT_TRUE(deps_log.OpenForWrite(deps_log_file_.path(), &err));
26652671

@@ -2730,7 +2736,7 @@ TEST_F(BuildWithDepsLogTest, TestInputMtimeRaceCondition) {
27302736
ASSERT_TRUE(build_log.OpenForWrite(build_log_file_.path(), *this, &err))
27312737
<< err;
27322738

2733-
DepsLog deps_log;
2739+
DepsLog deps_log(disk_interface);
27342740
ASSERT_TRUE(deps_log.Load(deps_log_file_.path(), &state, &err));
27352741
ASSERT_TRUE(deps_log.OpenForWrite(deps_log_file_.path(), &err));
27362742

@@ -2813,7 +2819,7 @@ TEST_F(BuildWithDepsLogTest, TestInputMtimeRaceConditionWithDepFile) {
28132819
ASSERT_TRUE(build_log.Load(build_log_file_.path(), &err));
28142820
ASSERT_TRUE(build_log.OpenForWrite(build_log_file_.path(), *this, &err));
28152821

2816-
DepsLog deps_log;
2822+
DepsLog deps_log(disk_interface);
28172823
ASSERT_TRUE(deps_log.Load(deps_log_file_.path(), &state, &err));
28182824
ASSERT_TRUE(deps_log.OpenForWrite(deps_log_file_.path(), &err));
28192825

@@ -2954,13 +2960,16 @@ TEST_F(BuildWithDepsLogTest, RestatDepfileDependencyDepsLog) {
29542960
"build out: cat in1\n"
29552961
" deps = gcc\n"
29562962
" depfile = in1.d\n";
2963+
2964+
SystemDiskInterface disk_interface;
2965+
29572966
{
29582967
State state;
29592968
ASSERT_NO_FATAL_FAILURE(AddCatRule(&state));
29602969
ASSERT_NO_FATAL_FAILURE(AssertParse(&state, manifest));
29612970

29622971
// Run the build once, everything should be ok.
2963-
DepsLog deps_log;
2972+
DepsLog deps_log(disk_interface);
29642973
ASSERT_TRUE(deps_log.OpenForWrite(deps_log_file_.path(), &err));
29652974
ASSERT_EQ("", err);
29662975

@@ -2986,7 +2995,7 @@ TEST_F(BuildWithDepsLogTest, RestatDepfileDependencyDepsLog) {
29862995
fs_.Create("header.in", "");
29872996

29882997
// Run the build again.
2989-
DepsLog deps_log;
2998+
DepsLog deps_log(disk_interface);
29902999
ASSERT_TRUE(deps_log.Load(deps_log_file_.path(), &state, &err));
29913000
ASSERT_TRUE(deps_log.OpenForWrite(deps_log_file_.path(), &err));
29923001

@@ -3014,12 +3023,14 @@ TEST_F(BuildWithDepsLogTest, DepFileOKDepsLog) {
30143023

30153024
fs_.Create("foo.c", "");
30163025

3026+
SystemDiskInterface disk_interface;
3027+
30173028
{
30183029
State state;
30193030
ASSERT_NO_FATAL_FAILURE(AssertParse(&state, manifest));
30203031

30213032
// Run the build once, everything should be ok.
3022-
DepsLog deps_log;
3033+
DepsLog deps_log(disk_interface);
30233034
ASSERT_TRUE(deps_log.OpenForWrite(deps_log_file_.path(), &err));
30243035
ASSERT_EQ("", err);
30253036

@@ -3039,7 +3050,7 @@ TEST_F(BuildWithDepsLogTest, DepFileOKDepsLog) {
30393050
State state;
30403051
ASSERT_NO_FATAL_FAILURE(AssertParse(&state, manifest));
30413052

3042-
DepsLog deps_log;
3053+
DepsLog deps_log(disk_interface);
30433054
ASSERT_TRUE(deps_log.Load(deps_log_file_.path(), &state, &err));
30443055
ASSERT_TRUE(deps_log.OpenForWrite(deps_log_file_.path(), &err));
30453056
ASSERT_EQ("", err);
@@ -3091,7 +3102,7 @@ TEST_F(BuildWithDepsLogTest, DiscoveredDepDuringBuildChanged) {
30913102
State state;
30923103
ASSERT_NO_FATAL_FAILURE(AssertParse(&state, manifest));
30933104

3094-
DepsLog deps_log;
3105+
DepsLog deps_log(disk_interface);
30953106
ASSERT_TRUE(deps_log.OpenForWrite(deps_log_file_.path(), &err));
30963107
ASSERT_EQ("", err);
30973108

@@ -3114,7 +3125,7 @@ TEST_F(BuildWithDepsLogTest, DiscoveredDepDuringBuildChanged) {
31143125
State state;
31153126
ASSERT_NO_FATAL_FAILURE(AssertParse(&state, manifest));
31163127

3117-
DepsLog deps_log;
3128+
DepsLog deps_log(disk_interface);
31183129
ASSERT_TRUE(deps_log.Load(deps_log_file_.path(), &state, &err));
31193130
ASSERT_TRUE(deps_log.OpenForWrite(deps_log_file_.path(), &err));
31203131
ASSERT_EQ("", err);
@@ -3137,7 +3148,7 @@ TEST_F(BuildWithDepsLogTest, DiscoveredDepDuringBuildChanged) {
31373148
State state;
31383149
ASSERT_NO_FATAL_FAILURE(AssertParse(&state, manifest));
31393150

3140-
DepsLog deps_log;
3151+
DepsLog deps_log(disk_interface);
31413152
ASSERT_TRUE(deps_log.Load(deps_log_file_.path(), &state, &err));
31423153
ASSERT_TRUE(deps_log.OpenForWrite(deps_log_file_.path(), &err));
31433154
ASSERT_EQ("", err);
@@ -3161,12 +3172,14 @@ TEST_F(BuildWithDepsLogTest, DepFileDepsLogCanonicalize) {
31613172

31623173
fs_.Create("x/y/z/foo.c", "");
31633174

3175+
SystemDiskInterface disk_interface;
3176+
31643177
{
31653178
State state;
31663179
ASSERT_NO_FATAL_FAILURE(AssertParse(&state, manifest));
31673180

31683181
// Run the build once, everything should be ok.
3169-
DepsLog deps_log;
3182+
DepsLog deps_log(disk_interface);
31703183
ASSERT_TRUE(deps_log.OpenForWrite(deps_log_file_.path(), &err));
31713184
ASSERT_EQ("", err);
31723185

@@ -3188,7 +3201,7 @@ TEST_F(BuildWithDepsLogTest, DepFileDepsLogCanonicalize) {
31883201
State state;
31893202
ASSERT_NO_FATAL_FAILURE(AssertParse(&state, manifest));
31903203

3191-
DepsLog deps_log;
3204+
DepsLog deps_log(disk_interface);
31923205
ASSERT_TRUE(deps_log.Load(deps_log_file_.path(), &state, &err));
31933206
ASSERT_TRUE(deps_log.OpenForWrite(deps_log_file_.path(), &err));
31943207
ASSERT_EQ("", err);
@@ -4258,6 +4271,8 @@ TEST_F(BuildWithDepsLogTest, ValidationThroughDepfile) {
42584271

42594272
string err;
42604273

4274+
SystemDiskInterface disk_interface;
4275+
42614276
{
42624277
fs_.Create("in", "");
42634278
fs_.Create("in2", "");
@@ -4268,7 +4283,7 @@ TEST_F(BuildWithDepsLogTest, ValidationThroughDepfile) {
42684283
ASSERT_NO_FATAL_FAILURE(AddCatRule(&state));
42694284
ASSERT_NO_FATAL_FAILURE(AssertParse(&state, manifest));
42704285

4271-
DepsLog deps_log;
4286+
DepsLog deps_log(disk_interface);
42724287
ASSERT_TRUE(deps_log.OpenForWrite(deps_log_file_.path(), &err));
42734288
ASSERT_EQ("", err);
42744289

@@ -4303,7 +4318,7 @@ TEST_F(BuildWithDepsLogTest, ValidationThroughDepfile) {
43034318
ASSERT_NO_FATAL_FAILURE(AddCatRule(&state));
43044319
ASSERT_NO_FATAL_FAILURE(AssertParse(&state, manifest));
43054320

4306-
DepsLog deps_log;
4321+
DepsLog deps_log(disk_interface);
43074322
ASSERT_TRUE(deps_log.Load(deps_log_file_.path(), &state, &err));
43084323
ASSERT_TRUE(deps_log.OpenForWrite(deps_log_file_.path(), &err));
43094324
ASSERT_EQ("", err);

src/deps_log.cc

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ static const int32_t kCurrentVersion = 4;
4444
// internal buffers having to have this size.
4545
static constexpr size_t kMaxRecordSize = (1 << 19) - 1;
4646

47+
DepsLog::DepsLog(DiskInterface& disk_interface)
48+
: disk_interface_(&disk_interface) {}
49+
4750
DepsLog::~DepsLog() {
4851
Close();
4952
}
@@ -155,7 +158,7 @@ void DepsLog::Close() {
155158
LoadStatus DepsLog::Load(const string& path, State* state, string* err) {
156159
METRIC_RECORD(".ninja_deps load");
157160
char buf[kMaxRecordSize + 1];
158-
FILE* f = fopen(path.c_str(), "rb");
161+
FILE* f = disk_interface_->OpenFile(path, "rb");
159162
if (!f) {
160163
if (errno == ENOENT)
161164
return LOAD_NOT_FOUND;
@@ -180,7 +183,7 @@ LoadStatus DepsLog::Load(const string& path, State* state, string* err) {
180183
else
181184
*err = "bad deps log signature or version; starting over";
182185
fclose(f);
183-
unlink(path.c_str());
186+
disk_interface_->RemoveFile(path);
184187
// Don't report this as a failure. An empty deps log will cause
185188
// us to rebuild the outputs anyway.
186189
return LOAD_SUCCESS;
@@ -329,13 +332,13 @@ bool DepsLog::Recompact(const string& path, string* err) {
329332
METRIC_RECORD(".ninja_deps recompact");
330333

331334
Close();
332-
string temp_path = path + ".recompact";
335+
std::string temp_path = path + ".recompact";
333336

334337
// OpenForWrite() opens for append. Make sure it's not appending to a
335338
// left-over file from a previous recompaction attempt that crashed somehow.
336-
unlink(temp_path.c_str());
339+
disk_interface_->RemoveFile(temp_path);
337340

338-
DepsLog new_log;
341+
DepsLog new_log(*disk_interface_);
339342
if (!new_log.OpenForWrite(temp_path, err))
340343
return false;
341344

@@ -365,12 +368,12 @@ bool DepsLog::Recompact(const string& path, string* err) {
365368
deps_.swap(new_log.deps_);
366369
nodes_.swap(new_log.nodes_);
367370

368-
if (unlink(path.c_str()) < 0) {
371+
if (disk_interface_->RemoveFile(path) < 0) {
369372
*err = strerror(errno);
370373
return false;
371374
}
372375

373-
if (rename(temp_path.c_str(), path.c_str()) < 0) {
376+
if (!disk_interface_->RenameFile(temp_path, path)) {
374377
*err = strerror(errno);
375378
return false;
376379
}
@@ -437,7 +440,7 @@ bool DepsLog::OpenForWriteIfNeeded() {
437440
if (file_path_.empty()) {
438441
return true;
439442
}
440-
file_ = fopen(file_path_.c_str(), "ab");
443+
file_ = disk_interface_->OpenFile(file_path_, "ab");
441444
if (!file_) {
442445
return false;
443446
}

src/deps_log.h

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@
1515
#ifndef NINJA_DEPS_LOG_H_
1616
#define NINJA_DEPS_LOG_H_
1717

18+
#include <stdio.h>
19+
20+
#include <memory>
1821
#include <string>
1922
#include <vector>
2023

21-
#include <stdio.h>
22-
24+
#include "disk_interface.h"
2325
#include "load_status.h"
2426
#include "timestamp.h"
2527

@@ -66,7 +68,13 @@ struct State;
6668
/// wins, allowing updates to just be appended to the file. A separate
6769
/// repacking step can run occasionally to remove dead records.
6870
struct DepsLog {
69-
DepsLog() : needs_recompaction_(false), file_(NULL) {}
71+
/// No default constructor.
72+
DepsLog() = delete;
73+
74+
/// Constructor takes a DiskInterface reference.
75+
DepsLog(DiskInterface& disk_interface);
76+
77+
/// Destructor
7078
~DepsLog();
7179

7280
// Writing (build-time) interface.
@@ -114,8 +122,10 @@ struct DepsLog {
114122
/// be set.
115123
bool OpenForWriteIfNeeded();
116124

117-
bool needs_recompaction_;
118-
FILE* file_;
125+
DiskInterface* disk_interface_ = nullptr;
126+
127+
bool needs_recompaction_ = false;
128+
FILE* file_ = nullptr;
119129
std::string file_path_;
120130

121131
/// Maps id -> Node.

0 commit comments

Comments
 (0)