library(files)
defines four predicates pertaining to deleting and
renaming files. rename/2
and dec10_rename/2
are
identical replacements for the DEC-10 Prolog/C-Prolog rename/2
command.
They should only be used to convert old code to Quintus Prolog.
New programs should use delete_file/1
and rename_file/2
.
delete_file(
+FileName)
| ?- delete_file('ask.otl'). yes | ?- delete_file('does_not_exist'). ! Existence error in delete_file/1 ! file nosuch does not exist ! O/S error : No such file or directory ! goal: delete_file(does_not_exist)
| ?- unix(system('cat </dev/null >search.d/tmp')), unix(system('chmod a-w search.d')), delete_file('search.d/tmp'). ! O/S error : Permission denied ! goal: delete_file('search.d/tmp')
| ?- delete_file("tmp"). ! Type error in argument 1 of delete_file/1 ! symbol expected, but [116,109,112] found ! goal: delete_file([116,109,112]) | ?- unix(system('rm tmp')). rm: override protection 444 for tmp? n yes % did NOT delete the file | ?- delete_file(tmp). yes % **DID** delete the file
This last example is important: the rm
command (see rm(1)
)
checks the permission bits of the file
(see chmod(1)
) and asks you whether you
really want to delete a file that you do not have write
permission for, even if you have permission to delete it.
delete_file/1
does not do this.
rename_file(
+OldName,
+NewName)
| ?- rename_file(does_not_exist, imaginary). ! Existence error in rename_file/2 ! file does_not_exist does not exist ! O/S error : No such file or directory ! goal: rename_file(does_not_exist,imaginary)
rename_file/2
and delete_file/1
have no effect on currently
open streams, whether opened by open/3
, see/1
, or tell/1
.
What will happen if you continue to use streams that used to be connected to files affected by these commands is system-dependent. Under UNIX, input will continue to come from a file as if it had not been renamed, and output will continue to go to a file as if it had not been renamed. For example:
% prolog | ?- compile(library(files)). <output of compile/1> yes | ?- open(fred, write, OutputStream), open(fred, read, InputStream ), delete_file(fred), format(OutputStream, 'foo.~n', []), flush_output(OutputStream), read(InputStream, Term), close(OutputStream), close(InputStream). OutputStream = '$stream'(10,3), InputStream = '$stream'(11,4), Term = foo
rename(
+OldName,
+NewName)
dec10_rename/2
below.
dec10_rename(
+OldName,
+NewName)
rename/2
, and is provided solely for the sake of
compatibility. If you are converting existing DEC-10 Prolog or C-Prolog code
to Quintus Prolog, the fact that rename/2
does close the
file and is sensitive to the fileerrors
flag should be useful.
In new programs we recommend the use of rename_file/2
and delete_file/1
.
OldName and
NewName must be atoms, otherwise an error is reported and
the command fails (this is not affected by the setting of
the fileerrors
flag). If NewName is []
, the file named by
OldName is deleted, otherwise it is renamed to
NewName. If the rename cannot be performed, what happens
next depends on the setting of the fileerrors
flag (see
the reference page for prolog_flag/3
).
If fileerrors
is on
, an error exception is raised. If fileerrors
is off
, the
command fails quietly. Examples:
| ?- prolog_flag(fileerrors, Setting). Setting = on | ?- dec10_rename(2, 1). ! Type error in argument 1 of dec10_rename/2 ! symbol expected, but 2 found ! goal: dec10_rename(2,1)
| ?- dec10_rename(does_not_exist, []). ! Existence error in dec10_rename/2 ! file does_not_exist does not exist ! O/S error : No such file or directory ! goal: dec10_rename(does_not_exist,[]) | ?- prolog_flag(fileerrors, _, off). yes
| ?- dec10_rename("old", "new"). ! Type error in argument 1 of dec10_rename/2 ! symbol expected, but [111,108,100] found ! goal: dec10_rename([111,108,100],[110,101,119]) | ?- dec10_rename(does_not_exist, []). no