1 /*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "file_output_stream.h"
18
19 #include <android-base/logging.h>
20 #include <sys/stat.h>
21 #include <sys/types.h>
22 #include <unistd.h>
23
24 #include "base/unix_file/fd_file.h"
25
26 namespace art {
27
FileOutputStream(File * file)28 FileOutputStream::FileOutputStream(File* file) : OutputStream(file->GetPath()), file_(file) {}
29
WriteFully(const void * buffer,size_t byte_count)30 bool FileOutputStream::WriteFully(const void* buffer, size_t byte_count) {
31 return file_->WriteFully(buffer, byte_count);
32 }
33
Seek(off_t offset,Whence whence)34 off_t FileOutputStream::Seek(off_t offset, Whence whence) {
35 static const bool allow_sparse_files = unix_file::AllowSparseFiles();
36 // If we are not allowed to generate sparse files, write zeros instead.
37 if (UNLIKELY(!allow_sparse_files)) {
38 // Check the current file size.
39 int fd = file_->Fd();
40 struct stat sb;
41 if (fstat(fd, &sb) == -1) {
42 return -1;
43 }
44 off_t file_size = sb.st_size;
45 // Calculate new desired offset.
46 switch (whence) {
47 case kSeekSet:
48 break;
49 case kSeekCurrent: {
50 off_t curr_offset = lseek(fd, 0, SEEK_CUR);
51 if (curr_offset == -1) {
52 return -1;
53 }
54 offset += curr_offset;
55 whence = kSeekSet;
56 break;
57 }
58 case kSeekEnd:
59 offset += file_size;
60 whence = kSeekSet;
61 break;
62 default:
63 LOG(FATAL) << "Unsupported seek type: " << whence;
64 UNREACHABLE();
65 }
66 // Write zeros if we are extending the file.
67 if (offset > file_size) {
68 off_t curr_offset = lseek(fd, 0, SEEK_END);
69 if (curr_offset == -1) {
70 return -1;
71 }
72 static const std::array<uint8_t, 1024> buffer{};
73 while (curr_offset < offset) {
74 size_t size = std::min<size_t>(offset - curr_offset, buffer.size());
75 ssize_t bytes_written = write(fd, buffer.data(), size);
76 if (bytes_written < 0) {
77 return -1;
78 }
79 curr_offset += bytes_written;
80 }
81 }
82 }
83 return lseek(file_->Fd(), offset, static_cast<int>(whence));
84 }
85
Flush()86 bool FileOutputStream::Flush() {
87 return file_->Flush() == 0;
88 }
89
90 } // namespace art
91