mirror of
https://github.com/genodelabs/genode.git
synced 2025-02-20 17:52:52 +00:00
libc: implement 'pread()' and 'pwrite()'
This patch implements the 'pread()' and 'pwrite()' functions in the libc. A lock guard prevents the parallel execution of either or both functions. Fixes #278.
This commit is contained in:
parent
0f8f87c3fe
commit
9124f303f7
@ -14,7 +14,7 @@ SRC_CC = atexit.cc dummies.cc rlimit.cc sysctl.cc readlink.cc munmap.cc \
|
||||
issetugid.cc errno.cc gai_strerror.cc clock_gettime.cc \
|
||||
gettimeofday.cc malloc.cc progname.cc fd_alloc.cc file_operations.cc \
|
||||
plugin.cc plugin_registry.cc select.cc exit.cc environ.cc nanosleep.cc \
|
||||
libc_mem_alloc.cc writev.cc
|
||||
libc_mem_alloc.cc writev.cc pread_pwrite.cc
|
||||
|
||||
#
|
||||
# Files from string library that are not included in libc-raw_string because
|
||||
|
75
libports/src/lib/libc/pread_pwrite.cc
Normal file
75
libports/src/lib/libc/pread_pwrite.cc
Normal file
@ -0,0 +1,75 @@
|
||||
/*
|
||||
* \brief 'pread()' and 'pwrite()' implementations
|
||||
* \author Christian Prochaska
|
||||
* \date 2012-07-11
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (C) 2012 Genode Labs GmbH
|
||||
*
|
||||
* This file is part of the Genode OS framework, which is distributed
|
||||
* under the terms of the GNU General Public License version 2.
|
||||
*/
|
||||
|
||||
/* Genode includes */
|
||||
#include <base/lock.h>
|
||||
|
||||
/* libc includes */
|
||||
#include <sys/types.h>
|
||||
#include <sys/uio.h>
|
||||
#include <unistd.h>
|
||||
|
||||
|
||||
static Genode::Lock rw_lock;
|
||||
|
||||
|
||||
struct Read
|
||||
{
|
||||
ssize_t operator()(int fd, void *buf, size_t count)
|
||||
{
|
||||
return read(fd, buf, count);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
struct Write
|
||||
{
|
||||
ssize_t operator()(int fd, const void *buf, size_t count)
|
||||
{
|
||||
return write(fd, buf, count);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
template <typename Rw_func, typename Buf_type>
|
||||
static ssize_t pread_pwrite_impl(Rw_func rw_func, int fd, Buf_type buf, ::size_t count, ::off_t offset)
|
||||
{
|
||||
Genode::Lock_guard<Genode::Lock> rw_lock_guard(rw_lock);
|
||||
|
||||
off_t old_offset = lseek(fd, 0, SEEK_CUR);
|
||||
|
||||
if (old_offset == -1)
|
||||
return -1;
|
||||
|
||||
if (lseek(fd, offset, SEEK_SET) == -1)
|
||||
return -1;
|
||||
|
||||
ssize_t result = rw_func(fd, buf, count);
|
||||
|
||||
if (lseek(fd, old_offset, SEEK_SET) == -1)
|
||||
return -1;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
extern "C" ssize_t pread(int fd, void *buf, ::size_t count, ::off_t offset)
|
||||
{
|
||||
return pread_pwrite_impl(Read(), fd, buf, count, offset);
|
||||
}
|
||||
|
||||
|
||||
extern "C" ssize_t pwrite(int fd, const void *buf, ::size_t count, ::off_t offset)
|
||||
{
|
||||
return pread_pwrite_impl(Write(), fd, buf, count, offset);
|
||||
}
|
@ -39,11 +39,15 @@
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int ret, fd;
|
||||
ssize_t count;
|
||||
|
||||
char const *dir_name = "/testdir";
|
||||
char const *file_name = "test.tst";
|
||||
char const *file_name2 = "test2.tst";
|
||||
char const *pattern = "a single line of text";
|
||||
|
||||
size_t pattern_size = strlen(pattern) + 1;
|
||||
|
||||
unsigned int iterations = 1;
|
||||
|
||||
try {
|
||||
@ -60,8 +64,7 @@ int main(int argc, char *argv[])
|
||||
|
||||
/* write pattern to a file */
|
||||
CALL_AND_CHECK(fd, open(file_name, O_CREAT | O_WRONLY), fd >= 0, "file_name=%s", file_name);
|
||||
size_t count = strlen(pattern);
|
||||
CALL_AND_CHECK(ret, write(fd, pattern, count), ret > 0, "count=%zd", count);
|
||||
CALL_AND_CHECK(count, write(fd, pattern, pattern_size), (size_t)count == pattern_size, "");
|
||||
CALL_AND_CHECK(ret, close(fd), ret == 0, "");
|
||||
|
||||
/* query file status of new file */
|
||||
@ -76,7 +79,7 @@ int main(int argc, char *argv[])
|
||||
/* read and verify file content */
|
||||
CALL_AND_CHECK(fd, open(file_name, O_RDONLY), fd >= 0, "file_name=%s", file_name);
|
||||
static char buf[512];
|
||||
CALL_AND_CHECK(count, read(fd, buf, sizeof(buf)), count > 0, "");
|
||||
CALL_AND_CHECK(count, read(fd, buf, sizeof(buf)), (size_t)count == pattern_size, "");
|
||||
CALL_AND_CHECK(ret, close(fd), ret == 0, "");
|
||||
printf("content of file: \"%s\"\n", buf);
|
||||
if (strcmp(buf, pattern) != 0) {
|
||||
@ -86,6 +89,26 @@ int main(int argc, char *argv[])
|
||||
printf("file content is correct\n");
|
||||
}
|
||||
|
||||
/* test 'pread()' and 'pwrite()' */
|
||||
CALL_AND_CHECK(fd, open(file_name2, O_CREAT | O_WRONLY), fd >= 0, "file_name=%s", file_name);
|
||||
/* write "a single line of" */
|
||||
CALL_AND_CHECK(count, pwrite(fd, pattern, (pattern_size - 6), 0), (size_t)count == (pattern_size - 6), "");
|
||||
/* write "line of text" at offset 9 */
|
||||
CALL_AND_CHECK(count, pwrite(fd, &pattern[9], (pattern_size - 9), 9), (size_t)count == (pattern_size - 9), "");
|
||||
CALL_AND_CHECK(ret, close(fd), ret == 0, "");
|
||||
CALL_AND_CHECK(fd, open(file_name2, O_RDONLY), fd >= 0, "file_name=%s", file_name);
|
||||
memset(buf, 0, sizeof(buf));
|
||||
/* read "single line of text" from offset 2 */
|
||||
CALL_AND_CHECK(count, pread(fd, buf, sizeof(buf), 2), (size_t)count == (pattern_size - 2), "");
|
||||
CALL_AND_CHECK(ret, close(fd), ret == 0, "");
|
||||
printf("content of file: \"%s\"\n", buf);
|
||||
if (strcmp(buf, &pattern[2]) != 0) {
|
||||
printf("unexpected content of file\n");
|
||||
return -1;
|
||||
} else {
|
||||
printf("file content is correct\n");
|
||||
}
|
||||
|
||||
/* read directory entries */
|
||||
DIR *dir;
|
||||
CALL_AND_CHECK(dir, opendir(dir_name), dir, "dir_name=\"%s\"", dir_name);
|
||||
|
Loading…
x
Reference in New Issue
Block a user