]> Repositorios git - scryer-prolog.git/commitdiff
Add delete_directory/1 predicate to the files library
authorPaulo Moura <[email protected]>
Tue, 18 May 2021 07:41:19 +0000 (08:41 +0100)
committerPaulo Moura <[email protected]>
Tue, 18 May 2021 07:41:19 +0000 (08:41 +0100)
src/clause_types.rs
src/lib/files.pl
src/machine/system_calls.rs

index 7820a9fd80b29b9e58c87d341be7c865aad0f027..56ce7e6c0a49a2f2372c25ec7f0b8c6801d4c8a5 100644 (file)
@@ -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),
index e88f53a204b34ca7fc1ca6b0413b8a2d3538f3c1..35dacf09c4d2ec1d0d002ec05dd9797a38c2b283 100644 (file)
@@ -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.
index 6e54145da5842410047b85e8fa8b9a13fd9f6463..aefaf864077dd73387b87f1b531349b413ee201f 100644 (file)
@@ -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() {