Do less implicit parse_filename calls

This commit is contained in:
Maxime Coste 2017-08-29 09:42:04 +07:00
parent 9001580c9b
commit 24234dffa3
2 changed files with 13 additions and 16 deletions

View File

@ -61,7 +61,7 @@ ByteCount get_byte_to_column(const Buffer& buffer, ColumnCount tabstop, DisplayC
Buffer* open_file_buffer(StringView filename, Buffer::Flags flags) Buffer* open_file_buffer(StringView filename, Buffer::Flags flags)
{ {
MappedFile file_data{filename}; MappedFile file_data{parse_filename(filename)};
return BufferManager::instance().create_buffer( return BufferManager::instance().create_buffer(
filename.str(), Buffer::Flags::File | flags, file_data, file_data.st.st_mtim); filename.str(), Buffer::Flags::File | flags, file_data, file_data.st.st_mtim);
} }
@ -69,9 +69,10 @@ Buffer* open_file_buffer(StringView filename, Buffer::Flags flags)
Buffer* open_or_create_file_buffer(StringView filename, Buffer::Flags flags) Buffer* open_or_create_file_buffer(StringView filename, Buffer::Flags flags)
{ {
auto& buffer_manager = BufferManager::instance(); auto& buffer_manager = BufferManager::instance();
if (file_exists(filename)) auto path = parse_filename(filename);
if (file_exists(path))
{ {
MappedFile file_data{filename}; MappedFile file_data{path};
return buffer_manager.create_buffer(filename.str(), Buffer::Flags::File | flags, return buffer_manager.create_buffer(filename.str(), Buffer::Flags::File | flags,
file_data, file_data.st.st_mtim); file_data, file_data.st.st_mtim);
} }

View File

@ -175,7 +175,7 @@ String read_fd(int fd, bool text)
String read_file(StringView filename, bool text) String read_file(StringView filename, bool text)
{ {
int fd = open(parse_filename(filename).c_str(), O_RDONLY); int fd = open(filename.zstr(), O_RDONLY);
if (fd == -1) if (fd == -1)
throw file_access_error(filename, strerror(errno)); throw file_access_error(filename, strerror(errno));
@ -185,15 +185,13 @@ String read_file(StringView filename, bool text)
MappedFile::MappedFile(StringView filename) MappedFile::MappedFile(StringView filename)
{ {
String real_filename = real_path(parse_filename(filename)); fd = open(filename.zstr(), O_RDONLY | O_NONBLOCK);
fd = open(real_filename.c_str(), O_RDONLY | O_NONBLOCK);
if (fd == -1) if (fd == -1)
throw file_access_error(real_filename, strerror(errno)); throw file_access_error(filename, strerror(errno));
fstat(fd, &st); fstat(fd, &st);
if (S_ISDIR(st.st_mode)) if (S_ISDIR(st.st_mode))
throw file_access_error(real_filename, "is a directory"); throw file_access_error(filename, "is a directory");
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);
} }
@ -214,9 +212,8 @@ MappedFile::operator StringView() const
bool file_exists(StringView filename) bool file_exists(StringView filename)
{ {
String real_filename = real_path(parse_filename(filename));
struct stat st; struct stat st;
return stat(real_filename.c_str(), &st) == 0; return stat(filename.zstr(), &st) == 0;
} }
void write(int fd, StringView data) void write(int fd, StringView data)
@ -315,22 +312,21 @@ void write_buffer_to_backup_file(Buffer& buffer)
String find_file(StringView filename, ConstArrayView<String> paths) String find_file(StringView filename, ConstArrayView<String> paths)
{ {
struct stat buf; struct stat buf;
if (filename.length() > 1 and filename[0_byte] == '/') if (filename.substr(0_byte, 1_byte) == "/")
{ {
if (stat(filename.zstr(), &buf) == 0 and S_ISREG(buf.st_mode)) if (stat(filename.zstr(), &buf) == 0 and S_ISREG(buf.st_mode))
return filename.str(); return filename.str();
return ""; return "";
} }
if (filename.length() > 2 and if (filename.substr(0_byte, 2_byte) == "~/")
filename[0_byte] == '~' and filename[1_byte] == '/')
{ {
String candidate = getenv("HOME") + filename.substr(1_byte).str(); String candidate = getenv("HOME") + filename.substr(1_byte);
if (stat(candidate.c_str(), &buf) == 0 and S_ISREG(buf.st_mode)) if (stat(candidate.c_str(), &buf) == 0 and S_ISREG(buf.st_mode))
return candidate; return candidate;
return ""; return "";
} }
for (auto candidate : paths) for (auto candidate : paths | transform(parse_filename))
{ {
if (not candidate.empty() and candidate.back() != '/') if (not candidate.empty() and candidate.back() != '/')
candidate += '/'; candidate += '/';