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 <unistd.h> | |
14 #include <errno.h> | |
15 | |
16 namespace rtc { | |
17 | |
18 #define HANDLE_EINTR(x) \ | |
19 ({ \ | |
20 decltype(x) eintr_wrapper_result; \ | |
21 do { \ | |
22 eintr_wrapper_result = (x); \ | |
23 } while (eintr_wrapper_result == -1 && errno == EINTR); \ | |
24 eintr_wrapper_result; \ | |
25 }) | |
sprang_webrtc
2016/08/15 09:43:07
I think I would actually preffer having explicit c
| |
26 | |
27 size_t File::WriteNoBestEffort(const uint8_t* data, size_t length) { | |
28 ssize_t rv = HANDLE_EINTR(write(file_, data, length)); | |
29 return rv < 0 ? 0 : size_t{rv}; | |
sprang_webrtc
2016/08/15 09:43:07
Prefer static_cast<size_t>() to size_t{}
Also don'
palmkvist
2016/08/16 08:55:35
Done.
| |
30 } | |
31 | |
32 size_t File::ReadNoBestEffort(uint8_t* buffer, size_t length) { | |
33 ssize_t rv = HANDLE_EINTR(read(file_, buffer, length)); | |
34 return rv < 0 ? 0 : size_t{rv}; | |
35 } | |
36 | |
37 size_t File::WriteAtNoBestEffort(const uint8_t* data, | |
38 size_t length, | |
39 size_t offset) { | |
40 ssize_t rv = HANDLE_EINTR(pwrite(file_, data, length, offset)); | |
41 return rv < 0 ? 0 : size_t{rv}; | |
42 } | |
43 | |
44 size_t File::ReadAtNoBestEffort(uint8_t* data, size_t length, size_t offset) { | |
45 ssize_t rv = HANDLE_EINTR(pread(file_, data, length, offset)); | |
46 return rv < 0 ? 0 : size_t{rv}; | |
47 } | |
48 | |
49 bool File::Seek(size_t offset) { | |
50 static_assert(sizeof(size_t) <= sizeof(off_t), "size_t must fit in off_t"); | |
51 return lseek(file_, off_t{offset}, SEEK_SET) != ((off_t)-1); | |
52 } | |
53 | |
54 bool File::Close() { | |
55 if (file_ == rtc::kInvalidPlatformFileValue) | |
56 return false; | |
57 bool ret = close(file_) == 0; | |
58 file_ = rtc::kInvalidPlatformFileValue; | |
59 return ret; | |
60 } | |
61 | |
62 } // namespace rtc | |
OLD | NEW |