Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(5)

Side by Side Diff: util/file/file_writer.h

Issue 936153002: Add FileReaderInterface. Move StringFileWriter to StringFile and (Closed) Base URL: https://chromium.googlesource.com/crashpad/crashpad@master
Patch Set: Remove unused #include Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « util/file/file_seeker.h ('k') | util/file/file_writer.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Crashpad Authors. All rights reserved. 1 // Copyright 2014 The Crashpad Authors. All rights reserved.
2 // 2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); 3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License. 4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at 5 // You may obtain a copy of the License at
6 // 6 //
7 // http://www.apache.org/licenses/LICENSE-2.0 7 // http://www.apache.org/licenses/LICENSE-2.0
8 // 8 //
9 // Unless required by applicable law or agreed to in writing, software 9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, 10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and 12 // See the License for the specific language governing permissions and
13 // limitations under the License. 13 // limitations under the License.
14 14
15 #ifndef CRASHPAD_UTIL_FILE_FILE_WRITER_H_ 15 #ifndef CRASHPAD_UTIL_FILE_FILE_WRITER_H_
16 #define CRASHPAD_UTIL_FILE_FILE_WRITER_H_ 16 #define CRASHPAD_UTIL_FILE_FILE_WRITER_H_
17 17
18 #include <fcntl.h> 18 #include <sys/types.h>
19 #include <stddef.h>
20 19
21 #include <string>
22 #include <vector> 20 #include <vector>
23 21
24 #include "base/basictypes.h" 22 #include "base/basictypes.h"
25 #include "base/files/file_path.h" 23 #include "base/files/file_path.h"
26 #include "build/build_config.h"
27 #include "util/file/file_io.h" 24 #include "util/file/file_io.h"
25 #include "util/file/file_seeker.h"
28 26
29 namespace crashpad { 27 namespace crashpad {
30 28
31 //! \brief A version of `iovec` with a `const` #iov_base field. 29 //! \brief A version of `iovec` with a `const` #iov_base field.
32 //! 30 //!
33 //! This structure is intended to be used for write operations. 31 //! This structure is intended to be used for write operations.
34 // 32 //
35 // Type compatibility with iovec is tested with static assertions in the 33 // Type compatibility with iovec is tested with static assertions in the
36 // implementation file. 34 // implementation file.
37 struct WritableIoVec { 35 struct WritableIoVec {
38 //! \brief The base address of a memory region for output. 36 //! \brief The base address of a memory region for output.
39 const void* iov_base; 37 const void* iov_base;
40 38
41 //! \brief The size of the memory pointed to by #iov_base. 39 //! \brief The size of the memory pointed to by #iov_base.
42 size_t iov_len; 40 size_t iov_len;
43 }; 41 };
44 42
45 //! \brief An interface to write to files and other file-like objects with 43 //! \brief An interface to write to files and other file-like objects with
46 //! semantics matching the underlying platform (POSIX or Windows). 44 //! semantics matching the underlying platform (POSIX or Windows).
47 class FileWriterInterface { 45 class FileWriterInterface : public FileSeekerInterface {
48 public: 46 public:
49 //! \brief Wraps WriteFile(), or provides an implementation with identical 47 //! \brief Wraps LoggingWriteFile(), or provides an implementation with
50 //! semantics. 48 //! identical semantics.
51 //! 49 //!
52 //! \return `true` if the operation succeeded, `false` if it failed, with an 50 //! \return `true` if the operation succeeded, `false` if it failed, with an
53 //! error message logged. 51 //! error message logged.
54 virtual bool Write(const void* data, size_t size) = 0; 52 virtual bool Write(const void* data, size_t size) = 0;
55 53
56 //! \brief Wraps `writev()` on POSIX or provides an alternate implementation 54 //! \brief Wraps `writev()` on POSIX or provides an alternate implementation
57 //! with identical semantics. This method will write entire buffers, 55 //! with identical semantics. This method will write entire buffers,
58 //! continuing after a short write or after being interrupted. On 56 //! continuing after a short write or after being interrupted. On
59 //! non-POSIX this is a simple wrapper around Write(). 57 //! non-POSIX this is a simple wrapper around Write().
60 //! 58 //!
61 //! \return `true` if the operation succeeded, `false` if it failed, with an 59 //! \return `true` if the operation succeeded, `false` if it failed, with an
62 //! error message logged. 60 //! error message logged.
63 //! 61 //!
64 //! \note The contents of \a iovecs are undefined when this method returns. 62 //! \note The contents of \a iovecs are undefined when this method returns.
65 virtual bool WriteIoVec(std::vector<WritableIoVec>* iovecs) = 0; 63 virtual bool WriteIoVec(std::vector<WritableIoVec>* iovecs) = 0;
66 64
67 //! \brief Wraps LoggingFileSeek() or provides an alternate implementation
68 //! with identical semantics.
69 //!
70 //! \return The return value of LoggingFileSeek(). `-1` on failure,
71 //! with an error message logged.
72 virtual FileOffset Seek(FileOffset offset, int whence) = 0;
73
74 protected: 65 protected:
75 ~FileWriterInterface() {} 66 ~FileWriterInterface() {}
76 }; 67 };
77 68
78 //! \brief A file writer backed by a FileHandle. 69 //! \brief A file writer backed by a FileHandle.
79 //! 70 //!
80 //! FileWriter requires users to provide a FilePath to open, but this class 71 //! FileWriter requires users to provide a FilePath to open, but this class
81 //! accepts an already-open FileHandle instead. Like FileWriter, this class may 72 //! accepts an already-open FileHandle instead. Like FileWriter, this class may
82 //! write to a filesystem-based file, but unlike FileWriter, this class is not 73 //! write to a filesystem-based file, but unlike FileWriter, this class is not
83 //! responsible for creating or closing the file. Users of this class must 74 //! responsible for creating or closing the file. Users of this class must
84 //! ensure that the file handle is closed appropriately elsewhere. Objects of 75 //! ensure that the file handle is closed appropriately elsewhere. Objects of
85 //! this class may be used to write to file handles not associated with 76 //! this class may be used to write to file handles not associated with
86 //! filesystem-based files, although special attention should be paid to the 77 //! filesystem-based files, although special attention should be paid to the
87 //! Seek() method, which may not function on file handles that do not refer to 78 //! Seek() method, which may not function on file handles that do not refer to
88 //! disk-based files. 79 //! disk-based files.
89 //! 80 //!
90 //! This class is expected to be used when other code is responsible for 81 //! This class is expected to be used when other code is responsible for
91 //! creating files and already provides file handles. 82 //! creating files and already provides file handles.
92 class WeakFileHandleFileWriter : public FileWriterInterface { 83 class WeakFileHandleFileWriter : public FileWriterInterface {
93 public: 84 public:
94 explicit WeakFileHandleFileWriter(FileHandle file_handle); 85 explicit WeakFileHandleFileWriter(FileHandle file_handle);
95 ~WeakFileHandleFileWriter(); 86 ~WeakFileHandleFileWriter();
96 87
97 // FileWriterInterface: 88 // FileWriterInterface:
98 bool Write(const void* data, size_t size) override; 89 bool Write(const void* data, size_t size) override;
99 bool WriteIoVec(std::vector<WritableIoVec>* iovecs) override; 90 bool WriteIoVec(std::vector<WritableIoVec>* iovecs) override;
100 91
92 // FileSeekerInterface:
93
101 //! \copydoc FileWriterInterface::Seek() 94 //! \copydoc FileWriterInterface::Seek()
102 //! 95 //!
103 //! \note This method is only guaranteed to function on file handles referring 96 //! \note This method is only guaranteed to function on file handles referring
104 //! to disk-based files. 97 //! to disk-based files.
105 FileOffset Seek(FileOffset offset, int whence) override; 98 FileOffset Seek(FileOffset offset, int whence) override;
106 99
107 private: 100 private:
108 void set_file_handle(FileHandle file_handle) { file_handle_ = file_handle; } 101 void set_file_handle(FileHandle file_handle) { file_handle_ = file_handle; }
109 102
110 FileHandle file_handle_; // weak 103 FileHandle file_handle_; // weak
111 104
112 // FileWriter uses this class as its internal implementation, and it needs to 105 // FileWriter uses this class as its internal implementation, and it needs to
113 // be able to call set_file_handle(). FileWriter cannot initialize an 106 // be able to call set_file_handle(). FileWriter cannot initialize a
114 // WeakFileHandleFileWriter with a correct file descriptor at the time of 107 // WeakFileHandleFileWriter with a correct file descriptor at the time of
115 // construction because no file descriptor will be available until 108 // construction because no file descriptor will be available until
116 // FileWriter::Open() is called. 109 // FileWriter::Open() is called.
117 friend class FileWriter; 110 friend class FileWriter;
118 111
119 DISALLOW_COPY_AND_ASSIGN(WeakFileHandleFileWriter); 112 DISALLOW_COPY_AND_ASSIGN(WeakFileHandleFileWriter);
120 }; 113 };
121 114
122 //! \brief A file writer implementation that wraps traditional system file 115 //! \brief A file writer implementation that wraps traditional system file
123 //! operations on files accessed through the filesystem. 116 //! operations on files accessed through the filesystem.
124 class FileWriter : public FileWriterInterface { 117 class FileWriter : public FileWriterInterface {
125 public: 118 public:
126 FileWriter(); 119 FileWriter();
127 ~FileWriter(); 120 ~FileWriter();
128 121
122 // FileWriterInterface:
123
129 //! \brief Wraps LoggingOpenFileForWrite(). 124 //! \brief Wraps LoggingOpenFileForWrite().
130 //! 125 //!
131 //! \return `true` if the operation succeeded, `false` if it failed, with an 126 //! \return `true` if the operation succeeded, `false` if it failed, with an
132 //! error message logged. 127 //! error message logged.
133 //! 128 //!
134 //! \note After a successful call, this method cannot be called again until 129 //! \note After a successful call, this method cannot be called again until
135 //! after Close(). 130 //! after Close().
136 bool Open(const base::FilePath& path, 131 bool Open(const base::FilePath& path,
137 FileWriteMode write_mode, 132 FileWriteMode write_mode,
138 FilePermissions permissions); 133 FilePermissions permissions);
(...skipping 12 matching lines...) Expand all
151 //! \note It is only valid to call this method between a successful Open() and 146 //! \note It is only valid to call this method between a successful Open() and
152 //! a Close(). 147 //! a Close().
153 bool Write(const void* data, size_t size) override; 148 bool Write(const void* data, size_t size) override;
154 149
155 //! \copydoc FileWriterInterface::WriteIoVec() 150 //! \copydoc FileWriterInterface::WriteIoVec()
156 //! 151 //!
157 //! \note It is only valid to call this method between a successful Open() and 152 //! \note It is only valid to call this method between a successful Open() and
158 //! a Close(). 153 //! a Close().
159 bool WriteIoVec(std::vector<WritableIoVec>* iovecs) override; 154 bool WriteIoVec(std::vector<WritableIoVec>* iovecs) override;
160 155
156 // FileSeekerInterface:
157
161 //! \copydoc FileWriterInterface::Seek() 158 //! \copydoc FileWriterInterface::Seek()
162 //! 159 //!
163 //! \note It is only valid to call this method between a successful Open() and 160 //! \note It is only valid to call this method between a successful Open() and
164 //! a Close(). 161 //! a Close().
165 FileOffset Seek(FileOffset offset, int whence) override; 162 FileOffset Seek(FileOffset offset, int whence) override;
166 163
167 private: 164 private:
168 ScopedFileHandle file_; 165 ScopedFileHandle file_;
169 WeakFileHandleFileWriter weak_file_handle_file_writer_; 166 WeakFileHandleFileWriter weak_file_handle_file_writer_;
170 167
171 DISALLOW_COPY_AND_ASSIGN(FileWriter); 168 DISALLOW_COPY_AND_ASSIGN(FileWriter);
172 }; 169 };
173 170
174 } // namespace crashpad 171 } // namespace crashpad
175 172
176 #endif // CRASHPAD_UTIL_FILE_FILE_WRITER_H_ 173 #endif // CRASHPAD_UTIL_FILE_FILE_WRITER_H_
OLDNEW
« no previous file with comments | « util/file/file_seeker.h ('k') | util/file/file_writer.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698