13#include "DebugServer2/GDBRemote/Mixins/FileOperationsMixin.h"
14#include "DebugServer2/Host/Platform.h"
27std::string FileOperationsMixin<T>::resolvePath(Session &session,
28 std::string
const &path)
const {
29 if (std::filesystem::path(path).is_absolute()) {
33 std::string workingDirectory;
34 if (this->onQueryWorkingDirectory(session, workingDirectory) != kSuccess ||
35 workingDirectory.empty()) {
41 return (std::filesystem::path(workingDirectory) / path).string();
45ErrorCode FileOperationsMixin<T>::onFileOpen(Session &session, std::string
const &path,
46 OpenFlags flags, uint32_t mode,
48 static int fileIdx = 0;
50 Host::File file(resolvePath(session, path), flags, mode);
52 return file.lastError();
56 _openFiles.emplace(fd, std::move(file));
62ErrorCode FileOperationsMixin<T>::onFileClose(Session &session,
int fd) {
63 auto const it = _openFiles.find(fd);
64 if (it == _openFiles.end()) {
65 return kErrorInvalidHandle;
73ErrorCode FileOperationsMixin<T>::onFileRead(Session &session,
int fd,
74 uint64_t &count, uint64_t offset,
76 auto it = _openFiles.find(fd);
77 if (it == _openFiles.end()) {
78 return kErrorInvalidHandle;
81 return it->second.pread(buffer, count, offset);
85ErrorCode FileOperationsMixin<T>::onFileWrite(Session &session,
int fd,
87 ByteVector
const &buffer,
89 auto it = _openFiles.find(fd);
90 if (it == _openFiles.end()) {
91 return kErrorInvalidHandle;
94 return it->second.pwrite(buffer, nwritten, offset);
98ErrorCode FileOperationsMixin<T>::onFileCreateDirectory(Session &session,
99 std::string
const &path,
101 return Host::File::createDirectory(resolvePath(session, path), flags);
105ErrorCode FileOperationsMixin<T>::onFileExists(Session &session,
106 std::string
const &path) {
107 return Host::Platform::IsFilePresent(resolvePath(session, path)) ? kSuccess
112ErrorCode FileOperationsMixin<T>::onFileGetSize(Session &session, std::string
const &path,
114 return Host::File::fileSize(resolvePath(session, path), size);
118ErrorCode FileOperationsMixin<T>::onFileGetMode(Session &session,
119 std::string
const &path,
120 uint32_t &mode)
const {
121 return Host::File::fileMode(resolvePath(session, path), mode);
125ErrorCode FileOperationsMixin<T>::onFileFstat(Session &session,
int fd,
126 ByteVector &buffer)
const {
127 auto it = _openFiles.find(fd);
128 if (it == _openFiles.end())
129 return kErrorInvalidHandle;
131 return it->second.fstat(buffer);
135ErrorCode FileOperationsMixin<T>::onFileRemove(Session &session,
136 std::string
const &path) {
137 return Host::File::unlink(resolvePath(session, path));
141ErrorCode FileOperationsMixin<T>::onFileSetPermissions(Session &session,
142 std::string
const &path,
144 return Host::File::chmod(resolvePath(session, path), mode);
148ErrorCode FileOperationsMixin<T>::onQueryModuleInfo(Session &session,
151 ModuleInfo &info)
const {
153 if (Platform::GetExecutableFileBuildID(path, buildId)) {
155 std::ostringstream ss;
156 for(
const auto b : buildId)
157 ss << std::uppercase << std::hex << std::setfill(
'0') << std::setw(2) << int(b);
159 info.uuid = ss.str();
166 auto error = File::fileSize(path, info.file_size);
167 if (error != kSuccess)
170 info.triple = triple;
171 info.file_path = path;
172 info.file_offset = 0;