Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(367)

Side by Side Diff: chrome/common/important_file_writer.cc

Issue 83001: ImportantFileWriter (Closed)
Patch Set: share more code Created 11 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chrome/common/important_file_writer.h ('k') | chrome/common/important_file_writer_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "chrome/common/important_file_writer.h"
6
7 #include <stdio.h>
8
9 #include <ostream>
10 #include <string>
11
12 #include "base/file_path.h"
13 #include "base/file_util.h"
14 #include "base/logging.h"
15 #include "base/string_util.h"
16 #include "base/task.h"
17 #include "base/thread.h"
18 #include "base/time.h"
19
20 using base::TimeDelta;
21
22 namespace {
23
24 const int kDefaultCommitIntervalMs = 10000;
25
26 class WriteToDiskTask : public Task {
27 public:
28 WriteToDiskTask(const FilePath& path, const std::string& data)
29 : path_(path),
30 data_(data) {
31 }
32
33 virtual void Run() {
34 // Write the data to a temp file then rename to avoid data loss if we crash
35 // while writing the file. Ensure that the temp file is on the same volume
36 // as target file, so it can be moved in one step, and that the temp file
37 // is securely created.
38 FilePath tmp_file_path;
39 FILE* tmp_file = file_util::CreateAndOpenTemporaryFileInDir(
40 path_.DirName(), &tmp_file_path);
41 if (!tmp_file) {
42 LogFailure("could not create temporary file");
43 return;
44 }
45
46 size_t bytes_written = fwrite(data_.data(), 1, data_.length(), tmp_file);
47 if (!file_util::CloseFile(tmp_file)) {
48 file_util::Delete(tmp_file_path, false);
49 LogFailure("failed to close temporary file");
50 return;
51 }
52 if (bytes_written < data_.length()) {
53 file_util::Delete(tmp_file_path, false);
54 LogFailure("error writing, bytes_written=" + UintToString(bytes_written));
55 return;
56 }
57
58 if (file_util::Move(tmp_file_path, path_)) {
59 LogSuccess();
60 return;
61 }
62
63 file_util::Delete(tmp_file_path, false);
64 LogFailure("could not rename temporary file");
65 }
66
67 private:
68 void LogSuccess() {
69 LOG(INFO) << "successfully saved " << path_.value();
70 }
71
72 void LogFailure(const std::string& message) {
73 LOG(WARNING) << "failed to write " << path_.value()
74 << ": " << message;
75 }
76
77 const FilePath path_;
78 const std::string data_;
79
80 DISALLOW_COPY_AND_ASSIGN(WriteToDiskTask);
81 };
82
83 } // namespace
84
85 ImportantFileWriter::ImportantFileWriter(const FilePath& path,
86 const base::Thread* backend_thread)
87 : path_(path),
88 backend_thread_(backend_thread),
89 commit_interval_(TimeDelta::FromMilliseconds(kDefaultCommitIntervalMs)) {
90 DCHECK(CalledOnValidThread());
91 }
92
93 ImportantFileWriter::~ImportantFileWriter() {
94 DCHECK(CalledOnValidThread());
95 if (timer_.IsRunning()) {
96 timer_.Stop();
97 CommitPendingData();
98 }
99 }
100
101 void ImportantFileWriter::WriteNow(const std::string& data) {
102 DCHECK(CalledOnValidThread());
103
104 if (timer_.IsRunning())
105 timer_.Stop();
106
107 Task* task = new WriteToDiskTask(path_, data);
108 if (backend_thread_) {
109 backend_thread_->message_loop()->PostTask(FROM_HERE, task);
110 } else {
111 task->Run();
112 delete task;
113 }
114 }
115
116 void ImportantFileWriter::ScheduleWrite(const std::string& data) {
117 DCHECK(CalledOnValidThread());
118
119 data_ = data;
120 if (!timer_.IsRunning()) {
121 timer_.Start(commit_interval_, this,
122 &ImportantFileWriter::CommitPendingData);
123 }
124 }
125
126 void ImportantFileWriter::CommitPendingData() {
127 WriteNow(data_);
128 }
OLDNEW
« no previous file with comments | « chrome/common/important_file_writer.h ('k') | chrome/common/important_file_writer_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698