# # patch "ChangeLog" # from [dc06fba2868bbb006f49aa90a4afd795a3c90397] # to [ae55065c097bcb4bf1f805408b49611d5533ddf5] # # patch "file_io.cc" # from [fabe4ff3fc7938a63350416b31446b04d5e331b6] # to [818512988302bec4b55e5221dd221a77e5532ec0] # ======================================================================== --- ChangeLog dc06fba2868bbb006f49aa90a4afd795a3c90397 +++ ChangeLog ae55065c097bcb4bf1f805408b49611d5533ddf5 @@ -1,5 +1,13 @@ 2005-08-25 Nathaniel Smith + * file_io.cc (assert_path_is_nonexistent, assert_path_is_file) + (assert_path_is_directory, require_path_is_nonexistent) + (require_path_is_file, require_path_is_directory) + (ident_existing_file, mkdir_p, make_dir_for, delete_file) + (delete_dir_recursive): Implement. + +2005-08-25 Nathaniel Smith + * Audit uses of 'file_exists', because its semantics have changed; it now checks to see if a path exists and is a regular file, rather than that it simply exists. A fair amount of code already ======================================================================== --- file_io.cc fabe4ff3fc7938a63350416b31446b04d5e331b6 +++ file_io.cc 818512988302bec4b55e5221dd221a77e5532ec0 @@ -15,12 +15,74 @@ #include "lua.hh" #include "sanity.hh" #include "transforms.hh" +#include "platform.hh" // this file deals with talking to the filesystem, loading and // saving files. using namespace std; +void +assert_path_is_nonexistent(any_path const & path) +{ + I(get_path_status(path) == path::nonexistent); +} + +void +assert_path_is_file(any_path const & path) +{ + I(get_path_status(path) == path::file); +} + +void +assert_path_is_directory(any_path const & path); +{ + I(get_path_status(path) == path::directory); +} + +void +require_path_is_nonexistent(any_path const & path, + boost::format const & message) +{ + N(!path_exists(path), message); +} + +void +require_path_is_file(any_path const & path, + boost::format const & message_if_nonexistent, + boost::format const & message_if_directory) +{ + switch (get_path_status(path)) + { + case path::nonexistent: + N(false, message_if_nonexistent); + break; + case path::file: + return; + case path::directory: + N(false, message_if_directory); + break; + } +} + +void +require_path_is_directory(any_path const & path, + boost::format const & message_if_nonexistent, + boost::format const & message_if_file) +{ + switch (get_path_status(path)) + { + case path::nonexistent: + N(false, message_if_nonexistent); + break; + case path::file: + N(false, message_if_file); + case path::directory: + return; + break; + } +} + bool path_exists(any_path const & p) { @@ -42,13 +104,18 @@ bool ident_existing_file(file_path const & p, file_id & ident, lua_hooks & lua) { - fs::path local_p(localized(p)); + fs::path local_p(p.as_external); if (!fs::exists(local_p)) return false; - if (fs::is_directory(local_p)) + switch (get_path_status(p)) { + case path::nonexistent: + return false; + case path::file: + break; + case path::directory: W(F("expected file '%s', but it is a directory.") % p()); return false; } @@ -73,24 +140,33 @@ } void -delete_file(local_path const & p) +mkdir_p(any_path const & p) { - N(file_exists(p), - F("file to delete '%s' does not exist") % p); - fs::remove(localized(p)); + fs::create_directories(p.as_external()); } void -delete_file(file_path const & p) +make_dir_for(any_path const & p) { - N(file_exists(p), - F("file to delete '%s' does not exist") % p); - fs::remove(localized(p)); + fs::path tmp(p.as_external(), fs::native); + if (tmp.has_branch_path()) + { + fs::create_directories(tmp.branch_path()); + } } void -delete_dir_recursive(file_path const & p) +delete_file(any_path const & p) { + require_path_is_file(p, + F("file to delete '%s' does not exist") % p, + F("file to delete, '%s', is not a file but a directory") % p); + fs::remove(p.as_external()); +} + +void +delete_dir_recursive(any_path const & p) +{ N(directory_exists(p), F("directory to delete '%s' does not exist") % p); fs::remove_all(localized(p)); @@ -152,28 +228,6 @@ localized(new_path)); } -void -mkdir_p(local_path const & p) -{ - fs::create_directories(localized(p)); -} - -void -mkdir_p(file_path const & p) -{ - fs::create_directories(localized(p)); -} - -void -make_dir_for(file_path const & p) -{ - fs::path tmp = localized(p); - if (tmp.has_branch_path()) - { - fs::create_directories(tmp.branch_path()); - } -} - static void read_data_impl(fs::path const & p, data & dat)