Add delete_directory/1 predicate to the files library
This commit is contained in:
@@ -174,6 +174,7 @@ pub(crate) enum SystemClauseType {
|
|||||||
DirectorySeparator,
|
DirectorySeparator,
|
||||||
MakeDirectory,
|
MakeDirectory,
|
||||||
DeleteFile,
|
DeleteFile,
|
||||||
|
DeleteDirectory,
|
||||||
WorkingDirectory,
|
WorkingDirectory,
|
||||||
PathCanonical,
|
PathCanonical,
|
||||||
FileTime,
|
FileTime,
|
||||||
@@ -335,6 +336,7 @@ impl SystemClauseType {
|
|||||||
&SystemClauseType::DirectorySeparator => clause_name!("$directory_separator"),
|
&SystemClauseType::DirectorySeparator => clause_name!("$directory_separator"),
|
||||||
&SystemClauseType::MakeDirectory => clause_name!("$make_directory"),
|
&SystemClauseType::MakeDirectory => clause_name!("$make_directory"),
|
||||||
&SystemClauseType::DeleteFile => clause_name!("$delete_file"),
|
&SystemClauseType::DeleteFile => clause_name!("$delete_file"),
|
||||||
|
&SystemClauseType::DeleteDirectory => clause_name!("$delete_directory"),
|
||||||
&SystemClauseType::WorkingDirectory => clause_name!("$working_directory"),
|
&SystemClauseType::WorkingDirectory => clause_name!("$working_directory"),
|
||||||
&SystemClauseType::PathCanonical => clause_name!("$path_canonical"),
|
&SystemClauseType::PathCanonical => clause_name!("$path_canonical"),
|
||||||
&SystemClauseType::FileTime => clause_name!("$file_time"),
|
&SystemClauseType::FileTime => clause_name!("$file_time"),
|
||||||
@@ -744,6 +746,7 @@ impl SystemClauseType {
|
|||||||
("$directory_separator", 1) => Some(SystemClauseType::DirectorySeparator),
|
("$directory_separator", 1) => Some(SystemClauseType::DirectorySeparator),
|
||||||
("$make_directory", 1) => Some(SystemClauseType::MakeDirectory),
|
("$make_directory", 1) => Some(SystemClauseType::MakeDirectory),
|
||||||
("$delete_file", 1) => Some(SystemClauseType::DeleteFile),
|
("$delete_file", 1) => Some(SystemClauseType::DeleteFile),
|
||||||
|
("$delete_directory", 1) => Some(SystemClauseType::DeleteDirectory),
|
||||||
("$working_directory", 2) => Some(SystemClauseType::WorkingDirectory),
|
("$working_directory", 2) => Some(SystemClauseType::WorkingDirectory),
|
||||||
("$path_canonical", 2) => Some(SystemClauseType::PathCanonical),
|
("$path_canonical", 2) => Some(SystemClauseType::PathCanonical),
|
||||||
("$file_time", 3) => Some(SystemClauseType::FileTime),
|
("$file_time", 3) => Some(SystemClauseType::FileTime),
|
||||||
|
|||||||
@@ -51,6 +51,7 @@
|
|||||||
file_exists/1,
|
file_exists/1,
|
||||||
directory_exists/1,
|
directory_exists/1,
|
||||||
delete_file/1,
|
delete_file/1,
|
||||||
|
delete_directory/1,
|
||||||
make_directory/1,
|
make_directory/1,
|
||||||
working_directory/2,
|
working_directory/2,
|
||||||
path_canonical/2,
|
path_canonical/2,
|
||||||
@@ -95,11 +96,21 @@ delete_file(File) :-
|
|||||||
list_of_chars(File),
|
list_of_chars(File),
|
||||||
'$delete_file'(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_must_exist(File, Context) :-
|
||||||
( file_exists(File) -> true
|
( file_exists(File) -> true
|
||||||
; throw(error(existence_error(file, File), Context))
|
; 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
|
Dir0 is the current working directory, and the working directory
|
||||||
is changed to Dir.
|
is changed to Dir.
|
||||||
|
|||||||
@@ -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 => {
|
&SystemClauseType::WorkingDirectory => {
|
||||||
if let Ok(dir) = env::current_dir() {
|
if let Ok(dir) = env::current_dir() {
|
||||||
let current = match dir.to_str() {
|
let current = match dir.to_str() {
|
||||||
|
|||||||
Reference in New Issue
Block a user