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