1 /*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <sys/stat.h>
18
19 #include "perfetto/base/build_config.h"
20 #include "perfetto/base/file_utils.h"
21 #include "perfetto/base/logging.h"
22 #include "perfetto/base/scoped_file.h"
23
24 #if !PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
25 #include <unistd.h>
26 #else
27 #include <corecrt_io.h>
28 #include <io.h>
29 #endif
30
31 namespace perfetto {
32 namespace base {
33 namespace {
34 constexpr size_t kBufSize = 2048;
35 }
36
ReadFileDescriptor(int fd,std::string * out)37 bool ReadFileDescriptor(int fd, std::string* out) {
38 // Do not override existing data in string.
39 size_t i = out->size();
40
41 struct stat buf {};
42 if (fstat(fd, &buf) != -1) {
43 if (buf.st_size > 0)
44 out->resize(i + static_cast<size_t>(buf.st_size));
45 }
46
47 ssize_t bytes_read;
48 for (;;) {
49 if (out->size() < i + kBufSize)
50 out->resize(out->size() + kBufSize);
51
52 bytes_read = PERFETTO_EINTR(read(fd, &((*out)[i]), kBufSize));
53 if (bytes_read > 0) {
54 i += static_cast<size_t>(bytes_read);
55 } else {
56 out->resize(i);
57 return bytes_read == 0;
58 }
59 }
60 }
61
ReadFileStream(FILE * f,std::string * out)62 bool ReadFileStream(FILE* f, std::string* out) {
63 return ReadFileDescriptor(fileno(f), out);
64 }
65
ReadFile(const std::string & path,std::string * out)66 bool ReadFile(const std::string& path, std::string* out) {
67 base::ScopedFile fd = base::OpenFile(path, O_RDONLY);
68 if (!fd)
69 return false;
70
71 return ReadFileDescriptor(*fd, out);
72 }
73
WriteAll(int fd,const void * buf,size_t count)74 ssize_t WriteAll(int fd, const void* buf, size_t count) {
75 size_t written = 0;
76 while (written < count) {
77 ssize_t wr = PERFETTO_EINTR(
78 write(fd, static_cast<const char*>(buf) + written, count - written));
79 if (wr == 0)
80 break;
81 if (wr < 0)
82 return wr;
83 written += static_cast<size_t>(wr);
84 }
85 return static_cast<ssize_t>(written);
86 }
87
FlushFile(int fd)88 bool FlushFile(int fd) {
89 PERFETTO_DCHECK(fd != 0);
90 #if PERFETTO_BUILDFLAG(PERFETTO_OS_LINUX) || \
91 PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID)
92 return !PERFETTO_EINTR(fdatasync(fd));
93 #elif PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
94 return !PERFETTO_EINTR(_commit(fd));
95 #else
96 return !PERFETTO_EINTR(fsync(fd));
97 #endif
98 }
99
100 } // namespace base
101 } // namespace perfetto
102