blob: 61a53b16503bda6902519df11c60278096979f11 [file] [log] [blame]
[email protected]512d03f2012-06-26 01:06:061// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]6faa0e0d2009-04-28 06:50:362// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]43486252012-10-24 16:33:365#ifndef BASE_FILES_IMPORTANT_FILE_WRITER_H_
6#define BASE_FILES_IMPORTANT_FILE_WRITER_H_
[email protected]6faa0e0d2009-04-28 06:50:367
8#include <string>
9
[email protected]43486252012-10-24 16:33:3610#include "base/base_export.h"
[email protected]6faa0e0d2009-04-28 06:50:3611#include "base/basictypes.h"
[email protected]e33c9512014-05-12 02:24:1312#include "base/callback.h"
[email protected]57999812013-02-24 05:40:5213#include "base/files/file_path.h"
[email protected]3b63f8f42011-03-28 01:54:1514#include "base/memory/ref_counted.h"
[email protected]c9177502011-01-01 04:48:4915#include "base/threading/non_thread_safe.h"
[email protected]99084f62013-06-28 00:49:0716#include "base/time/time.h"
17#include "base/timer/timer.h"
[email protected]6faa0e0d2009-04-28 06:50:3618
19namespace base {
[email protected]43486252012-10-24 16:33:3620
[email protected]0de615a2012-11-08 04:40:5921class SequencedTaskRunner;
[email protected]6faa0e0d2009-04-28 06:50:3622class Thread;
[email protected]6faa0e0d2009-04-28 06:50:3623
24// Helper to ensure that a file won't be corrupted by the write (for example on
25// application crash). Consider a naive way to save an important file F:
26//
27// 1. Open F for writing, truncating it.
28// 2. Write new data to F.
29//
30// It's good when it works, but it gets very bad if step 2. doesn't complete.
31// It can be caused by a crash, a computer hang, or a weird I/O error. And you
32// end up with a broken file.
33//
34// To be safe, we don't start with writing directly to F. Instead, we write to
35// to a temporary file. Only after that write is successful, we rename the
36// temporary file to target filename.
37//
38// If you want to know more about this approach and ext3/ext4 fsync issues, see
39// http://valhenson.livejournal.com/37921.html
[email protected]43486252012-10-24 16:33:3640class BASE_EXPORT ImportantFileWriter : public NonThreadSafe {
[email protected]6faa0e0d2009-04-28 06:50:3641 public:
[email protected]6c1164042009-05-08 14:41:0842 // Used by ScheduleSave to lazily provide the data to be saved. Allows us
43 // to also batch data serializations.
[email protected]f59f33e2012-11-01 12:05:2744 class BASE_EXPORT DataSerializer {
[email protected]6c1164042009-05-08 14:41:0845 public:
[email protected]6c1164042009-05-08 14:41:0846 // Should put serialized string in |data| and return true on successful
47 // serialization. Will be called on the same thread on which
48 // ImportantFileWriter has been created.
49 virtual bool SerializeData(std::string* data) = 0;
[email protected]512d03f2012-06-26 01:06:0650
51 protected:
52 virtual ~DataSerializer() {}
[email protected]6c1164042009-05-08 14:41:0853 };
54
[email protected]95b42e22012-11-29 14:00:1255 // Save |data| to |path| in an atomic manner (see the class comment above).
56 // Blocks and writes data on the current thread.
57 static bool WriteFileAtomically(const FilePath& path,
58 const std::string& data);
59
[email protected]6faa0e0d2009-04-28 06:50:3660 // Initialize the writer.
[email protected]6fad2632009-11-02 05:59:3761 // |path| is the name of file to write.
[email protected]0de615a2012-11-08 04:40:5962 // |task_runner| is the SequencedTaskRunner instance where on which we will
63 // execute file I/O operations.
[email protected]6faa0e0d2009-04-28 06:50:3664 // All non-const methods, ctor and dtor must be called on the same thread.
[email protected]6658ca82010-05-20 18:20:2965 ImportantFileWriter(const FilePath& path,
[email protected]0de615a2012-11-08 04:40:5966 base::SequencedTaskRunner* task_runner);
[email protected]6faa0e0d2009-04-28 06:50:3667
[email protected]6c1164042009-05-08 14:41:0868 // You have to ensure that there are no pending writes at the moment
69 // of destruction.
[email protected]6faa0e0d2009-04-28 06:50:3670 ~ImportantFileWriter();
71
[email protected]a83d42292010-08-17 22:51:1072 const FilePath& path() const { return path_; }
[email protected]6faa0e0d2009-04-28 06:50:3673
[email protected]6c1164042009-05-08 14:41:0874 // Returns true if there is a scheduled write pending which has not yet
75 // been started.
76 bool HasPendingWrite() const;
77
[email protected]6faa0e0d2009-04-28 06:50:3678 // Save |data| to target filename. Does not block. If there is a pending write
79 // scheduled by ScheduleWrite, it is cancelled.
80 void WriteNow(const std::string& data);
81
[email protected]6c1164042009-05-08 14:41:0882 // Schedule a save to target filename. Data will be serialized and saved
83 // to disk after the commit interval. If another ScheduleWrite is issued
84 // before that, only one serialization and write to disk will happen, and
85 // the most recent |serializer| will be used. This operation does not block.
86 // |serializer| should remain valid through the lifetime of
87 // ImportantFileWriter.
88 void ScheduleWrite(DataSerializer* serializer);
89
90 // Serialize data pending to be saved and execute write on backend thread.
91 void DoScheduledWrite();
[email protected]6faa0e0d2009-04-28 06:50:3692
[email protected]e33c9512014-05-12 02:24:1393 // Registers |on_next_successful_write| to be called once, on the next
94 // successful write event. Only one callback can be set at once.
95 void RegisterOnNextSuccessfulWriteCallback(
96 const base::Closure& on_next_successful_write);
97
[email protected]43486252012-10-24 16:33:3698 TimeDelta commit_interval() const {
[email protected]6faa0e0d2009-04-28 06:50:3699 return commit_interval_;
100 }
101
[email protected]43486252012-10-24 16:33:36102 void set_commit_interval(const TimeDelta& interval) {
[email protected]6faa0e0d2009-04-28 06:50:36103 commit_interval_ = interval;
104 }
105
106 private:
[email protected]e33c9512014-05-12 02:24:13107 // Helper method for WriteNow().
108 bool PostWriteTask(const std::string& data);
109
110 // If |result| is true and |on_next_successful_write_| is set, invokes
111 // |on_successful_write_| and then resets it; no-ops otherwise.
112 void ForwardSuccessfulWrite(bool result);
113
114 // Invoked once and then reset on the next successful write event.
115 base::Closure on_next_successful_write_;
116
[email protected]6faa0e0d2009-04-28 06:50:36117 // Path being written to.
118 const FilePath path_;
119
[email protected]0de615a2012-11-08 04:40:59120 // TaskRunner for the thread on which file I/O can be done.
121 const scoped_refptr<base::SequencedTaskRunner> task_runner_;
[email protected]6658ca82010-05-20 18:20:29122
[email protected]6faa0e0d2009-04-28 06:50:36123 // Timer used to schedule commit after ScheduleWrite.
[email protected]43486252012-10-24 16:33:36124 OneShotTimer<ImportantFileWriter> timer_;
[email protected]6faa0e0d2009-04-28 06:50:36125
[email protected]6c1164042009-05-08 14:41:08126 // Serializer which will provide the data to be saved.
127 DataSerializer* serializer_;
[email protected]6faa0e0d2009-04-28 06:50:36128
129 // Time delta after which scheduled data will be written to disk.
[email protected]43486252012-10-24 16:33:36130 TimeDelta commit_interval_;
[email protected]6faa0e0d2009-04-28 06:50:36131
[email protected]e33c9512014-05-12 02:24:13132 WeakPtrFactory<ImportantFileWriter> weak_factory_;
133
[email protected]6faa0e0d2009-04-28 06:50:36134 DISALLOW_COPY_AND_ASSIGN(ImportantFileWriter);
135};
136
[email protected]43486252012-10-24 16:33:36137} // namespace base
138
139#endif // BASE_FILES_IMPORTANT_FILE_WRITER_H_