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 // For 64-bit file access (off_t = off64_t, lseek64, etc).
6 #define _FILE_OFFSET_BITS 64
8 #include "net/base/file_stream_context.h"
13 #include <sys/types.h>
16 #include "base/basictypes.h"
17 #include "base/bind.h"
18 #include "base/bind_helpers.h"
19 #include "base/callback.h"
20 #include "base/files/file_path.h"
21 #include "base/location.h"
22 #include "base/logging.h"
23 #include "base/metrics/histogram.h"
24 #include "base/posix/eintr_wrapper.h"
25 #include "base/task_runner_util.h"
26 #include "net/base/io_buffer.h"
27 #include "net/base/net_errors.h"
29 #if defined(OS_ANDROID)
30 // Android's bionic libc only supports the LFS transitional API.
39 // We cast back and forth, so make sure it's the size we're expecting.
40 COMPILE_ASSERT(sizeof(int64
) == sizeof(off_t
), off_t_64_bit
);
42 // Make sure our Whence mappings match the system headers.
43 COMPILE_ASSERT(FROM_BEGIN
== SEEK_SET
&&
44 FROM_CURRENT
== SEEK_CUR
&&
45 FROM_END
== SEEK_END
, whence_matches_system
);
47 FileStream::Context::Context(const BoundNetLog
& bound_net_log
,
48 const scoped_refptr
<base::TaskRunner
>& task_runner
)
49 : file_(base::kInvalidPlatformFileValue
),
51 async_in_progress_(false),
53 bound_net_log_(bound_net_log
),
54 task_runner_(task_runner
) {
57 FileStream::Context::Context(base::PlatformFile file
,
58 const BoundNetLog
& bound_net_log
,
60 const scoped_refptr
<base::TaskRunner
>& task_runner
)
63 async_in_progress_(false),
65 bound_net_log_(bound_net_log
),
66 task_runner_(task_runner
) {
69 FileStream::Context::~Context() {
72 int64
FileStream::Context::GetFileSize() const {
74 if (fstat(file_
, &info
) != 0) {
75 IOResult result
= IOResult::FromOSError(errno
);
76 RecordError(result
, FILE_ERROR_SOURCE_GET_SIZE
);
80 return static_cast<int64
>(info
.st_size
);
83 int FileStream::Context::ReadAsync(IOBuffer
* in_buf
,
85 const CompletionCallback
& callback
) {
86 DCHECK(!async_in_progress_
);
88 scoped_refptr
<IOBuffer
> buf
= in_buf
;
89 const bool posted
= base::PostTaskAndReplyWithResult(
92 base::Bind(&Context::ReadFileImpl
, base::Unretained(this), buf
, buf_len
),
93 base::Bind(&Context::ProcessAsyncResult
,
94 base::Unretained(this),
96 FILE_ERROR_SOURCE_READ
));
99 async_in_progress_
= true;
100 return ERR_IO_PENDING
;
103 int FileStream::Context::ReadSync(char* in_buf
, int buf_len
) {
104 scoped_refptr
<IOBuffer
> buf
= new WrappedIOBuffer(in_buf
);
105 IOResult result
= ReadFileImpl(buf
, buf_len
);
106 RecordError(result
, FILE_ERROR_SOURCE_READ
);
107 return result
.result
;
110 int FileStream::Context::WriteAsync(IOBuffer
* in_buf
,
112 const CompletionCallback
& callback
) {
113 DCHECK(!async_in_progress_
);
115 scoped_refptr
<IOBuffer
> buf
= in_buf
;
116 const bool posted
= base::PostTaskAndReplyWithResult(
119 base::Bind(&Context::WriteFileImpl
, base::Unretained(this), buf
, buf_len
),
120 base::Bind(&Context::ProcessAsyncResult
,
121 base::Unretained(this),
122 IntToInt64(callback
),
123 FILE_ERROR_SOURCE_WRITE
));
126 async_in_progress_
= true;
127 return ERR_IO_PENDING
;
130 int FileStream::Context::WriteSync(const char* in_buf
, int buf_len
) {
131 scoped_refptr
<IOBuffer
> buf
= new WrappedIOBuffer(in_buf
);
132 IOResult result
= WriteFileImpl(buf
, buf_len
);
133 RecordError(result
, FILE_ERROR_SOURCE_WRITE
);
134 return result
.result
;
137 int FileStream::Context::Truncate(int64 bytes
) {
138 if (ftruncate(file_
, bytes
) != 0) {
139 IOResult result
= IOResult::FromOSError(errno
);
140 RecordError(result
, FILE_ERROR_SOURCE_SET_EOF
);
141 return result
.result
;
147 FileStream::Context::IOResult
FileStream::Context::SeekFileImpl(Whence whence
,
149 off_t res
= lseek(file_
, static_cast<off_t
>(offset
),
150 static_cast<int>(whence
));
151 if (res
== static_cast<off_t
>(-1))
152 return IOResult::FromOSError(errno
);
154 return IOResult(res
, 0);
157 FileStream::Context::IOResult
FileStream::Context::FlushFileImpl() {
158 ssize_t res
= HANDLE_EINTR(fsync(file_
));
160 return IOResult::FromOSError(errno
);
162 return IOResult(res
, 0);
165 FileStream::Context::IOResult
FileStream::Context::ReadFileImpl(
166 scoped_refptr
<IOBuffer
> buf
,
168 // Loop in the case of getting interrupted by a signal.
169 ssize_t res
= HANDLE_EINTR(read(file_
, buf
->data(),
170 static_cast<size_t>(buf_len
)));
172 return IOResult::FromOSError(errno
);
174 return IOResult(res
, 0);
177 FileStream::Context::IOResult
FileStream::Context::WriteFileImpl(
178 scoped_refptr
<IOBuffer
> buf
,
180 ssize_t res
= HANDLE_EINTR(write(file_
, buf
->data(), buf_len
));
182 return IOResult::FromOSError(errno
);
184 return IOResult(res
, 0);
187 FileStream::Context::IOResult
FileStream::Context::CloseFileImpl() {
188 bool success
= base::ClosePlatformFile(file_
);
189 file_
= base::kInvalidPlatformFileValue
;
191 return IOResult::FromOSError(errno
);
193 return IOResult(OK
, 0);