[](https://fuchsia.dev/docs/glossary#directory-capability)


allow components
to connect to a directory provided by another component.

For information on directories that are isolated per-component, see
[storage capabilities](https://fuchsia.dev/docs/concepts/components/v2/capabilities/storage).

## Providing directory capabilities

To provide a directory capability, a component must declare the capability and
[route](https://fuchsia.dev/fuchsia-src/concepts/components/v2/capabilities/directory#route) it from `self`. The component hosts the directory capability in
its


[](https://fuchsia.dev/docs/glossary#outgoing-directory)


.

To define the capability, add a `capabilities` declaration for it:

    {
        capabilities: [
            {
                directory: "data",
                rights: ["r*"],
                path: "/published-data",
            },
        ],
    }

This defines a capability hosted by this component whose outgoing directory path
is `/published-data`, and whose maximum usable
[rights](https://fuchsia.dev/fuchsia-src/concepts/components/v2/capabilities/directory#directory-capability-rights) are "read-only".

## Routing directory capabilities

Components route directory capabilities by [exposing](https://fuchsia.dev/fuchsia-src/concepts/components/v2/capabilities/directory#expose) them to their
parent and [offering](https://fuchsia.dev/fuchsia-src/concepts/components/v2/capabilities/directory#offer) them to their children.

For more details on how the framework routes component capabilities,
see [capability routing](https://fuchsia.dev/docs/concepts/components/v2/capabilities/README#routing).

### Exposing

Exposing a directory capability gives the component's parent access to that
capability:

    {
        expose: [
            {
                directory: "data",
                from: "self",
            },
        ],
    }

You may optionally specify:

- [`as`](https://fuchsia.dev/fuchsia-src/concepts/components/v2/capabilities/directory#renaming)
- [`rights`](https://fuchsia.dev/fuchsia-src/concepts/components/v2/capabilities/directory#directory-capability-rights)
- [`subdir`](https://fuchsia.dev/fuchsia-src/concepts/components/v2/capabilities/directory#subdirectories)

### Offering

Offering a storage capability gives a child component access to that
capability:

    {
        offer: [
            {
                directory: "data",
                from: "parent",
                to: [ "#child-a", "#child-b" ],
            },
        ],
    }

You may optionally specify:

- [`as`](https://fuchsia.dev/fuchsia-src/concepts/components/v2/capabilities/directory#renaming)
- [`rights`](https://fuchsia.dev/fuchsia-src/concepts/components/v2/capabilities/directory#directory-capability-rights)
- [`subdir`](https://fuchsia.dev/fuchsia-src/concepts/components/v2/capabilities/directory#subdirectories)

## Consuming directory capabilities

To consume a storage capability, the component must request the capability and
open the corresponding path in its


[](https://fuchsia.dev/docs/glossary#namespace)


.

To request the capability, add a `use` declaration for it:

    {
        use: [
            {
                directory: "data",
                rights: ["r*"],
                path: "/data",
            },
        ],
    }

This populates the component's namespace with a directory at the provided `path`
containing the shared directory contents.

You must specify [`rights`](https://fuchsia.dev/fuchsia-src/concepts/components/v2/capabilities/directory#directory-capability-rights).
You may optionally specify [`subdir`](https://fuchsia.dev/fuchsia-src/concepts/components/v2/capabilities/directory#subdirectories).

### Consuming optional directory capabilities

See [Connect Components: Consuming optional capabilities](https://fuchsia.dev/docs/development/components/connect#consuming-optional-capabilities).

## Directory capability rights

Directory rights enable components to control access to directories as they are
routed throughout the system. Directory rights are applied as follows:

- [`capabilities`](https://fuchsia.dev/reference/cml#capabilities): *Required* . Provides the base set of rights available for the directory. Any rights specified in a `use`, `offer`, or `expose` must be a subset of what is declared here.
- [`use`](https://fuchsia.dev/reference/cml#use): *Required*. Describes the access rights requested by the consuming component.
- [`offer`](https://fuchsia.dev/reference/cml#offer): *Optional* . Modified rights available to the destination component. Rights are inherited from the `offer` source if not present.
- [`expose`](https://fuchsia.dev/reference/cml#expose): *Optional* . Modified rights available to the destination component. Rights are inherited from the `expose` source if not present.

The `rights` field can contain any combination of the following
[`fuchsia.io.Rights`](https://cs.opensource.google/fuchsia/fuchsia/+/main:/sdk/fidl/fuchsia.io/rights-abilities.fidl) tokens:

    rights: [
      "connect",
      "enumerate",
      "traverse",
      "read_bytes",
      "write_bytes",
      "execute_bytes",
      "update_attributes",
      "get_attributes",
      "modify_directory",
    ]

The framework provides a simplified form for declaring `rights` using *aliases*.
Each alias represents the combination of FIDL rights tokens to provide common
read, write, or execute access:

| Alias | FIDL rights |
|---|---|
| `r*` | `connect, enumerate, traverse, read_bytes,` `get_attributes` |
| `w*` | `connect, enumerate, traverse, write_bytes,` `update_attributes, modify_directory` |
| `x*` | `connect, enumerate, traverse, execute_bytes` |
| `rw*` | `connect, enumerate, traverse, read_bytes, write_bytes,` `get_attributes, update_attributes, modify_directory` |
| `rx*` | `connect, enumerate, traverse, read_bytes, execute_bytes,` `get_attributes` |

The `rights` field may only contain one alias. Additional FIDL rights may be
appended as long as they do not duplicate rights expressed by the alias.

### Example

Consider the following example where component `A` requests *read-write* access
to the `data` directory:

    // A.cml
    {
        use: [
            {
                directory: "data",
                rights: ["rw*"],
                path: "/data",
            },
        ],
    }

However, the parent component `B` offers the directory `data` to component `A`
with only *read-only* rights. In this case the routing fails and `data` wouldn't
be present in A's namespace.

    // B.cml
    {
        capabilities: [
            {
                directory: "data",
                rights: ["r*"],
                path: "/published-data",
            },
        ],
        offer: [
            {
                directory: "data",
                from: "self",
                to: [ "#A" ],
            },
        ],
    }

## Subdirectories

You may `expose`, `offer`, or `use` a subdirectory of a directory capability:

    {
        offer: [
            {
                directory: "data",
                from: "parent",
                to: [ "#child-a", "#child-b" ],
                subdir: "children",
            },
        ],
    }

## Renaming directories

You may `expose` or `offer` a directory capability by a different name:

    {
        offer: [
            {
                directory: "data",
                from: "#child-a",
                to: [ "#child-b" ],
                as: "a-data",
            },
        ],
    }

[](https://issues.fuchsia.dev/issues/new?component=1439230&template=1881640&description=Issue+on:+https://fuchsia.dev/docs/concepts/components/v2/capabilities/directory.md "Report a bug or suggest improvements on this page") [](https://cs.opensource.google/fuchsia/fuchsia/+/main:docs/concepts/components/v2/capabilities/directory.md "View the source code of this page") [](https://ci.android.com/edit?repo=fuchsia/fuchsia/main&file=docs/concepts/components/v2/capabilities/directory.md&cloneRepo=fuchsia/fuchsia:refs/heads/main&fullPath=fuchsia/docs/concepts/components/v2/capabilities/directory.md "Edit the source code of this page")