Cache

Cache — paths and tags (UUID/LABEL) caching

Functions

Types and Values

struct libmnt_cache

Description

The cache is a very simple API for working with tags (LABEL, UUID, ...) and paths. The cache uses libblkid as a backend for TAGs resolution.

All returned paths are always canonicalized.

Functions

mnt_new_cache ()

struct libmnt_cache *
mnt_new_cache (void);

Returns

new struct libmnt_cache instance or NULL in case of ENOMEM error.


mnt_free_cache ()

void
mnt_free_cache (struct libmnt_cache *cache);

Deallocates the cache. This function does not care about reference count. Don't use this function directly -- it's better to use use mnt_unref_cache().

Parameters

cache

pointer to struct libmnt_cache instance

 

mnt_ref_cache ()

void
mnt_ref_cache (struct libmnt_cache *cache);

Increments reference counter.

Parameters

cache

cache pointer

 

mnt_unref_cache ()

void
mnt_unref_cache (struct libmnt_cache *cache);

De-increments reference counter, on zero the cache is automatically deallocated by mnt_free_cache().

Parameters

cache

cache pointer

 

mnt_cache_device_has_tag ()

int
mnt_cache_device_has_tag (struct libmnt_cache *cache,
                          const char *devname,
                          const char *token,
                          const char *value);

Look up cache to check if tag +value are associated with devname .

Parameters

cache

paths cache

 

devname

path to the device

 

token

tag name (e.g "LABEL")

 

value

tag value

 

Returns

1 on success or 0.


mnt_cache_find_tag_value ()

char *
mnt_cache_find_tag_value (struct libmnt_cache *cache,
                          const char *devname,
                          const char *token);

Parameters

cache

cache for results

 

devname

device name

 

token

tag name ("LABEL" or "UUID")

 

Returns

LABEL or UUID for the devname or NULL in case of error.


mnt_cache_read_tags ()

int
mnt_cache_read_tags (struct libmnt_cache *cache,
                     const char *devname);

Reads devname LABEL and UUID to the cache .

Parameters

cache

pointer to struct libmnt_cache instance

 

devname

path device

 

Returns

0 if at least one tag was added, 1 if no tag was added or negative number in case of error.


mnt_cache_set_targets ()

int
mnt_cache_set_targets (struct libmnt_cache *cache,
                       struct libmnt_table *mtab);

Add to cache reference to mtab . This allows to avoid unnecessary paths canonicalization in mnt_resolve_target().

Parameters

cache

cache pointer

 

mtab

table with already canonicalized mountpoints

 

Returns

negative number in case of error, or 0 o success.


mnt_get_fstype ()

char *
mnt_get_fstype (const char *devname,
                int *ambi,
                struct libmnt_cache *cache);

Parameters

devname

device name

 

ambi

returns TRUE if probing result is ambivalent (optional argument)

 

cache

cache for results or NULL

 

Returns

filesystem type or NULL in case of error. The result has to be deallocated by free() if cache is NULL.


mnt_pretty_path ()

char *
mnt_pretty_path (const char *path,
                 struct libmnt_cache *cache);

Converts path:

  • to the absolute path

  • /dev/dm-N to /dev/mapper/name

  • /dev/loopN to the loop backing filename

  • empty path (NULL) to 'none'

Parameters

path

any path

 

cache

NULL or pointer to the cache

 

Returns

newly allocated string with path, result always has to be deallocated by free().


mnt_resolve_path ()

char *
mnt_resolve_path (const char *path,
                  struct libmnt_cache *cache);

Converts path:

  • to the absolute path

  • /dev/dm-N to /dev/mapper/name

Parameters

path

"native" path

 

cache

cache for results or NULL

 

Returns

absolute path or NULL in case of error. The result has to be deallocated by free() if cache is NULL.


mnt_resolve_spec ()

char *
mnt_resolve_spec (const char *spec,
                  struct libmnt_cache *cache);

Parameters

spec

path or tag

 

cache

paths cache

 

Returns

canonicalized path or NULL. The result has to be deallocated by free() if cache is NULL.


mnt_resolve_tag ()

char *
mnt_resolve_tag (const char *token,
                 const char *value,
                 struct libmnt_cache *cache);

Parameters

token

tag name

 

value

tag value

 

cache

for results or NULL

 

Returns

device name or NULL in case of error. The result has to be deallocated by free() if cache is NULL.


mnt_resolve_target ()

char *
mnt_resolve_target (const char *path,
                    struct libmnt_cache *cache);

Like mnt_resolve_path(), unless cache is not NULL and mnt_cache_set_targets(cache, mtab) was called: if path is found in the cached mtab and the matching entry was provided by the kernel, assume that path is already canonicalized. By avoiding a call to realpath(2) on known mount points, there is a lower risk of stepping on a stale mount point, which can result in an application freeze. This is also faster in general, as stat(2) on a mount point is slower than on a regular file.

Parameters

path

"native" path, a potential mount point

 

cache

cache for results or NULL.

 

Returns

absolute path or NULL in case of error. The result has to be deallocated by free() if cache is NULL.

Types and Values

struct libmnt_cache

struct libmnt_cache;

Stores canonicalized paths and evaluated tags