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/logging.h"
21 #include "perfetto/ext/base/file_utils.h"
22 #include "perfetto/ext/base/scoped_file.h"
23 #include "perfetto/ext/base/utils.h"
24 
25 #if !PERFETTO_BUILDFLAG(PERFETTO_OS_WIN) || \
26     PERFETTO_BUILDFLAG(PERFETTO_COMPILER_GCC)
27 #include <unistd.h>
28 #else
29 #include <corecrt_io.h>
30 #include <io.h>
31 #endif
32 
33 namespace perfetto {
34 namespace base {
35 namespace {
36 constexpr size_t kBufSize = 2048;
37 }
38 
ReadFileDescriptor(int fd,std::string * out)39 bool ReadFileDescriptor(int fd, std::string* out) {
40   // Do not override existing data in string.
41   size_t i = out->size();
42 
43   struct stat buf {};
44   if (fstat(fd, &buf) != -1) {
45     if (buf.st_size > 0)
46       out->resize(i + static_cast<size_t>(buf.st_size));
47   }
48 
49   ssize_t bytes_read;
50   for (;;) {
51     if (out->size() < i + kBufSize)
52       out->resize(out->size() + kBufSize);
53 
54     bytes_read = PERFETTO_EINTR(read(fd, &((*out)[i]), kBufSize));
55     if (bytes_read > 0) {
56       i += static_cast<size_t>(bytes_read);
57     } else {
58       out->resize(i);
59       return bytes_read == 0;
60     }
61   }
62 }
63 
ReadFileStream(FILE * f,std::string * out)64 bool ReadFileStream(FILE* f, std::string* out) {
65   return ReadFileDescriptor(fileno(f), out);
66 }
67 
ReadFile(const std::string & path,std::string * out)68 bool ReadFile(const std::string& path, std::string* out) {
69   base::ScopedFile fd = base::OpenFile(path, O_RDONLY);
70   if (!fd)
71     return false;
72 
73   return ReadFileDescriptor(*fd, out);
74 }
75 
WriteAll(int fd,const void * buf,size_t count)76 ssize_t WriteAll(int fd, const void* buf, size_t count) {
77   size_t written = 0;
78   while (written < count) {
79     ssize_t wr = PERFETTO_EINTR(
80         write(fd, static_cast<const char*>(buf) + written, count - written));
81     if (wr == 0)
82       break;
83     if (wr < 0)
84       return wr;
85     written += static_cast<size_t>(wr);
86   }
87   return static_cast<ssize_t>(written);
88 }
89 
FlushFile(int fd)90 bool FlushFile(int fd) {
91   PERFETTO_DCHECK(fd != 0);
92 #if PERFETTO_BUILDFLAG(PERFETTO_OS_LINUX) || \
93     PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID)
94 #ifdef __DragonFly__
95   return !PERFETTO_EINTR(fsync(fd));
96 #else
97   return !PERFETTO_EINTR(fdatasync(fd));
98 #endif
99 #elif PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
100   return !PERFETTO_EINTR(_commit(fd));
101 #else
102   return !PERFETTO_EINTR(fsync(fd));
103 #endif
104 }
105 
106 }  // namespace base
107 }  // namespace perfetto
108