blob: 0bd8a7fd358f037ba26a8768a46b55627a112be3 [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]e33c9512014-05-12 02:24:1311#include "base/callback.h"
[email protected]57999812013-02-24 05:40:5212#include "base/files/file_path.h"
avi543540e2015-12-24 05:15:3213#include "base/macros.h"
[email protected]3b63f8f42011-03-28 01:54:1514#include "base/memory/ref_counted.h"
bcwhitebe133b52016-04-11 20:03:3815#include "base/strings/string_piece.h"
[email protected]c9177502011-01-01 04:48:4916#include "base/threading/non_thread_safe.h"
[email protected]99084f62013-06-28 00:49:0717#include "base/time/time.h"
18#include "base/timer/timer.h"
[email protected]6faa0e0d2009-04-28 06:50:3619
20namespace base {
[email protected]43486252012-10-24 16:33:3621
[email protected]0de615a2012-11-08 04:40:5922class SequencedTaskRunner;
[email protected]6faa0e0d2009-04-28 06:50:3623class Thread;
[email protected]6faa0e0d2009-04-28 06:50:3624
25// Helper to ensure that a file won't be corrupted by the write (for example on
26// application crash). Consider a naive way to save an important file F:
27//
28// 1. Open F for writing, truncating it.
29// 2. Write new data to F.
30//
31// It's good when it works, but it gets very bad if step 2. doesn't complete.
32// It can be caused by a crash, a computer hang, or a weird I/O error. And you
33// end up with a broken file.
34//
35// To be safe, we don't start with writing directly to F. Instead, we write to
36// to a temporary file. Only after that write is successful, we rename the
37// temporary file to target filename.
38//
39// If you want to know more about this approach and ext3/ext4 fsync issues, see
avif9cc1eea62016-03-21 17:04:2240// http://blog.valerieaurora.org/2009/04/16/dont-panic-fsync-ext34-and-your-data/
[email protected]43486252012-10-24 16:33:3641class BASE_EXPORT ImportantFileWriter : public NonThreadSafe {
[email protected]6faa0e0d2009-04-28 06:50:3642 public:
[email protected]6c1164042009-05-08 14:41:0843 // Used by ScheduleSave to lazily provide the data to be saved. Allows us
44 // to also batch data serializations.
[email protected]f59f33e2012-11-01 12:05:2745 class BASE_EXPORT DataSerializer {
[email protected]6c1164042009-05-08 14:41:0846 public:
[email protected]6c1164042009-05-08 14:41:0847 // Should put serialized string in |data| and return true on successful
48 // serialization. Will be called on the same thread on which
49 // ImportantFileWriter has been created.
50 virtual bool SerializeData(std::string* data) = 0;
[email protected]512d03f2012-06-26 01:06:0651
52 protected:
53 virtual ~DataSerializer() {}
[email protected]6c1164042009-05-08 14:41:0854 };
55
[email protected]95b42e22012-11-29 14:00:1256 // Save |data| to |path| in an atomic manner (see the class comment above).
57 // Blocks and writes data on the current thread.
bcwhitebe133b52016-04-11 20:03:3858 static bool WriteFileAtomically(const FilePath& path, StringPiece data);
[email protected]95b42e22012-11-29 14:00:1259
[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.
thestig1433edb2015-08-06 21:45:2765 ImportantFileWriter(const FilePath& path,
vmpstr82b0c16d2016-03-18 19:17:2866 scoped_refptr<SequencedTaskRunner> task_runner);
thestig1433edb2015-08-06 21:45:2767
68 // Same as above, but with a custom commit interval.
69 ImportantFileWriter(const FilePath& path,
vmpstr82b0c16d2016-03-18 19:17:2870 scoped_refptr<SequencedTaskRunner> task_runner,
thestig1433edb2015-08-06 21:45:2771 TimeDelta interval);
[email protected]6faa0e0d2009-04-28 06:50:3672
[email protected]6c1164042009-05-08 14:41:0873 // You have to ensure that there are no pending writes at the moment
74 // of destruction.
[email protected]6faa0e0d2009-04-28 06:50:3675 ~ImportantFileWriter();
76
[email protected]a83d42292010-08-17 22:51:1077 const FilePath& path() const { return path_; }
[email protected]6faa0e0d2009-04-28 06:50:3678
[email protected]6c1164042009-05-08 14:41:0879 // Returns true if there is a scheduled write pending which has not yet
80 // been started.
81 bool HasPendingWrite() const;
82
[email protected]6faa0e0d2009-04-28 06:50:3683 // Save |data| to target filename. Does not block. If there is a pending write
thestig1433edb2015-08-06 21:45:2784 // scheduled by ScheduleWrite(), it is cancelled.
dcheng093de9b2016-04-04 21:25:5185 void WriteNow(std::unique_ptr<std::string> data);
[email protected]6faa0e0d2009-04-28 06:50:3686
[email protected]6c1164042009-05-08 14:41:0887 // Schedule a save to target filename. Data will be serialized and saved
88 // to disk after the commit interval. If another ScheduleWrite is issued
89 // before that, only one serialization and write to disk will happen, and
90 // the most recent |serializer| will be used. This operation does not block.
91 // |serializer| should remain valid through the lifetime of
92 // ImportantFileWriter.
93 void ScheduleWrite(DataSerializer* serializer);
94
95 // Serialize data pending to be saved and execute write on backend thread.
96 void DoScheduledWrite();
[email protected]6faa0e0d2009-04-28 06:50:3697
[email protected]e33c9512014-05-12 02:24:1398 // Registers |on_next_successful_write| to be called once, on the next
99 // successful write event. Only one callback can be set at once.
100 void RegisterOnNextSuccessfulWriteCallback(
thestig1433edb2015-08-06 21:45:27101 const Closure& on_next_successful_write);
[email protected]e33c9512014-05-12 02:24:13102
[email protected]43486252012-10-24 16:33:36103 TimeDelta commit_interval() const {
[email protected]6faa0e0d2009-04-28 06:50:36104 return commit_interval_;
105 }
106
[email protected]6faa0e0d2009-04-28 06:50:36107 private:
[email protected]e33c9512014-05-12 02:24:13108 // Helper method for WriteNow().
hashimoto1ad9bfe2015-04-28 05:27:19109 bool PostWriteTask(const Callback<bool()>& task);
[email protected]e33c9512014-05-12 02:24:13110
111 // If |result| is true and |on_next_successful_write_| is set, invokes
112 // |on_successful_write_| and then resets it; no-ops otherwise.
113 void ForwardSuccessfulWrite(bool result);
114
115 // Invoked once and then reset on the next successful write event.
thestig1433edb2015-08-06 21:45:27116 Closure on_next_successful_write_;
[email protected]e33c9512014-05-12 02:24:13117
[email protected]6faa0e0d2009-04-28 06:50:36118 // Path being written to.
119 const FilePath path_;
120
[email protected]0de615a2012-11-08 04:40:59121 // TaskRunner for the thread on which file I/O can be done.
thestig1433edb2015-08-06 21:45:27122 const scoped_refptr<SequencedTaskRunner> task_runner_;
[email protected]6658ca82010-05-20 18:20:29123
[email protected]6faa0e0d2009-04-28 06:50:36124 // Timer used to schedule commit after ScheduleWrite.
danakj8c3eb802015-09-24 07:53:00125 OneShotTimer timer_;
[email protected]6faa0e0d2009-04-28 06:50:36126
[email protected]6c1164042009-05-08 14:41:08127 // Serializer which will provide the data to be saved.
128 DataSerializer* serializer_;
[email protected]6faa0e0d2009-04-28 06:50:36129
130 // Time delta after which scheduled data will be written to disk.
thestig1433edb2015-08-06 21:45:27131 const TimeDelta commit_interval_;
[email protected]6faa0e0d2009-04-28 06:50:36132
[email protected]e33c9512014-05-12 02:24:13133 WeakPtrFactory<ImportantFileWriter> weak_factory_;
134
[email protected]6faa0e0d2009-04-28 06:50:36135 DISALLOW_COPY_AND_ASSIGN(ImportantFileWriter);
136};
137
[email protected]43486252012-10-24 16:33:36138} // namespace base
139
140#endif // BASE_FILES_IMPORTANT_FILE_WRITER_H_