blob: bdf2223ecc01c272dc88597d44480d3b3c36bbd9 [file] [log] [blame]
[email protected]6faa0e0d2009-04-28 06:50:361// 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#ifndef CHROME_COMMON_IMPORTANT_FILE_WRITER_H_
6#define CHROME_COMMON_IMPORTANT_FILE_WRITER_H_
7
8#include <string>
9
10#include "base/basictypes.h"
11#include "base/file_path.h"
12#include "base/non_thread_safe.h"
13#include "base/time.h"
14#include "base/timer.h"
15
16namespace base {
17class Thread;
18}
19
20// Helper to ensure that a file won't be corrupted by the write (for example on
21// application crash). Consider a naive way to save an important file F:
22//
23// 1. Open F for writing, truncating it.
24// 2. Write new data to F.
25//
26// It's good when it works, but it gets very bad if step 2. doesn't complete.
27// It can be caused by a crash, a computer hang, or a weird I/O error. And you
28// end up with a broken file.
29//
30// To be safe, we don't start with writing directly to F. Instead, we write to
31// to a temporary file. Only after that write is successful, we rename the
32// temporary file to target filename.
33//
34// If you want to know more about this approach and ext3/ext4 fsync issues, see
35// http://valhenson.livejournal.com/37921.html
36class ImportantFileWriter : public NonThreadSafe {
37 public:
[email protected]6c1164042009-05-08 14:41:0838 // Used by ScheduleSave to lazily provide the data to be saved. Allows us
39 // to also batch data serializations.
40 class DataSerializer {
41 public:
42 virtual ~DataSerializer() {}
43
44 // Should put serialized string in |data| and return true on successful
45 // serialization. Will be called on the same thread on which
46 // ImportantFileWriter has been created.
47 virtual bool SerializeData(std::string* data) = 0;
48 };
49
[email protected]6faa0e0d2009-04-28 06:50:3650 // Initialize the writer.
[email protected]6fad2632009-11-02 05:59:3751 // |path| is the name of file to write.
[email protected]6faa0e0d2009-04-28 06:50:3652 // All non-const methods, ctor and dtor must be called on the same thread.
[email protected]6fad2632009-11-02 05:59:3753 ImportantFileWriter(const FilePath& path);
[email protected]6faa0e0d2009-04-28 06:50:3654
[email protected]6c1164042009-05-08 14:41:0855 // You have to ensure that there are no pending writes at the moment
56 // of destruction.
[email protected]6faa0e0d2009-04-28 06:50:3657 ~ImportantFileWriter();
58
59 FilePath path() const { return path_; }
60
[email protected]6c1164042009-05-08 14:41:0861 // Returns true if there is a scheduled write pending which has not yet
62 // been started.
63 bool HasPendingWrite() const;
64
[email protected]6faa0e0d2009-04-28 06:50:3665 // Save |data| to target filename. Does not block. If there is a pending write
66 // scheduled by ScheduleWrite, it is cancelled.
67 void WriteNow(const std::string& data);
68
[email protected]6c1164042009-05-08 14:41:0869 // Schedule a save to target filename. Data will be serialized and saved
70 // to disk after the commit interval. If another ScheduleWrite is issued
71 // before that, only one serialization and write to disk will happen, and
72 // the most recent |serializer| will be used. This operation does not block.
73 // |serializer| should remain valid through the lifetime of
74 // ImportantFileWriter.
75 void ScheduleWrite(DataSerializer* serializer);
76
77 // Serialize data pending to be saved and execute write on backend thread.
78 void DoScheduledWrite();
[email protected]6faa0e0d2009-04-28 06:50:3679
80 base::TimeDelta commit_interval() const {
81 return commit_interval_;
82 }
83
84 void set_commit_interval(const base::TimeDelta& interval) {
85 commit_interval_ = interval;
86 }
87
88 private:
[email protected]6faa0e0d2009-04-28 06:50:3689 // Path being written to.
90 const FilePath path_;
91
[email protected]6faa0e0d2009-04-28 06:50:3692 // Timer used to schedule commit after ScheduleWrite.
93 base::OneShotTimer<ImportantFileWriter> timer_;
94
[email protected]6c1164042009-05-08 14:41:0895 // Serializer which will provide the data to be saved.
96 DataSerializer* serializer_;
[email protected]6faa0e0d2009-04-28 06:50:3697
98 // Time delta after which scheduled data will be written to disk.
99 base::TimeDelta commit_interval_;
100
101 DISALLOW_COPY_AND_ASSIGN(ImportantFileWriter);
102};
103
104#endif // CHROME_COMMON_IMPORTANT_FILE_WRITER_H_