Module Filesystem_asyncSource

Sourceval executable_name : File_path.t Core.Lazy.t

The currently running executable.

OCaml semantics do not guarantee an absolute path here.

File I/O Wrappers

These functions abstract over either In_channel and Out_channel, or Async.Reader and Async.Writer.

Sourceval read_file : File_path.t -> string Async_kernel.Deferred.t
Sourceval write_file : ?perm:Filesystem_types_unix.File_permissions.t -> File_path.t -> contents:string -> unit Async_kernel.Deferred.t
Sourceval load_as_sexp : File_path.t -> of_sexp:(Core.Sexp.t -> 'a) -> 'a Async_kernel.Deferred.t
Sourceval load_as_sexps : File_path.t -> of_sexp:(Core.Sexp.t -> 'a) -> 'a list Async_kernel.Deferred.t
Sourceval save_as_sexp : ?perm:Filesystem_types_unix.File_permissions.t -> File_path.t -> 'a -> sexp_of:('a -> Core.Sexp.t) -> unit Async_kernel.Deferred.t
Sourceval save_as_sexps : ?perm:Filesystem_types_unix.File_permissions.t -> File_path.t -> 'a list -> sexp_of:('a -> Core.Sexp.t) -> unit Async_kernel.Deferred.t

Filename Wrappers

These functions abstract over Filename_unix.

Sys Wrappers

These functions abstract over either Core.Sys or Async.Sys.

