1 // Copyright (c) 2012 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.
5 #ifndef BASE_FILES_IMPORTANT_FILE_WRITER_H_
6 #define BASE_FILES_IMPORTANT_FILE_WRITER_H_
10 #include "base/base_export.h"
11 #include "base/basictypes.h"
12 #include "base/file_path.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/threading/non_thread_safe.h"
15 #include "base/time.h"
16 #include "base/timer.h"
20 class SequencedTaskRunner
;
23 // Helper to ensure that a file won't be corrupted by the write (for example on
24 // application crash). Consider a naive way to save an important file F:
26 // 1. Open F for writing, truncating it.
27 // 2. Write new data to F.
29 // It's good when it works, but it gets very bad if step 2. doesn't complete.
30 // It can be caused by a crash, a computer hang, or a weird I/O error. And you
31 // end up with a broken file.
33 // To be safe, we don't start with writing directly to F. Instead, we write to
34 // to a temporary file. Only after that write is successful, we rename the
35 // temporary file to target filename.
37 // If you want to know more about this approach and ext3/ext4 fsync issues, see
38 // http://valhenson.livejournal.com/37921.html
39 class BASE_EXPORT ImportantFileWriter
: public NonThreadSafe
{
41 // Used by ScheduleSave to lazily provide the data to be saved. Allows us
42 // to also batch data serializations.
43 class BASE_EXPORT DataSerializer
{
45 // Should put serialized string in |data| and return true on successful
46 // serialization. Will be called on the same thread on which
47 // ImportantFileWriter has been created.
48 virtual bool SerializeData(std::string
* data
) = 0;
51 virtual ~DataSerializer() {}
54 // Initialize the writer.
55 // |path| is the name of file to write.
56 // |task_runner| is the SequencedTaskRunner instance where on which we will
57 // execute file I/O operations.
58 // All non-const methods, ctor and dtor must be called on the same thread.
59 ImportantFileWriter(const FilePath
& path
,
60 base::SequencedTaskRunner
* task_runner
);
62 // You have to ensure that there are no pending writes at the moment
64 ~ImportantFileWriter();
66 const FilePath
& path() const { return path_
; }
68 // Returns true if there is a scheduled write pending which has not yet
70 bool HasPendingWrite() const;
72 // Save |data| to target filename. Does not block. If there is a pending write
73 // scheduled by ScheduleWrite, it is cancelled.
74 void WriteNow(const std::string
& data
);
76 // Schedule a save to target filename. Data will be serialized and saved
77 // to disk after the commit interval. If another ScheduleWrite is issued
78 // before that, only one serialization and write to disk will happen, and
79 // the most recent |serializer| will be used. This operation does not block.
80 // |serializer| should remain valid through the lifetime of
81 // ImportantFileWriter.
82 void ScheduleWrite(DataSerializer
* serializer
);
84 // Serialize data pending to be saved and execute write on backend thread.
85 void DoScheduledWrite();
87 TimeDelta
commit_interval() const {
88 return commit_interval_
;
91 void set_commit_interval(const TimeDelta
& interval
) {
92 commit_interval_
= interval
;
96 // Path being written to.
99 // TaskRunner for the thread on which file I/O can be done.
100 const scoped_refptr
<base::SequencedTaskRunner
> task_runner_
;
102 // Timer used to schedule commit after ScheduleWrite.
103 OneShotTimer
<ImportantFileWriter
> timer_
;
105 // Serializer which will provide the data to be saved.
106 DataSerializer
* serializer_
;
108 // Time delta after which scheduled data will be written to disk.
109 TimeDelta commit_interval_
;
111 DISALLOW_COPY_AND_ASSIGN(ImportantFileWriter
);
116 #endif // BASE_FILES_IMPORTANT_FILE_WRITER_H_