fsautoproc
Basic file transformation automation management utility
Loading...
Searching...
No Matches
index.c File Reference

File index mapping and serialization implementation. More...

#include "index.h"
#include <errno.h>
#include <inttypes.h>
#include <stdlib.h>
#include <string.h>
#include "jemalloc/jemalloc.h"
#include "je.h"
#include "log.h"
Include dependency graph for index.c:

Macros

#define INDEXMAXFP   512
 The maximum filepath length of a file in the index.
#define indexbucket(hash)
 Maps and casts the hash value to a bucket index via the last N bits (where N is the number of buckets, INDEXBUCKETS).

Functions

uint64_t indexhash (const char *fp)
 Hashes the filepath string for use in the index map.
struct inode_sindexfind (const struct index_s *idx, const char *fp, const uint64_t fphash)
 Searches the index for a node with a matching filepath.
static int indexnodecmp (const void *a, const void *b)
 Compares two file nodes for sorting in ascending order by filepath.
int indexwrite (struct index_s *idx, FILE *s)
 Flattens the index map into a sorted array of nodes (by filepath). The list is then written to the file stream and freed.
int indexread (struct index_s *idx, FILE *s)
 Reads a file stream and deserializes the contents into a map of individual file nodes.
static void indexappend (struct ibucket_s *bucket, struct inode_s *node)
 Appends the node to the end of the bucket (linked list), potentially assigning a new head if the bucket is empty.
struct inode_sindexput (struct index_s *idx, const char *fp, const uint64_t fphash, const struct fsstat_s st)
 Copies the node and inserts it into the index mapping.
static void indexfree_r (struct inode_s *idx)
 Recursively frees a linked list of nodes starting from a given head.
void indexfree (struct index_s *idx)
 Frees all nodes in the index map.
struct inode_s ** indexlist (const struct index_s *idx)
 Flattens the index map into an unsorted array of nodes. The list is dynamically allocated and must be freed by the caller. Array size is determined by the size field in the index struct.

Detailed Description

File index mapping and serialization implementation.

Macro Definition Documentation

◆ indexbucket

#define indexbucket ( hash)
Value:
((int) (hash & INDEXBUCKETSMASK))
#define INDEXBUCKETSMASK
The bitmask used for deriving the bucket index from the hash value.
Definition index.h:36

Maps and casts the hash value to a bucket index via the last N bits (where N is the number of buckets, INDEXBUCKETS).

◆ INDEXMAXFP

#define INDEXMAXFP   512

The maximum filepath length of a file in the index.

Function Documentation

◆ indexappend()

void indexappend ( struct ibucket_s * bucket,
struct inode_s * node )
static

Appends the node to the end of the bucket (linked list), potentially assigning a new head if the bucket is empty.

Parameters
headThe head of the linked list
nodeThe node pointer to insert
Here is the caller graph for this function:

◆ indexfind()

struct inode_s * indexfind ( const struct index_s * idx,
const char * fp,
uint64_t fphash )

Searches the index for a node with a matching filepath.

Parameters
idxThe index to search
fpThe search value (filepath) to compare
fphashThe hash value of the filepath to compare
Returns
If a match is found, its pointer is returned, otherwise NULL.
Here is the caller graph for this function:

◆ indexfree()

void indexfree ( struct index_s * idx)

Frees all nodes in the index map.

Parameters
idxThe index to free
Here is the call graph for this function:
Here is the caller graph for this function:

◆ indexfree_r()

void indexfree_r ( struct inode_s * idx)
static

Recursively frees a linked list of nodes starting from a given head.

Parameters
idxThe head of the linked list
Here is the caller graph for this function:

◆ indexhash()

uint64_t indexhash ( const char * fp)

Hashes the filepath string for use in the index map.

Note
64-bit FNV-1a implementation is used for hashing.
Parameters
fpThe filepath string to hash
Returns
The hashed value of the filepath.
Here is the caller graph for this function:

◆ indexlist()

struct inode_s ** indexlist ( const struct index_s * idx)

Flattens the index map into an unsorted array of nodes. The list is dynamically allocated and must be freed by the caller. Array size is determined by the size field in the index struct.

Parameters
idxThe index to flatten
Returns
If successful, a pointer to an array of size idx->size is returned. Otherwise, NULL is returned and errno is set.
Here is the caller graph for this function:

◆ indexnodecmp()

int indexnodecmp ( const void * a,
const void * b )
static

Compares two file nodes for sorting in ascending order by filepath.

Parameters
aThe first file node to compare
bThe second file node to compare
Returns
The result of the comparison.
Note
This function is equivalent to strcmp(a->fp, b->fp).
Here is the caller graph for this function:

◆ indexput()

struct inode_s * indexput ( struct index_s * idx,
const char * fp,
uint64_t fphash,
struct fsstat_s st )

Copies the node and inserts it into the index mapping.

Parameters
idxThe index to insert into
fpThe file path to use for the new node, duplicated internally
fphashThe file path hash value to use for the new node
stThe file stat info to use for the new node
Returns
The pointer to the new node in the index map, otherwise NULL is returned and errno is set.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ indexread()

int indexread ( struct index_s * idx,
FILE * s )

Reads a file stream and deserializes the contents into a map of individual file nodes.

Parameters
idxThe index to populate
sThe file stream to read from
Returns
If successful, 0 is returned. Otherwise, -1 is returned and errno is set.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ indexwrite()

int indexwrite ( struct index_s * idx,
FILE * s )

Flattens the index map into a sorted array of nodes (by filepath). The list is then written to the file stream and freed.

Parameters
idxThe index to flatten
sThe file stream to write to
Returns
If successful, 0 is returned. Otherwise, -1 is returned and errno is set.
Here is the call graph for this function:
Here is the caller graph for this function: