1 // Copyright 2017 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #pragma once
16 
17 #ifndef _WIN32
18 #include <unistd.h>
19 #else
20 #include <stdint.h>
21 #endif
22 
23 namespace android {
24 namespace base {
25 
26 // This is a cross-platform version of pread(2)/pwrite(2) syscalls, with the
27 // following from their POSIX versions:
28 //
29 // - On Windows it utilizes ReadFile()/WriteFile() + OVERLAPPED structure to
30 //   pass the position in. As per MSDN, this way APIs still update the current
31 //   file position (as opposed to the POSIX ones)
32 //
33 // - Windows version doesn't return most of the |errno| error codes, using
34 //   EINVAL for most of the issues and EAGAIN if it's a nonblocking FD.
35 //
36 // That's why the best way of using android::base::pread() and pwrite() is to
37 // never use it together with regular read()/write() calls, especially in
38 // multi-threaded code. Nobody can predict the active read position when using
39 // them on different platforms.
40 //
41 
42 #ifndef _WIN32
43 using ::pread;
44 using ::pwrite;
45 #else
46 int64_t pread(int fd, void* buf, size_t count, int64_t offset);
47 int64_t pwrite(int fd, const void* buf, size_t count, int64_t offset);
48 #endif
49 
50 }  // namespace base
51 }  // namespace android
52