From 9fb63762dc90dcb037b0f008aac9fc218f3b3aea Mon Sep 17 00:00:00 2001 From: Vitor Santos Costa Date: Tue, 12 Feb 2013 16:21:26 -0600 Subject: [PATCH] delete_file should be SICStus compatible --- H/pl-incl.h | 2 ++ library/system.yap | 70 ++++++++++++++++++++++++++++++++++++++++++++++ os/pl-files.c | 4 +-- 3 files changed, 74 insertions(+), 2 deletions(-) diff --git a/H/pl-incl.h b/H/pl-incl.h index 261c8563d..1dbb03586 100755 --- a/H/pl-incl.h +++ b/H/pl-incl.h @@ -46,6 +46,8 @@ #define PLVERSION YAP_VERSION #define PLNAME "yap" +#define SWIP "swi_" + /* try not to pollute the SWI space */ #ifdef P #undef P diff --git a/library/system.yap b/library/system.yap index c6262291a..bd30d8c5f 100644 --- a/library/system.yap +++ b/library/system.yap @@ -88,7 +88,77 @@ check_int(I, Inp) :- throw(error(type_error(integer,I),Inp)). % file operations +% file operations +delete_file(IFile) :- + true_file_name(IFile, File), + delete_file(File, off, on, off). + +delete_file(IFile, Opts) :- + true_file_name(IFile, File), + process_delete_file_opts(Opts, Dir, Recurse, Ignore, delete_file(File,Opts)), + delete_file(File, Dir, Recurse, Ignore). + +process_delete_file_opts(V, _, _, _, T) :- var(V), !, + throw(error(instantiation_error,T)). +process_delete_file_opts([], off, off, off, _) :- !. +process_delete_file_opts([V|_], _, _, _, T) :- var(V), !, + throw(error(instantiation_error,T)). +process_delete_file_opts([directory|Opts], on, Recurse, Ignore, T) :- !, + process_delete_file_opts(Opts, _, Recurse, Ignore, T). +process_delete_file_opts([recursive|Opts], Dir, on, Ignore, T) :- !, + process_delete_file_opts(Opts, Dir, _, Ignore, T). +process_delete_file_opts([ignore|Opts], Dir, Recurse, on, T) :- !, + process_delete_file_opts(Opts, Dir, Recurse, _, T). +process_delete_file_opts(Opts, _, _, _, T) :- + throw(error(domain_error(delete_file_option,Opts),T)). + +delete_file(IFile, Dir, Recurse, Ignore) :- + true_file_name(IFile, File), + file_property(File, Type, _, _, _Permissions, _, Ignore), + delete_file(Type, File, Dir, Recurse, Ignore). + +delete_file(N, File, _Dir, _Recurse, Ignore) :- number(N), !, % error. + handle_system_error(N, Ignore, delete_file(File)). +delete_file(directory, File, Dir, Recurse, Ignore) :- + delete_directory(Dir, File, Recurse, Ignore). +delete_file(_, File, _Dir, _Recurse, Ignore) :- + unlink_file(File, Ignore). + +unlink_file(IFile, Ignore) :- + true_file_name(IFile, File), + unlink(File, N), + handle_system_error(N, Ignore, delete_file(File)). + +delete_directory(on, File, _Recurse, Ignore) :- + rm_directory(File, Ignore). +delete_directory(off, File, Recurse, Ignore) :- + delete_directory(Recurse, File, Ignore). + +rm_directory(File, Ignore) :- + rmdir(File, Error), + handle_system_error(Error, Ignore, delete_file(File)). + +delete_directory(on, File, Ignore) :- + directory_files(File, FileList, Ignore), + path_separator(D), + atom_concat(File, D, FileP), + delete_dirfiles(FileList, FileP, Ignore), + rmdir(File, Ignore). + +delete_dirfiles([], _, _). +delete_dirfiles(['.'|Fs], File, Ignore) :- !, + delete_dirfiles(Fs, File, Ignore). +delete_dirfiles(['..'|Fs], File, Ignore) :- !, + delete_dirfiles(Fs, File, Ignore). +delete_dirfiles([F|Fs], File, Ignore) :- + atom_concat(File,F,TrueF), + delete_file(TrueF, off, on, Ignore), + delete_dirfiles(Fs, File, Ignore). + +directory_files(File, FileList, Ignore) :- + list_directory(File, FileList, Error), + handle_system_error(Error, Ignore, directory_files(File, FileList)). handle_system_error(Error, _Ignore, _G) :- var(Error), !. handle_system_error(Error, off, G) :- atom(Error), !, diff --git a/os/pl-files.c b/os/pl-files.c index 2c290d6e1..4642b8e68 100644 --- a/os/pl-files.c +++ b/os/pl-files.c @@ -828,7 +828,7 @@ PRED_IMPL("tmp_file_stream", 3, tmp_file_stream, 0) static -PRED_IMPL("delete_file", 1, delete_file, 0) +PRED_IMPL("swi_delete_file", 1, delete_file, 0) { PRED_LD char *n; atom_t aname; @@ -1125,7 +1125,7 @@ BeginPredDefs(files) PRED_DEF("exists_directory", 1, exists_directory, 0) PRED_DEF("tmp_file", 2, tmp_file, 0) PRED_DEF("tmp_file_stream", 3, tmp_file_stream, 0) - PRED_DEF("delete_file", 1, delete_file, 0) + PRED_DEF("swi_delete_file", 1, delete_file, 0) PRED_DEF("delete_directory", 1, delete_directory, 0) PRED_DEF("make_directory", 1, make_directory, 0) PRED_DEF("same_file", 2, same_file, 0)