2011-02-22 12:05:20 +00:00
|
|
|
/*
|
|
|
|
* iohelper.c: Helper program to perform I/O operations on files
|
|
|
|
*
|
|
|
|
* Copyright (C) 2011 Red Hat, Inc.
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library 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
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*
|
|
|
|
* Author: Daniel P. Berrange <berrange@redhat.com>
|
|
|
|
*
|
|
|
|
* Current support
|
|
|
|
* - Read existing file
|
|
|
|
* - Write existing file
|
|
|
|
* - Create & write new file
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include <locale.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "util.h"
|
|
|
|
#include "threads.h"
|
2011-07-19 18:32:58 +00:00
|
|
|
#include "virfile.h"
|
2011-02-22 12:05:20 +00:00
|
|
|
#include "memory.h"
|
|
|
|
#include "virterror_internal.h"
|
|
|
|
#include "configmake.h"
|
|
|
|
|
|
|
|
#define VIR_FROM_THIS VIR_FROM_STORAGE
|
|
|
|
|
2011-07-12 15:17:52 +00:00
|
|
|
static int
|
|
|
|
prepare(const char *path, int oflags, int mode,
|
|
|
|
unsigned long long offset)
|
2011-02-22 12:05:20 +00:00
|
|
|
{
|
2011-07-12 15:17:52 +00:00
|
|
|
int fd = -1;
|
2011-02-22 12:05:20 +00:00
|
|
|
|
2011-07-07 17:57:43 +00:00
|
|
|
if (oflags & O_CREAT) {
|
|
|
|
fd = open(path, oflags, mode);
|
2011-02-22 12:05:20 +00:00
|
|
|
} else {
|
2011-07-07 17:57:43 +00:00
|
|
|
fd = open(path, oflags);
|
2011-02-22 12:05:20 +00:00
|
|
|
}
|
|
|
|
if (fd < 0) {
|
|
|
|
virReportSystemError(errno, _("Unable to open %s"), path);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (offset) {
|
|
|
|
if (lseek(fd, offset, SEEK_SET) < 0) {
|
|
|
|
virReportSystemError(errno, _("Unable to seek %s to %llu"),
|
|
|
|
path, offset);
|
2011-07-12 15:17:52 +00:00
|
|
|
VIR_FORCE_CLOSE(fd);
|
2011-02-22 12:05:20 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-12 15:17:52 +00:00
|
|
|
cleanup:
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
runIO(const char *path, int fd, int oflags, unsigned long long length)
|
|
|
|
{
|
2011-07-12 14:35:05 +00:00
|
|
|
void *base = NULL; /* Location to be freed */
|
|
|
|
char *buf = NULL; /* Aligned location within base */
|
2011-07-12 15:17:52 +00:00
|
|
|
size_t buflen = 1024*1024;
|
2011-07-12 14:35:05 +00:00
|
|
|
intptr_t alignMask = 64*1024 - 1;
|
2011-07-12 15:17:52 +00:00
|
|
|
int ret = -1;
|
|
|
|
int fdin, fdout;
|
|
|
|
const char *fdinname, *fdoutname;
|
|
|
|
unsigned long long total = 0;
|
2011-07-12 14:35:05 +00:00
|
|
|
bool direct = O_DIRECT && ((oflags & O_DIRECT) != 0);
|
|
|
|
bool shortRead = false; /* true if we hit a short read */
|
|
|
|
off_t end = 0;
|
2011-07-12 15:17:52 +00:00
|
|
|
|
2011-07-12 14:35:05 +00:00
|
|
|
#if HAVE_POSIX_MEMALIGN
|
|
|
|
if (posix_memalign(&base, alignMask + 1, buflen)) {
|
2011-02-22 12:05:20 +00:00
|
|
|
virReportOOMError();
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2011-07-12 14:35:05 +00:00
|
|
|
buf = base;
|
|
|
|
#else
|
|
|
|
if (VIR_ALLOC_N(buf, buflen + alignMask) < 0) {
|
|
|
|
virReportOOMError();
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
base = buf;
|
|
|
|
buf = (char *) (((intptr_t) base + alignMask) & alignMask);
|
|
|
|
#endif
|
2011-02-22 12:05:20 +00:00
|
|
|
|
2011-07-07 17:57:43 +00:00
|
|
|
switch (oflags & O_ACCMODE) {
|
2011-02-22 12:05:20 +00:00
|
|
|
case O_RDONLY:
|
|
|
|
fdin = fd;
|
|
|
|
fdinname = path;
|
|
|
|
fdout = STDOUT_FILENO;
|
|
|
|
fdoutname = "stdout";
|
2011-07-12 14:35:05 +00:00
|
|
|
/* To make the implementation simpler, we give up on any
|
|
|
|
* attempt to use O_DIRECT in a non-trivial manner. */
|
|
|
|
if (direct && ((end = lseek(fd, 0, SEEK_CUR)) != 0 || length)) {
|
|
|
|
virReportSystemError(end < 0 ? errno : EINVAL, "%s",
|
|
|
|
_("O_DIRECT read needs entire seekable file"));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2011-02-22 12:05:20 +00:00
|
|
|
break;
|
|
|
|
case O_WRONLY:
|
|
|
|
fdin = STDIN_FILENO;
|
|
|
|
fdinname = "stdin";
|
|
|
|
fdout = fd;
|
|
|
|
fdoutname = path;
|
2011-07-12 14:35:05 +00:00
|
|
|
/* To make the implementation simpler, we give up on any
|
|
|
|
* attempt to use O_DIRECT in a non-trivial manner. */
|
|
|
|
if (direct && (end = lseek(fd, 0, SEEK_END)) != 0) {
|
|
|
|
virReportSystemError(end < 0 ? errno : EINVAL, "%s",
|
|
|
|
_("O_DIRECT write needs empty seekable file"));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2011-02-22 12:05:20 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case O_RDWR:
|
|
|
|
default:
|
|
|
|
virReportSystemError(EINVAL,
|
|
|
|
_("Unable to process file with flags %d"),
|
2011-07-07 17:57:43 +00:00
|
|
|
(oflags & O_ACCMODE));
|
2011-02-22 12:05:20 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
ssize_t got;
|
|
|
|
|
|
|
|
if (length &&
|
|
|
|
(length - total) < buflen)
|
|
|
|
buflen = length - total;
|
|
|
|
|
|
|
|
if (buflen == 0)
|
|
|
|
break; /* End of requested data from client */
|
|
|
|
|
|
|
|
if ((got = saferead(fdin, buf, buflen)) < 0) {
|
|
|
|
virReportSystemError(errno, _("Unable to read %s"), fdinname);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
if (got == 0)
|
|
|
|
break; /* End of file before end of requested data */
|
2011-07-12 14:35:05 +00:00
|
|
|
if (got < buflen || (buflen & alignMask)) {
|
|
|
|
/* O_DIRECT can handle at most one short read, at end of file */
|
|
|
|
if (direct && shortRead) {
|
|
|
|
virReportSystemError(EINVAL, "%s",
|
|
|
|
_("Too many short reads for O_DIRECT"));
|
|
|
|
}
|
|
|
|
shortRead = true;
|
|
|
|
}
|
2011-02-22 12:05:20 +00:00
|
|
|
|
|
|
|
total += got;
|
2011-07-12 14:35:05 +00:00
|
|
|
if (fdout == fd && direct && shortRead) {
|
|
|
|
end = total;
|
|
|
|
memset(buf + got, 0, buflen - got);
|
|
|
|
got = (got + alignMask) & ~alignMask;
|
|
|
|
}
|
2011-02-22 12:05:20 +00:00
|
|
|
if (safewrite(fdout, buf, got) < 0) {
|
|
|
|
virReportSystemError(errno, _("Unable to write %s"), fdoutname);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2011-07-12 14:35:05 +00:00
|
|
|
if (end && ftruncate(fd, end) < 0) {
|
|
|
|
virReportSystemError(errno, _("Unable to truncate %s"), fdoutname);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2011-02-22 12:05:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
if (VIR_CLOSE(fd) < 0 &&
|
|
|
|
ret == 0) {
|
|
|
|
virReportSystemError(errno, _("Unable to close %s"), path);
|
|
|
|
ret = -1;
|
|
|
|
}
|
|
|
|
|
2011-07-12 14:35:05 +00:00
|
|
|
VIR_FREE(base);
|
2011-02-22 12:05:20 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-07-12 15:17:52 +00:00
|
|
|
static const char *program_name;
|
|
|
|
|
|
|
|
ATTRIBUTE_NORETURN static void
|
|
|
|
usage(int status)
|
|
|
|
{
|
|
|
|
if (status) {
|
|
|
|
fprintf(stderr, _("%s: try --help for more details"), program_name);
|
|
|
|
} else {
|
|
|
|
printf(_("Usage: %s FILENAME OFLAGS MODE OFFSET LENGTH DELETE\n"
|
|
|
|
" or: %s FILENAME LENGTH FD\n"),
|
|
|
|
program_name, program_name);
|
|
|
|
}
|
|
|
|
exit(status);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
2011-02-22 12:05:20 +00:00
|
|
|
{
|
|
|
|
const char *path;
|
|
|
|
virErrorPtr err;
|
|
|
|
unsigned long long offset;
|
|
|
|
unsigned long long length;
|
2011-07-12 15:17:52 +00:00
|
|
|
int oflags = -1;
|
2011-02-22 12:05:20 +00:00
|
|
|
int mode;
|
2011-07-12 15:17:52 +00:00
|
|
|
unsigned int delete = 0;
|
|
|
|
int fd = -1;
|
|
|
|
int lengthIndex = 0;
|
|
|
|
|
|
|
|
program_name = argv[0];
|
2011-02-22 12:05:20 +00:00
|
|
|
|
|
|
|
if (setlocale(LC_ALL, "") == NULL ||
|
|
|
|
bindtextdomain(PACKAGE, LOCALEDIR) == NULL ||
|
|
|
|
textdomain(PACKAGE) == NULL) {
|
2011-07-12 15:17:52 +00:00
|
|
|
fprintf(stderr, _("%s: initialization failed\n"), program_name);
|
2011-02-22 12:05:20 +00:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (virThreadInitialize() < 0 ||
|
|
|
|
virErrorInitialize() < 0 ||
|
|
|
|
virRandomInitialize(time(NULL) ^ getpid())) {
|
2011-07-12 15:17:52 +00:00
|
|
|
fprintf(stderr, _("%s: initialization failed\n"), program_name);
|
2011-02-22 12:05:20 +00:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
path = argv[1];
|
|
|
|
|
2011-07-12 15:17:52 +00:00
|
|
|
if (argc > 1 && STREQ(argv[1], "--help"))
|
|
|
|
usage(EXIT_SUCCESS);
|
|
|
|
if (argc == 7) { /* FILENAME OFLAGS MODE OFFSET LENGTH DELETE */
|
|
|
|
lengthIndex = 5;
|
|
|
|
if (virStrToLong_i(argv[2], NULL, 10, &oflags) < 0) {
|
|
|
|
fprintf(stderr, _("%s: malformed file flags %s"),
|
|
|
|
program_name, argv[2]);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
if (virStrToLong_i(argv[3], NULL, 10, &mode) < 0) {
|
|
|
|
fprintf(stderr, _("%s: malformed file mode %s"),
|
|
|
|
program_name, argv[3]);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
if (virStrToLong_ull(argv[4], NULL, 10, &offset) < 0) {
|
|
|
|
fprintf(stderr, _("%s: malformed file offset %s"),
|
|
|
|
program_name, argv[4]);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
if (argc == 7 && virStrToLong_ui(argv[6], NULL, 10, &delete) < 0) {
|
|
|
|
fprintf(stderr, _("%s: malformed delete flag %s"),
|
|
|
|
program_name, argv[6]);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
fd = prepare(path, oflags, mode, offset);
|
|
|
|
} else if (argc == 4) { /* FILENAME LENGTH FD */
|
|
|
|
lengthIndex = 2;
|
|
|
|
if (virStrToLong_i(argv[3], NULL, 10, &fd) < 0) {
|
|
|
|
fprintf(stderr, _("%s: malformed fd %s"),
|
|
|
|
program_name, argv[3]);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
#ifdef F_GETFL
|
|
|
|
oflags = fcntl(fd, F_GETFL);
|
|
|
|
#else
|
|
|
|
/* Stupid mingw. */
|
|
|
|
if (fd == STDIN_FILENO)
|
|
|
|
oflags = O_RDONLY;
|
|
|
|
else if (fd == STDOUT_FILENO)
|
|
|
|
oflags = O_WRONLY;
|
|
|
|
#endif
|
|
|
|
if (oflags < 0) {
|
|
|
|
fprintf(stderr, _("%s: unable to determine access mode of fd %d"),
|
|
|
|
program_name, fd);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
} else { /* unknown argc pattern */
|
|
|
|
usage(EXIT_FAILURE);
|
2011-02-22 12:05:20 +00:00
|
|
|
}
|
|
|
|
|
2011-07-12 15:17:52 +00:00
|
|
|
if (virStrToLong_ull(argv[lengthIndex], NULL, 10, &length) < 0) {
|
|
|
|
fprintf(stderr, _("%s: malformed file length %s"),
|
|
|
|
program_name, argv[lengthIndex]);
|
2011-04-05 10:27:35 +00:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2011-02-22 12:05:20 +00:00
|
|
|
|
2011-07-12 15:17:52 +00:00
|
|
|
if (fd < 0 || runIO(path, fd, oflags, length) < 0)
|
2011-02-22 12:05:20 +00:00
|
|
|
goto error;
|
|
|
|
|
2011-04-05 10:27:35 +00:00
|
|
|
if (delete)
|
|
|
|
unlink(path);
|
|
|
|
|
2011-02-22 12:05:20 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
error:
|
|
|
|
err = virGetLastError();
|
|
|
|
if (err) {
|
2011-07-12 15:17:52 +00:00
|
|
|
fprintf(stderr, "%s: %s\n", program_name, err->message);
|
2011-02-22 12:05:20 +00:00
|
|
|
} else {
|
2011-07-12 15:17:52 +00:00
|
|
|
fprintf(stderr, _("%s: unknown failure with %s\n"),
|
|
|
|
program_name, path);
|
2011-02-22 12:05:20 +00:00
|
|
|
}
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|