Files
mercury/tests/hard_coded/dir_fold.m
Zoltan Somogyi 25b4b67403 Carve io.file.m out of io.m.
library/io.file.m:
library/io.m:
    Move two sections of io.m, the "file handling predicates" section
    and the "handling temporary files" section to the new submodule io.file.m.

    Leave behind in io.m "forwarding predicates", predicates that do nothing
    except call the moved predicates in io.file.m, to provide backward
    compatibility. But do mark the forwarding predicates as obsolete,
    to tell people to update their (at their leisure, since the obsoleteness
    warning can be turned off).

    Also leave behind in io.m the definitions of the two types used
    by some parameters of some of the moved predicates. Document the reason
    why this is done.

library/MODULES_DOC:
    List the new module among the documented modules.

NEWS:
    Announce the changes.

browser/browse.m:
browser/interactive_query.m:
browser/listing.m:
compiler/analysis.file.m:
compiler/compile_target_code.m:
compiler/export.m:
compiler/fact_table.m:
compiler/file_util.m:
compiler/handle_options.m:
compiler/make.build.m:
compiler/make.module_dep_file.m:
compiler/make.module_target.m:
compiler/make.program_target.m:
compiler/make.util.m:
compiler/mercury_compile_main.m:
compiler/module_cmds.m:
compiler/parse_module.m:
compiler/passes_aux.m:
compiler/prog_event.m:
compiler/recompilation.check.m:
compiler/write_deps_file.m:
compiler/write_module_interface_files.m:
deep_profiler/conf.m:
deep_profiler/mdprof_cgi.m:
library/dir.m:
mdbcomp/program_representation.m:
ssdb/ssdb.m:
    Call the file operation predicates directly in io.file.m, not indirectly
    through io.m.

    In two modules, add a #include of fcntl.h in C code. These modules contain
    C code that needs this #include, but until now, they got it via a copy
    in an automatically generated C header file of a foreign_decl pragma
    in io.m that contained that #include. This diff moves that foreign_decl
    to io.file.m, removing that crutch.

tests/debugger/browser_test.m:
tests/hard_coded/bit_buffer_test.m:
tests/hard_coded/bitmap_test.m:
tests/hard_coded/construct_bug.m:
tests/hard_coded/dir_fold.m:
tests/hard_coded/dir_test.m:
tests/hard_coded/read_binary_int16.m:
tests/hard_coded/read_binary_int32.m:
tests/hard_coded/read_binary_int64.m:
tests/hard_coded/read_binary_uint16.m:
tests/hard_coded/read_binary_uint32.m:
tests/hard_coded/read_binary_uint64.m:
tests/hard_coded/read_bitmap_size.m:
tests/hard_coded/remove_file.m:
tests/hard_coded/write_binary.m:
tests/hard_coded/write_binary_int8.m:
tests/hard_coded/write_binary_multibyte_int.m:
tests/hard_coded/write_binary_uint8.m:
    Call the file operation predicates directly in io.file.m, not indirectly
    through io.m.
2022-03-08 06:01:21 +11:00

75 lines
2.0 KiB
Mathematica

%---------------------------------------------------------------------------%
% vim: ts=4 sw=4 et ft=mercury
%---------------------------------------------------------------------------%
:- module dir_fold.
:- interface.
:- import_module io.
:- pred main(io::di, io::uo) is det.
%---------------------------------------------------------------------------%
:- implementation.
:- import_module bool.
:- import_module dir.
:- import_module int.
:- import_module io.file.
:- import_module unit.
%---------------------------------------------------------------------------%
main(!IO) :-
DirName = "empty",
dir.make_directory(DirName, ResMkdir, !IO),
(
ResMkdir = ok,
% dir.foldl2 used to leak a file descriptor on empty directories.
% Repeat enough times to make it apparent. On Linux, the default
% maximum number of open files to a process is 1024.
repeat(DirName, 1025, Res, !IO),
io.file.remove_file(DirName, _, !IO),
(
Res = ok,
io.write_string("done.\n", !IO)
;
Res = error(Error),
report_error(io.error_message(Error), !IO)
)
;
ResMkdir = error(Error),
report_error(io.error_message(Error), !IO)
).
:- pred repeat(string::in, int::in, io.res::out, io::di, io::uo) is det.
repeat(DirName, Count, Res, !IO) :-
( if Count > 0 then
dir.foldl2(nothing, DirName, unit, Res0, !IO),
(
Res0 = ok(_ : unit),
repeat(DirName, Count - 1, Res, !IO)
;
Res0 = error(_, Error),
Res = error(Error)
)
else
Res = ok
).
:- pred nothing(string::in, string::in, io.file_type::in, bool::out,
unit::in, unit::out, io::di, io::uo) is det.
nothing(_DirName, _BaseName, _FileType, Continue, !Data, !IO) :-
Continue = yes.
:- pred report_error(string::in, io::di, io::uo) is det.
report_error(Error, !IO) :-
io.stderr_stream(Stream, !IO),
io.write_string(Stream, Error, !IO),
io.nl(Stream, !IO),
io.set_exit_status(1, !IO).