Skip to content

Proposal: Add tempdir utilities Stdenv.tmp, Path.open_temp_dir, etc. #831

@vog

Description

@vog

Currently, Eio has no facilities for managing temporary files directories.

I propose to add a new capability Eio.Stdenv.tmp that points to the system temporary directory (e.g. /tmp), which represents the capability to create temporary files or directories. In a first step I'd propose to just wrap Filename.get_temp_dir_name:

let tmp : Eio.Fs.dir_ty Eio.Path.t =
  Path.(fs / Eio_unix.run_in_systhread Filename.get_temp_dir_name)

Of course, some solution without Eio_unix.run_in_systhread would also be nice.

In addition I'd propose to add a typical open/with pair of functions to Eio.Path for creating (and automatically removing) temporary directories:

let open_temp_dir ~(sw : Switch.t) ?(perms : int = 0o700)
    ?(prefix : string = "tmp-") ?(suffix : string = "")
    (tmp : Fs.dir_ty Path.t) : [< `Close | Fs.dir_ty ] Path.t
    =
  let temp_dir_string =
    let tmp_string = native_exn tmp in
    Eio_unix.run_in_systhread @@ fun () ->
    Filename.temp_dir ~temp_dir:tmp_string ~perms prefix suffix
  in
  let temp_dir = tmp / temp_dir_string in
  Switch.on_release sw (fun () -> rmtree ~missing_ok:true temp_dir);
  open_dir ~sw temp_dir
let with_temp_dir (type a) ?(perms : int option) ?(prefix : string option)
    ?(suffix : string option) (tmp : Fs.dir_ty Path.t)
    (f : [< `Close | Fs.dir_ty ] Path.t -> a) : a =
  Switch.run @@ fun sw ->
  let temp_dir = open_temp_dir ~sw ?perms ?prefix ?suffix tmp in
  f temp_dir

As seen above, I'd propose to differ from Filename.temp_dir in that prefix and suffix should be optional with their typically used defaults. And, again, in the future we might replace it with something that doesn't need Eio_unix.run_in_systhread.

The usage would look like this:

let () =
  Eio_main.run @@ fun env ->
  let tmp = Eio.Stdenv.tmp env in
  Eio.Path.with_temp_dir tmp begin fun temp_dir ->
    ...
  end

In addition, one might think of functions to create temporary files as well, but I believe the above would be the perfect start, as once you can safely create temporary directories, you can also put any further files into it. The latter has the advantage that you have full control over their filenames, which is especially handy when wrapping command line tools.

  • Does this make sense to you?
  • Would you accept a PR implementing this?
  • Do you have proposals for improvement?
  • Would it make sense to have a separate resource type for Stdenv.tmp? This way, we could restrict this to the creation of files and directories, without allowing a function to access any other temp files that some other corner of the application might have created. On the other hand, this might be overengineering. What do you think?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions