Filesystem/A/0

From CommonJS Spec Wiki
Jump to: navigation, search

STATUS: IMPLEMENTATIONS, UNRESOLVED ISSUES WITH PERMISSIONS

Implementations
Flusspferd, GPSEE, Narwhal on Node (0.5), Narwhal on Rhino (8a45686), RingoJS, sorrow.js (permissions as integers)

The "fs-base" module exports a minimal, engine-specific interface for manipulating a file system and constructing raw byte streams. The IO stream API is beyond the scope of this specification. It is the intent of this specification to provide exactly and only the components of a file system API that cannot be implemented in pure JavaScript.

Pending the ratification of Filesystem/A: Implementations may opt to provide more methods, matching the interfaces specified in Filesystem/A, if they can be implemented with better performance natively than in pure JavaScript based on the methods specified here.

Contents

Specification

Types

Arguments used throughout this document will have the following types, unless explicitly specified otherwise:

The "fs-base" module exports the following constructors:

Files

openRaw(path, mode, permissions)
returns a raw byte stream object with the given mode and permissions from the IO system. The details of this object are unspecified, except

"openRaw" throws an exception when "path" cannot be opened, or "path" refers to a directory.

move(source, target)
Moves a file at one path to another. Failure to move the file, or specifying a directory for target when source is a file must throw an exception.
remove(path String)
Removes the file at the given path. Throws an exception if the path corresponds to anything that is not a file or a symbolic link. If "path" refers to a symbolic link, removes the symbolic link.
touch(path, mtime_opt Date)
Sets the modification time of a file or directory at a given path to a specified time, or the current time. Creates an empty file at the given path if no file (special or otherwise) or directory exists, using the default permissions (as though openRaw were called with no permissions argument). If the underlying file system does not support milliseconds, the time is truncated (not rounded) to the nearest supported unit. On file systems that support last-accessed time, this must be set to match the modification time. Where possible, the underlying implementation should insure that file creation and time stamp modification are transactionally related to the same file, rather than the same directory entry.

Directories

makeDirectory(path, permissions_opt)
Create a single directory specified by path. If the directory cannot be created for any reason an exception must be thrown. This includes if the parent directories of "path" are not present. The permissions object passed to this method is used as the argument to the Permissions constructor. The resultant Permissions instance is applied to the given path during directory creation.
removeDirectory(path)
Removes a directory if it is empty. If path is not empty, not a directory, or cannot be removed for another reason an exception must be thrown. If path is a link and refers canonically to a directory, the link must be removed.
move(source, target)
Moves a directory from one path to another on the same file system. Does not copy the directory under any circumstances. A conforming implementation must move the directory using the operating system's file-system-atomic move or rename call. If it cannot be moved for any reason an exception must be thrown. An exception must be thrown if "target" specifies an existing directory.
*Note*: this is the same method used to move files. The behaviour differs depending on whether source is a file or directory.

Paths

canonical(path) String
returns the canonical path to a given abstract path. Canonical paths are both absolute and intrinsic, such that all paths that refer to a given file (whether it exists or not) have the same corresponding canonical path. This function is equivalent to joining the given path to the current working directory (if the path is relative), joining all symbolic links along the path, and normalizing the result to remove relative path (. or ..) references.
workingDirectory() String
returns the current working directory as an absolute String (not as an object with a toString method)
changeWorkingDirectory(path)
changes the current working directory to the given path, resolved on the current working directory. Throws an exception if the operation failed.

Security

owner(path) String
owner(path) Number optional
returns the name of the owner of a file with typeof string. Where the owner name is not defined, a numeric userId with typeof number may be returned instead.
group(path) String
group(path) Number optional
returns the name of the group owner of a file with typeof string. Where the group name is not defined, a numeric groupId with typeof number may be returned instead. This interface is optional but recommended when Permissions support a group member; the numeric interface shall not be implemented unless the String interface is implemented.
changeOwner(path, name String)
changeOwner(path, userId Number) optional
sets the owner of a given file or directory. If path is a symbolic link, the target file or directory is affected instead. Throws an exception if the operation fails for any reason (including that current user does not have permission to change the owner). This method must accept any return values from the "owner" method.
changeGroup(path, name String)
changeGroup(path, userId Number) optional
sets the group ownership of a given file or directory. If path is a symbolic link, the target file or directory is affected instead. Throws an exception if the operation fails for any reason (including that current user does not have permission to change the group). This method must accept any return values from the "group" method. This interface is optional but recommended when Permissions support a group member; the numeric interface shall not be implemented unless the String interface is implemented.
permissions(path) Permissions
returns a Permissions object describing the current permissions for a given path. If path is a symbolic link, the returned permissions describe the target file or directory and not the link file itself.
changePermissions(path, permissions Permissions)
sets the permissions for a given path. The permissions object passed to this method is used as the argument to the Permissions constructor. The resultant Permissions instance is applied to the given path if it is a file or directory; if path is a symbolic link it will be applied to the link target instead.

