clang-format
This commit is contained in:
@@ -63,208 +63,184 @@
|
||||
#define TESTFILE "bigseekfile"
|
||||
|
||||
static const char *slogans[] = {
|
||||
"QUO USQUE TANDEM ABUTERE CATILINA PATENTIA NOSTRA",
|
||||
"QUEM IN FINEM SESE EFFRENATA IACTABIT AUDACIA"
|
||||
};
|
||||
"QUO USQUE TANDEM ABUTERE CATILINA PATENTIA NOSTRA",
|
||||
"QUEM IN FINEM SESE EFFRENATA IACTABIT AUDACIA"};
|
||||
|
||||
static
|
||||
void
|
||||
write_slogan(int fd, unsigned which, bool failok)
|
||||
{
|
||||
size_t len;
|
||||
ssize_t r;
|
||||
static void write_slogan(int fd, unsigned which, bool failok) {
|
||||
size_t len;
|
||||
ssize_t r;
|
||||
|
||||
len = strlen(slogans[which]);
|
||||
r = write(fd, slogans[which], len);
|
||||
if (r < 0) {
|
||||
if (failok && errno == EFBIG) {
|
||||
return;
|
||||
}
|
||||
err(1, "write");
|
||||
}
|
||||
if (failok) {
|
||||
errx(1, "write: expected failure but wrote %zd bytes", r);
|
||||
}
|
||||
if ((size_t)r != len) {
|
||||
errx(1, "write: result %zd bytes, expected %zu", r, len);
|
||||
}
|
||||
len = strlen(slogans[which]);
|
||||
r = write(fd, slogans[which], len);
|
||||
if (r < 0) {
|
||||
if (failok && errno == EFBIG) {
|
||||
return;
|
||||
}
|
||||
err(1, "write");
|
||||
}
|
||||
if (failok) {
|
||||
errx(1, "write: expected failure but wrote %zd bytes", r);
|
||||
}
|
||||
if ((size_t)r != len) {
|
||||
errx(1, "write: result %zd bytes, expected %zu", r, len);
|
||||
}
|
||||
}
|
||||
|
||||
static
|
||||
void
|
||||
check_slogan(int fd, unsigned which)
|
||||
{
|
||||
char buf[256];
|
||||
size_t len;
|
||||
ssize_t r;
|
||||
unsigned i, wrongcount;
|
||||
static void check_slogan(int fd, unsigned which) {
|
||||
char buf[256];
|
||||
size_t len;
|
||||
ssize_t r;
|
||||
unsigned i, wrongcount;
|
||||
|
||||
r = read(fd, buf, sizeof(buf));
|
||||
if (r < 0) {
|
||||
err(1, "read");
|
||||
}
|
||||
if (r == 0) {
|
||||
errx(1, "read: Unexpected EOF");
|
||||
}
|
||||
r = read(fd, buf, sizeof(buf));
|
||||
if (r < 0) {
|
||||
err(1, "read");
|
||||
}
|
||||
if (r == 0) {
|
||||
errx(1, "read: Unexpected EOF");
|
||||
}
|
||||
|
||||
/* we should get either a full buffer or the length of the slogan */
|
||||
len = strlen(slogans[which]);
|
||||
if ((size_t)r != sizeof(buf) && (size_t)r != len) {
|
||||
errx(1, "read: result %zd bytes, expected %zu or %zu",
|
||||
r, sizeof(buf), len);
|
||||
}
|
||||
/* we should get either a full buffer or the length of the slogan */
|
||||
len = strlen(slogans[which]);
|
||||
if ((size_t)r != sizeof(buf) && (size_t)r != len) {
|
||||
errx(1, "read: result %zd bytes, expected %zu or %zu", r, sizeof(buf), len);
|
||||
}
|
||||
|
||||
/* slogan should match */
|
||||
if (memcmp(buf, slogans[which], len) != 0) {
|
||||
warnx("read: got wrong data");
|
||||
warnx("expected: %s", slogans[which]);
|
||||
buf[sizeof(buf) - 1] = 0;
|
||||
errx(1, "found: %s", buf);
|
||||
}
|
||||
/* slogan should match */
|
||||
if (memcmp(buf, slogans[which], len) != 0) {
|
||||
warnx("read: got wrong data");
|
||||
warnx("expected: %s", slogans[which]);
|
||||
buf[sizeof(buf) - 1] = 0;
|
||||
errx(1, "found: %s", buf);
|
||||
}
|
||||
|
||||
/* bytes past the slogan (if any) should be 0 */
|
||||
wrongcount = 0;
|
||||
for (i=len; i<(size_t)r; i++) {
|
||||
if (buf[i] != 0) {
|
||||
warnx("read: buf[%zu] was 0x%x, expected 0", i,
|
||||
(unsigned char)buf[i]);
|
||||
wrongcount++;
|
||||
}
|
||||
}
|
||||
if (wrongcount > 0) {
|
||||
errx(1, "%u bytes of trash in file", wrongcount);
|
||||
}
|
||||
/* bytes past the slogan (if any) should be 0 */
|
||||
wrongcount = 0;
|
||||
for (i = len; i < (size_t)r; i++) {
|
||||
if (buf[i] != 0) {
|
||||
warnx("read: buf[%zu] was 0x%x, expected 0", i, (unsigned char)buf[i]);
|
||||
wrongcount++;
|
||||
}
|
||||
}
|
||||
if (wrongcount > 0) {
|
||||
errx(1, "%u bytes of trash in file", wrongcount);
|
||||
}
|
||||
}
|
||||
|
||||
static
|
||||
void
|
||||
try_reading(int fd)
|
||||
{
|
||||
char buf[16];
|
||||
ssize_t r;
|
||||
static void try_reading(int fd) {
|
||||
char buf[16];
|
||||
ssize_t r;
|
||||
|
||||
r = read(fd, buf, sizeof(buf));
|
||||
if (r == 0) {
|
||||
/* expected EOF */
|
||||
return;
|
||||
}
|
||||
if (r < 0) {
|
||||
err(1, "read");
|
||||
}
|
||||
errx(1, "read: Expected EOF but got %zd bytes", r);
|
||||
r = read(fd, buf, sizeof(buf));
|
||||
if (r == 0) {
|
||||
/* expected EOF */
|
||||
return;
|
||||
}
|
||||
if (r < 0) {
|
||||
err(1, "read");
|
||||
}
|
||||
errx(1, "read: Expected EOF but got %zd bytes", r);
|
||||
}
|
||||
|
||||
static
|
||||
void
|
||||
try_writing(int fd)
|
||||
{
|
||||
write_slogan(fd, 1, true);
|
||||
static void try_writing(int fd) { write_slogan(fd, 1, true); }
|
||||
|
||||
static void dolseek(int fd, off_t pos, int whence, const char *whencestr,
|
||||
off_t expected) {
|
||||
off_t result;
|
||||
|
||||
result = lseek(fd, pos, whence);
|
||||
if (result == -1) {
|
||||
err(1, "lseek(fd, 0x%llx, %s)", pos, whencestr);
|
||||
}
|
||||
if (result != expected) {
|
||||
errx(1,
|
||||
"lseek(fd, 0x%llx, %s): Wrong return value"
|
||||
" (got 0x%llx, expected 0x%llx)",
|
||||
pos, whencestr, result, expected);
|
||||
}
|
||||
}
|
||||
|
||||
static
|
||||
void
|
||||
dolseek(int fd, off_t pos, int whence, const char *whencestr, off_t expected)
|
||||
{
|
||||
off_t result;
|
||||
static void try_seeking(int fd, off_t pos, off_t cursize) {
|
||||
printf("Seeking to (and near) 0x%llx\n", pos);
|
||||
|
||||
result = lseek(fd, pos, whence);
|
||||
if (result == -1) {
|
||||
err(1, "lseek(fd, 0x%llx, %s)", pos, whencestr);
|
||||
}
|
||||
if (result != expected) {
|
||||
errx(1, "lseek(fd, 0x%llx, %s): Wrong return value"
|
||||
" (got 0x%llx, expected 0x%llx)", pos, whencestr,
|
||||
result, expected);
|
||||
}
|
||||
/* Go to the place. */
|
||||
dolseek(fd, pos, SEEK_SET, "SEEK_SET", pos);
|
||||
|
||||
/* Go to where we already are. */
|
||||
dolseek(fd, 0, SEEK_CUR, "SEEK_CUR", pos);
|
||||
|
||||
if (pos >= 10) {
|
||||
/* Back up a little. */
|
||||
dolseek(fd, -10, SEEK_CUR, "SEEK_CUR", pos - 10);
|
||||
|
||||
/* Forward a little. */
|
||||
dolseek(fd, 20, SEEK_CUR, "SEEK_CUR", pos + 10);
|
||||
} else {
|
||||
/* Just forward a little. */
|
||||
dolseek(fd, 10, SEEK_CUR, "SEEK_CUR", pos + 10);
|
||||
}
|
||||
|
||||
/* Via SEEK_END. */
|
||||
dolseek(fd, pos, SEEK_END, "SEEK_END", pos + cursize);
|
||||
|
||||
/* Go back to the exact place. */
|
||||
dolseek(fd, pos, SEEK_SET, "SEEK_SET", pos);
|
||||
}
|
||||
|
||||
static
|
||||
void
|
||||
try_seeking(int fd, off_t pos, off_t cursize)
|
||||
{
|
||||
printf("Seeking to (and near) 0x%llx\n", pos);
|
||||
int main(void) {
|
||||
off_t cursize;
|
||||
int fd;
|
||||
|
||||
/* Go to the place. */
|
||||
dolseek(fd, pos, SEEK_SET, "SEEK_SET", pos);
|
||||
printf("Creating file...\n");
|
||||
fd = open(TESTFILE, O_RDWR | O_CREAT | O_TRUNC, 0664);
|
||||
if (fd < 0) {
|
||||
err(1, "%s", TESTFILE);
|
||||
}
|
||||
|
||||
/* Go to where we already are. */
|
||||
dolseek(fd, 0, SEEK_CUR, "SEEK_CUR", pos);
|
||||
printf("Writing something at offset 0\n");
|
||||
write_slogan(fd, 0, false);
|
||||
cursize = strlen(slogans[0]);
|
||||
|
||||
if (pos >= 10) {
|
||||
/* Back up a little. */
|
||||
dolseek(fd, -10, SEEK_CUR, "SEEK_CUR", pos - 10);
|
||||
try_seeking(fd, (off_t)0x1000LL, cursize);
|
||||
|
||||
/* Forward a little. */
|
||||
dolseek(fd, 20, SEEK_CUR, "SEEK_CUR", pos + 10);
|
||||
}
|
||||
else {
|
||||
/* Just forward a little. */
|
||||
dolseek(fd, 10, SEEK_CUR, "SEEK_CUR", pos + 10);
|
||||
}
|
||||
printf("Writing something else\n");
|
||||
write_slogan(fd, 1, false);
|
||||
cursize = (off_t)0x1000LL + strlen(slogans[1]);
|
||||
|
||||
/* Via SEEK_END. */
|
||||
dolseek(fd, pos, SEEK_END, "SEEK_END", pos + cursize);
|
||||
try_seeking(fd, (off_t)0, cursize);
|
||||
|
||||
/* Go back to the exact place. */
|
||||
dolseek(fd, pos, SEEK_SET, "SEEK_SET", pos);
|
||||
}
|
||||
|
||||
int
|
||||
main(void)
|
||||
{
|
||||
off_t cursize;
|
||||
int fd;
|
||||
|
||||
printf("Creating file...\n");
|
||||
fd = open(TESTFILE, O_RDWR|O_CREAT|O_TRUNC, 0664);
|
||||
if (fd < 0) {
|
||||
err(1, "%s", TESTFILE);
|
||||
}
|
||||
|
||||
printf("Writing something at offset 0\n");
|
||||
write_slogan(fd, 0, false);
|
||||
cursize = strlen(slogans[0]);
|
||||
|
||||
try_seeking(fd, (off_t)0x1000LL, cursize);
|
||||
|
||||
printf("Writing something else\n");
|
||||
write_slogan(fd, 1, false);
|
||||
cursize = (off_t)0x1000LL + strlen(slogans[1]);
|
||||
|
||||
try_seeking(fd, (off_t)0, cursize);
|
||||
|
||||
/* If seek is totally bust, this will fail. */
|
||||
printf("Checking what we wrote\n");
|
||||
check_slogan(fd, 0);
|
||||
|
||||
try_seeking(fd, (off_t)0x1000LL, cursize);
|
||||
printf("Checking the other thing we wrote\n");
|
||||
check_slogan(fd, 1);
|
||||
|
||||
try_seeking(fd, (off_t)0x20LL, cursize);
|
||||
try_seeking(fd, (off_t)0x7fffffffLL, cursize);
|
||||
try_seeking(fd, (off_t)0x80000000LL, cursize);
|
||||
try_seeking(fd, (off_t)0x80000020LL, cursize);
|
||||
try_seeking(fd, (off_t)0x100000000LL, cursize);
|
||||
try_seeking(fd, (off_t)0x100000020LL, cursize);
|
||||
try_seeking(fd, (off_t)0x180000000LL, cursize);
|
||||
try_seeking(fd, (off_t)0x180000020LL, cursize);
|
||||
|
||||
printf("Now trying to read (should get EOF)\n");
|
||||
try_reading(fd);
|
||||
|
||||
printf("Now trying to write (should get EFBIG)\n");
|
||||
try_writing(fd);
|
||||
|
||||
try_seeking(fd, (off_t)0x100000000LL, cursize);
|
||||
|
||||
/* If seek truncates to 32 bits, this might read the slogan instead */
|
||||
printf("Trying to read again (should get EOF)\n");
|
||||
try_reading(fd);
|
||||
|
||||
printf("Passed.\n");
|
||||
|
||||
close(fd);
|
||||
remove(TESTFILE);
|
||||
return 0;
|
||||
/* If seek is totally bust, this will fail. */
|
||||
printf("Checking what we wrote\n");
|
||||
check_slogan(fd, 0);
|
||||
|
||||
try_seeking(fd, (off_t)0x1000LL, cursize);
|
||||
printf("Checking the other thing we wrote\n");
|
||||
check_slogan(fd, 1);
|
||||
|
||||
try_seeking(fd, (off_t)0x20LL, cursize);
|
||||
try_seeking(fd, (off_t)0x7fffffffLL, cursize);
|
||||
try_seeking(fd, (off_t)0x80000000LL, cursize);
|
||||
try_seeking(fd, (off_t)0x80000020LL, cursize);
|
||||
try_seeking(fd, (off_t)0x100000000LL, cursize);
|
||||
try_seeking(fd, (off_t)0x100000020LL, cursize);
|
||||
try_seeking(fd, (off_t)0x180000000LL, cursize);
|
||||
try_seeking(fd, (off_t)0x180000020LL, cursize);
|
||||
|
||||
printf("Now trying to read (should get EOF)\n");
|
||||
try_reading(fd);
|
||||
|
||||
printf("Now trying to write (should get EFBIG)\n");
|
||||
try_writing(fd);
|
||||
|
||||
try_seeking(fd, (off_t)0x100000000LL, cursize);
|
||||
|
||||
/* If seek truncates to 32 bits, this might read the slogan instead */
|
||||
printf("Trying to read again (should get EOF)\n");
|
||||
try_reading(fd);
|
||||
|
||||
printf("Passed.\n");
|
||||
|
||||
close(fd);
|
||||
remove(TESTFILE);
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user