# Disk space consumption in IPFS

**URL:** <https://discuss.ipfs.tech/t/disk-space-consumption-in-ipfs/14516>\
**Category:** Kubo\
**Created:** [June 21, 2022, 8:59pm UTC](https://discuss.ipfs.tech/t/disk-space-consumption-in-ipfs/14516 "2022-06-21T20:59:04Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![hector](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ipfs.tech/hector/32/178_2.png) [@hector](https://discuss.ipfs.tech/u/hector)\
**Post date:** [June 23, 2022, 8:21am UTC](https://discuss.ipfs.tech/t/disk-space-consumption-in-ipfs/14516/4 "2022-06-23T08:21:29Z")

</div>

Hello,

> [@justincjohnson](#):
>
> - Then get the size by summing up `node.Size()` ([go-ipfs/gateway\_handler\_unixfs\_dir.go at 88d88158c61dc657b2c9d83107ec02d0d6040cef · ipfs/go-ipfs · GitHub](https://github.com/ipfs/go-ipfs/blob/88d88158c61dc657b2c9d83107ec02d0d6040cef/core/corehttp/gateway_handler_unixfs_dir.go#L133-L146))
> - `node.Size()` comes from `BlockStat.Size()` ([go-ipfs/block.go at 88d88158c61dc657b2c9d83107ec02d0d6040cef · ipfs/go-ipfs · GitHub](https://github.com/ipfs/go-ipfs/blob/88d88158c61dc657b2c9d83107ec02d0d6040cef/core/coreapi/block.go#L149-L151))

You slipped here, unfortunately.

The size is summed from directory entries. These directories and nodes come from [GitHub - ipfs/go-ipfs-files: An old files library, please migrate to `github.com/ipfs/go-libipfs/files` instead.](https://github.com/ipfs/go-ipfs-files). Directory and Node are interfaces: the actual type behind is unixfs (called from [here](https://github.com/ipfs/go-ipfs/blob/88d88158c61dc657b2c9d83107ec02d0d6040cef/core/corehttp/gateway_handler_unixfs.go#L23-L35) , which arrives to [https://github.com/ipfs/go-unixfs/blob/v0.4.0/file/unixfile.go#L153](https://github.com/ipfs/go-unixfs/blob/v0.4.0/file/unixfile.go#L153)).

In term of directories, the underlying type would be ufsDirectory ([https://github.com/ipfs/go-unixfs/blob/59752aec6306c2ca2d9a020a2a9556d5f8bce956/file/unixfile.go#L19](https://github.com/ipfs/go-unixfs/blob/59752aec6306c2ca2d9a020a2a9556d5f8bce956/file/unixfile.go#L19). This has a Size() which is the sum of the [sizes of the links](https://github.com/ipfs/go-unixfs/blob/59752aec6306c2ca2d9a020a2a9556d5f8bce956/io/directory.go#L81-L83) (at least for basicDirectories). That is the size of the data of a unixfs directory node itself.

The gateway wants to show the sum of the sizes of the files and folders in the directory. The size of a unixfs folder would be counted as I mentioned just above. The rest would be unixfs files, which are of type [ufsFile](https://github.com/ipfs/go-unixfs/blob/59752aec6306c2ca2d9a020a2a9556d5f8bce956/file/unixfile.go#L177), and that implements the Size() method with: [FileSize()](https://github.com/ipfs/go-unixfs/blob/59752aec6306c2ca2d9a020a2a9556d5f8bce956/io/dagreader.go#L60).

FileSize() eventually gets the[FileSize field of the unixfs node](https://github.com/ipfs/go-unixfs/blob/59752aec6306c2ca2d9a020a2a9556d5f8bce956/pb/unixfs.pb.go#L125), which is a field that does not require any computing as it comes already [hardcoded in the unixfs file protobuf](https://github.com/ipfs/go-unixfs/blob/59752aec6306c2ca2d9a020a2a9556d5f8bce956/pb/unixfs.pb.go#L78).

As you know, a big file will be made of many Blocks glued together in a MerkleDAG. The FileSize is set during the construction of the dags and contains the sum of sizes of the blocks ([https://github.com/ipfs/go-unixfs/blob/59752aec6306c2ca2d9a020a2a9556d5f8bce956/importer/balanced/builder.go#L157-L170](https://github.com/ipfs/go-unixfs/blob/59752aec6306c2ca2d9a020a2a9556d5f8bce956/importer/balanced/builder.go#L157-L170)).

And this is why the directory view in the gateway can report the total size of the files in the directory. Note this requires fetching the root node for every item in the directory, which is why a recent optimization avoids that for directories with many entries, in which case I think it won’t show the size.

* * *

Now for the topic of what size is what, there are several layers wrapping each other (Merkledag (“ipfs object stat…” for merkledag-pb dag nodes), unixfs (“ipfs files stat” for unixfs nodes) and blocks (“ipfs block stat”, which is what the the BlockStat you found gives), each of them with a concept of what “size” is. See [Finding the DataType of a dag-pb block via the HTTP API - #6 by hector](https://discuss.ipfs.tech/t/finding-the-datatype-of-a-dag-pb-block-via-the-http-api/9326/6). “ipfs dag stat” would operate on the same level as “ipfs object stat”, except for arbitrary IPLD DAG nodes, not necessarily merkledag-pb.

Hope that helps.

---

_[View the full topic](https://discuss.ipfs.tech/t/disk-space-consumption-in-ipfs/14516)._
