---
name: "mdex_add"
section: "3"
platform: "linux"
package: "liblibrecast-dev"
version: "0.9.1-1"
release: "Debian 13 (Trixie)"
platforms: ["linux"]
url: "https://new.carta.tech/mdex_add"
---

# mdex\_add(3)

index data, files and directories

## NAME

mdex\_add, mdex\_addfile, mdex\_alias - index data, files and directories

## LIBRARY

Librecast library (*liblibrecast*, *-llibrecast*)

## SYNOPSIS

```
#include <librecast/mdex.h>
```

**int mdex\_add(mdex\_t** *\*mdex***, void** *\*data***, size\_t** *len***, q\_t** *\*q***, int** *flags***);** **int mdex\_addfile(mdex\_t** *\*mdex***, const char** *\*path***, q\_t** *\*q***, int** *flags***);** **int mdex\_alias(mdex\_t** *\*mdex***, unsigned char** *\*alias***, size\_t** *aliaslen***,**                **unsigned char** *\*hash***, size\_t** *hashlen***);**

Compile and link with *-llibrecast*.

## DESCRIPTION

The **mdex\_add** function builds a merkle tree from *data* with length *len* and adds entries for both the tree and chunks to *mdex*.

The **mdex\_addfile** function builds a merkle tree from the file or directory at *path* and adds entries for the trees and chunks to *mdex*.

The **mdex\_alias** function adds an entry *alias* of length *aliaslen* which points to the entry *hash* of length *hashlen* which must already exist in *mdex*.

If *q* is not NULL, it must point to a queue *q\_t* structure previously initialized with [**q\_init**(3)](/q_init/3.md). This will be passed through to [**mtree\_build**(3)](/mtree_build/3.md) to build the merkle tree. This is normally used in conjunction with a threadpool created with [**q\_pool\_create**(3)](/q_pool_create/3.md) with threads all calling [**q\_job\_seek**(3)](/q_job_seek/3.md).

The *flags* argument is the bitwise OR of zero of more of the following flags:

- **MDEX\_ALIAS**

  Entry is a pointer to another entry.

**MDEX\_RAND** Entry should be sent using random symbols.

**MDEX\_RECURSE** Recursively index all files and directories below *path*. (mdex\_addfile)

**MDEX\_MOUNT** If set, stay within the same filesystem (i.e., do not cross mount points).

- **MDEX\_SYMLINK**

  Follow symbolic links when recursively indexing.

## RETURN VALUE

These function return zero on success. On error, -1 is returned, and *errno* is set to indicate the error.

## ERRORS

- **EINVAL**

  Invalid argument. Both *mdex* and *hash* are required (not NULL), and *hashlen* \> 0.

**ENOMEM** Not enough space/cannot allocate memory (POSIX.1-2001).

## SEE ALSO

[**mdex\_init**(3)](/mdex_init/3.md), [**mdex\_free**(3)](/mdex_free/3.md), [**mdex\_get**(3)](/mdex_get/3.md), [**mdex\_put**(3)](/mdex_put/3.md), [**mdex\_del**(3)](/mdex_del/3.md), [**mtree\_build**(3)](/mtree_build/3.md), [**q\_init**(3)](/q_init/3.md), [**q\_pool\_create**(3)](/q_pool_create/3.md), [**q\_job\_seek**(3)](/q_job_seek/3.md) [**lc\_share**(3)](/lc_share/3.md)
