# # patch "ChangeLog" # from [5e40e2689e6d8938571513054cfd7398daa50333] # to [f58f51b0e61938788c1c383f434efa2d0878d009] # # patch "app_state.cc" # from [df20ed8050fffe10bcc15ed02d546f32b6869271] # to [e9c1fc2b7db033ec7a595f2d555d981d7058e788] # # patch "commands.cc" # from [4ebc397acf584d56851c22da3e9c043c37f01b8b] # to [d1359da214f1e6da840ff7a3cb14272e7c0bc6d4] # # patch "database.cc" # from [14d91115f61f8eec147d98df9d5bcb29e028ccbd] # to [b160ec3991f2e2b57e8bcdc46c4a90da34c34ecf] # # patch "file_io.cc" # from [a3744fefaa6ad87cfefecaed427b21adc16581bd] # to [548d122b7de1dda7d5d0f45e9ae3ba0d82cb4d17] # # patch "file_io.hh" # from [9bab162dd5812f8bcd47cb881e6337b06f8ba191] # to [eeb9bf17b3e142348bcacffee768fb75ee9a4241] # # patch "lua.cc" # from [674472a130344358fe313b2313834a58de0a048e] # to [2e3108f8ec82ea73a605aa3ee1c60461220733c4] # # patch "rcs_import.cc" # from [d06a24693ec6589feae476c622b542bc212faffb] # to [aa25b973c3d4e38c3f7e7695ff5c270dc81ef338] # # patch "transforms.cc" # from [f5ba47a9ad3725a4bdd5341328ae91ae6ac0410e] # to [5f58fbc79d392c30ccbe9d72ef2f5d057dc286d0] # # patch "work.cc" # from [865b13a4864fdb4e7a2199c1bd39a04ea9fb6cb0] # to [05d5c32cb54e9701beff52303846ce1d2c7bb35c] # ======================================================================== --- ChangeLog 5e40e2689e6d8938571513054cfd7398daa50333 +++ ChangeLog f58f51b0e61938788c1c383f434efa2d0878d009 @@ -1,5 +1,12 @@ 2005-08-24 Nathaniel Smith + * file_io.hh + ({assert,require}_path_is_{nonexistent,file,directory}): New + functions. + Use them everywhere. + +2005-08-24 Nathaniel Smith + * basic_io.hh: #include "paths.hh". * monotone.cc (add_rcfile): Remove obsolete absolutification, etc. ======================================================================== --- app_state.cc df20ed8050fffe10bcc15ed02d546f32b6869271 +++ app_state.cc e9c1fc2b7db033ec7a595f2d555d981d7058e788 @@ -276,10 +276,9 @@ void app_state::set_root(system_path const & path) { - N(path_state(path), - F("search root '%s' does not exist\n") % path); - N(is_directory(path), - F("search root '%s' is not a directory\n") % path); + require_path_is_directory(path, + F("search root '%s' does not exist") % path, + F("search root '%s' is not a directory\n") % path); search_root = path; L(F("set search root to %s\n") % search_root); } ======================================================================== --- commands.cc 4ebc397acf584d56851c22da3e9c043c37f01b8b +++ commands.cc d1359da214f1e6da840ff7a3cb14272e7c0bc6d4 @@ -282,7 +282,7 @@ { if (path.empty()) return; - N(!path_state(path), F("pid file '%s' already exists") % path); + require_path_is_nonexistent(path, F("pid file '%s' already exists") % path); file.open(path.as_external().c_str()); file << get_process_id(); file.flush(); ======================================================================== --- database.cc 14d91115f61f8eec147d98df9d5bcb29e028ccbd +++ database.cc b160ec3991f2e2b57e8bcdc46c4a90da34c34ecf @@ -131,29 +131,24 @@ static void check_sqlite_format_version(system_path const & filename) { - switch (path_state(filename)) + require_path_is_file(filename, + F("database %s does not exist") % filename, + F("%s is a directory, not a database") % filename); + + // sqlite 3 files begin with this constant string + // (version 2 files begin with a different one) + std::string version_string("SQLite format 3"); + + std::ifstream file(filename.as_external().c_str()); + N(file, F("unable to probe database version in file %s") % filename); + + for (std::string::const_iterator i = version_string.begin(); + i != version_string.end(); ++i) { - case path::nonexistent: - return; - case path::directory: - N(false, F("database %s is a directory") % filename); - break; - case path::file: - // sqlite 3 files begin with this constant string - // (version 2 files begin with a different one) - std::string version_string("SQLite format 3"); - - std::ifstream file(filename.as_external().c_str()); - N(file, F("unable to probe database version in file %s") % filename); - - for (std::string::const_iterator i = version_string.begin(); - i != version_string.end(); ++i) - { - char c; - file.get(c); - N(c == *i, F("database %s is not an sqlite version 3 file, " - "try dump and reload") % filename); - } + char c; + file.get(c); + N(c == *i, F("database %s is not an sqlite version 3 file, " + "try dump and reload") % filename); } } @@ -178,13 +173,9 @@ N(!filename.empty(), F("no database specified")); if (! init) - { - path::state state = path_state(filename); - N(state != path::nonexistent, - F("database %s does not exist") % filename); - N(state != path::directory, - F("database %s is a directory") % filename); - } + require_path_is_file(filename, + F("database %s does not exist") % filename, + F("%s is a directory, not a database") % filename); check_sqlite_format_version(filename); int error; @@ -215,11 +206,12 @@ F("could not initialize database: %s: already exists") % filename); - system_path journal(filename.as_external() + "-journal"); - N(!path_state(journal), - F("existing (possibly stale) journal file '%s' " - "has same stem as new database '%s'") - % journal % filename); + system_path journal(filename.as_internal() + "-journal"); + require_path_is_nonexistent(journal, + F("existing (possibly stale) journal file '%s' " + "has same stem as new database '%s'\n" + "cancelling database creation") + % journal % filename) sqlite3 *s = sql(true); I(s != NULL); @@ -334,8 +326,8 @@ N(!filename.empty(), F("need database name")); - N(!path_state(filename), - F("cannot create %s; it already exists") % filename); + require_path_is_nonexistent(filename, + F("cannot create %s; it already exists") % filename); int error = sqlite3_open(filename.as_external().c_str(), &__sql); if (error) throw oops((F("could not open database: %s: %s") ======================================================================== --- file_io.cc a3744fefaa6ad87cfefecaed427b21adc16581bd +++ file_io.cc 548d122b7de1dda7d5d0f45e9ae3ba0d82cb4d17 @@ -215,6 +215,22 @@ return false; } + +// A path::state can be used as a boolean context for exists/doesn't exist, +// or can be used in a switch statement to consider all possibilities. +namespace path +{ + typedef enum + { + nonexistent = 0, + directory = 1, + file = 2, + } state; +}; +path::state path_state(any_path const & path); + + + bool directory_exists(local_path const & p) { ======================================================================== --- file_io.hh 9bab162dd5812f8bcd47cb881e6337b06f8ba191 +++ file_io.hh eeb9bf17b3e142348bcacffee768fb75ee9a4241 @@ -6,6 +6,8 @@ // licensed to the public under the terms of the GNU GPL (>= 2) // see the file COPYING for details +#include "boost/format.hpp" + #include "vocab.hh" #include "paths.hh" @@ -43,22 +45,27 @@ fs::path & working_copy_restriction); system_path get_homedir(); -std::string absolutify_for_command_line(std::string const & path); -// A path::state can be used as a boolean context for exists/doesn't exist, -// or can be used in a switch statement to consider all possibilities. -namespace path -{ - typedef enum - { - nonexistent = 0, - directory = 1, - file = 2, - } state; -}; -path::state path_state(any_path const & path); +// use I() +void assert_path_is_nonexistent(any_path const & path); +void assert_path_is_file(any_path const & path); +void assert_path_is_directory(any_path const & path); +// use N() +void require_path_is_nonexistent(any_path const & path, + boost::format const & message); +void require_path_is_file(any_path const & path, + boost::format const & message_if_nonexistent, + boost::format const & message_if_directory); +void require_path_is_directory(any_path const & path, + boost::format const & message_if_nonexistent, + boost::format const & message_if_file); + +// returns true if there is a file or directory at 'path' +bool path_exists(any_path const & path); +// returns true if there is a directory at 'path' bool directory_exists(any_path const & path); +// returns true if there is a file at 'path' bool file_exists(any_path const & path); bool ident_existing_file(file_path const & p, file_id & ident, lua_hooks & lua); ======================================================================== --- lua.cc 674472a130344358fe313b2313834a58de0a048e +++ lua.cc 2e3108f8ec82ea73a605aa3ee1c60461220733c4 @@ -702,7 +702,7 @@ lua_hooks::load_rcfile(any_path const & rc, bool required) { I(st); - if (path_state(rc)) + if (path_exists(rc)) { L(F("opening rcfile '%s' ...\n") % rc); N(run_file(st, rc.as_external()), ======================================================================== --- rcs_import.cc d06a24693ec6589feae476c622b542bc212faffb +++ rcs_import.cc aa25b973c3d4e38c3f7e7695ff5c270dc81ef338 @@ -725,7 +725,7 @@ cvs_history cvs; I(! filename.empty()); - I(path_state(filename) == path::file); + assert_path_is_file(filename); P(F("parsing RCS file %s\n") % filename); rcs_file r; @@ -1219,7 +1219,7 @@ import_cvs_repo(system_path const & cvsroot, app_state & app) { - N(path_state(cvsroot / "CVSROOT") != path::directory, + N(!directory_exists(cvsroot / "CVSROOT"), F("%s appears to be a CVS repository root directory\n" "try importing a module instead, with 'cvs_import %s/") % cvsroot % cvsroot); @@ -1244,10 +1244,9 @@ { transaction_guard guard(app.db); cvs_tree_walker walker(cvs, app.db); - N(path_state(cvsroot) != path::nonexistent, - F("path %s does not exist") % cvsroot); - N(path_state(cvsroot) == path::directory, - F("path %s is not a directory") % cvsroot); + require_path_is_directory(cvsroot, + F("path %s does not exist") % cvsroot, + F("path %s is not a directory") % cvsroot); app.db.ensure_open(); change_current_working_dir(cvsroot); walk_tree(walker); ======================================================================== --- transforms.cc f5ba47a9ad3725a4bdd5341328ae91ae6ac0410e +++ transforms.cc 5f58fbc79d392c30ccbe9d72ef2f5d057dc286d0 @@ -347,7 +347,7 @@ { // no conversions necessary, use streaming form // Best to be safe and check it isn't a dir. - I(path_state(file) == path::file); + assert_path_is_file(file); Botan::Pipe p(new Botan::Hash_Filter("SHA-1"), new Botan::Hex_Encoder()); Botan::DataSource_Stream infile(file.as_external()); p.process_msg(infile); ======================================================================== --- work.cc 865b13a4864fdb4e7a2199c1bd39a04ea9fb6cb0 +++ work.cc 05d5c32cb54e9701beff52303846ce1d2c7bb35c @@ -320,7 +320,7 @@ { bookkeeping_path w_path; get_work_path(w_path); - if (path_state(w_path)) + if (path_exists(w_path)) { L(F("checking for un-committed work file %s\n") % w_path); data w_data;