| 1 |
/*
|
| 2 |
* sbio.c
|
| 3 |
*
|
| 4 |
* IO functions.
|
| 5 |
*
|
| 6 |
* Copyright 1999-2006 Gentoo Foundation
|
| 7 |
*
|
| 8 |
*
|
| 9 |
* This program is free software; you can redistribute it and/or modify it
|
| 10 |
* under the terms of the GNU General Public License as published by the
|
| 11 |
* Free Software Foundation version 2 of the License.
|
| 12 |
*
|
| 13 |
* This program is distributed in the hope that it will be useful, but
|
| 14 |
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
| 16 |
* General Public License for more details.
|
| 17 |
*
|
| 18 |
* You should have received a copy of the GNU General Public License along
|
| 19 |
* with this program; if not, write to the Free Software Foundation, Inc.,
|
| 20 |
* 675 Mass Ave, Cambridge, MA 02139, USA.
|
| 21 |
*
|
| 22 |
* $Header$
|
| 23 |
*/
|
| 24 |
|
| 25 |
|
| 26 |
#include <errno.h>
|
| 27 |
#include <stdio.h>
|
| 28 |
#include <sys/stat.h>
|
| 29 |
#include <unistd.h>
|
| 30 |
#include <fcntl.h>
|
| 31 |
|
| 32 |
#include "sbutil.h"
|
| 33 |
|
| 34 |
|
| 35 |
static int (*sbio_open)(const char *, int, mode_t) = (void *)open;
|
| 36 |
|
| 37 |
|
| 38 |
void sb_set_open(void *new_open)
|
| 39 |
{
|
| 40 |
if (!check_ptr(new_open))
|
| 41 |
return;
|
| 42 |
|
| 43 |
sbio_open = new_open;
|
| 44 |
}
|
| 45 |
|
| 46 |
/* General purpose function to _reliably_ open a file
|
| 47 |
*
|
| 48 |
* Returns the file descriptor or -1 on error (and errno set)
|
| 49 |
*/
|
| 50 |
|
| 51 |
int sb_open(const char *path, int flags, mode_t mode)
|
| 52 |
{
|
| 53 |
int fd;
|
| 54 |
|
| 55 |
fd = sbio_open(path, flags, mode);
|
| 56 |
if (-1 == fd)
|
| 57 |
DBG_MSG("Failed to open file '%s'!\n", path);
|
| 58 |
|
| 59 |
return fd;
|
| 60 |
}
|
| 61 |
|
| 62 |
/* General purpose function to _reliably_ read from a file.
|
| 63 |
*
|
| 64 |
* Returns total read bytes or -1 on error.
|
| 65 |
*/
|
| 66 |
|
| 67 |
size_t sb_read(int fd, void *buf, size_t count)
|
| 68 |
{
|
| 69 |
ssize_t n;
|
| 70 |
size_t accum = 0;
|
| 71 |
|
| 72 |
do {
|
| 73 |
n = read(fd, buf + accum, count - accum);
|
| 74 |
|
| 75 |
if (n > 0) {
|
| 76 |
accum += n;
|
| 77 |
continue;
|
| 78 |
}
|
| 79 |
|
| 80 |
if (n < 0) {
|
| 81 |
if (EINTR == errno) {
|
| 82 |
/* Reset errno to not trigger DBG_MSG */
|
| 83 |
errno = 0;
|
| 84 |
continue;
|
| 85 |
}
|
| 86 |
|
| 87 |
DBG_MSG("Failed to read from fd=%i!\n", fd);
|
| 88 |
return -1;
|
| 89 |
}
|
| 90 |
|
| 91 |
/* Found EOF */
|
| 92 |
break;
|
| 93 |
} while (accum < count);
|
| 94 |
|
| 95 |
return accum;
|
| 96 |
}
|
| 97 |
|
| 98 |
/* General purpose function to _reliably_ write to a file
|
| 99 |
*
|
| 100 |
* If returned value is less than count, there was a fatal
|
| 101 |
* error and value tells how many bytes were actually written
|
| 102 |
*/
|
| 103 |
|
| 104 |
size_t sb_write(int fd, const void *buf, size_t count)
|
| 105 |
{
|
| 106 |
ssize_t n;
|
| 107 |
size_t accum = 0;
|
| 108 |
|
| 109 |
do {
|
| 110 |
n = write(fd, buf + accum, count - accum);
|
| 111 |
if (n < 0) {
|
| 112 |
if (EINTR == errno) {
|
| 113 |
/* Reset errno to not trigger DBG_MSG */
|
| 114 |
errno = 0;
|
| 115 |
continue;
|
| 116 |
}
|
| 117 |
|
| 118 |
DBG_MSG("Failed to write to fd=%i!\n", fd);
|
| 119 |
break;
|
| 120 |
}
|
| 121 |
|
| 122 |
accum += n;
|
| 123 |
} while (accum < count);
|
| 124 |
|
| 125 |
return accum;
|
| 126 |
}
|
| 127 |
|
| 128 |
/* General purpose function to _reliably_ close a file
|
| 129 |
*
|
| 130 |
* Returns 0 if successful or negative number on error (and errno set)
|
| 131 |
*/
|
| 132 |
|
| 133 |
int sb_close(int fd)
|
| 134 |
{
|
| 135 |
int res;
|
| 136 |
|
| 137 |
do {
|
| 138 |
res = close(fd);
|
| 139 |
} while ((res < 0) && (EINTR == errno));
|
| 140 |
|
| 141 |
/* Do not care about errors here */
|
| 142 |
errno = 0;
|
| 143 |
|
| 144 |
return res;
|
| 145 |
}
|
| 146 |
|