1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
|
#define _LARGEFILE64_SOURCE
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "io.h"
#include "error.h"
#include "gen.h"
ssize_t READ(int fd, char *whereto, size_t len)
{
ssize_t cnt=0;
while(len>0)
{
ssize_t rc;
rc = read(fd, whereto, len);
if (rc == -1)
{
if (errno != EINTR)
{
if (errno != EIO)
error_exit("unexpected error while reading: %s (%d)\n", strerror(errno), errno);
return -1;
}
}
else if (rc == 0)
{
break;
}
else
{
whereto += rc;
len -= rc;
cnt += rc;
}
}
return cnt;
}
ssize_t WRITE(int fd, char *whereto, size_t len)
{
ssize_t cnt=0;
while(len>0)
{
ssize_t rc;
rc = write(fd, whereto, len);
if (rc == -1)
{
if (errno != EINTR)
{
fprintf(stderr, "WRITE::write: %d\n", errno);
return -1;
}
}
else if (rc == 0)
{
break;
}
else
{
whereto += rc;
len -= rc;
cnt += rc;
}
}
return cnt;
}
ssize_t phantom_write(int fd, char *in, size_t nbytes)
{
ssize_t cnt = 0;
size_t move_a_little;
off64_t curpos;
int dummy_rc;
/* cannot create phantom blocks if in O_APPEND mode */
dummy_rc = fcntl(fd, F_GETFL);
if (dummy_rc == -1)
{
fprintf(stderr, "phantom_write::fnctl: %d\n", errno);
return -1; /* error */
}
if ((dummy_rc & O_APPEND) == O_APPEND)
return WRITE(fd, in, nbytes); /* in append you cannot create phantom blocks */
curpos = lseek64(fd, (off64_t)0, SEEK_CUR);
/* could not determine current location, bail out */
if (curpos == -1)
{
fprintf(stderr, "phantom_write::lseek error: %d\n", errno);
return -1;
}
/* determine how many bytes to seek to get at the start of
* a sector
*/
move_a_little = 512 - (curpos & 511);
if (move_a_little == 512)
move_a_little = 0;
else
move_a_little = min(nbytes, move_a_little);
while(nbytes > 0)
{
int rc;
/* first, move to the start of sector. otherwhise a lseek()
* will not have the effect of creating phantom blocks
*/
if (move_a_little > 0)
{
if ((rc = WRITE(fd, in, move_a_little)) <= 0)
{
fprintf(stderr, "phantom_write::WRITE: error\n");
return rc;
}
move_a_little -= rc;
}
/* if more then 512 bytes to write, try to create phantom
* blocks
*/
else if (nbytes >= 512)
{
int loop = 0;
char nulls = 1;
for(loop=0; loop<512; loop++)
{
if (in[loop])
{
nulls = 0;
break;
}
}
if (nulls)
{
if (lseek64(fd, (off64_t)512, SEEK_CUR) == -1)
{
fprintf(stderr, "phantom_write::lseek: %d\n", errno);
return -1;
}
rc = 512;
}
else
{
if ((rc = WRITE(fd, in, 512)) <= 0)
{
fprintf(stderr, "phantom_write::WRITE: error: %d\n", errno);
return rc;
}
}
}
else
{
if ((rc = WRITE(fd, in, nbytes)) <= 0)
{
fprintf(stderr, "phantom_write::WRITE: error: %d\n", errno);
return rc;
}
}
in += rc;
cnt += rc;
nbytes -= rc;
}
return cnt;
}
|