Links

Symbolic and hard links must not be emulated with Windows Shortcuts. On systems where symbolic links are not supported, symbolicLink and readLink must be undefined. On systems where hard links are not supported, hardLink must be undefined.

symbolicLink(source, target)
creates a symbolic link at the target path that refers to the source path. Must throw an exception if the target already exists. Conforming implementations must not rewrite or canonicalize either the source or target arguments, nor validate that the link target exists, before passing to the underlying filesystem layer. Note: The intent is to allow users to create directory hierarchies with symbolic links that can be freely moved around a filesystem and maintain internal referential integrity.
hardLink(source, target)
creates a hard link at the target path that shares storage with the source path. Throws an exception if this is not possible, such as when the source and target are on separate logical volumes or hard links are not supported by the volume.
readLink(path) String
returns the immediate target of a symbolic link at a given path. Throws an exception if there is no symbolic link at the given path or the link cannot be read. This function differs from canonical in that it may return a path that itself is a symbolic link.

Tests

exists(path)
returns true if a file (of any type) or a directory exists at a given path. If the file is a broken symbolic link, returns false.
isFile(path)
returns true if a path exists and that it, after resolution of symbolic links, corresponds to a regular file.
isDirectory(path)
returns whether a path exists and that it, after resolution of symbolic links, corresponds to a directory.
isLink(target)
returns whether a symbolic link exists at "target". Windows Shortcuts must not be equated with symbolic links. This function must not follow/resolve symbolic links.
isReadable(path)
returns whether a path exists and that it could be opened for reading at the time of the call using "openRaw" for files or "list" for directories.
isWriteable(path)
If a path exists, returns whether a file may be opened for writing, or entries added or removed from an existing directory. If the path does not exist, returns whether entries for files, directories, or links can be created at its location.
same(pathA, pathB) Boolean
returns whether two paths refer to the same storage (file or directory), either by virtue of symbolic or hard links, such that modifying one would modify the other. In the case where either some or all paths do not exist, we return false. If we are unable to verify if the storage is the same (such as by having insufficient permissions), an exception is thrown.
sameFilesystem(pathA, pathB) Boolean
returns whether two paths refer to an entity on the same filesystem. An exception will be thrown if it is not possible to determine this.

Attributes

size(path) Number
returns the size of a file in bytes, or throws an exception if the path does not correspond to an accessible path, or is not a regular file or a link. If path is a link, returns the size of the final link target, rather than the link itself.
Care should be taken that this number returned is suitably large (i.e. that we can get useful figures for files over 1GB (30bits+sign bit). If the size of a file cannot be represented by a JavaScript number, "size" must throw a RangeError.
lastModified(path) Date
returns the time that a file was last modified as a Date object.

Listing

list(path) Array * String
returns the names of all the files in a directory, in lexically sorted order. Throws an exception if the directory cannot be traversed (or path is not a directory).
Note: this means that list("x") of a directory containing "a" and "b" would return ["a", "b"], not ["x/a", "x/b"].
iterate(path) Iterator * String
returns an iterator that lazily browses a directory, backward and forward, for the base names of entries in that directory.

Iterator Objects

Iterator objects have the following members:

next() String or Path
returns the next path in the iteration or throws "StopIteration" if there is none.
iterator()
returns itself
close()
closes the iteration. After calling close, all calls to next and prev must throw StopIteration.

Extended Attributes (optional)

Extended attribute methods may be defined on systems that may support the feature on some volumes. See [1].

getAttribute(path, key String, default ByteString) ByteString
Gets the value of an extended attribute. If a third argument is provided, including undefined, and there is no corresponding extended attribute for the requested key, the default is returned. Otherwise, if there is no extended attribute for the requested key, getAttribute must throw an exception. Throws a ValueError if the volume does not support extended attributes.
setAttribute(path, key String, value ByteString)
Sets an extended attribute. Throws a ValueError if the volume does not support extended attributes.
removeAttribute(path, key String)
Removes the extended attribute for a given key. If there is no corresponding key, throws an exception. Throws a ValueError if the volume does not support extended attributes.
listAttributeNames(path) Array * String
returns an Array of Strings of the keys of all extended attributes on a given path. Throws a ValueError if the volume does not support extended attributes.

Unicode

Notes

The following topics have been deferred for a future specification:

Implementations

Related Discussions

Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox