Do not keep MappedFile fd opened
According to the mmap man page this is not necessary, and this avoids exposing the fd.
This commit is contained in:
parent
e04a14cf73
commit
0e572589f3
11
src/file.cc
11
src/file.cc
|
@ -208,7 +208,7 @@ String read_file(StringView filename, bool text)
|
||||||
MappedFile::MappedFile(StringView filename)
|
MappedFile::MappedFile(StringView filename)
|
||||||
: data{nullptr}
|
: data{nullptr}
|
||||||
{
|
{
|
||||||
fd = open(filename.zstr(), O_RDONLY | O_NONBLOCK);
|
int fd = open(filename.zstr(), O_RDONLY | O_NONBLOCK);
|
||||||
if (fd == -1)
|
if (fd == -1)
|
||||||
throw file_access_error(filename, strerror(errno));
|
throw file_access_error(filename, strerror(errno));
|
||||||
|
|
||||||
|
@ -222,16 +222,13 @@ MappedFile::MappedFile(StringView filename)
|
||||||
data = (const char*)mmap(nullptr, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
|
data = (const char*)mmap(nullptr, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
|
||||||
if (data == MAP_FAILED)
|
if (data == MAP_FAILED)
|
||||||
throw file_access_error{filename, strerror(errno)};
|
throw file_access_error{filename, strerror(errno)};
|
||||||
|
close(fd);
|
||||||
}
|
}
|
||||||
|
|
||||||
MappedFile::~MappedFile()
|
MappedFile::~MappedFile()
|
||||||
{
|
{
|
||||||
if (fd != -1)
|
if (data != nullptr)
|
||||||
{
|
munmap((void*)data, st.st_size);
|
||||||
if (data != nullptr)
|
|
||||||
munmap((void*)data, st.st_size);
|
|
||||||
close(fd);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
MappedFile::operator StringView() const
|
MappedFile::operator StringView() const
|
||||||
|
|
|
@ -49,7 +49,6 @@ struct MappedFile
|
||||||
|
|
||||||
operator StringView() const;
|
operator StringView() const;
|
||||||
|
|
||||||
int fd;
|
|
||||||
const char* data;
|
const char* data;
|
||||||
struct stat st {};
|
struct stat st {};
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue
Block a user