© 2009 OMTP Ltd. All rights reserved. OMTP and OMTP BONDI are registered trademarks of OMTP Ltd.
BONDI filesystem API.
FileArray
FileSystemSuccessCallback
FileSystemManager
FileSystemListener
File
FileStream
FileSystemManagerObject
| Interface | Method |
|---|---|
| FileSystemSuccessCallback | void onSuccess(File file) |
| FileSystemManager | DOMString getDefaultLocation(DOMString specifier, unsigned long minFreeSpace) StringArray getRootLocations() File resolve(DOMString location) void registerEventListener(FileSystemListener listener) void unregisterEventListener(FileSystemListener listener) |
| FileSystemListener | void mountEvent(DOMString location) void unmountEvent(DOMString location) |
| File | FileArray listFiles() FileStream open(DOMString mode, DOMString encoding) PendingOperation copyTo(FileSystemSuccessCallback successCallback, ErrorCallback errorCallback, DOMString filePath, boolean overwrite) PendingOperation moveTo(FileSystemSuccessCallback successCallback, ErrorCallback errorCallback, DOMString filePath, boolean overwrite) File createDirectory(DOMString dirPath) File createFile(DOMString filePath) File resolve(DOMString filePath) boolean deleteDirectory(boolean recursive) boolean deleteFile() |
| FileStream | void close() DOMString read(unsigned long charCount) ByteArray readBytes(unsigned long byteCount) DOMString readBase64(unsigned long byteCount) void write(DOMString stringData) void writeBytes(ByteArray byteData) void writeBase64(DOMString base64Data) |
| FileSystemManagerObject |
The BONDI filesystem API provides access to the filesystem of a device. The filesystem is abstractly represented as a collection of disjoint filesystem root locations each corresponding to some specific location in the device filesystem. The filesystem API exposes the hierarchies below these root locations as a single virtual filesystem but provides no access to other parts of the device filesystem.
The roots that are exposed are determined by the platform also by the context in which the filesystem API is invoked.
Each root has a string name. Each file or directory within the virtual filesystem is addressed using a fully-qualified path of the form: <root name>/<path> where <rootname> is the name of the root and <path> is the path to the file or directory relative to that root.
The list of the supported root locations can be retrieved by calling filesystem.getRootLocations().
The default location for a specific type of file, if one exists, can be obtained by calling the filesystem.getDefaultLocation(...) method. A set of platform-independent symbolic names for media types is defined. The default location for a given file type may itself be a root, or some subdirectory of a root. Although the set of roots available on a device is platform-dependent, the getDefaultLocation(...) functionality can be used to ensure that web applications can be written in a platform-independent way.
In order to access specific locations which are prefixed with a value retrieved by filesystem.getDefaultLocation or some root location returned by filesystem.getRootLocations a File handle must be retrieved using the filesystem.resolve call.
A File handle represents either a file or a directory. If it is a file the isFile attribute will be true, otherwise the isDirectory attribute will be true. A file can be opened for reading and writing, using a FileStream handle. A directory can be used to list its contents which is a list of files and sub-directories. There is a resolve method on directories as well in order to resolve files or sub-directories more conveniently than processing directory listings.
The "/" character is used as the (path) component separator. The use of "." or ".." in location components is not required to be supported.
All path characters need to be UTF-8 encoded.
function errorCB(err) {
alert("BONDI filesystem API couldn't be requested: " + err.message);
}
function successCB() {
var docLocation = bondi.filesystem.getDefaultLocation("documents");
var docDir = bondi.filesystem.resolve(docLocation);
var docFiles = docDir.listFiles();
for(var i = 0; i < docFiles.length; i++) {
// displays name of each image file in image directory
alert(docFiles[i].name);
}
var testFile = docDir.createFile("test.txt");
var out = testFile.open("w", "UTF-8");
// writes Hello World to test.txt
out.write("Hello World");
out.close();
}
bondi.requestFeature(successCB, errorCB, "filesystem");
This is the URI used to declare this API's feature set, for use in bondi.requestFeature. For the URL, the list of features included by the feature set is provided.
All the FileSystem features
Includes API features:
http://bondi.omtp.org/api/filesystem.read
http://bondi.omtp.org/api/filesystem.write
This is the list of URIs used to declare this API's features, for use in bondi.requestFeature. For each URL, the list of functions covered is provided.
When any of the features
http://bondi.omtp.org/api/filesystem.read
http://bondi.omtp.org/api/filesystem.write
is successfully requested, the interface
FileSystemManager
is instantiated, and the resulting object appears in the global
namespace as
Bondi.fileSystemManager.
Filesystem read operations, including reading directory listings, file contents and resolving root locations.
Device capabilities:
io.file.read
Filesystem write operations, including creating files/directories, writing to files, deleting files/directories, moving and copying files.
Device capabilities:
io.file.write
io.file.read
Read directory or file
Security parameters:
name:
Name of directory or file, in virtual filesystem,
e.g. rootlocation/filename
io.file.write
Write directory or file
Security parameters:
name:
Name of directory or file, in virtual filesystem,
e.g. rootlocation/filename
FileArray
Array of File handles.
typedef sequence<File> FileArray;
This array type is returned when directory listings are requested.
FileSystemSuccessCallback
File system specific success callback.
[Callback=FunctionOnly, NoInterfaceObject] interface FileSystemSuccessCallback {
void onSuccess(in File file);
};
This callback interface specifies a success callback with a function taking a File object as input argument. It is used in asynchronous operations such as copying, moving and deleting files.
onSuccess
Method invoked when the asynchronous call completes succesfully
void onSuccess(in File file);
FileSystemManager
Manager class exposed as the filesystem modules API.
interface FileSystemManager {
readonly attribute unsigned long maxPathLength;
DOMString getDefaultLocation(in DOMString specifier, in unsigned long minFreeSpace)
raises(DeviceAPIError);
StringArray getRootLocations();
File resolve(in DOMString location)
raises(SecurityError, DeviceAPIError);
void registerEventListener(in FileSystemListener listener)
raises(DeviceAPIError);
void unregisterEventListener(in FileSystemListener listener)
raises(DeviceAPIError);
};
This manager class exposes the filesystem base API, such as determining root and default locations, resolving a given location into a File Handle and registering filesystem listeners for filesystem events.
function errorCB(err) {
alert("BONDI filesystem API couldn't be requested: " + err.message);
}
function successCB() {
var docLocation = bondi.filesystem.getDefaultLocation("documents");
var docDir = bondi.filesystem.resolve(docLocation);
var docFiles = docDir.listFiles();
for(var i = 0; i < docFiles.length; i++) {
// displays name of each image file in image directory
alert(docFiles[i].name);
}
var testFile = docDir.createFile("test.txt");
var out = testFile.open("w", "UTF-8");
// writes Hello World to test.txt
out.write("Hello World");
out.close();
}
bondi.requestFeature(successCB, errorCB, "filesystem");
readonly
unsigned long maxPathLength
Contains the platform-dependent maximum path length.
Read-only.
alert(bondi.filesystem.maxPathLength);
getDefaultLocation
Returns a default location path for the given arguments, including the root location prefix.
DOMString getDefaultLocation(in DOMString specifier, in unsigned long minFreeSpace);
Optionally this method can be called with the space argument specifying the minimum free disk space required.
This function resolves location specifiers to location paths.
The following location specifiers are supported:
"wgt:package" the widget package location
"wgt:private" the widgets private storage
"wgt:public" the widgets public storage
"wgt:temp" the widgets temporary storage
"documents" the documents location,
e.g. the "My Documents" directory on some systems
"images" the images location,
e.g. the "My Pictures" directory on some systems
"videos" the videos location,
e.g. the "My Videos" directory on some systems
"temp" the temporary storage,
e.g resolving to "Temp"
"sdcard" the sdcard storage, if any
INVALID_ARGUMENT_ERROR if the specifier or space arguments are invalid
var wgtLocation = bondi.filesystem.getDefaultLocation('wgt:package');
getRootLocations
Returns all root locations.
StringArray getRootLocations();
The root locations are symbolic names for filesystem locations which are accessible and supported by the underlying platform.
Usually the following root locations will be supported:
"documents" represents "My Documents" on some platforms
"images" represents "My Pictures" on some platforms
"videos" represents "My Videos" on some platforms
"temp" represents "tmp" on some platforms
"sdcard" represents the sdcard on some platforms
See also the getDefaultLocation method.
var locations = bondi.filesystem.getRootLocations();
for(var i = 0; i < locations.length; i++) {
// locations[i] is a resolvable root location
}
resolve
Resolves a location to a File handle.
File resolve(in DOMString location);
Validates and resolves the given location to a File handle and returns a handle. A valid location is prefixed with a valid root or default location and must address an existing and accessible file.
PERMISSION_DENIED_ERROR when access is denied by the security policy.
INVALID_ARGUMENT_ERROR if invalid location was given.
var location = bondi.filesystem.getDefaultLocation("temp");
var temp = bondi.filesystem.resolve(location);
var documents = bondi.filesystem.getDefaultLocation("documents");
var mydoc = bondi.filesystem.resolve(documents + "/data/2009/mydoc.txt");
registerEventListener
Registers a filesystem event listener.
void registerEventListener(in FileSystemListener listener);
Filesystem event listeners are used in order to retrieve notifications if root or default locations such as storage cards are mounted/unmounted into/from the device filesystem.
INVALID_ARGUMENT_ERROR if the given listener is invalid or not a listener.
var listener = { mountEvent: function(location) { alert('mounted ' + location); },
unmountEvent: function(location) { alert('unmounted ' + location); };
bondi.filesystem.registerEventListener(listener);
unregisterEventListener
Unregisters a filesystem event listener.
void unregisterEventListener(in FileSystemListener listener);
INVALID_ARGUMENT_ERROR if the given listener is invalid or not a listener.
bondi.filesystem.unregisterEventListener(listener);
FileSystemListener
Filesystem event listener class.
interface FileSystemListener {
void mountEvent(in DOMString location);
void unmountEvent(in DOMString location);
};
This listener implements two methods to retrieve mount and unmount notifications if root or default locations on a storage card get available or unavailable.
var fsEventImpl = {
mountEvent: function(location) {
// location has been mounted
},
unmountEvent: function(location) {
// location has been unmounted
}
};
mountEvent
Called when a new root location gets available.
void mountEvent(in DOMString location);
A new location could be a storage card for example.
unmountEvent
Called when a location gets unavailable.
void unmountEvent(in DOMString location);
Such a location could be a storage card for example.
File
File class.
interface File {
readonly attribute File parent;
readonly attribute boolean readOnly;
readonly attribute boolean isFile;
readonly attribute boolean isDirectory;
readonly attribute Date created;
readonly attribute Date modified;
readonly attribute DOMString path;
readonly attribute DOMString name;
readonly attribute DOMString absolutePath;
readonly attribute unsigned long fileSize;
readonly attribute Map metadata;
FileArray listFiles()
raises(SecurityError, DeviceAPIError);
FileStream open(in DOMString mode, in DOMString encoding)
raises(SecurityError, DeviceAPIError);
PendingOperation copyTo(in FileSystemSuccessCallback successCallback,
in ErrorCallback errorCallback,
in DOMString filePath,
in boolean overwrite)
raises(SecurityError, DeviceAPIError);
PendingOperation moveTo(in FileSystemSuccessCallback successCallback,
in ErrorCallback errorCallback,
in DOMString filePath,
in boolean overwrite)
raises(SecurityError, DeviceAPIError);
File createDirectory(in DOMString dirPath)
raises(SecurityError, DeviceAPIError);
File createFile(in DOMString filePath)
raises(SecurityError, DeviceAPIError);
File resolve(in DOMString filePath)
raises(SecurityError, DeviceAPIError);
boolean deleteDirectory(in boolean recursive)
raises(SecurityError, DeviceAPIError);
boolean deleteFile()
raises(SecurityError, DeviceAPIError);
};
This interface represents the file abstraction in use. A file handle can address files or directories. A file handle represents a file if the isFile property is true, otherwise it represents a directory.
A file handle representing a file can be opened for I/O operations such as reading and writing.
A file handle representing a directory can list all files in the current directory.
// list directory contents
var files = dir.listFiles();
for(var i = 0; i < files.length; i++) {
// alerts each name of dir's contents
alert(files[i].name);
}
// opens a file for writing
var file = dir.createFile("test.txt");
var out = file.open("w", "UTF-8");
// writes Hello World to test.txt
out.write("Hello World");
out.close();
readonly
File parent
Parent directory handle.
Read-only.
null if there is no parent directory.
If there is no parent directory, this represents a root location.
var parent = file.parent;
if(parent != null) {
// parent directory handle
}
readonly
boolean readOnly
File/directory access state in the filesystem.
Read-only.
false if there is write access.
This attribute represents the actual state of a file/directory in the filesystem. It does not check if the accessor has io.file.write permission.
if(file.readOnly) {
// file cannot be written
}
readonly
boolean isFile
File type.
Read-only.
true if this handle is a file. false if this handle is a directory.
if(file.isFile) {
// is a file
}
readonly
boolean isDirectory
File type.
Read-only.
true if this handle is a directory. false if this handle is a file.
if(file.isDirectory) {
// is a directory
}
readonly
Date created
Creation timestamp.
Read-only.
The creation timestamp of this file. This is the timestamp when the file was first created in the filesystem. This is equivalent to the timestamp when a call to createFile() succeeds.
It is unspecified and platform-dependent if the creation timestamp changes when a file is moved.
alert(file.created); // displays the creation timestamp
readonly
Date modified
Modification timestamp.
Read-only.
The modification timestamp of this file. This is the timestamp of the most recent modification to the file, usually when the last write operation succeeded. Opening a file for reading does not change the modification timestamp.
alert(file.modified); // displays the modification timestamp
readonly
DOMString path
Path of this file, excluding the file name.
Read-only.
If the file path is /baz/foo.bar, then the path is /baz/.
The encoding of file paths is UTF-8.
alert(file.path); // should be /baz/ if the file is /baz/foo.bar
readonly
DOMString name
File name, excluding any path components.
Read-only.
Assumed the file path is /baz/foo.bar, then the file name is foo.bar.
The encoding of file names is UTF-8.
alert(file.name); // should be foo.bar if the file path is /baz/foo.bar
readonly
DOMString absolutePath
Absolute path of this file.
Read-only.
Assumed the file path is /baz/foo.bar, then this is the absolute path.
The encoding of file paths is UTF-8.
alert(file.absolutePath); // should be /baz/foo.bar if the file is /baz/foo.bar
readonly
unsigned long fileSize
Size in bytes of this file.
Read-only.
If it is attempted to read this attribute on a directory, undefined is returned.
alert(file.fileSize); // displays the file size
readonly
Map metadata
Files metadata.
The actual map contents are implementation dependent.
// should display the file author, if supported by the
// platform, undefined otherwise
alert(file.metadata.author);
listFiles
Returns list of all files of this directory.
FileArray listFiles();
The list of files contains directories and files, it does not contain the directories "." and "..".
PERMISSION_DENIED_ERROR when access is denied by the security policy.
IO_ERROR if this is not a directory.
var files = dir.listFiles();
for(var i = 0; i < files.length; i++) {
// files[i] iterate over all files of this directory
}
open
Opens the file in the given mode supporting the given encoding.
FileStream open(in DOMString mode, in DOMString encoding);
PERMISSION_DENIED_ERROR when access is denied by the security policy.
IO_ERROR if this is not a file.
INVALID_ARGUMENT_ERROR if invalid mode or unsupported encoding is supplied.
// opens file for reading
var in = file.open("r", "UTF-8");
copyTo
Copies this file.
PendingOperation copyTo(in FileSystemSuccessCallback successCallback, in ErrorCallback errorCallback, in DOMString filePath, in boolean overwrite);
The copy will be created in the given path. If this function fails and the error callback is called, it will pass an DeviceAPIError IO_ERROR to the callback.
The encoding of file paths is UTF-8.
PERMISSION_DENIED_ERROR when access is denied by the security policy.
IO_ERROR if the file cannot be copied.
IO_ERROR if any characters in the path are not supported.
IO_ERROR if overwrite is false and target file exists.
IO_ERROR if it is attempted to move to a directory.
// copies this file to /temp/file.copy
var op = file.copyTo(function(copiedFile) { alert("file copied"); }, null, "/temp/file.copy", false);
moveTo
Moves this file.
PendingOperation moveTo(in FileSystemSuccessCallback successCallback, in ErrorCallback errorCallback, in DOMString filePath, in boolean overwrite);
The file will be moved atomically to the given path. This is different to copyTo and deleting the old file, because this operation does not need extra disk space on certain platforms. If this function fails and the error callback is called, it will pass an DeviceAPIError IO_ERROR to the callback.
The encoding of file paths is UTF-8.
PERMISSION_DENIED_ERROR when access is denied by the security policy.
IO_ERROR if the file cannot be moved.
IO_ERROR if any characters in the path are not supported.
IO_ERROR if overwrite is false and target file exists.
IO_ERROR if it is attempted to move to a directory.
// moves this file to /temp/file.move
var op = file.moveTo(function(movedFile) { file = movedFile; }, null, "/temp/file.move");
createDirectory
Creates a directory.
File createDirectory(in DOMString dirPath);
The new directory will be created relatively to the current directory this operation is performed on. It will attempt to create all necessary sub-directories as well. The use of "." or ".." in path components is not supported. If the bottom-most directory being created already exists this method will throw an IO_ERROR.
The encoding of file paths is UTF-8.
PERMISSION_DENIED_ERROR when access is denied by the security policy.
IO_ERROR if the directory cannot be created.
IO_ERROR if any characters in the path are not supported.
IO_ERROR if a file with the same name exists.
IO_ERROR if any path component does not exist.
IO_ERROR if this is not a directory.
var newDir = dir.createDirectory("newDir");
var anotherNewDir = dir.createDirectory("newDir1/subNewDir1");
createFile
Creates a new empty file.
File createFile(in DOMString filePath);
The new empty file is created in the given path relatively to the current directory the operation is performed on. The use of "." or ".." in path components is not supported. If the bottom-most file being created already exists this method will throw an IO_ERROR. If any path component does not exist this method will throw an IO_ERROR.
The encoding of file paths is UTF-8.
PERMISSION_DENIED_ERROR when access is denied by the security policy.
IO_ERROR if the file cannot be created.
IO_ERROR if any characters in the path are not supported.
IO_ERROR if the file already exists.
IO_ERROR if any path component does not exist.
IO_ERROR if this is not a directory.
var newFile = dir.createFile("newFile");
resolve
Resolves an existing file or directory relatively to the current directory this operation is performed on; and returns a file handle for it.
File resolve(in DOMString filePath);
The filePath is not allowed to contain the "." or ".." directories.
The encoding of file paths is UTF-8.
PERMISSION_DENIED_ERROR when access is denied by the security policy.
IO_ERROR if the file or directory cannot be resolved.
IO_ERROR if any characters in the path are not supported.
IO_ERROR if this is not a directory.
var hellofile = dir.resolve("foo/hello.txt");
deleteDirectory
Deletes this directory.
boolean deleteDirectory(in boolean recursive);
This function attempts to delete a directory or directory tree synchronously and returns true on success, false otherwise. It may throw an IO_ERROR if a recursive deletion partially fails and any deleted data so far cannot be recovered. This might happen due the lack of filesystem permissions or if any directories or files are openened by other processes.
PERMISSION_DENIED_ERROR when access is denied by the security policy.
IO_ERROR if the directory or any file or sub-directory cannot be deleted.
IO_ERROR if the directory is not empty and this operation is not performed recursively.
IO_ERROR if this is a file.
var fullPath = dir.fullPath;
if(dir.deleteDirectory(true)) {
// subsequent accesses to dir will throw an error
alert(fullPath + ' deleted');
}
deleteFile
Deletes this file.
boolean deleteFile();
Returns true on success, false otherwise.
PERMISSION_DENIED_ERROR when access is denied by the security policy.
IO_ERROR if the file cannot be deleted due the lack of filesystem permissions or if it is locked or opened by another process.
IO_ERROR if this is a directory.
var fullPath = dir.fullPath;
if(dir.deleteFile()) {
// subsequent accesses to dir will throw an error
alert(fullPath + ' deleted');
}
FileStream
FileStream API.
interface FileStream {
readonly attribute boolean eof;
attribute unsigned long position
setraises(DeviceAPIError);
readonly attribute unsigned long bytesAvailable;
void close();
DOMString read(in unsigned long charCount)
raises(DeviceAPIError);
ByteArray readBytes(in unsigned long byteCount)
raises(DeviceAPIError);
DOMString readBase64(in unsigned long byteCount)
raises(DeviceAPIError);
void write(in DOMString stringData)
raises(DeviceAPIError);
void writeBytes(in ByteArray byteData)
raises(DeviceAPIError);
void writeBase64(in DOMString base64Data)
raises(DeviceAPIError);
};
A FileStream represents a handle to a File opened for read and/or write operations. Read and write operations are performed relative to a file pointer which represents the current position in the file.
A series of read/write methods are available that permit both binary and text to be processed.
Once a file stream is closed, any operation attempted on this stream will result in a normal JavaScript error.
readonly
boolean eof
Indicates whether or not the current file pointer is at the end of the file.
true if the position is at the end of the current file stream.
if(stream.eof) {
// file has been read completely
}
unsigned long position
Get/set stream position for reads/writes.
The stream position is an offset of bytes from the start of the file stream. When invoking an operation that reads or writes from the stream, the operation will take place from the position in the position attribute.
IO_ERROR if a position was given that is out of the stream range.
alert(stream.position); // displays current stream position
// alters current stream position to the begin of the file,
// like seek() in C
stream.position = 0;
readonly
unsigned long bytesAvailable
Returns the number of bytes that are available for reading from the stream.
The number of bytes available for reading is the maximum amount of bytes that can be read in the next read operation.
-1 if eof is true.
alert(stream.bytesAvailable); // displays the available bytes to be read
close
Closes this FileStream.
void close();
Flushes any pending buffered writes and closes the File. Always succeeds. Note that pending writes might not succeed.
stream.close(); // closes this stream, no subsequent access to stream allowed
read
Reads the specified number of characters.
DOMString read(in unsigned long charCount);
Reads specified number of characters and returns them as string. The resulting string length might be shorter than charCount if eof is true.
IO_ERROR if an error occurs during read.
var text = stream.read(0);
stream.close();
readBytes
Reads the specified number of bytes from this FileStream.
ByteArray readBytes(in unsigned long byteCount);
If 0 is supplied it will read all available bytes in a single read operation.
IO_ERROR if an error occurs during readBytes.
// reads up to 256 bytes from the stream
var raw = stream.readBytes(256);
for(var i = 0; i < raw.length; i++) {
// raw[i] contains the i-th byte of the current data chunk
}
readBase64
Reads the specified number of bytes from this FileStream, encoding the result in base64.
DOMString readBase64(in unsigned long byteCount);
If 0 is supplied it will read all available bytes in a single read operation.
IO_ERROR if an error occurs during readBase64.
// reads up to 256 bytes from the stream
var base64 = stream.readBase64(256);
write
Writes the specified DOMString to this FileStream.
void write(in DOMString stringData);
IO_ERROR if an error occurs during write.
var text = "Hello world";
stream.write(text);
writeBytes
Writes the specified bytes to this FileStream.
void writeBytes(in ByteArray byteData);
IO_ERROR if an error occurs during writeBytes.
var bytes = in.readBytes(256);
out.writeBytes(bytes); // writes the bytes read from in to out
writeBase64
Converts the specified base64 DOMString to bytes and writes the result to this FileStream.
void writeBase64(in DOMString base64Data);
IO_ERROR if an error occurs during writeBase64.
var base64 = in.readBase64(256);
out.writeBase64(base64); // writes the base64 data read from in to out
FileSystemManagerObject
Specifies what is instantiated at feature request
interface FileSystemManagerObject {
readonly attribute FileSystemManager fileSystemManager;
};
Bondi implements FileSystemManagerObject;