OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. |
| 3 * |
| 4 * Use of this source code is governed by a BSD-style license |
| 5 * that can be found in the LICENSE file in the root of the source |
| 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ |
| 10 |
| 11 #include "webrtc/base/file.h" |
| 12 |
| 13 #include <errno.h> |
| 14 #include <fcntl.h> |
| 15 #include <sys/stat.h> |
| 16 #include <sys/types.h> |
| 17 #include <unistd.h> |
| 18 |
| 19 namespace rtc { |
| 20 |
| 21 File File::Open(const std::string& path) { |
| 22 return File(::open(path.c_str(), O_RDWR)); |
| 23 } |
| 24 |
| 25 size_t File::Write(const uint8_t* data, size_t length) { |
| 26 size_t total_written = 0; |
| 27 do { |
| 28 ssize_t written; |
| 29 do { |
| 30 written = ::write(file_, data + total_written, length - total_written); |
| 31 } while (written == -1 && errno == EINTR); |
| 32 if (written == -1) |
| 33 break; |
| 34 total_written += written; |
| 35 } while (total_written < length); |
| 36 return total_written; |
| 37 } |
| 38 |
| 39 size_t File::Read(uint8_t* buffer, size_t length) { |
| 40 size_t total_read = 0; |
| 41 do { |
| 42 ssize_t read; |
| 43 do { |
| 44 read = ::read(file_, buffer + total_read, length - total_read); |
| 45 } while (read == -1 && errno == EINTR); |
| 46 if (read == -1) |
| 47 break; |
| 48 total_read += read; |
| 49 } while (total_read < length); |
| 50 return total_read; |
| 51 } |
| 52 |
| 53 size_t File::WriteAt(const uint8_t* data, size_t length, size_t offset) { |
| 54 size_t total_written = 0; |
| 55 do { |
| 56 ssize_t written; |
| 57 do { |
| 58 written = ::pwrite(file_, data + total_written, length - total_written, |
| 59 offset + total_written); |
| 60 } while (written == -1 && errno == EINTR); |
| 61 if (written == -1) |
| 62 break; |
| 63 total_written += written; |
| 64 } while (total_written < length); |
| 65 return total_written; |
| 66 } |
| 67 |
| 68 size_t File::ReadAt(uint8_t* buffer, size_t length, size_t offset) { |
| 69 size_t total_read = 0; |
| 70 do { |
| 71 ssize_t read; |
| 72 do { |
| 73 read = ::pread(file_, buffer + total_read, length - total_read, |
| 74 offset + total_read); |
| 75 } while (read == -1 && errno == EINTR); |
| 76 if (read == -1) |
| 77 break; |
| 78 total_read += read; |
| 79 } while (total_read < length); |
| 80 return total_read; |
| 81 } |
| 82 |
| 83 bool File::Seek(size_t offset) { |
| 84 static_assert(sizeof(size_t) <= sizeof(off_t), "size_t must fit in off_t"); |
| 85 return lseek(file_, off_t{offset}, SEEK_SET) != ((off_t)-1); |
| 86 } |
| 87 |
| 88 bool File::Close() { |
| 89 if (file_ == rtc::kInvalidPlatformFileValue) |
| 90 return false; |
| 91 bool ret = close(file_) == 0; |
| 92 file_ = rtc::kInvalidPlatformFileValue; |
| 93 return ret; |
| 94 } |
| 95 |
| 96 } // namespace rtc |
OLD | NEW |