From: Paulo Moura Date: Tue, 18 May 2021 07:41:19 +0000 (+0100) Subject: Add delete_directory/1 predicate to the files library X-Git-Tag: v0.9.0~62^2 X-Git-Url: https://git.sagredo.dev/?a=commitdiff_plain;h=eeac3bc4368a2fcaf7f051a3bcb4e75ee2a05339;p=scryer-prolog.git Add delete_directory/1 predicate to the files library --- diff --git a/src/clause_types.rs b/src/clause_types.rs index 7820a9fd..56ce7e6c 100644 --- a/src/clause_types.rs +++ b/src/clause_types.rs @@ -174,6 +174,7 @@ pub(crate) enum SystemClauseType { DirectorySeparator, MakeDirectory, DeleteFile, + DeleteDirectory, WorkingDirectory, PathCanonical, FileTime, @@ -335,6 +336,7 @@ impl SystemClauseType { &SystemClauseType::DirectorySeparator => clause_name!("$directory_separator"), &SystemClauseType::MakeDirectory => clause_name!("$make_directory"), &SystemClauseType::DeleteFile => clause_name!("$delete_file"), + &SystemClauseType::DeleteDirectory => clause_name!("$delete_directory"), &SystemClauseType::WorkingDirectory => clause_name!("$working_directory"), &SystemClauseType::PathCanonical => clause_name!("$path_canonical"), &SystemClauseType::FileTime => clause_name!("$file_time"), @@ -744,6 +746,7 @@ impl SystemClauseType { ("$directory_separator", 1) => Some(SystemClauseType::DirectorySeparator), ("$make_directory", 1) => Some(SystemClauseType::MakeDirectory), ("$delete_file", 1) => Some(SystemClauseType::DeleteFile), + ("$delete_directory", 1) => Some(SystemClauseType::DeleteDirectory), ("$working_directory", 2) => Some(SystemClauseType::WorkingDirectory), ("$path_canonical", 2) => Some(SystemClauseType::PathCanonical), ("$file_time", 3) => Some(SystemClauseType::FileTime), diff --git a/src/lib/files.pl b/src/lib/files.pl index e88f53a2..35dacf09 100644 --- a/src/lib/files.pl +++ b/src/lib/files.pl @@ -51,6 +51,7 @@ file_exists/1, directory_exists/1, delete_file/1, + delete_directory/1, make_directory/1, working_directory/2, path_canonical/2, @@ -95,11 +96,21 @@ delete_file(File) :- list_of_chars(File), '$delete_file'(File). +delete_directory(Directory) :- + directory_must_exist(Directory, delete_directory/1), + list_of_chars(Directory), + '$delete_directory'(Directory). + file_must_exist(File, Context) :- ( file_exists(File) -> true ; throw(error(existence_error(file, File), Context)) ). +directory_must_exist(Directory, Context) :- + ( directory_exists(Directory) -> true + ; throw(error(existence_error(directory, Directory), Context)) + ). + /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Dir0 is the current working directory, and the working directory is changed to Dir. diff --git a/src/machine/system_calls.rs b/src/machine/system_calls.rs index 6e54145d..aefaf864 100644 --- a/src/machine/system_calls.rs +++ b/src/machine/system_calls.rs @@ -889,6 +889,17 @@ impl MachineState { } } } + &SystemClauseType::DeleteDirectory => { + let directory = self.heap_pstr_iter(self[temp_v!(1)]).to_string(); + + match fs::remove_dir(directory) { + Ok(_) => {} + _ => { + self.fail = true; + return Ok(()); + } + } + } &SystemClauseType::WorkingDirectory => { if let Ok(dir) = env::current_dir() { let current = match dir.to_str() {