2026-04-02 16:00:27 +02:00
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
2026-04-02 16:25:49 +02:00
|
|
|
// Copyright (C) 2011 Andrea Mazzoleni
|
2011-03-04 20:59:21 +01:00
|
|
|
|
|
|
|
|
#ifndef __HANDLE_H
|
|
|
|
|
#define __HANDLE_H
|
|
|
|
|
|
2011-09-23 21:48:50 +02:00
|
|
|
#include "state.h"
|
2015-02-02 23:39:46 +01:00
|
|
|
#include "support.h"
|
2025-03-27 12:57:41 -07:00
|
|
|
#include "bw.h"
|
2011-09-23 21:48:50 +02:00
|
|
|
|
2011-03-04 20:59:21 +01:00
|
|
|
/****************************************************************************/
|
|
|
|
|
/* handle */
|
|
|
|
|
|
|
|
|
|
struct snapraid_handle {
|
|
|
|
|
char path[PATH_MAX]; /**< Path of the file. */
|
|
|
|
|
struct snapraid_disk* disk; /**< Disk of the file. */
|
2013-04-28 19:01:36 +02:00
|
|
|
struct snapraid_file* file; /**< File opened. When the file is closed, it's set to 0. */
|
2011-03-04 20:59:21 +01:00
|
|
|
int f; /**< Handle of the file. */
|
2026-09-09 09:58:56 +02:00
|
|
|
struct stat st; /**< Stat info of the opened file. st_size tracks the current EOF after recovery writes. */
|
2016-05-15 14:18:44 +02:00
|
|
|
struct advise_struct advise; /**< Advise information. */
|
2013-06-01 22:49:38 +02:00
|
|
|
int created; /**< If the file was created, otherwise it was already existing. */
|
2026-08-14 11:40:15 +02:00
|
|
|
int readonly_errno; /**< Non-zero if opened read-only as fallback. */
|
2025-03-27 12:57:41 -07:00
|
|
|
struct snapraid_bw* bw; /**< Context for bandwidth limiting. */
|
2011-03-04 20:59:21 +01:00
|
|
|
};
|
|
|
|
|
|
2011-03-18 22:38:11 +01:00
|
|
|
/**
|
2026-08-29 21:25:49 +02:00
|
|
|
* Create or reopen 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 the normal file is created, the handle->created is set.
|
2011-06-12 14:18:36 +02:00
|
|
|
* The initial size of the file is stored in the file->st struct.
|
2011-06-11 09:38:19 +02:00
|
|
|
* 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.
|
2011-03-04 20:59:21 +01:00
|
|
|
*/
|
2014-09-04 22:52:04 +02:00
|
|
|
int handle_create(struct snapraid_handle* handle, struct snapraid_file* file, int mode);
|
2011-03-04 20:59:21 +01:00
|
|
|
|
2026-08-29 21:25:49 +02:00
|
|
|
/**
|
|
|
|
|
* 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);
|
|
|
|
|
|
2014-12-28 19:11:58 +01:00
|
|
|
/**
|
|
|
|
|
* Truncate a file if required.
|
|
|
|
|
*/
|
|
|
|
|
int handle_truncate(struct snapraid_handle* handle, struct snapraid_file* file);
|
|
|
|
|
|
2011-03-04 20:59:21 +01:00
|
|
|
/**
|
2015-10-07 22:07:20 +02:00
|
|
|
* Open a file.
|
2011-03-04 20:59:21 +01:00
|
|
|
* The file is opened for reading.
|
|
|
|
|
*/
|
2026-06-03 20:49:36 +02:00
|
|
|
int handle_open(struct snapraid_handle* handle, struct snapraid_file* file, int mode, log_ptr* out_missing);
|
2011-03-04 20:59:21 +01:00
|
|
|
|
2011-03-12 23:36:37 +01:00
|
|
|
/**
|
2015-10-07 22:07:20 +02:00
|
|
|
* Close a file.
|
2011-03-12 23:36:37 +01:00
|
|
|
*/
|
2011-03-18 22:38:11 +01:00
|
|
|
int handle_close(struct snapraid_handle* handle);
|
2011-03-04 20:59:21 +01:00
|
|
|
|
2011-03-12 23:36:37 +01:00
|
|
|
/**
|
|
|
|
|
* Read a block from a file.
|
2011-04-23 21:38:26 +02:00
|
|
|
* If the read block is shorter, it's padded with 0.
|
2011-03-12 23:36:37 +01:00
|
|
|
*/
|
2026-06-04 00:34:51 +02:00
|
|
|
ssize_t handle_read(struct snapraid_handle* handle, block_off_t file_pos, unsigned char* block_buffer, unsigned block_size, log_ptr* out_missing);
|
2011-03-04 20:59:21 +01:00
|
|
|
|
2011-03-12 23:36:37 +01:00
|
|
|
/**
|
2015-10-07 22:07:20 +02:00
|
|
|
* Write a block to a file.
|
2011-03-12 23:36:37 +01:00
|
|
|
*/
|
2015-10-03 10:43:02 +02:00
|
|
|
int handle_write(struct snapraid_handle* handle, block_off_t file_pos, unsigned char* block_buffer, unsigned block_size);
|
2011-03-04 20:59:21 +01:00
|
|
|
|
2011-08-31 23:33:43 +02:00
|
|
|
/**
|
2015-10-07 22:07:20 +02:00
|
|
|
* Change the modification time of the file to the saved value.
|
2011-08-31 23:33:43 +02:00
|
|
|
*/
|
|
|
|
|
int handle_utime(struct snapraid_handle* handle);
|
|
|
|
|
|
2011-09-23 21:48:50 +02:00
|
|
|
/**
|
2015-10-07 22:07:20 +02:00
|
|
|
* Map the unsorted list of disk to an ordered vector.
|
2011-09-23 21:48:50 +02:00
|
|
|
* \param diskmax The size of the vector.
|
|
|
|
|
* \return The allocated vector of pointers.
|
|
|
|
|
*/
|
2016-01-18 22:38:01 +01:00
|
|
|
struct snapraid_handle* handle_mapping(struct snapraid_state* state, unsigned* diskmax);
|
2011-09-23 21:48:50 +02:00
|
|
|
|
2011-03-04 20:59:21 +01:00
|
|
|
#endif
|
2026-09-14 22:21:16 +02:00
|
|
|
|