2011-03-04 20:59:21 +01:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2011 Andrea Mazzoleni
|
|
|
|
|
*
|
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#ifndef __ELEM_H
|
|
|
|
|
#define __ELEM_H
|
|
|
|
|
|
|
|
|
|
#include "md5.h"
|
|
|
|
|
#include "tommylist.h"
|
|
|
|
|
#include "tommyhash.h"
|
|
|
|
|
#include "tommyhashdyn.h"
|
|
|
|
|
#include "tommyarray.h"
|
|
|
|
|
|
|
|
|
|
/****************************************************************************/
|
|
|
|
|
/* snapraid */
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Size of the hash used as a checksum.
|
|
|
|
|
*/
|
|
|
|
|
#define HASH_MAX MD5_SIZE
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Invalid position.
|
|
|
|
|
*/
|
|
|
|
|
#define POS_INVALID -1
|
|
|
|
|
|
|
|
|
|
/**
|
2011-03-12 10:09:59 +01:00
|
|
|
* Basic block position type.
|
2011-03-06 22:52:50 +01:00
|
|
|
* With 32 bits and 128k blocks you can address 256 TB.
|
2011-03-04 20:59:21 +01:00
|
|
|
*/
|
2011-03-12 10:09:59 +01:00
|
|
|
typedef uint32_t block_off_t;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Basic data position type.
|
|
|
|
|
*/
|
|
|
|
|
typedef uint64_t data_off_t;
|
2011-03-04 20:59:21 +01:00
|
|
|
|
|
|
|
|
struct snapraid_file;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Block of a file.
|
|
|
|
|
*/
|
|
|
|
|
struct snapraid_block {
|
2011-03-12 10:09:59 +01:00
|
|
|
block_off_t parity_pos; /**< Position of the block in the parity. */
|
2011-03-04 20:59:21 +01:00
|
|
|
int is_hashed; /**< If the hash of the block is valid. */
|
|
|
|
|
struct snapraid_file* file; /**< Back pointer to the file owning this block. */
|
|
|
|
|
unsigned char hash[HASH_MAX]; /**< Hash of the block. */
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* File.
|
|
|
|
|
*/
|
|
|
|
|
struct snapraid_file {
|
|
|
|
|
char sub[PATH_MAX]; /**< Sub path of the file. Without the disk dir. The disk is implicit. */
|
2011-03-12 10:09:59 +01:00
|
|
|
data_off_t size; /**< Size of the file. */
|
2011-03-04 20:59:21 +01:00
|
|
|
struct snapraid_block* blockvec; /**< All the blocks of the file. */
|
2011-03-12 10:09:59 +01:00
|
|
|
block_off_t blockmax; /**< Number of blocks. */
|
2011-03-04 20:59:21 +01:00
|
|
|
time_t mtime; /**< Modification time. */
|
|
|
|
|
int is_present; /**< If it's seen as present. */
|
|
|
|
|
|
|
|
|
|
/* nodes for data structures */
|
|
|
|
|
tommy_node nodelist;
|
|
|
|
|
tommy_hashdyn_node nodeset;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Disk.
|
|
|
|
|
*/
|
|
|
|
|
struct snapraid_disk {
|
|
|
|
|
char name[PATH_MAX]; /**< Name of the disk. */
|
|
|
|
|
char dir[PATH_MAX]; /**< Mount point of the disk. */
|
2011-03-12 10:09:59 +01:00
|
|
|
block_off_t first_free_block; /**< First free searching block. */
|
2011-03-04 20:59:21 +01:00
|
|
|
tommy_list filelist; /**< List of all the files. */
|
|
|
|
|
tommy_hashdyn fileset; /**< Hashtable by sub of all the files. */
|
|
|
|
|
tommy_array blockarr; /**< Block array of the disk. */
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the relative position of a block inside the file.
|
|
|
|
|
*/
|
2011-03-12 10:09:59 +01:00
|
|
|
block_off_t block_file_pos(struct snapraid_block* block);
|
2011-03-04 20:59:21 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the size in bytes of the block.
|
|
|
|
|
* If it's the last block of a file it could less than block_size.
|
|
|
|
|
*/
|
|
|
|
|
unsigned block_file_size(struct snapraid_block* block, unsigned block_size);
|
|
|
|
|
|
2011-03-12 10:09:59 +01:00
|
|
|
struct snapraid_file* file_alloc(unsigned block_size, const char* sub, data_off_t size, time_t mtime);
|
2011-03-04 20:59:21 +01:00
|
|
|
|
|
|
|
|
void file_free(struct snapraid_file* file);
|
|
|
|
|
|
|
|
|
|
int file_compare(const void* void_arg, const void* void_data);
|
|
|
|
|
|
|
|
|
|
static inline tommy_uint32_t file_hash(const char* sub)
|
|
|
|
|
{
|
|
|
|
|
return tommy_hash_u32(0, sub, strlen(sub));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct snapraid_disk* disk_alloc(const char* name, const char* dir);
|
|
|
|
|
|
|
|
|
|
void disk_free(struct snapraid_disk* disk);
|
|
|
|
|
|
2011-03-12 10:09:59 +01:00
|
|
|
static inline struct snapraid_block* disk_block_get(struct snapraid_disk* disk, block_off_t pos)
|
2011-03-04 20:59:21 +01:00
|
|
|
{
|
|
|
|
|
if (pos < tommy_array_size(&disk->blockarr))
|
|
|
|
|
return tommy_array_get(&disk->blockarr, pos);
|
|
|
|
|
else
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|