From 014ff1f71c72ba279c0dd106f2bbe9a631beb0cc Mon Sep 17 00:00:00 2001 From: Mathias Gumz Date: Wed, 4 Mar 2009 19:54:48 +0100 Subject: [PATCH] dont try to read file when they are actually a directory --- util/fluxbox-update_configs.cc | 35 +++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/util/fluxbox-update_configs.cc b/util/fluxbox-update_configs.cc index 43dd1f22..90a40d0a 100644 --- a/util/fluxbox-update_configs.cc +++ b/util/fluxbox-update_configs.cc @@ -44,6 +44,8 @@ #include #endif +#include + #include #include #include @@ -63,8 +65,8 @@ using std::list; using std::exit; using std::getenv; -string read_file(string filename); -void write_file(string filename, string &contents); +string read_file(const string& filename); +void write_file(const string& filename, const string &contents); void save_all_files(); int run_updates(int old_version, FbTk::ResourceManager &rm) { @@ -455,18 +457,28 @@ int main(int argc, char **argv) { return 0; } -static set modified_files; +namespace { + +set modified_files; // we may want to put a size limit on this cache, so it doesn't grow too big -static map file_cache; +map file_cache; + +}; // returns the contents of the file given, either from the cache or by reading // the file from disk -string read_file(string filename) { +string read_file(const string& filename) { // check if we already have the file in memory map::iterator it = file_cache.find(filename); if (it != file_cache.end()) return it->second; + stat s; + stat(filename.c_str(), &s); + + if (! (s.st_mode & S_IFREG)) + return ""; + // nope, we'll have to read the file ifstream infile(filename.c_str()); string whole_file = ""; @@ -474,10 +486,15 @@ string read_file(string filename) { if (!infile) // failed to open file return whole_file; + string linebuffer; while (!infile.eof()) { - string linebuffer; - getline(infile, linebuffer); + + // check if we read something at all. if its 0, its a strange file + // (eg a directory) or we are at the end + if (infile.gcount() == 0) { + break; + } whole_file += linebuffer + "\n"; } infile.close(); @@ -488,7 +505,7 @@ string read_file(string filename) { #ifdef NOT_USED // remove the file from the cache, writing to disk if it's been changed -void forget_file(string filename) { +void forget_file(const string& filename) { map::iterator cache_it = file_cache.find(filename); // check if we knew about the file to begin with if (cache_it == file_cache.end()) @@ -511,7 +528,7 @@ void forget_file(string filename) { // updates the file contents in the cache and marks the file as modified so it // gets saved later -void write_file(string filename, string &contents) { +void write_file(const string& filename, const string &contents) { modified_files.insert(filename); file_cache[filename] = contents; }