#include "perfetto/ext/base/file_utils.h"
#include <sys/stat.h>
#include <sys/types.h>
#include <algorithm>
#include <deque>
#include <optional>
#include <string>
#include <vector>
#include "perfetto/base/build_config.h"
#include "perfetto/base/compiler.h"
#include "perfetto/base/logging.h"
#include "perfetto/base/platform_handle.h"
#include "perfetto/base/status.h"
#include "perfetto/ext/base/platform.h"
#include "perfetto/ext/base/scoped_file.h"
#include "perfetto/ext/base/string_utils.h"
#include "perfetto/ext/base/utils.h"
#if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
#include <Windows.h>
#include <direct.h>
#include <io.h>
#include <stringapiset.h>
#else
#include <dirent.h>
#include <unistd.h>
#endif
#if PERFETTO_BUILDFLAG(PERFETTO_OS_LINUX) || \
PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID) || \
PERFETTO_BUILDFLAG(PERFETTO_OS_APPLE)
#define PERFETTO_SET_FILE_PERMISSIONS
#include <fcntl.h>
#include <grp.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#endif
namespace perfetto {
namespace base {
namespace {
constexpr size_t kBufSize = …;
#if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
int CloseFindHandle(HANDLE h) {
return FindClose(h) ? 0 : -1;
}
std::optional<std::wstring> ToUtf16(const std::string str) {
int len = MultiByteToWideChar(CP_UTF8, 0, str.data(),
static_cast<int>(str.size()), nullptr, 0);
if (len < 0) {
return std::nullopt;
}
std::vector<wchar_t> tmp;
tmp.resize(static_cast<std::vector<wchar_t>::size_type>(len));
len =
MultiByteToWideChar(CP_UTF8, 0, str.data(), static_cast<int>(str.size()),
tmp.data(), static_cast<int>(tmp.size()));
if (len < 0) {
return std::nullopt;
}
PERFETTO_CHECK(static_cast<std::vector<wchar_t>::size_type>(len) ==
tmp.size());
return std::wstring(tmp.data(), tmp.size());
}
#endif
}
ssize_t Read(int fd, void* dst, size_t dst_size) { … }
bool ReadFileDescriptor(int fd, std::string* out) { … }
bool ReadPlatformHandle(PlatformHandle h, std::string* out) { … }
bool ReadFileStream(FILE* f, std::string* out) { … }
bool ReadFile(const std::string& path, std::string* out) { … }
ssize_t WriteAll(int fd, const void* buf, size_t count) { … }
ssize_t WriteAllHandle(PlatformHandle h, const void* buf, size_t count) { … }
bool FlushFile(int fd) { … }
bool Mkdir(const std::string& path) { … }
bool Rmdir(const std::string& path) { … }
int CloseFile(int fd) { … }
ScopedFile OpenFile(const std::string& path, int flags, FileOpenMode mode) { … }
ScopedFstream OpenFstream(const char* path, const char* mode) { … }
bool FileExists(const std::string& path) { … }
int ClosePlatformHandle(PlatformHandle handle) { … }
base::Status ListFilesRecursive(const std::string& dir_path,
std::vector<std::string>& output) { … }
std::string GetFileExtension(const std::string& filename) { … }
base::Status SetFilePermissions(const std::string& file_path,
const std::string& group_name_or_id,
const std::string& mode_bits) { … }
std::optional<uint64_t> GetFileSize(const std::string& file_path) { … }
std::optional<uint64_t> GetFileSize(PlatformHandle fd) { … }
}
}