Files
FEX-Emu--FEX/FEXHeaderUtils/FEXHeaderUtils/SymlinkChecks.h
T
Ryan Houdek 94bbd415a2 FHU: Prepend SPDX identifier
Added with `sed -i '1 i\\/\/ SPDX-License-Identifier: MIT' *.h`
2023-09-18 11:45:18 -07:00

33 lines
909 B
C++

// SPDX-License-Identifier: MIT
#pragma once
#include <FEXCore/fextl/string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <span>
#include <unistd.h>
namespace FHU::Symlinks {
#ifndef _WIN32
// Checks to see if a filepath is a symlink.
inline bool IsSymlink(const fextl::string &Filename) {
struct stat Buffer{};
int Result = lstat(Filename.c_str(), &Buffer);
return Result == 0 && S_ISLNK(Buffer.st_mode);
}
// Resolves a symlink path.
// Doesn't handle recursive symlinks.
// Doesn't append null terminator character.
// Returns a string_view of the resolved path, or an empty view on error.
inline std::string_view ResolveSymlink(const fextl::string &Filename, std::span<char> ResultBuffer) {
ssize_t Result = readlink(Filename.c_str(), ResultBuffer.data(), ResultBuffer.size());
if (Result == -1) {
return {};
}
return std::string_view(ResultBuffer.data(), Result);
}
#endif
}