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
3 // found in the LICENSE file.
5 // This file defines FileStream, a basic interface for reading and writing files
6 // synchronously or asynchronously with support for seeking to an offset.
7 // Note that even when used asynchronously, only one operation is supported at
10 #ifndef NET_BASE_FILE_STREAM_H_
11 #define NET_BASE_FILE_STREAM_H_
14 #include "base/platform_file.h"
15 #include "net/base/completion_callback.h"
16 #include "net/base/file_stream_whence.h"
17 #include "net/base/net_export.h"
18 #include "net/base/net_log.h"
20 #include "net/base/file_stream_win.h"
21 #elif defined(OS_POSIX)
22 #include "net/base/file_stream_posix.h"
31 class NET_EXPORT FileStream
{
33 // Creates a |FileStream| with a new |BoundNetLog| (based on |net_log|)
34 // attached. |net_log| may be NULL if no logging is needed.
35 explicit FileStream(net::NetLog
* net_log
);
37 // Construct a FileStream with an existing file handle and opening flags.
38 // |file| is valid file handle.
39 // |flags| is a bitfield of base::PlatformFileFlags when the file handle was
41 // |net_log| is the net log pointer to use to create a |BoundNetLog|. May be
42 // NULL if logging is not needed.
43 // The already opened file will not be automatically closed when FileStream
45 FileStream(base::PlatformFile file
, int flags
, net::NetLog
* net_log
);
47 // If the file stream was opened with Open() or OpenSync(), the underlying
48 // file will be closed automatically by the destructor, if not closed
50 virtual ~FileStream();
52 // Call this method to close the FileStream, which was previously opened in
53 // the async mode (PLATFORM_FILE_ASYNC) asynchronously.
55 // Once the operation is done, |callback| will be run on the thread where
56 // Close() was called, with OK (i.e. an error is not propagated just like
57 // CloseSync() does not).
59 // It is not OK to call Close() multiple times. The behavior is not defined.
60 // Note that there must never be any pending async operations.
61 virtual void Close(const CompletionCallback
& callback
);
63 // Call this method to close the FileStream synchronously.
64 // It is OK to call CloseSync() multiple times. Redundant calls are
65 // ignored. Note that if there are any pending async operations, they'll
67 virtual void CloseSync();
69 // Call this method to open the FileStream asynchronously. The remaining
70 // methods cannot be used unless the file is opened successfully. Returns
71 // ERR_IO_PENDING if the operation is started. If the operation cannot be
72 // started then an error code is returned.
74 // Once the operation is done, |callback| will be run on the thread where
75 // Open() was called, with the result code. open_flags is a bitfield of
76 // base::PlatformFileFlags.
78 // If the file stream is not closed manually, the underlying file will be
79 // automatically closed when FileStream is destructed in an asynchronous
80 // manner (i.e. the file stream is closed in the background but you don't
82 virtual int Open(const FilePath
& path
, int open_flags
,
83 const CompletionCallback
& callback
);
85 // Call this method to open the FileStream synchronously.
86 // The remaining methods cannot be used unless this method returns OK. If
87 // the file cannot be opened then an error code is returned. open_flags is
88 // a bitfield of base::PlatformFileFlags
90 // If the file stream is not closed manually, the underlying file will be
91 // automatically closed when FileStream is destructed.
92 virtual int OpenSync(const FilePath
& path
, int open_flags
);
94 // Returns true if Open succeeded and Close has not been called.
95 virtual bool IsOpen() const;
97 // Adjust the position from where data is read asynchronously.
98 // Upon success, ERR_IO_PENDING is returned and |callback| will be run
99 // on the thread where Seek() was called with the the stream position
100 // relative to the start of the file. Otherwise, an error code is returned.
101 // It is invalid to request any asynchronous operations while there is an
102 // in-flight asynchronous operation.
103 virtual int Seek(Whence whence
, int64 offset
,
104 const Int64CompletionCallback
& callback
);
106 // Adjust the position from where data is read synchronously.
107 // Upon success, the stream position relative to the start of the file is
108 // returned. Otherwise, an error code is returned. It is not valid to
109 // call SeekSync while a Read call has a pending completion.
110 virtual int64
SeekSync(Whence whence
, int64 offset
);
112 // Returns the number of bytes available to read from the current stream
113 // position until the end of the file. Otherwise, an error code is returned.
114 virtual int64
Available();
116 // Call this method to read data from the current stream position
117 // asynchronously. Up to buf_len bytes will be copied into buf. (In
118 // other words, partial reads are allowed.) Returns the number of bytes
119 // copied, 0 if at end-of-file, or an error code if the operation could
122 // The file must be opened with PLATFORM_FILE_ASYNC, and a non-null
123 // callback must be passed to this method. If the read could not
124 // complete synchronously, then ERR_IO_PENDING is returned, and the
125 // callback will be run on the thread where Read() was called, when the
126 // read has completed.
128 // It is valid to destroy or close the file stream while there is an
129 // asynchronous read in progress. That will cancel the read and allow
130 // the buffer to be freed.
132 // It is invalid to request any asynchronous operations while there is an
133 // in-flight asynchronous operation.
135 // This method must not be called if the stream was opened WRITE_ONLY.
136 virtual int Read(IOBuffer
* buf
, int buf_len
,
137 const CompletionCallback
& callback
);
139 // Call this method to read data from the current stream position
140 // synchronously. Up to buf_len bytes will be copied into buf. (In
141 // other words, partial reads are allowed.) Returns the number of bytes
142 // copied, 0 if at end-of-file, or an error code if the operation could
145 // The file must not be opened with PLATFORM_FILE_ASYNC.
146 // This method must not be called if the stream was opened WRITE_ONLY.
147 virtual int ReadSync(char* buf
, int buf_len
);
149 // Performs the same as ReadSync, but ensures that exactly buf_len bytes
150 // are copied into buf. A partial read may occur, but only as a result of
151 // end-of-file or fatal error. Returns the number of bytes copied into buf,
152 // 0 if at end-of-file and no bytes have been read into buf yet,
153 // or an error code if the operation could not be performed.
154 virtual int ReadUntilComplete(char *buf
, int buf_len
);
156 // Call this method to write data at the current stream position
157 // asynchronously. Up to buf_len bytes will be written from buf. (In
158 // other words, partial writes are allowed.) Returns the number of
159 // bytes written, or an error code if the operation could not be
162 // The file must be opened with PLATFORM_FILE_ASYNC, and a non-null
163 // callback must be passed to this method. If the write could not
164 // complete synchronously, then ERR_IO_PENDING is returned, and the
165 // callback will be run on the thread where Write() was called when
166 // the write has completed.
168 // It is valid to destroy or close the file stream while there is an
169 // asynchronous write in progress. That will cancel the write and allow
170 // the buffer to be freed.
172 // It is invalid to request any asynchronous operations while there is an
173 // in-flight asynchronous operation.
175 // This method must not be called if the stream was opened READ_ONLY.
176 virtual int Write(IOBuffer
* buf
, int buf_len
,
177 const CompletionCallback
& callback
);
179 // Call this method to write data at the current stream position
180 // synchronously. Up to buf_len bytes will be written from buf. (In
181 // other words, partial writes are allowed.) Returns the number of
182 // bytes written, or an error code if the operation could not be
185 // The file must not be opened with PLATFORM_FILE_ASYNC.
186 // This method must not be called if the stream was opened READ_ONLY.
187 virtual int WriteSync(const char* buf
, int buf_len
);
189 // Truncates the file to be |bytes| length. This is only valid for writable
190 // files. After truncation the file stream is positioned at |bytes|. The new
191 // position is returned, or a value < 0 on error.
192 // WARNING: one may not truncate a file beyond its current length on any
193 // platform with this call.
194 virtual int64
Truncate(int64 bytes
);
196 // Forces out a filesystem sync on this file to make sure that the file was
197 // written out to disk and is not currently sitting in the buffer. This does
198 // not have to be called, it just forces one to happen at the time of
201 /// Returns an error code if the operation could not be performed.
203 // This method should not be called if the stream was opened READ_ONLY.
206 // Turns on UMA error statistics gathering.
207 void EnableErrorStatistics();
209 // Sets the source reference for net-internals logging.
210 // Creates source dependency events between |owner_bound_net_log| and
211 // |bound_net_log_|. Each gets an event showing the dependency on the other.
212 // If only one of those is valid, it gets an event showing that a change
213 // of ownership happened, but without details.
214 void SetBoundNetLogSource(const net::BoundNetLog
& owner_bound_net_log
);
216 // Returns the underlying platform file for testing.
217 base::PlatformFile
GetPlatformFileForTesting();
222 #elif defined(OS_POSIX)
223 FileStreamPosix impl_
;
226 DISALLOW_COPY_AND_ASSIGN(FileStream
);
231 #endif // NET_BASE_FILE_STREAM_H_