Viktor Palmkvist | 4ec6a0c | 2016-09-02 13:38:32 +0200 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "rtc_base/file.h" |
Viktor Palmkvist | 4ec6a0c | 2016-09-02 13:38:32 +0200 | [diff] [blame] | 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 | #include <limits> |
| 20 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 21 | #include "rtc_base/checks.h" |
Viktor Palmkvist | 4ec6a0c | 2016-09-02 13:38:32 +0200 | [diff] [blame] | 22 | |
| 23 | namespace rtc { |
| 24 | |
Viktor Palmkvist | 4ec6a0c | 2016-09-02 13:38:32 +0200 | [diff] [blame] | 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) { |
kwiberg | 352444f | 2016-11-28 15:58:53 -0800 | [diff] [blame] | 84 | RTC_DCHECK_LE(offset, std::numeric_limits<off_t>::max()); |
Viktor Palmkvist | 4ec6a0c | 2016-09-02 13:38:32 +0200 | [diff] [blame] | 85 | return lseek(file_, static_cast<off_t>(offset), SEEK_SET) != -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 |