diff options
author | Mathias Gumz <akira at fluxbox dot org> | 2009-03-04 18:54:48 (GMT) |
---|---|---|
committer | Mathias Gumz <akira at fluxbox dot org> | 2009-03-04 18:54:48 (GMT) |
commit | 014ff1f71c72ba279c0dd106f2bbe9a631beb0cc (patch) | |
tree | 3309f90f71772c7e78d71cc07848aca03460fdbb /util | |
parent | e7700166604d737ff23427877100c8888c761494 (diff) | |
download | fluxbox_paul-014ff1f71c72ba279c0dd106f2bbe9a631beb0cc.zip fluxbox_paul-014ff1f71c72ba279c0dd106f2bbe9a631beb0cc.tar.bz2 |
dont try to read file when they are actually a directory
Diffstat (limited to 'util')
-rw-r--r-- | util/fluxbox-update_configs.cc | 35 |
1 files changed, 26 insertions, 9 deletions
diff --git a/util/fluxbox-update_configs.cc b/util/fluxbox-update_configs.cc index 43dd1f2..90a40d0 100644 --- a/util/fluxbox-update_configs.cc +++ b/util/fluxbox-update_configs.cc | |||
@@ -44,6 +44,8 @@ | |||
44 | #include <string.h> | 44 | #include <string.h> |
45 | #endif | 45 | #endif |
46 | 46 | ||
47 | #include <sys/stat.h> | ||
48 | |||
47 | #include <iostream> | 49 | #include <iostream> |
48 | #include <fstream> | 50 | #include <fstream> |
49 | #include <set> | 51 | #include <set> |
@@ -63,8 +65,8 @@ using std::list; | |||
63 | using std::exit; | 65 | using std::exit; |
64 | using std::getenv; | 66 | using std::getenv; |
65 | 67 | ||
66 | string read_file(string filename); | 68 | string read_file(const string& filename); |
67 | void write_file(string filename, string &contents); | 69 | void write_file(const string& filename, const string &contents); |
68 | void save_all_files(); | 70 | void save_all_files(); |
69 | 71 | ||
70 | int run_updates(int old_version, FbTk::ResourceManager &rm) { | 72 | int run_updates(int old_version, FbTk::ResourceManager &rm) { |
@@ -455,18 +457,28 @@ int main(int argc, char **argv) { | |||
455 | return 0; | 457 | return 0; |
456 | } | 458 | } |
457 | 459 | ||
458 | static set<string> modified_files; | 460 | namespace { |
461 | |||
462 | set<string> modified_files; | ||
459 | // we may want to put a size limit on this cache, so it doesn't grow too big | 463 | // we may want to put a size limit on this cache, so it doesn't grow too big |
460 | static map<string,string> file_cache; | 464 | map<string,string> file_cache; |
465 | |||
466 | }; | ||
461 | 467 | ||
462 | // returns the contents of the file given, either from the cache or by reading | 468 | // returns the contents of the file given, either from the cache or by reading |
463 | // the file from disk | 469 | // the file from disk |
464 | string read_file(string filename) { | 470 | string read_file(const string& filename) { |
465 | // check if we already have the file in memory | 471 | // check if we already have the file in memory |
466 | map<string,string>::iterator it = file_cache.find(filename); | 472 | map<string,string>::iterator it = file_cache.find(filename); |
467 | if (it != file_cache.end()) | 473 | if (it != file_cache.end()) |
468 | return it->second; | 474 | return it->second; |
469 | 475 | ||
476 | stat s; | ||
477 | stat(filename.c_str(), &s); | ||
478 | |||
479 | if (! (s.st_mode & S_IFREG)) | ||
480 | return ""; | ||
481 | |||
470 | // nope, we'll have to read the file | 482 | // nope, we'll have to read the file |
471 | ifstream infile(filename.c_str()); | 483 | ifstream infile(filename.c_str()); |
472 | string whole_file = ""; | 484 | string whole_file = ""; |
@@ -474,10 +486,15 @@ string read_file(string filename) { | |||
474 | if (!infile) // failed to open file | 486 | if (!infile) // failed to open file |
475 | return whole_file; | 487 | return whole_file; |
476 | 488 | ||
489 | string linebuffer; | ||
477 | while (!infile.eof()) { | 490 | while (!infile.eof()) { |
478 | string linebuffer; | ||
479 | |||
480 | getline(infile, linebuffer); | 491 | getline(infile, linebuffer); |
492 | |||
493 | // check if we read something at all. if its 0, its a strange file | ||
494 | // (eg a directory) or we are at the end | ||
495 | if (infile.gcount() == 0) { | ||
496 | break; | ||
497 | } | ||
481 | whole_file += linebuffer + "\n"; | 498 | whole_file += linebuffer + "\n"; |
482 | } | 499 | } |
483 | infile.close(); | 500 | infile.close(); |
@@ -488,7 +505,7 @@ string read_file(string filename) { | |||
488 | 505 | ||
489 | #ifdef NOT_USED | 506 | #ifdef NOT_USED |
490 | // remove the file from the cache, writing to disk if it's been changed | 507 | // remove the file from the cache, writing to disk if it's been changed |
491 | void forget_file(string filename) { | 508 | void forget_file(const string& filename) { |
492 | map<string,string>::iterator cache_it = file_cache.find(filename); | 509 | map<string,string>::iterator cache_it = file_cache.find(filename); |
493 | // check if we knew about the file to begin with | 510 | // check if we knew about the file to begin with |
494 | if (cache_it == file_cache.end()) | 511 | if (cache_it == file_cache.end()) |
@@ -511,7 +528,7 @@ void forget_file(string filename) { | |||
511 | 528 | ||
512 | // updates the file contents in the cache and marks the file as modified so it | 529 | // updates the file contents in the cache and marks the file as modified so it |
513 | // gets saved later | 530 | // gets saved later |
514 | void write_file(string filename, string &contents) { | 531 | void write_file(const string& filename, const string &contents) { |
515 | modified_files.insert(filename); | 532 | modified_files.insert(filename); |
516 | file_cache[filename] = contents; | 533 | file_cache[filename] = contents; |
517 | } | 534 | } |