summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMaxime Coste <frrrwww@gmail.com>2012-11-20 18:52:36 +0100
committerMaxime Coste <frrrwww@gmail.com>2012-11-20 18:52:36 +0100
commit713fa9d40647e0c8f4236ffcfc85e65f3fcbb784 (patch)
treeeba3b420a304e0afb5c2b5983ebec1e167d2ade8
parent7c69d170df0f9781a5a69d85c4edc6001501c280 (diff)
more exception safety with file descriptors
-rw-r--r--src/file.cc6
1 files changed, 3 insertions, 3 deletions
diff --git a/src/file.cc b/src/file.cc
index ad98e613..10d6b273 100644
--- a/src/file.cc
+++ b/src/file.cc
@@ -55,6 +55,7 @@ String read_file(const String& filename)
throw file_access_error(filename, strerror(errno));
}
+ auto close_fd = on_scope_end([fd]{ close(fd); });
String content;
char buf[256];
@@ -66,7 +67,6 @@ String read_file(const String& filename)
content += String(buf, buf + size);
}
- close(fd);
return content;
}
@@ -80,6 +80,7 @@ Buffer* create_buffer_from_file(const String& filename)
throw file_access_error(filename, strerror(errno));
}
+ auto close_fd = on_scope_end([fd]{ close(fd); });
if (Buffer* buffer = BufferManager::instance().get_buffer(filename))
delete buffer;
@@ -121,7 +122,6 @@ Buffer* create_buffer_from_file(const String& filename)
}
at_file_begin = false;
}
- close(fd);
OptionManager& option_manager = buffer->option_manager();
option_manager.set_option("eolformat", Option(crlf ? "crlf" : "lf"));
@@ -166,6 +166,7 @@ void write_buffer_to_file(const Buffer& buffer, const String& filename)
O_CREAT | O_WRONLY | O_TRUNC, 0644);
if (fd == -1)
throw file_access_error(filename, strerror(errno));
+ auto close_fd = on_scope_end([fd]{ close(fd); });
if (buffer.option_manager()["BOM"].as_string() == "utf-8")
::write(fd, "\xEF\xBB\xBF", 3);
@@ -178,7 +179,6 @@ void write_buffer_to_file(const Buffer& buffer, const String& filename)
write(fd, linedata.subrange(0, linedata.size()-1), filename);
write(fd, eoldata, filename);
}
- close(fd);
}
}