• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef INCLUDE_PERFETTO_EXT_BASE_FILE_UTILS_H_
18 #define INCLUDE_PERFETTO_EXT_BASE_FILE_UTILS_H_
19 
20 #include <fcntl.h>  // For mode_t & O_RDONLY/RDWR. Exists also on Windows.
21 #include <stddef.h>
22 
23 #include <string>
24 
25 #include "perfetto/base/build_config.h"
26 #include "perfetto/base/export.h"
27 #include "perfetto/ext/base/scoped_file.h"
28 #include "perfetto/ext/base/utils.h"
29 
30 namespace perfetto {
31 namespace base {
32 
33 #if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
34 using FileOpenMode = int;
35 #else
36 using FileOpenMode = mode_t;
37 #endif
38 
39 constexpr FileOpenMode kFileModeInvalid = static_cast<FileOpenMode>(-1);
40 
41 bool ReadPlatformHandle(PlatformHandle, std::string* out);
42 bool ReadFileDescriptor(int fd, std::string* out);
43 bool ReadFileStream(FILE* f, std::string* out);
44 bool ReadFile(const std::string& path, std::string* out);
45 
46 // A wrapper around read(2). It deals with Linux vs Windows includes. It also
47 // deals with handling EINTR. Has the same semantics of UNIX's read(2).
48 ssize_t Read(int fd, void* dst, size_t dst_size);
49 
50 // Call write until all data is written or an error is detected.
51 //
52 // man 2 write:
53 //   If a write() is interrupted by a signal handler before any bytes are
54 //   written, then the call fails with the error EINTR; if it is
55 //   interrupted after at least one byte has been written, the call
56 //   succeeds, and returns the number of bytes written.
57 ssize_t WriteAll(int fd, const void* buf, size_t count);
58 
59 ssize_t WriteAllHandle(PlatformHandle, const void* buf, size_t count);
60 
61 ScopedFile OpenFile(const std::string& path,
62                     int flags,
63                     FileOpenMode = kFileModeInvalid);
64 
65 // This is an alias for close(). It's to avoid leaking Windows.h in headers.
66 // Exported because ScopedFile is used in the /include/ext API by Chromium
67 // component builds.
68 int PERFETTO_EXPORT CloseFile(int fd);
69 
70 bool FlushFile(int fd);
71 
72 // Returns true if mkdir succeeds, false if it fails (see errno in that case).
73 bool Mkdir(const std::string& path);
74 
75 // Calls rmdir() on UNIX, _rmdir() on Windows.
76 bool Rmdir(const std::string& path);
77 
78 // Wrapper around access(path, F_OK).
79 bool FileExists(const std::string& path);
80 
81 }  // namespace base
82 }  // namespace perfetto
83 
84 #endif  // INCLUDE_PERFETTO_EXT_BASE_FILE_UTILS_H_
85