snapraid/cmdline/handle.h

93 lines
3.4 KiB
C

// SPDX-License-Identifier: GPL-3.0-or-later
// Copyright (C) 2011 Andrea Mazzoleni
#ifndef HANDLE_H
#define HANDLE_H
#include "state.h"
#include "support.h"
#include "bw.h"
/****************************************************************************/
/* handle */
struct snapraid_handle {
char path[PATH_MAX]; /**< Path of the file. */
struct snapraid_disk* disk; /**< Disk of the file. */
struct snapraid_file* file; /**< File opened. When the file is closed, it's set to 0. */
int f; /**< Handle of the file. */
struct stat st; /**< Stat info of the opened file. st_size tracks the current EOF after recovery writes. */
struct advise_struct advise; /**< Advise information. */
int created; /**< If the file was created, otherwise it was already existing. */
int readonly_errno; /**< Non-zero if opened read-only as fallback. */
struct snapraid_bw* bw; /**< Context for bandwidth limiting. */
};
/**
* Open or create a recovery file.
* A new recovery attempt always uses the normal filename, even if a previous .unrecoverable file exists.
* FILE_IS_QUARANTINED determines whether the .unrecoverable filename is opened in the current run.
* If create is non-zero, a missing file is created and handle->created is set.
* If create is zero, a missing file is not created, returning -1 with errno == ENOENT.
* The initial size of the file is stored in the file->st struct.
* If the file cannot be opened for write access, it's opened with read-only access.
* The read-only access works only if the file has already the correct size and doesn't need to be modified.
*/
int handle_open_create(struct snapraid_handle* handle, struct snapraid_file* file, int mode, int create);
/**
* Move an opened file to its persistent .unrecoverable name.
* A previous .unrecoverable from an older recovery attempt is replaced.
* If already quarantined in the current run, do nothing.
* On error, log the failure and return -1 with errno set.
*/
int handle_quarantine(struct snapraid_handle* handle, struct snapraid_file* file, int mode);
/**
* Remove an .unrecoverable file left by a previous recovery attempt.
* A missing .unrecoverable file is not an error.
* On error, log the failure and return -1 with errno set.
*/
int handle_unrecoverable_remove(struct snapraid_handle* handle, struct snapraid_file* file);
/**
* Truncate a file if required.
*/
int handle_truncate(struct snapraid_handle* handle, struct snapraid_file* file);
/**
* Open a file.
* The file is opened for reading.
*/
int handle_open(struct snapraid_handle* handle, struct snapraid_file* file, int mode, log_ptr* out_missing);
/**
* Close a file.
*/
int handle_close(struct snapraid_handle* handle);
/**
* Read a block from a file.
* If the read block is shorter, it's padded with 0.
*/
ssize_t handle_read(struct snapraid_handle* handle, block_off_t file_pos, unsigned char* block_buffer, unsigned block_size, log_ptr* out_missing);
/**
* Write a block to a file.
*/
int handle_write(struct snapraid_handle* handle, block_off_t file_pos, unsigned char* block_buffer, unsigned block_size);
/**
* Change the modification time of the file to the saved value.
*/
int handle_utime(struct snapraid_handle* handle);
/**
* Map the unsorted list of disk to an ordered vector.
* \param diskmax The size of the vector.
* \return The allocated vector of pointers.
*/
struct snapraid_handle* handle_mapping(struct snapraid_state* state, unsigned* diskmax);
#endif