Line data Source code
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 : #include <limits>
20 :
21 : #include "webrtc/base/checks.h"
22 :
23 : namespace rtc {
24 :
25 0 : size_t File::Write(const uint8_t* data, size_t length) {
26 0 : size_t total_written = 0;
27 0 : do {
28 : ssize_t written;
29 0 : do {
30 0 : written = ::write(file_, data + total_written, length - total_written);
31 0 : } while (written == -1 && errno == EINTR);
32 0 : if (written == -1)
33 0 : break;
34 0 : total_written += written;
35 0 : } while (total_written < length);
36 0 : return total_written;
37 : }
38 :
39 0 : size_t File::Read(uint8_t* buffer, size_t length) {
40 0 : size_t total_read = 0;
41 0 : do {
42 : ssize_t read;
43 0 : do {
44 0 : read = ::read(file_, buffer + total_read, length - total_read);
45 0 : } while (read == -1 && errno == EINTR);
46 0 : if (read == -1)
47 0 : break;
48 0 : total_read += read;
49 0 : } while (total_read < length);
50 0 : return total_read;
51 : }
52 :
53 0 : size_t File::WriteAt(const uint8_t* data, size_t length, size_t offset) {
54 0 : size_t total_written = 0;
55 0 : do {
56 : ssize_t written;
57 0 : do {
58 0 : written = ::pwrite(file_, data + total_written, length - total_written,
59 0 : offset + total_written);
60 0 : } while (written == -1 && errno == EINTR);
61 0 : if (written == -1)
62 0 : break;
63 0 : total_written += written;
64 0 : } while (total_written < length);
65 0 : return total_written;
66 : }
67 :
68 0 : size_t File::ReadAt(uint8_t* buffer, size_t length, size_t offset) {
69 0 : size_t total_read = 0;
70 0 : do {
71 : ssize_t read;
72 0 : do {
73 0 : read = ::pread(file_, buffer + total_read, length - total_read,
74 0 : offset + total_read);
75 0 : } while (read == -1 && errno == EINTR);
76 0 : if (read == -1)
77 0 : break;
78 0 : total_read += read;
79 0 : } while (total_read < length);
80 0 : return total_read;
81 : }
82 :
83 0 : bool File::Seek(size_t offset) {
84 0 : RTC_DCHECK_LE(offset, std::numeric_limits<off_t>::max());
85 0 : return lseek(file_, static_cast<off_t>(offset), SEEK_SET) != -1;
86 : }
87 :
88 0 : bool File::Close() {
89 0 : if (file_ == rtc::kInvalidPlatformFileValue)
90 0 : return false;
91 0 : bool ret = close(file_) == 0;
92 0 : file_ = rtc::kInvalidPlatformFileValue;
93 0 : return ret;
94 : }
95 :
96 : } // namespace rtc
|