blob: 9bc8f07cfe4b9f8c323cb8694cdaf7c7ecff4b07 [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"
12#include "base/file_path.h"
[email protected]3b63f8f42011-03-28 01:54:1513#include "base/memory/ref_counted.h"
[email protected]c9177502011-01-01 04:48:4914#include "base/threading/non_thread_safe.h"
[email protected]6faa0e0d2009-04-28 06:50:3615#include "base/time.h"
16#include "base/timer.h"
17
18namespace base {
[email protected]43486252012-10-24 16:33:3619
[email protected]0de615a2012-11-08 04:40:5920class SequencedTaskRunner;
[email protected]6faa0e0d2009-04-28 06:50:3621class Thread;
[email protected]6faa0e0d2009-04-28 06:50:3622
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:
25//
26// 1. Open F for writing, truncating it.
27// 2. Write new data to F.
28//
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.
32//
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.
36//
37// If you want to know more about this approach and ext3/ext4 fsync issues, see
38// http://valhenson.livejournal.com/37921.html
[email protected]43486252012-10-24 16:33:3639class BASE_EXPORT ImportantFileWriter : public NonThreadSafe {
[email protected]6faa0e0d2009-04-28 06:50:3640 public:
[email protected]6c1164042009-05-08 14:41:0841 // Used by ScheduleSave to lazily provide the data to be saved. Allows us
42 // to also batch data serializations.
[email protected]f59f33e2012-11-01 12:05:2743 class BASE_EXPORT DataSerializer {
[email protected]6c1164042009-05-08 14:41:0844 public:
[email protected]6c1164042009-05-08 14:41:0845 // 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;
[email protected]512d03f2012-06-26 01:06:0649
50 protected:
51 virtual ~DataSerializer() {}
[email protected]6c1164042009-05-08 14:41:0852 };
53
[email protected]95b42e22012-11-29 14:00:1254 // Save |data| to |path| in an atomic manner (see the class comment above).
55 // Blocks and writes data on the current thread.
56 static bool WriteFileAtomically(const FilePath& path,
57 const std::string& data);
58
[email protected]6faa0e0d2009-04-28 06:50:3659 // Initialize the writer.
[email protected]6fad2632009-11-02 05:59:3760 // |path| is the name of file to write.
[email protected]0de615a2012-11-08 04:40:5961 // |task_runner| is the SequencedTaskRunner instance where on which we will
62 // execute file I/O operations.
[email protected]6faa0e0d2009-04-28 06:50:3663 // All non-const methods, ctor and dtor must be called on the same thread.
[email protected]6658ca82010-05-20 18:20:2964 ImportantFileWriter(const FilePath& path,
[email protected]0de615a2012-11-08 04:40:5965 base::SequencedTaskRunner* task_runner);
[email protected]6faa0e0d2009-04-28 06:50:3666
[email protected]6c1164042009-05-08 14:41:0867 // You have to ensure that there are no pending writes at the moment
68 // of destruction.
[email protected]6faa0e0d2009-04-28 06:50:3669 ~ImportantFileWriter();
70
[email protected]a83d42292010-08-17 22:51:1071 const FilePath& path() const { return path_; }
[email protected]6faa0e0d2009-04-28 06:50:3672
[email protected]6c1164042009-05-08 14:41:0873 // Returns true if there is a scheduled write pending which has not yet
74 // been started.
75 bool HasPendingWrite() const;
76
[email protected]6faa0e0d2009-04-28 06:50:3677 // Save |data| to target filename. Does not block. If there is a pending write
78 // scheduled by ScheduleWrite, it is cancelled.
79 void WriteNow(const std::string& data);
80
[email protected]6c1164042009-05-08 14:41:0881 // Schedule a save to target filename. Data will be serialized and saved
82 // to disk after the commit interval. If another ScheduleWrite is issued
83 // before that, only one serialization and write to disk will happen, and
84 // the most recent |serializer| will be used. This operation does not block.
85 // |serializer| should remain valid through the lifetime of
86 // ImportantFileWriter.
87 void ScheduleWrite(DataSerializer* serializer);
88
89 // Serialize data pending to be saved and execute write on backend thread.
90 void DoScheduledWrite();
[email protected]6faa0e0d2009-04-28 06:50:3691
[email protected]43486252012-10-24 16:33:3692 TimeDelta commit_interval() const {
[email protected]6faa0e0d2009-04-28 06:50:3693 return commit_interval_;
94 }
95
[email protected]43486252012-10-24 16:33:3696 void set_commit_interval(const TimeDelta& interval) {
[email protected]6faa0e0d2009-04-28 06:50:3697 commit_interval_ = interval;
98 }
99
100 private:
[email protected]6faa0e0d2009-04-28 06:50:36101 // Path being written to.
102 const FilePath path_;
103
[email protected]0de615a2012-11-08 04:40:59104 // TaskRunner for the thread on which file I/O can be done.
105 const scoped_refptr<base::SequencedTaskRunner> task_runner_;
[email protected]6658ca82010-05-20 18:20:29106
[email protected]6faa0e0d2009-04-28 06:50:36107 // Timer used to schedule commit after ScheduleWrite.
[email protected]43486252012-10-24 16:33:36108 OneShotTimer<ImportantFileWriter> timer_;
[email protected]6faa0e0d2009-04-28 06:50:36109
[email protected]6c1164042009-05-08 14:41:08110 // Serializer which will provide the data to be saved.
111 DataSerializer* serializer_;
[email protected]6faa0e0d2009-04-28 06:50:36112
113 // Time delta after which scheduled data will be written to disk.
[email protected]43486252012-10-24 16:33:36114 TimeDelta commit_interval_;
[email protected]6faa0e0d2009-04-28 06:50:36115
116 DISALLOW_COPY_AND_ASSIGN(ImportantFileWriter);
117};
118
[email protected]43486252012-10-24 16:33:36119} // namespace base
120
121#endif // BASE_FILES_IMPORTANT_FILE_WRITER_H_