all repos — fluxbox @ 014ff1f71c72ba279c0dd106f2bbe9a631beb0cc

custom fork of the fluxbox windowmanager

dont try to read file when they are actually a directory
Mathias Gumz akira at fluxbox dot org
commit

014ff1f71c72ba279c0dd106f2bbe9a631beb0cc

parent

e7700166604d737ff23427877100c8888c761494

1 files changed, 26 insertions(+), 9 deletions(-)

jump to
M util/fluxbox-update_configs.ccutil/fluxbox-update_configs.cc

@@ -44,6 +44,8 @@ #else

#include <string.h> #endif +#include <sys/stat.h> + #include <iostream> #include <fstream> #include <set>

@@ -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 @@

return 0; } -static set<string> modified_files; +namespace { + +set<string> modified_files; // we may want to put a size limit on this cache, so it doesn't grow too big -static map<string,string> file_cache; +map<string,string> 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<string,string>::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 @@

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 @@ }

#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<string,string>::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 @@ #endif // NOT_USED

// 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; }