Sourceval exists : ?follow_symlinks:bool -> File_path.t -> [ `Yes | `No | `Unknown ] Async_kernel.Deferred.t
Sourceval exists_exn : ?follow_symlinks:bool -> File_path.t -> bool Async_kernel.Deferred.t
Sourceval is_directory : ?follow_symlinks:bool -> File_path.t -> [ `Yes | `No | `Unknown ] Async_kernel.Deferred.t
Sourceval is_directory_exn : ?follow_symlinks:bool -> File_path.t -> bool Async_kernel.Deferred.t
Sourceval is_file : ?follow_symlinks:bool -> File_path.t -> [ `Yes | `No | `Unknown ] Async_kernel.Deferred.t
Sourceval is_file_exn : ?follow_symlinks:bool -> File_path.t -> bool Async_kernel.Deferred.t

Unix Wrappers

These functions abstract over either Core_unix or Async.Unix.

Please note that rmdir raises on a non-empty directory. If you want to delete a directory and all of its contents, see rm.

Sourceval rename : src:File_path.t -> dst:File_path.t -> unit Async_kernel.Deferred.t
Sourceval update_timestamps : ?at:Core.Time_ns.t -> File_path.t -> unit Async_kernel.Deferred.t

Set the file's access_time and modify_time to at.

Sourceval update_timestamps_separately : File_path.t -> access_time:Core.Time_ns.t -> modify_time:Core.Time_ns.t -> unit Async_kernel.Deferred.t

Set the file's access_time and modify_time to the given values.

Command-line tool functionality

These functions provide similar functionality to command-line tools like rm, either by mimicking them or by calling them directly.

Sourceval rm : ?force:bool -> ?recursive:bool -> File_path.t -> unit Async_kernel.Deferred.t

Removes files.

If ~recursive:true is supplied, also recursively removes directories.

If ~force:false is supplied, will raise if given nonexistent or write-protected files. Defaults to true because programmatically-called rm is not interactive, we expect it to just succeed silently.

Links, both hard and symbolic

Creates the given path as a hard link to referring_to.

Creates the given path as a symbolic link to referring_to.

Reads the contents of a symbolic link.

As symlink. Raises if referring_to is not a valid path, but preserves non-canonical paths such as "multiple//and//trailing/".

As readlink. Preserves non-canonical paths such as "multiple//and//trailing/".

Current Directory Functions

These functions combine File_path and getcwd.

Like File_path.make_absolute ~under:(getcwd ()). Avoids calling getcwd unless necessary.

Like File_path.make_relative ~if_under:(getcwd ()). Avoids calling getcwd unless necessary.

Like make_relative_to_cwd. Raises instead of returning None.

Sourceval make_relative_to_cwd_if_possible : File_path.t -> File_path.t Async_kernel.Deferred.t

Like make_relative_to_cwd. Returns the original path instead of None.

Temporary Files and Directories

Sourceval default_temp_dir : File_path.t Core.Lazy.t

The system-determined default temporary directory.

Has the same value as Core.Filename.temp_dir_name. OCaml semantics do not guarantee an absolute path here.

Sourceval within_temp_dir : ?in_dir:File_path.t -> ?on_cleanup_error: unit Async_kernel.Deferred.t Filesystem_types_unix.On_cleanup_error.t -> ?perm:Filesystem_types_unix.File_permissions.t -> ?prefix:string -> ?suffix:string -> (unit -> 'a Async_kernel.Deferred.t) -> 'a Async_kernel.Deferred.t

Creates a new directory with a unique name, chdirs to it, runs the given function, chdirs back, and then recursively deletes the temporary directory and its contents.

If the directory is renamed or removed before the function ends, there is a race condition. Some other function or process may create a temporary directory or file by the same name, and this function may attempt to delete that.

Creates a new directory with a unique name, runs the given function with the directory's path, and then recursively deletes the temporary directory and its contents.

Has the same race condition as within_temp_dir if the path is renamed or removed before the function finishes.

Sourceval with_temp_file : ?in_dir:File_path.t -> ?on_cleanup_error: unit Async_kernel.Deferred.t Filesystem_types_unix.On_cleanup_error.t -> ?perm:Filesystem_types_unix.File_permissions.t -> ?prefix:string -> ?suffix:string -> (File_path.Absolute.t -> 'a Async_kernel.Deferred.t) -> 'a Async_kernel.Deferred.t

Creates a new file with a unique name, runs the given function with the file's path, and then deletes the temporary file.

Has the same race condition as within_temp_dir if the path is renamed or removed before the function finishes. Overwriting the file atomically by renaming something else to it should not have the same race condition.

Sourceval create_temp_dir : ?in_dir:File_path.t -> ?perm:Filesystem_types_unix.File_permissions.t -> ?prefix:string -> ?suffix:string -> unit -> File_path.Absolute.t Async_kernel.Deferred.t

Creates a new directory with a unique name.

Sourceval create_temp_file : ?in_dir:File_path.t -> ?perm:Filesystem_types_unix.File_permissions.t -> ?prefix:string -> ?suffix:string -> unit -> File_path.Absolute.t Async_kernel.Deferred.t

Creates a new, empty file with a unique name.

Advisory File Locks

The flock system call supports mutual exclusion. When an exclusive lock on a file is held, other attempts to lock it fail or block. When a shared lock on a file is held, other attempts at shared locks succeed and attempts at exclusive locks fail or block.

No other access is controlled by these locks. For locks to affect access such as reading and writing, processes must cooperate with a locking protocol.

As of Linux 2.6.12, flock-based locks do function over NFS.

Sourcemodule Fd : Core.T with type t = Async_unix.Unix.Fd.t
Sourceval flock : ?shared:bool -> File_path.t -> Flock.t Async_kernel.Deferred.t

Locks the given file via the flock system call, returning when the lock has been acquired. If the file does not exist, this call raises.

If the file cannot be locked immediately due to an existing lock, this call waits until the file can be locked. If this lock and existing locks are shared, the file can be locked immediately.

If the same process calls flock twice on the same file you will end up with two competing locks. You have been warned!

Sourceval flock_of_fd : ?shared:bool -> Fd.t -> Flock.t Async_kernel.Deferred.t

Like flock above. Uses an already-open file descriptor for locking.

Like flock above. If the file does not exist, creates it with the given permissions before locking.

Sourceval try_flock : ?shared:bool -> File_path.t -> Flock.t option Async_kernel.Deferred.t

Like flock above. If the file cannot be locked immediately due to an existing lock, this call returns None instead of waiting.

Sourceval try_flock_of_fd : ?shared:bool -> Fd.t -> Flock.t option Async_kernel.Deferred.t

Like try_flock above. Uses an already-open file descriptor for locking.

Sourceval try_flock_create : ?perm:Filesystem_types_unix.File_permissions.t -> ?shared:bool -> File_path.t -> Flock.t option Async_kernel.Deferred.t

Like try_flock above. If the file does not exist, creates it with the given permissions before locking.

Releases the given lock. These locks are automatically released on exit or exec, so this only needs to be used if the lock needs to be released during the execution of the program. Does not close any file descriptors passed to *_of_fd creators.

Sourceval flock_fd : Flock.t -> Fd.t

Produces the file descriptor underlying a lock.

Sourceval with_flock : ?shared:bool -> File_path.t -> f:(Flock.t -> 'a Async_kernel.Deferred.t) -> 'a Async_kernel.Deferred.t

Acquires a lock for the duration of f. Calls flock before f, and funlock after f returns or raises.

Sourceval with_flock_of_fd : ?shared:bool -> Fd.t -> f:(Flock.t -> 'a Async_kernel.Deferred.t) -> 'a Async_kernel.Deferred.t

Like with_flock. Uses an already-open file descriptor for locking.

Like with_flock above. Calls flock_create instead of flock.