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/>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "portable.h"
|
|
|
|
|
|
|
|
|
|
#include "elem.h"
|
2015-01-02 18:52:04 +01:00
|
|
|
#include "support.h"
|
2011-03-04 20:59:21 +01:00
|
|
|
#include "handle.h"
|
|
|
|
|
|
|
|
|
|
/****************************************************************************/
|
|
|
|
|
/* handle */
|
|
|
|
|
|
2014-09-04 22:52:04 +02:00
|
|
|
int handle_create(struct snapraid_handle* handle, struct snapraid_file* file, int mode)
|
2011-03-18 22:38:11 +01:00
|
|
|
{
|
|
|
|
|
int ret;
|
2013-04-04 22:52:56 +02:00
|
|
|
int flags;
|
2011-03-18 22:38:11 +01:00
|
|
|
|
|
|
|
|
/* if it's the same file, and already opened, nothing to do */
|
|
|
|
|
if (handle->file == file && handle->f != -1) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-16 23:52:10 +01:00
|
|
|
pathprint(handle->path, sizeof(handle->path), "%s%s", handle->disk->dir, file->sub);
|
2011-03-04 20:59:21 +01:00
|
|
|
|
2013-02-20 13:29:58 +01:00
|
|
|
ret = mkancestor(handle->path);
|
2011-03-22 23:20:12 +01:00
|
|
|
if (ret != 0) {
|
2014-02-12 23:04:25 +01:00
|
|
|
/* LCOV_EXCL_START */
|
2011-03-22 23:20:12 +01:00
|
|
|
return -1;
|
2014-02-12 23:04:25 +01:00
|
|
|
/* LCOV_EXCL_STOP */
|
2011-03-22 23:20:12 +01:00
|
|
|
}
|
|
|
|
|
|
2013-06-01 22:49:38 +02:00
|
|
|
/* initial values, changed later if required */
|
|
|
|
|
handle->created = 0;
|
|
|
|
|
|
2014-01-26 10:17:13 +01:00
|
|
|
/* flags for opening */
|
|
|
|
|
/* O_BINARY: open as binary file (Windows only) */
|
2012-11-04 00:26:30 +01:00
|
|
|
/* O_NOFOLLOW: do not follow links to ensure to open the real file */
|
2014-01-26 10:17:13 +01:00
|
|
|
/* O_SEQUENTIAL: improve performance for sequential access (Windows only) */
|
|
|
|
|
flags = O_BINARY | O_NOFOLLOW;
|
2014-09-04 22:52:04 +02:00
|
|
|
if ((mode & MODE_SEQUENTIAL) != 0)
|
2013-04-04 22:52:56 +02:00
|
|
|
flags |= O_SEQUENTIAL;
|
2014-01-26 10:17:13 +01:00
|
|
|
|
|
|
|
|
/* open for read write */
|
|
|
|
|
handle->f = open(handle->path, flags | O_RDWR);
|
|
|
|
|
|
|
|
|
|
/* if failed for missing write permission */
|
|
|
|
|
if (handle->f == -1 && (errno == EACCES || errno == EROFS)) {
|
|
|
|
|
/* open for real-only */
|
|
|
|
|
handle->f = open(handle->path, flags | O_RDONLY);
|
2011-06-11 09:38:19 +02:00
|
|
|
}
|
|
|
|
|
|
2013-06-01 22:49:38 +02:00
|
|
|
/* if failed for missing file */
|
|
|
|
|
if (handle->f == -1 && errno == ENOENT) {
|
2014-01-26 10:17:13 +01:00
|
|
|
char path_from[PATH_MAX];
|
|
|
|
|
|
|
|
|
|
/* check if exists a .unrecoverable copy, and rename to the real one */
|
2014-09-27 00:32:39 +02:00
|
|
|
pathprint(path_from, sizeof(path_from), "%s.unrecoverable", handle->path);
|
2014-01-26 10:17:13 +01:00
|
|
|
|
|
|
|
|
if (rename(path_from, handle->path) == 0) {
|
|
|
|
|
/* open for read write */
|
|
|
|
|
handle->f = open(handle->path, flags | O_RDWR);
|
|
|
|
|
} else {
|
|
|
|
|
/* create it */
|
|
|
|
|
handle->f = open(handle->path, flags | O_RDWR | O_CREAT, 0600);
|
|
|
|
|
if (handle->f != -1) {
|
|
|
|
|
/* mark it as created if really done */
|
|
|
|
|
handle->created = 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-06-01 22:49:38 +02:00
|
|
|
}
|
|
|
|
|
|
2011-03-04 20:59:21 +01:00
|
|
|
if (handle->f == -1) {
|
2014-02-12 23:04:25 +01:00
|
|
|
/* LCOV_EXCL_START */
|
2011-03-18 22:38:11 +01:00
|
|
|
/* invalidate for error */
|
|
|
|
|
handle->file = 0;
|
|
|
|
|
handle->f = -1;
|
2011-10-09 07:51:25 +02:00
|
|
|
handle->valid_size = 0;
|
2011-03-18 22:38:11 +01:00
|
|
|
|
2015-02-22 14:31:03 +01:00
|
|
|
msg_error("Error opening file '%s'. %s.\n", handle->path, strerror(errno));
|
2011-03-18 22:38:11 +01:00
|
|
|
return -1;
|
2014-02-12 23:04:25 +01:00
|
|
|
/* LCOV_EXCL_STOP */
|
2011-03-04 20:59:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* just opened */
|
|
|
|
|
handle->file = file;
|
|
|
|
|
|
2011-03-19 22:00:08 +01:00
|
|
|
/* get the stat info */
|
|
|
|
|
ret = fstat(handle->f, &handle->st);
|
2011-03-04 20:59:21 +01:00
|
|
|
if (ret != 0) {
|
2014-02-12 23:04:25 +01:00
|
|
|
/* LCOV_EXCL_START */
|
2015-02-22 14:31:03 +01:00
|
|
|
msg_error("Error accessing file '%s'. %s.\n", handle->path, strerror(errno));
|
2011-03-18 22:38:11 +01:00
|
|
|
return -1;
|
2014-02-12 23:04:25 +01:00
|
|
|
/* LCOV_EXCL_STOP */
|
2011-03-04 20:59:21 +01:00
|
|
|
}
|
|
|
|
|
|
2013-03-03 14:16:46 +01:00
|
|
|
/* get the size of the existing data */
|
2011-10-09 07:51:25 +02:00
|
|
|
handle->valid_size = handle->st.st_size;
|
|
|
|
|
|
2011-03-04 20:59:21 +01:00
|
|
|
#if HAVE_POSIX_FADVISE
|
2014-09-04 22:52:04 +02:00
|
|
|
if ((mode & MODE_SEQUENTIAL) != 0) {
|
2014-09-27 00:32:39 +02:00
|
|
|
/* advise sequential access */
|
2013-04-04 22:52:56 +02:00
|
|
|
ret = posix_fadvise(handle->f, 0, 0, POSIX_FADV_SEQUENTIAL);
|
|
|
|
|
if (ret != 0) {
|
2014-02-12 23:04:25 +01:00
|
|
|
/* LCOV_EXCL_START */
|
2015-02-22 14:31:03 +01:00
|
|
|
msg_error("Error advising file '%s'. %s.\n", handle->path, strerror(ret));
|
2013-04-04 22:52:56 +02:00
|
|
|
return -1;
|
2014-02-12 23:04:25 +01:00
|
|
|
/* LCOV_EXCL_STOP */
|
2013-04-04 22:52:56 +02:00
|
|
|
}
|
2011-03-04 20:59:21 +01:00
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-28 19:11:58 +01:00
|
|
|
int handle_truncate(struct snapraid_handle* handle, struct snapraid_file* file)
|
|
|
|
|
{
|
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
|
|
ret = ftruncate(handle->f, file->size);
|
|
|
|
|
if (ret != 0) {
|
|
|
|
|
/* LCOV_EXCL_START */
|
|
|
|
|
if (errno == EACCES) {
|
2015-02-22 14:31:03 +01:00
|
|
|
msg_error("Failed to truncate file '%s' for missing write permission.\n", handle->path);
|
2014-12-28 19:11:58 +01:00
|
|
|
} else {
|
2015-02-22 14:31:03 +01:00
|
|
|
msg_error("Error truncating file '%s'. %s.\n", handle->path, strerror(errno));
|
2014-12-28 19:11:58 +01:00
|
|
|
}
|
|
|
|
|
return -1;
|
|
|
|
|
/* LCOV_EXCL_STOP */
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* adjust the size to the truncated size */
|
|
|
|
|
handle->valid_size = file->size;
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-02 23:39:46 +01:00
|
|
|
int handle_open(struct snapraid_handle* handle, struct snapraid_file* file, int mode, fptr* out)
|
2011-03-04 20:59:21 +01:00
|
|
|
{
|
|
|
|
|
int ret;
|
2013-04-04 22:52:56 +02:00
|
|
|
int flags;
|
2011-03-04 20:59:21 +01:00
|
|
|
|
|
|
|
|
/* if already opened, nothing to do */
|
|
|
|
|
if (handle->file == file && handle->f != -1) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2011-08-26 14:18:38 +02:00
|
|
|
pathprint(handle->path, sizeof(handle->path), "%s%s", handle->disk->dir, file->sub);
|
2011-03-04 20:59:21 +01:00
|
|
|
|
2014-12-28 19:11:58 +01:00
|
|
|
/* for sure not created */
|
2013-06-01 22:49:38 +02:00
|
|
|
handle->created = 0;
|
|
|
|
|
|
2014-01-26 10:17:13 +01:00
|
|
|
/* flags for opening */
|
|
|
|
|
/* O_BINARY: open as binary file (Windows only) */
|
2012-11-04 00:26:30 +01:00
|
|
|
/* O_NOFOLLOW: do not follow links to ensure to open the real file */
|
2014-01-26 10:17:13 +01:00
|
|
|
/* O_SEQUENTIAL: improve performance for sequential access (Windows only) */
|
|
|
|
|
flags = O_BINARY | O_NOFOLLOW;
|
2014-09-04 22:52:04 +02:00
|
|
|
if ((mode & MODE_SEQUENTIAL) != 0)
|
2013-04-04 22:52:56 +02:00
|
|
|
flags |= O_SEQUENTIAL;
|
2014-01-26 10:17:13 +01:00
|
|
|
|
|
|
|
|
/* open for read */
|
|
|
|
|
handle->f = open_noatime(handle->path, flags | O_RDONLY);
|
2011-03-04 20:59:21 +01:00
|
|
|
if (handle->f == -1) {
|
|
|
|
|
/* invalidate for error */
|
|
|
|
|
handle->file = 0;
|
|
|
|
|
handle->f = -1;
|
2011-10-09 07:51:25 +02:00
|
|
|
handle->valid_size = 0;
|
2011-03-04 20:59:21 +01:00
|
|
|
|
2015-02-02 23:39:46 +01:00
|
|
|
out("Error opening file '%s'. %s.\n", handle->path, strerror(errno));
|
2011-03-18 22:38:11 +01:00
|
|
|
return -1;
|
2011-03-04 20:59:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* just opened */
|
|
|
|
|
handle->file = file;
|
|
|
|
|
|
2011-03-19 22:00:08 +01:00
|
|
|
/* get the stat info */
|
|
|
|
|
ret = fstat(handle->f, &handle->st);
|
|
|
|
|
if (ret != 0) {
|
2014-02-12 23:04:25 +01:00
|
|
|
/* LCOV_EXCL_START */
|
2015-02-02 23:39:46 +01:00
|
|
|
out("Error accessing file '%s'. %s.\n", handle->path, strerror(errno));
|
2011-03-19 22:00:08 +01:00
|
|
|
return -1;
|
2014-02-12 23:04:25 +01:00
|
|
|
/* LCOV_EXCL_STOP */
|
2011-03-19 22:00:08 +01:00
|
|
|
}
|
|
|
|
|
|
2012-07-10 23:24:31 +02:00
|
|
|
/* get the size of the existing data */
|
2011-10-09 07:51:25 +02:00
|
|
|
handle->valid_size = handle->st.st_size;
|
|
|
|
|
|
2011-03-04 20:59:21 +01:00
|
|
|
#if HAVE_POSIX_FADVISE
|
2014-09-04 22:52:04 +02:00
|
|
|
if ((mode & MODE_SEQUENTIAL) != 0) {
|
2013-04-04 22:52:56 +02:00
|
|
|
/* advise sequential access */
|
|
|
|
|
ret = posix_fadvise(handle->f, 0, 0, POSIX_FADV_SEQUENTIAL);
|
|
|
|
|
if (ret != 0) {
|
2014-02-12 23:04:25 +01:00
|
|
|
/* LCOV_EXCL_START */
|
2015-02-02 23:39:46 +01:00
|
|
|
out("Error advising file '%s'. %s.\n", handle->path, strerror(ret));
|
2013-04-04 22:52:56 +02:00
|
|
|
return -1;
|
2014-02-12 23:04:25 +01:00
|
|
|
/* LCOV_EXCL_STOP */
|
2013-04-04 22:52:56 +02:00
|
|
|
}
|
2011-03-04 20:59:21 +01:00
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-18 22:38:11 +01:00
|
|
|
int handle_close(struct snapraid_handle* handle)
|
2011-03-04 20:59:21 +01:00
|
|
|
{
|
|
|
|
|
int ret;
|
|
|
|
|
|
2011-08-27 15:19:19 +02:00
|
|
|
/* close if open */
|
2011-03-04 20:59:21 +01:00
|
|
|
if (handle->f != -1) {
|
|
|
|
|
ret = close(handle->f);
|
|
|
|
|
if (ret != 0) {
|
2014-02-12 23:04:25 +01:00
|
|
|
/* LCOV_EXCL_START */
|
2015-02-22 14:31:03 +01:00
|
|
|
msg_error("Error closing file '%s'. %s.\n", handle->file->sub, strerror(errno));
|
2013-06-18 21:13:36 +02:00
|
|
|
|
2011-03-18 22:38:11 +01:00
|
|
|
/* invalidate for error */
|
|
|
|
|
handle->file = 0;
|
|
|
|
|
handle->f = -1;
|
2011-10-09 07:51:25 +02:00
|
|
|
handle->valid_size = 0;
|
2011-03-18 22:38:11 +01:00
|
|
|
return -1;
|
2014-02-12 23:04:25 +01:00
|
|
|
/* LCOV_EXCL_STOP */
|
2011-03-04 20:59:21 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-08-27 15:19:19 +02:00
|
|
|
/* reset the descriptor */
|
2011-03-04 20:59:21 +01:00
|
|
|
handle->file = 0;
|
|
|
|
|
handle->f = -1;
|
2011-10-09 07:51:25 +02:00
|
|
|
handle->valid_size = 0;
|
2011-03-18 22:38:11 +01:00
|
|
|
|
|
|
|
|
return 0;
|
2011-03-04 20:59:21 +01:00
|
|
|
}
|
|
|
|
|
|
2015-02-02 23:39:46 +01:00
|
|
|
int handle_read(struct snapraid_handle* handle, struct snapraid_block* block, unsigned char* block_buffer, unsigned block_size, fptr* out)
|
2011-03-04 20:59:21 +01:00
|
|
|
{
|
|
|
|
|
ssize_t read_ret;
|
2011-03-12 10:09:59 +01:00
|
|
|
data_off_t offset;
|
2011-03-04 20:59:21 +01:00
|
|
|
unsigned read_size;
|
2012-03-13 19:11:09 +01:00
|
|
|
unsigned count;
|
2011-03-04 20:59:21 +01:00
|
|
|
|
2011-03-12 10:09:59 +01:00
|
|
|
offset = block_file_pos(block) * (data_off_t)block_size;
|
2011-03-04 20:59:21 +01:00
|
|
|
|
2011-10-09 07:51:25 +02:00
|
|
|
/* check if we are going to read only not initialized data */
|
|
|
|
|
if (offset >= handle->valid_size) {
|
2015-02-02 23:39:46 +01:00
|
|
|
out("Reading missing data from file '%s' at offset %" PRIu64 ".\n", handle->path, offset);
|
2011-10-09 07:51:25 +02:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-04 20:59:21 +01:00
|
|
|
read_size = block_file_size(block, block_size);
|
|
|
|
|
|
2012-03-13 19:11:09 +01:00
|
|
|
#if !HAVE_PREAD
|
2011-03-12 10:09:59 +01:00
|
|
|
if (lseek(handle->f, offset, SEEK_SET) != offset) {
|
2014-02-12 23:04:25 +01:00
|
|
|
/* LCOV_EXCL_START */
|
2015-02-02 23:39:46 +01:00
|
|
|
out("Error seeking file '%s' at offset %" PRIu64 ". %s.\n", handle->path, offset, strerror(errno));
|
2011-03-18 22:38:11 +01:00
|
|
|
return -1;
|
2014-02-12 23:04:25 +01:00
|
|
|
/* LCOV_EXCL_STOP */
|
2011-03-12 10:09:59 +01:00
|
|
|
}
|
2012-03-13 19:11:09 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
count = 0;
|
|
|
|
|
do {
|
2011-03-12 10:09:59 +01:00
|
|
|
|
2012-03-13 19:11:09 +01:00
|
|
|
#if HAVE_PREAD
|
|
|
|
|
read_ret = pread(handle->f, block_buffer + count, read_size - count, offset + count);
|
|
|
|
|
#else
|
|
|
|
|
read_ret = read(handle->f, block_buffer + count, read_size - count);
|
2011-03-12 10:09:59 +01:00
|
|
|
#endif
|
2012-03-13 19:11:09 +01:00
|
|
|
|
|
|
|
|
if (read_ret < 0) {
|
2014-02-12 23:04:25 +01:00
|
|
|
/* LCOV_EXCL_START */
|
2015-02-02 23:39:46 +01:00
|
|
|
out("Error reading file '%s' at offset %" PRIu64 " for size %u. %s.\n", handle->path, offset + count, read_size - count, strerror(errno));
|
2012-03-13 19:11:09 +01:00
|
|
|
return -1;
|
2014-02-12 23:04:25 +01:00
|
|
|
/* LCOV_EXCL_STOP */
|
2012-03-13 19:11:09 +01:00
|
|
|
}
|
|
|
|
|
if (read_ret == 0) {
|
2015-02-02 23:39:46 +01:00
|
|
|
out("Unexpected end of file '%s' at offset %" PRIu64 ". %s.\n", handle->path, offset, strerror(errno));
|
2012-03-13 19:11:09 +01:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
count += read_ret;
|
|
|
|
|
} while (count < read_size);
|
2011-03-04 20:59:21 +01:00
|
|
|
|
2011-04-23 21:38:26 +02:00
|
|
|
/* pad with 0 */
|
|
|
|
|
if (read_size < block_size) {
|
|
|
|
|
memset(block_buffer + read_size, 0, block_size - read_size);
|
|
|
|
|
}
|
|
|
|
|
|
2011-09-03 11:01:12 +02:00
|
|
|
/* Here isn't needed to call posix_fadvise(..., POSIX_FADV_DONTNEED) */
|
|
|
|
|
/* because we already advised sequential access with POSIX_FADV_SEQUENTIAL. */
|
2011-03-29 18:27:25 +02:00
|
|
|
/* In Linux 2.6.33 it's enough to ensure that data is not kept in the cache. */
|
|
|
|
|
/* Better to do nothing and save a syscall for each block. */
|
|
|
|
|
|
2011-10-31 09:41:12 +01:00
|
|
|
/* Here we cannot call posix_fadvise(..., POSIX_FADV_WILLNEED) for the next block */
|
|
|
|
|
/* because it may be blocking */
|
|
|
|
|
/* See Ted Ts'o comment in "posix_fadvise(POSIX_FADV_WILLNEED) waits before returning?" */
|
|
|
|
|
/* at: https://lkml.org/lkml/2010/12/6/122 */
|
|
|
|
|
/* We relay only on the automatic readahead triggered by POSIX_FADV_SEQUENTIAL. */
|
|
|
|
|
|
2011-03-04 20:59:21 +01:00
|
|
|
return read_size;
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-18 22:38:11 +01:00
|
|
|
int handle_write(struct snapraid_handle* handle, struct snapraid_block* block, unsigned char* block_buffer, unsigned block_size)
|
2011-03-04 20:59:21 +01:00
|
|
|
{
|
|
|
|
|
ssize_t write_ret;
|
2011-03-12 10:09:59 +01:00
|
|
|
data_off_t offset;
|
2011-03-04 20:59:21 +01:00
|
|
|
unsigned write_size;
|
|
|
|
|
|
2011-03-12 10:09:59 +01:00
|
|
|
offset = block_file_pos(block) * (data_off_t)block_size;
|
2011-03-04 20:59:21 +01:00
|
|
|
|
|
|
|
|
write_size = block_file_size(block, block_size);
|
|
|
|
|
|
2011-03-12 10:09:59 +01:00
|
|
|
#if HAVE_PWRITE
|
2011-03-04 20:59:21 +01:00
|
|
|
write_ret = pwrite(handle->f, block_buffer, write_size, offset);
|
2011-03-12 10:09:59 +01:00
|
|
|
#else
|
|
|
|
|
if (lseek(handle->f, offset, SEEK_SET) != offset) {
|
2014-02-12 23:04:25 +01:00
|
|
|
/* LCOV_EXCL_START */
|
2015-02-22 14:31:03 +01:00
|
|
|
msg_error("Error seeking file '%s'. %s.\n", handle->path, strerror(errno));
|
2011-03-18 22:38:11 +01:00
|
|
|
return -1;
|
2014-02-12 23:04:25 +01:00
|
|
|
/* LCOV_EXCL_STOP */
|
2011-03-12 10:09:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
write_ret = write(handle->f, block_buffer, write_size);
|
2011-03-22 23:20:12 +01:00
|
|
|
#endif
|
2011-03-30 17:13:42 +02:00
|
|
|
if (write_ret != (ssize_t)write_size) { /* conversion is safe because block_size is always small */
|
2014-02-12 23:04:25 +01:00
|
|
|
/* LCOV_EXCL_START */
|
2015-02-22 14:31:03 +01:00
|
|
|
msg_error("Error writing file '%s'. %s.\n", handle->path, strerror(errno));
|
2011-03-18 22:38:11 +01:00
|
|
|
return -1;
|
2014-02-12 23:04:25 +01:00
|
|
|
/* LCOV_EXCL_STOP */
|
2011-03-04 20:59:21 +01:00
|
|
|
}
|
2011-03-18 22:38:11 +01:00
|
|
|
|
2011-10-09 07:51:25 +02:00
|
|
|
/* adjust the size of the valid data */
|
|
|
|
|
if (handle->valid_size < offset + write_size) {
|
|
|
|
|
handle->valid_size = offset + write_size;
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-29 18:27:25 +02:00
|
|
|
/* Here doesn't make sense to call posix_fadvise(..., POSIX_FADV_DONTNEED) because */
|
2012-07-10 23:24:31 +02:00
|
|
|
/* at this time the data is still not yet written and it cannot be discharged. */
|
2011-03-29 18:27:25 +02:00
|
|
|
|
2011-03-18 22:38:11 +01:00
|
|
|
return 0;
|
2011-03-04 20:59:21 +01:00
|
|
|
}
|
|
|
|
|
|
2014-05-18 15:11:18 +02:00
|
|
|
int handle_utime(struct snapraid_handle* handle)
|
2011-08-31 23:33:43 +02:00
|
|
|
{
|
|
|
|
|
int ret;
|
|
|
|
|
|
2014-05-18 15:11:18 +02:00
|
|
|
/* do nothing if not opened */
|
|
|
|
|
if (handle->f == -1)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
ret = fmtime(handle->f, handle->file->mtime_sec, handle->file->mtime_nsec);
|
2012-09-19 21:52:15 +02:00
|
|
|
|
2011-08-31 23:33:43 +02:00
|
|
|
if (ret != 0) {
|
2014-02-12 23:04:25 +01:00
|
|
|
/* LCOV_EXCL_START */
|
2015-02-22 14:31:03 +01:00
|
|
|
msg_error("Error timing file '%s'. %s.\n", handle->file->sub, strerror(errno));
|
2011-08-31 23:33:43 +02:00
|
|
|
return -1;
|
2014-02-12 23:04:25 +01:00
|
|
|
/* LCOV_EXCL_STOP */
|
2011-08-31 23:33:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2011-03-23 19:22:56 +01:00
|
|
|
|
2011-09-23 21:48:50 +02:00
|
|
|
struct snapraid_handle* handle_map(struct snapraid_state* state, unsigned* handlemax)
|
|
|
|
|
{
|
|
|
|
|
tommy_node* i;
|
|
|
|
|
unsigned j;
|
2011-10-01 19:18:32 +02:00
|
|
|
unsigned size = 0;
|
2011-09-23 21:48:50 +02:00
|
|
|
struct snapraid_handle* handle;
|
|
|
|
|
|
2011-10-01 19:18:32 +02:00
|
|
|
/* get the size of the mapping */
|
|
|
|
|
size = 0;
|
2014-09-27 00:32:39 +02:00
|
|
|
for (i = state->maplist; i != 0; i = i->next) {
|
2011-10-01 19:18:32 +02:00
|
|
|
struct snapraid_map* map = i->data;
|
|
|
|
|
if (map->position > size)
|
|
|
|
|
size = map->position;
|
|
|
|
|
}
|
|
|
|
|
++size; /* size is one more than the max */
|
2011-09-23 21:48:50 +02:00
|
|
|
|
2011-10-01 19:18:32 +02:00
|
|
|
handle = malloc_nofail(size * sizeof(struct snapraid_handle));
|
2011-09-23 21:48:50 +02:00
|
|
|
|
2014-09-27 00:32:39 +02:00
|
|
|
for (j = 0; j < size; ++j) {
|
2011-10-01 19:18:32 +02:00
|
|
|
/* default for empty position */
|
|
|
|
|
handle[j].disk = 0;
|
2011-09-23 21:48:50 +02:00
|
|
|
handle[j].file = 0;
|
|
|
|
|
handle[j].f = -1;
|
2011-10-09 07:51:25 +02:00
|
|
|
handle[j].valid_size = 0;
|
2011-09-23 21:48:50 +02:00
|
|
|
}
|
|
|
|
|
|
2011-10-01 19:18:32 +02:00
|
|
|
/* set the vector */
|
2014-09-27 00:32:39 +02:00
|
|
|
for (i = state->disklist; i != 0; i = i->next) {
|
2011-10-01 19:18:32 +02:00
|
|
|
struct snapraid_map* map;
|
|
|
|
|
struct snapraid_disk* disk = i->data;
|
|
|
|
|
tommy_node* k;
|
|
|
|
|
|
|
|
|
|
/* search the mapping for this disk */
|
2014-09-27 00:32:39 +02:00
|
|
|
for (k = state->maplist; k != 0; k = k->next) {
|
2011-10-01 19:18:32 +02:00
|
|
|
map = k->data;
|
|
|
|
|
if (strcmp(disk->name, map->name) == 0)
|
|
|
|
|
break;
|
|
|
|
|
}
|
2014-09-27 00:32:39 +02:00
|
|
|
if (k == 0) {
|
2014-02-03 22:40:51 +01:00
|
|
|
/* LCOV_EXCL_START */
|
2015-02-22 14:31:03 +01:00
|
|
|
msg_error("Internal error for inconsistent disk mapping.\n");
|
2011-10-01 19:18:32 +02:00
|
|
|
exit(EXIT_FAILURE);
|
2014-02-03 22:40:51 +01:00
|
|
|
/* LCOV_EXCL_STOP */
|
2011-10-01 19:18:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
handle[map->position].disk = disk;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
*handlemax = size;
|
2011-09-23 21:48:50 +02:00
|
|
|
return handle;
|
|
|
|
|
}
|
2011-10-01 19:18:32 +02:00
|
|
|
|