OLD | NEW |
(Empty) | |
| 1 // 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 |
| 16 namespace base { |
| 17 class 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 |
| 36 class ImportantFileWriter : public NonThreadSafe { |
| 37 public: |
| 38 // Initialize the writer. |
| 39 // |path| is the name of file to write. Disk operations will be executed on |
| 40 // |backend_thread|, or current thread if |backend_thread| is NULL. |
| 41 // |
| 42 // All non-const methods, ctor and dtor must be called on the same thread. |
| 43 ImportantFileWriter(const FilePath& path, const base::Thread* backend_thread); |
| 44 |
| 45 // On destruction all pending writes are executed on |backend_thread|. |
| 46 ~ImportantFileWriter(); |
| 47 |
| 48 FilePath path() const { return path_; } |
| 49 |
| 50 // Save |data| to target filename. Does not block. If there is a pending write |
| 51 // scheduled by ScheduleWrite, it is cancelled. |
| 52 void WriteNow(const std::string& data); |
| 53 |
| 54 // Schedule saving |data| to target filename. Data will be saved to disk after |
| 55 // the commit interval. If an other ScheduleWrite is issued before that, only |
| 56 // one write to disk will happen - with the more recent data. This operation |
| 57 // does not block. |
| 58 void ScheduleWrite(const std::string& data); |
| 59 |
| 60 base::TimeDelta commit_interval() const { |
| 61 return commit_interval_; |
| 62 } |
| 63 |
| 64 void set_commit_interval(const base::TimeDelta& interval) { |
| 65 commit_interval_ = interval; |
| 66 } |
| 67 |
| 68 private: |
| 69 // If there is a data scheduled to write, issue a disk operation. |
| 70 void CommitPendingData(); |
| 71 |
| 72 // Path being written to. |
| 73 const FilePath path_; |
| 74 |
| 75 // Thread on which disk operation run. NULL means no separate thread is used. |
| 76 const base::Thread* backend_thread_; |
| 77 |
| 78 // Timer used to schedule commit after ScheduleWrite. |
| 79 base::OneShotTimer<ImportantFileWriter> timer_; |
| 80 |
| 81 // Data scheduled to save. |
| 82 std::string data_; |
| 83 |
| 84 // Time delta after which scheduled data will be written to disk. |
| 85 base::TimeDelta commit_interval_; |
| 86 |
| 87 DISALLOW_COPY_AND_ASSIGN(ImportantFileWriter); |
| 88 }; |
| 89 |
| 90 #endif // CHROME_COMMON_IMPORTANT_FILE_WRITER_H_ |
OLD | NEW |