| 1 |
/****************************************************************************** |
| 2 |
* |
| 3 |
* Common file operations. |
| 4 |
* |
| 5 |
* Copyright (c) 2009 by Citrix Systems, Inc. (Patrick Colp) |
| 6 |
* |
| 7 |
* This program is free software; you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU General Public License as published by |
| 9 |
* the Free Software Foundation; either version 2 of the License, or |
| 10 |
* (at your option) any later version. |
| 11 |
* |
| 12 |
* This program is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
* GNU General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU General Public License |
| 18 |
* along with this program; If not, see <http://www.gnu.org/licenses/>. |
| 19 |
*/ |
| 20 |
|
| 21 |
|
| 22 |
#include <unistd.h> |
| 23 |
#include <xenctrl.h> |
| 24 |
|
| 25 |
static int file_op(int fd, void *page, int i, |
| 26 |
ssize_t (*fn)(int, void *, size_t)) |
| 27 |
{ |
| 28 |
off_t offset = i; |
| 29 |
int total = 0; |
| 30 |
int bytes; |
| 31 |
|
| 32 |
offset = lseek(fd, offset << XC_PAGE_SHIFT, SEEK_SET); |
| 33 |
if ( offset == (off_t)-1 ) |
| 34 |
return -1; |
| 35 |
|
| 36 |
while ( total < XC_PAGE_SIZE ) |
| 37 |
{ |
| 38 |
bytes = fn(fd, page + total, XC_PAGE_SIZE - total); |
| 39 |
if ( bytes <= 0 ) |
| 40 |
return -1; |
| 41 |
|
| 42 |
total += bytes; |
| 43 |
} |
| 44 |
|
| 45 |
return 0; |
| 46 |
} |
| 47 |
|
| 48 |
static ssize_t my_write(int fd, void *buf, size_t count) |
| 49 |
{ |
| 50 |
return write(fd, buf, count); |
| 51 |
} |
| 52 |
|
| 53 |
int read_page(int fd, void *page, int i) |
| 54 |
{ |
| 55 |
return file_op(fd, page, i, &read); |
| 56 |
} |
| 57 |
|
| 58 |
int write_page(int fd, void *page, int i) |
| 59 |
{ |
| 60 |
return file_op(fd, page, i, &my_write); |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
/* |
| 65 |
* Local variables: |
| 66 |
* mode: C |
| 67 |
* c-file-style: "BSD" |
| 68 |
* c-basic-offset: 4 |
| 69 |
* indent-tabs-mode: nil |
| 70 |
* End: |
| 71 |
*/ |