OLD | NEW |
1 // Copyright (c) 2009 The Chromium OS Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium OS 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 CHROMEOS_PLATFORM_UPDATE_ENGINE_FILE_WRITER_H__ | 5 #ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_FILE_WRITER_H__ |
6 #define CHROMEOS_PLATFORM_UPDATE_ENGINE_FILE_WRITER_H__ | 6 #define CHROMEOS_PLATFORM_UPDATE_ENGINE_FILE_WRITER_H__ |
7 | 7 |
8 #include <sys/types.h> | 8 #include <sys/types.h> |
9 #include <sys/stat.h> | 9 #include <sys/stat.h> |
10 #include <fcntl.h> | 10 #include <fcntl.h> |
11 #include <unistd.h> | 11 #include <unistd.h> |
12 #include "chromeos/obsolete_logging.h" | 12 #include "chromeos/obsolete_logging.h" |
13 #include "update_engine/utils.h" | 13 #include "update_engine/utils.h" |
14 | 14 |
15 // FileWriter is a class that is used to (synchronously, for now) write to | 15 // FileWriter is a class that is used to (synchronously, for now) write to |
16 // a file. This file is a thin wrapper around open/write/close system calls, | 16 // a file. This file is a thin wrapper around open/write/close system calls, |
17 // but provides and interface that can be customized by subclasses that wish | 17 // but provides and interface that can be customized by subclasses that wish |
18 // to filter the data. | 18 // to filter the data. |
19 | 19 |
20 namespace chromeos_update_engine { | 20 namespace chromeos_update_engine { |
21 | 21 |
22 class FileWriter { | 22 class FileWriter { |
23 public: | 23 public: |
| 24 FileWriter() {} |
24 virtual ~FileWriter() {} | 25 virtual ~FileWriter() {} |
25 | 26 |
26 // Wrapper around open. Returns 0 on success or -errno on error. | 27 // Wrapper around open. Returns 0 on success or -errno on error. |
27 virtual int Open(const char* path, int flags, mode_t mode) = 0; | 28 virtual int Open(const char* path, int flags, mode_t mode) = 0; |
28 | 29 |
29 // Wrapper around write. Returns bytes written on success or | 30 // Wrapper around write. Returns bytes written on success or |
30 // -errno on error. | 31 // -errno on error. |
31 virtual int Write(const void* bytes, size_t count) = 0; | 32 virtual int Write(const void* bytes, size_t count) = 0; |
32 | 33 |
33 // Wrapper around close. Returns 0 on success or -errno on error. | 34 // Wrapper around close. Returns 0 on success or -errno on error. |
34 virtual int Close() = 0; | 35 virtual int Close() = 0; |
| 36 |
| 37 private: |
| 38 DISALLOW_COPY_AND_ASSIGN(FileWriter); |
35 }; | 39 }; |
36 | 40 |
37 // Direct file writer is probably the simplest FileWriter implementation. | 41 // Direct file writer is probably the simplest FileWriter implementation. |
38 // It calls the system calls directly. | 42 // It calls the system calls directly. |
39 | 43 |
40 class DirectFileWriter : public FileWriter { | 44 class DirectFileWriter : public FileWriter { |
41 public: | 45 public: |
42 DirectFileWriter() : fd_(-1) {} | 46 DirectFileWriter() : fd_(-1) {} |
43 virtual ~DirectFileWriter() {} | 47 virtual ~DirectFileWriter() {} |
44 | 48 |
45 virtual int Open(const char* path, int flags, mode_t mode); | 49 virtual int Open(const char* path, int flags, mode_t mode); |
46 virtual int Write(const void* bytes, size_t count); | 50 virtual int Write(const void* bytes, size_t count); |
47 virtual int Close(); | 51 virtual int Close(); |
48 | 52 |
49 int fd() const { return fd_; } | 53 int fd() const { return fd_; } |
50 | 54 |
51 private: | 55 private: |
52 int fd_; | 56 int fd_; |
| 57 |
| 58 DISALLOW_COPY_AND_ASSIGN(DirectFileWriter); |
53 }; | 59 }; |
54 | 60 |
55 class ScopedFileWriterCloser { | 61 class ScopedFileWriterCloser { |
56 public: | 62 public: |
57 explicit ScopedFileWriterCloser(FileWriter* writer) : writer_(writer) {} | 63 explicit ScopedFileWriterCloser(FileWriter* writer) : writer_(writer) {} |
58 ~ScopedFileWriterCloser() { | 64 ~ScopedFileWriterCloser() { |
59 int err = writer_->Close(); | 65 int err = writer_->Close(); |
60 if (err) | 66 if (err) |
61 LOG(ERROR) << "FileWriter::Close failed: " | 67 LOG(ERROR) << "FileWriter::Close failed: " |
62 << utils::ErrnoNumberAsString(-err); | 68 << utils::ErrnoNumberAsString(-err); |
63 } | 69 } |
64 private: | 70 private: |
65 FileWriter* writer_; | 71 FileWriter* writer_; |
| 72 |
| 73 DISALLOW_COPY_AND_ASSIGN(ScopedFileWriterCloser); |
66 }; | 74 }; |
67 | 75 |
68 } // namespace chromeos_update_engine | 76 } // namespace chromeos_update_engine |
69 | 77 |
70 #endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_FILE_WRITER_H__ | 78 #endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_FILE_WRITER_H__ |
OLD | NEW |