Class type stream_fs
for filesystems with stream access to files
The class type Netfs.stream_fs is an abstraction for both kernel-level and user-level filesystems. It is used as parameter for algorithms (like globbing) that operate on filesystems but do not want to assume any particular filesystem. Only stream access is provided (no seek).
File paths:
The filesystem supports hierarchical file names. File paths use Unix conventions, i.e.
/
is the root.
is the same directory..
is the parent directoryAll paths need to be absolute (i.e. start with /
).
There can be additional constraints on paths:
Implementations may impose more constraints that cannot be expressed here (case insensitivity, path length, exclusion of special names etc.).
Virtuality:
There is no assumption that /
is the real root of the local filesystem.
It can actually be anywhere - a local subdirectory, or a remote directory,
or a fictive root. There needs not to be any protection against "running
beyond root", e.g. with the path /..
.
This class type also supports remote filesystems, and thus there is no concept of file handle (because this would exclude a number of implementations).
Errors:
Errors should generally be indicated by raising Unix_error
. For
many error codes the interpretation is already given by POSIX. Here
are some more special cases:
EINVAL
: should also be used for invalid paths, or when a flag
cannot be supported (and it is non-ignorable)ENOSYS
: should also be used if an operation is generally unavailableIn case of hard errors (like socket errors when communicating with the
remote server) there is no need to stick to Unix_error
, though.
Subtyping:
The class type Netfs.stream_fs is subtypable, and subtypes can add more features by:
Omitted:
Real filesystems usually provide a lot more features than what is represented here, such as:
This definition here is intentionally minimalistic. In the future
this class type will be extended, and more more common filesystem features
will be covered. See Netfs.empty_fs for a way how to ensure that
your definition of a stream_fs
can still be built after stream_fs
has been extended.
stream_fs
The intersection of write_flag
and write_file_flag
Note `Dummy
: this flag is always ignored. There are two reasons
for having it:
if <condition> then `Create else `Dummy
)Tests:
`N
: the file name exists`E
: the file exists`D
: the file exists and is a directory`F
: the file exists and is regular`H
: the file exists and is a symlink (possibly to a non-existing
target)`R
: the file exists and is readable`W
: the file exists and is writable`X
: the file exists and is executable`S
: the file exists and is non-emptyThe encoding must be ASCII-compatible
(Netconversion.is_ascii_compatible). If None
the
ASCII encoding is assumed for codes 0-127, and no meaning is
defined for byte codes 128-255.
Code points that must not occur in path components between slashes.
This is given as ranges (from,to)
. The code points are interpreted
as Unicode code points if an encoding is available, and as byte codes
otherwise. For example, for Unix the code points 0 and 47 (slash)
are normally the only excluded code points.
Whether the effect of ..
can be obtained by stripping off the
last path component, i.e. whether
Filename.dirname path <=> path ^ "/.."
read flags filename
: Opens the file filename
for reading,
and returns the input stream. Flags:
`Skip n
: Skips the first n
bytes of the file. On many
filesystems this is more efficient than reading n
bytes and
dropping them; however, there is no guarantee that this
optimization exists.`Binary
: Opens the file in binary mode (if there is such
a distinction)`Streaming
for network filesystems: If possible, open the
file in streaming mode, and avoid to copy the whole file to the local
disk before returning the Netchannels.in_obj_channel.
Streaming mode is faster, but has also downsides. Especially,
the implementation of read
can do less to recover from
transient network problems (like retrying the whole download).
Support for this flag is optional, and it is ignored if
there is no extra streaming mode.read_file flags filename
: Opens the file filename
for reading,
and returns the contents as a local_file
. Use the method
filename
to get the file name of the local file. The file
may be temporary, but this is not required. The method close
of the returned object should be called when the file is no
longer needed. In case of a temporary file, the file can then
be deleted. Flags:
`Binary
: Opens the file in binary mode (if there is such
a distinction)write flags filename
: Opens (and optionally creates) the filename
for writing, and returns the output stream. Flags:
`Create
: If the file does not exist, create it`Truncate
: If the file exists, truncate it to zero before
writing`Exclusive
: The `Create
is done exclusively`Binary
: Opens the file in binary mode (if there is such
a distinction)`Streaming
: see read
(above) for explanationsSome filesystems refuse this operation if neither `Create
nor
`Truncate
is specified because overwriting an existing file
is not supported. There are also filesystems that cannot even
modify files by truncating them first, but only allow to write
to new files.
It is unspecified whether the file appears in the directory directly
after calling write
or first when the stream is closed.
write_file flags filename localfile
: Opens the file filename
for writing, and copies the contents of the localfile
to it.
It is ensured that the method close
of localfile
is called
once the operation is finished (whether successful or not).
Flags:
`Create
: If the (remote) file does not exist, create it`Truncate
: If the file exists, truncate it to zero before
writing`Exclusive
: The `Create
is done exclusively`Binary
: Opens the file in binary mode (if there is such
a distinction)`Link
: Allows that the destination file is created as a hard
link of the original file. This is tried whatever other mode
is specified. If not successful, a copy is done instead.Returns the size of a file. Note that there is intentionally no distinction between text and binary mode - implementations must always assume binary mode.
Removes the file or symlink. Implementation are free to also support the removal of empty directories.
Flags:
`Recursive
: Remove the contents of the non-empty directory
recursively. This is an optional feature. There needs not to
be any protection against operations done by other processes
that affect the directory tree being deleted.Renames the file. There is no guarantee that a rename is atomic
symlink flags oldpath newpath
: Creates a symlink. This
is an exclusive create, i.e. the operation fails if newpath
already exists.
Reads the contents of a directory. Whether "." and ".." are returned is platform-dependent. The entries can be returned in any order.
Creates a new directory. Flags:
`Path
: Creates missing parent directories. This is an
optional feature. (If not supported, ENOENT is reported.)`Nonexcl
: Non-exclusive create.Copies a file to a new name. This does not descent into directories. Also, symlinks are resolved, and the linked file is copied.
Cancels any ongoing write
. The user must also call
the close_out
method after cancelling. The effect
is that after the close no more network activity will occur.
This is a class where all methods fail with ENOSYS
. The string
argument is the detail in the Unix_error
, normally the module
name of the user of this class.
empty_fs
is intended as base class for implementations of stream_fs
outside Ocamlnet. When stream_fs
is extended by new methods, these
methods are at least defined, and no build error occurs. So the
definition should look like
class my_fs ... =
object
inherit Netfs.empty_fs "my_fs"
method read flags name = ...
(* Add here all methods you can define, and omit the rest *)
end
local_fs()
: Returns a filesystem object for the local filesystem.
encoding
: Specifies the character encoding of paths. The default
is system-dependent.root
: the root of the returned object is the directory root
of the local filesystem. If omitted, the root is the root of
the local filesystem (i.e. / for Unix, and see comments for
Windows below). Use root="."
to make the current working
directory the root. Note that "." like other relative paths
are interpreted at the time when the access method is executed.enable_relative_paths
: Normally, only absolute paths can be
passed to the access methods like read
. By setting this option
to true
one can also enable relative paths. These are taken
relative to the working directory, and not relative to root
.
Relative names are off by default because there is usually no
counterpart in network filesystems.Unix in general: There is no notion of character encoding of
paths. Paths are just bytes. Because of this, the default encoding
is None
. If a different encoding is passed to local_fs
, these
bytes are just interpreted in this encoding. There is no conversion.
For desktop programs, though, usually the character encoding of the locale is taken for filenames. You can get this by passing
let encoding =
Netconversion.user_encoding()
as encoding
argument.
Windows: If the root
argument is not passed to local_fs
it is possible to access the whole filesystem:
c:/
are also considered
as absolute/c:/
mean the same//hostname
are supportedHowever, when a root
directory is passed, these additional
notations are not possible anymore - paths must start with /
,
and there is neither support for drive letters nor for UNC paths.
The encoding
arg defaults to current ANSI codepage,
and it is
not supported to request a different encoding. (The difficulty is
that the Win32 bindings of the relevant OS functions always assume
the ANSI encoding.)
There is no support for backslashes as path separators (such paths will be rejected), for better compatibility with other platforms.
stream_fs
copy orig_fs orig_name dest_fs dest_name
: Copies the file orig_name
from orig_fs
to the file dest_name
in dest_fs
. By default,
the destination file is truncated and overwritten if it already
exists.
If orig_fs
and dest_fs
are the same object, the copy
method
is called to perform the operation. Otherwise, the data is read
chunk by chunk from the file in orig_fs
and then written to
the destination file in dest_fs
.
Symlinks are resolved, and the linked file is copied, not the link as such.
The copy does not preserve ownerships, file permissions, or
timestamps. (The stream_fs
object does not represent these.)
There is no protection against copying an object to itself.
replace
: If set, the destination file is removed and created again
if it already existsstreaming
: use streaming mode for reading and writing filescopy_into orig_fs orig_name dest_fs dest_name
:
Like copy
, but this version also supports recursive copies. The
dest_name
must be an existing directory, and the file or tree at
orig_name
is copied into it.
Symlinks are copied as symlinks.
If replace
and the destination file/directory already exists,
it is deleted before doing the copy.
subst
: See Netfs.convert_pathstreaming
: use streaming mode for reading and writing filesiter pre fs start
: Iterates over the file hierarchy at start
.
The function pre
is called for every filename. The filenames
passed to pre
are relative to start
. The start
must
be a directory.
For directories, the pre
function is called for the directory
before it is called for the members of the directories.
The function post
can additionally be passed. It is only called
for directories, but after the members.
pre
is called as pre rk lk
where rk
is the file kind after
following symlinks and lk
the file kind without following symlinks
(the link itself).
Example: iter pre fs "/foo"
would call
pre "dir" `Directory `Directory
(meaning the directory "/foo/dir")pre "dir/file1" `File `File
pre "dir/file2" `File `Symlink
post "dir"
Note: symlinks to non-existing files are reported as
pre name `None `Symlink
.
convert_path oldfs newfs oldpath
: The encoding of oldpath
(which is assumed to reside in oldfs
) is converted to the encoding
of newfs
and returned.
It is possible that the conversion is not possible, and
the function subst
is then called with the problematic code point as
argument (in the encoding of oldfs
). The default subst
function
just raises Netconversion.Cannot_represent.
If one of the filesystem objects does not specify an encoding,
the file name is not converted, but simply returned as-is. This
may result in errors when newfs
has an encoding while oldfs
does not have one because the file name might use byte representations
that are illegal in newfs
.