1
1
mirror of https://github.com/rui314/mold.git synced 2024-09-11 13:06:59 +03:00
mold/common/mapped-file-unix.cc
2024-03-20 17:37:57 +09:00

41 lines
875 B
C++

#include "common.h"
namespace mold {
MappedFile *open_file_impl(const std::string &path, std::string &error) {
i64 fd = ::open(path.c_str(), O_RDONLY);
if (fd == -1) {
if (errno != ENOENT)
error = "opening " + path + " failed: " + errno_string();
return nullptr;
}
struct stat st;
if (fstat(fd, &st) == -1)
error = path + ": fstat failed: " + errno_string();
MappedFile *mf = new MappedFile;
mf->name = path;
mf->size = st.st_size;
if (st.st_size > 0) {
mf->data = (u8 *)mmap(nullptr, st.st_size, PROT_READ | PROT_WRITE,
MAP_PRIVATE, fd, 0);
if (mf->data == MAP_FAILED)
error = path + ": mmap failed: " + errno_string();
}
close(fd);
return mf;
}
void MappedFile::unmap() {
if (size == 0 || parent || !data)
return;
munmap(data, size);
data = nullptr;
}
} // namespace mold