• 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 #include <vector>
25 
26 #include "perfetto/base/build_config.h"
27 #include "perfetto/base/export.h"
28 #include "perfetto/base/status.h"
29 #include "perfetto/ext/base/scoped_file.h"
30 #include "perfetto/ext/base/utils.h"
31 
32 namespace perfetto {
33 namespace base {
34 
35 #if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
36 using FileOpenMode = int;
37 #else
38 using FileOpenMode = mode_t;
39 #endif
40 
41 constexpr FileOpenMode kFileModeInvalid = static_cast<FileOpenMode>(-1);
42 
43 bool ReadPlatformHandle(PlatformHandle, std::string* out);
44 bool ReadFileDescriptor(int fd, std::string* out);
45 bool ReadFileStream(FILE* f, std::string* out);
46 bool ReadFile(const std::string& path, std::string* out);
47 
48 // A wrapper around read(2). It deals with Linux vs Windows includes. It also
49 // deals with handling EINTR. Has the same semantics of UNIX's read(2).
50 ssize_t Read(int fd, void* dst, size_t dst_size);
51 
52 // Call write until all data is written or an error is detected.
53 //
54 // man 2 write:
55 //   If a write() is interrupted by a signal handler before any bytes are
56 //   written, then the call fails with the error EINTR; if it is
57 //   interrupted after at least one byte has been written, the call
58 //   succeeds, and returns the number of bytes written.
59 ssize_t WriteAll(int fd, const void* buf, size_t count);
60 
61 ssize_t WriteAllHandle(PlatformHandle, const void* buf, size_t count);
62 
63 ScopedFile OpenFile(const std::string& path,
64                     int flags,
65                     FileOpenMode = kFileModeInvalid);
66 ScopedFstream OpenFstream(const char* path, const char* mode);
67 
68 // This is an alias for close(). It's to avoid leaking Windows.h in headers.
69 // Exported because ScopedFile is used in the /include/ext API by Chromium
70 // component builds.
71 int PERFETTO_EXPORT_COMPONENT CloseFile(int fd);
72 
73 bool FlushFile(int fd);
74 
75 // Returns true if mkdir succeeds, false if it fails (see errno in that case).
76 bool Mkdir(const std::string& path);
77 
78 // Calls rmdir() on UNIX, _rmdir() on Windows.
79 bool Rmdir(const std::string& path);
80 
81 // Wrapper around access(path, F_OK).
82 bool FileExists(const std::string& path);
83 
84 // Gets the extension for a filename. If the file has two extensions, returns
85 // only the last one (foo.pb.gz => .gz). Returns empty string if there is no
86 // extension.
87 std::string GetFileExtension(const std::string& filename);
88 
89 // Puts the path to all files under |dir_path| in |output|, recursively walking
90 // subdirectories. File paths are relative to |dir_path|. Only files are
91 // included, not directories. Path separator is always '/', even on windows (not
92 // '\').
93 base::Status ListFilesRecursive(const std::string& dir_path,
94                                 std::vector<std::string>& output);
95 
96 }  // namespace base
97 }  // namespace perfetto
98 
99 #endif  // INCLUDE_PERFETTO_EXT_BASE_FILE_UTILS_H_
100