use canonicalized filenames for buffers

This commit is contained in:
Maxime Coste 2013-03-22 14:27:30 +01:00
parent 5bba01ca9c
commit 0082238c75
3 changed files with 12 additions and 4 deletions

View File

@ -94,7 +94,7 @@ void edit(const CommandParameters& params, Context& context)
if (param_count == 0 or param_count > 3) if (param_count == 0 or param_count > 3)
throw wrong_argument_count(); throw wrong_argument_count();
const String& name = parser[0]; String name = canonicalize_filename(parse_filename(parser[0]));
Buffer* buffer = nullptr; Buffer* buffer = nullptr;
if (not force_reload) if (not force_reload)

View File

@ -46,6 +46,11 @@ String parse_filename(const String& filename)
return result; return result;
} }
String canonicalize_filename(const String& filename)
{
return filename.replace(R"(((^|(?<=/))\./)+|[^/]+/\.\./)", "");
}
String read_file(const String& filename) String read_file(const String& filename)
{ {
int fd = open(parse_filename(filename).c_str(), O_RDONLY); int fd = open(parse_filename(filename).c_str(), O_RDONLY);
@ -71,9 +76,11 @@ String read_file(const String& filename)
return content; return content;
} }
Buffer* create_buffer_from_file(const String& filename) Buffer* create_buffer_from_file(String filename)
{ {
int fd = open(parse_filename(filename).c_str(), O_RDONLY); filename = canonicalize_filename(parse_filename(filename));
int fd = open(filename.c_str(), O_RDONLY);
if (fd == -1) if (fd == -1)
{ {
if (errno == ENOENT) if (errno == ENOENT)

View File

@ -26,9 +26,10 @@ class Buffer;
// parse ~/ and $env values in filename and returns the translated filename // parse ~/ and $env values in filename and returns the translated filename
String parse_filename(const String& filename); String parse_filename(const String& filename);
String canonicalize_filename(const String& filename);
String read_file(const String& filename); String read_file(const String& filename);
Buffer* create_buffer_from_file(const String& filename); Buffer* create_buffer_from_file(String filename);
void write_buffer_to_file(const Buffer& buffer, const String& filename); void write_buffer_to_file(const Buffer& buffer, const String& filename);
String find_file(const String& filename, const memoryview<String>& paths); String find_file(const String& filename, const memoryview<String>& paths);