|
16 | 16 | #include <sys/stat.h>
|
17 | 17 | #include <unistd.h>
|
18 | 18 |
|
| 19 | +#if defined(OS_LINUX) |
| 20 | +#include <endian.h> |
| 21 | +#elif defined(OS_FREEBSD) |
| 22 | +#include <sys/_endian.h> |
| 23 | +#elif defined(OS_DARWIN) |
| 24 | +#include <libkern/OSByteOrder.h> |
| 25 | +#define htobe32(x) OSSwapHostToBigInt32(x) |
| 26 | +#define htobe64(x) OSSwapHostToBigInt64(x) |
| 27 | +#endif |
| 28 | + |
19 | 29 | namespace ds2 {
|
20 | 30 | namespace Host {
|
21 | 31 |
|
@@ -118,6 +128,40 @@ ErrorCode File::pwrite(ByteVector const &buf, uint64_t &count,
|
118 | 128 | return _lastError = kSuccess;
|
119 | 129 | }
|
120 | 130 |
|
| 131 | +// lldb expects stat data is returned as a packed buffer with total size of 64 |
| 132 | +// bytes. The field order is the same as the POSIX defined stat struct. All |
| 133 | +// fields are encoded as 4-byte, big-endian unsigned integers except for |
| 134 | +// st_size, st_blksize, and st_blocks which are all 8-byte, big-endian unsigned |
| 135 | +// integers. |
| 136 | +ErrorCode File::fstat(ByteVector &buffer) const { |
| 137 | + struct stat s; |
| 138 | + if (::fstat(_fd, &s) < 0) |
| 139 | + return Platform::TranslateError(); |
| 140 | + |
| 141 | + const auto appendInteger = [&buffer](auto value) -> void { |
| 142 | + buffer.insert(buffer.end(), |
| 143 | + reinterpret_cast<uint8_t*>(&value), |
| 144 | + reinterpret_cast<uint8_t*>(&value) + sizeof(value)); |
| 145 | + }; |
| 146 | + |
| 147 | + appendInteger(htobe32(static_cast<uint32_t>(s.st_dev))); |
| 148 | + appendInteger(htobe32(static_cast<uint32_t>(s.st_ino))); |
| 149 | + appendInteger(htobe32(static_cast<uint32_t>(s.st_mode))); |
| 150 | + appendInteger(htobe32(static_cast<uint32_t>(s.st_nlink))); |
| 151 | + appendInteger(htobe32(static_cast<uint32_t>(s.st_uid))); |
| 152 | + appendInteger(htobe32(static_cast<uint32_t>(s.st_gid))); |
| 153 | + appendInteger(htobe32(static_cast<uint32_t>(s.st_rdev))); |
| 154 | + appendInteger(htobe64(static_cast<uint64_t>(s.st_size))); |
| 155 | + appendInteger(htobe64(static_cast<uint64_t>(s.st_blksize))); |
| 156 | + appendInteger(htobe64(static_cast<uint64_t>(s.st_blocks))); |
| 157 | + appendInteger(htobe32(static_cast<uint32_t>(s.st_atime))); |
| 158 | + appendInteger(htobe32(static_cast<uint32_t>(s.st_mtime))); |
| 159 | + appendInteger(htobe32(static_cast<uint32_t>(s.st_ctime))); |
| 160 | + DS2ASSERT(buffer.size() == 64); |
| 161 | + |
| 162 | + return kSuccess; |
| 163 | +} |
| 164 | + |
121 | 165 | ErrorCode File::chmod(std::string const &path, uint32_t mode) {
|
122 | 166 | if (::chmod(path.c_str(), mode) < 0) {
|
123 | 167 | return Platform::TranslateError();
|
|
0 commit comments