[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[tyndur-devel] [PATCH v2 3/6] libc: lio_compat_write/close()
* libc: lio_compat_write/close aus den stdio-Funktionen
herausgesplittet. Mehr oder weniger nur verschobener Code.
Signed-off-by: Kevin Wolf <kevin@xxxxxxxxxx>
---
src/modules/include/lostio.h | 7 ++++
src/modules/lib/lostio/client/file.c | 50 ++++++++++++++++++++++++++++
src/modules/lib/stdlibc/file.c | 60 +++++----------------------------
3 files changed, 66 insertions(+), 51 deletions(-)
diff --git a/src/modules/include/lostio.h b/src/modules/include/lostio.h
index 4bff6e7..00576b0 100644
--- a/src/modules/include/lostio.h
+++ b/src/modules/include/lostio.h
@@ -192,6 +192,13 @@ void lostio_type_directory_use_as(typeid_t id);
/// Stream oeffnen
io_resource_t* lio_compat_open(const char* filename, uint8_t attr);
+/// Stream schliessen
+int lio_compat_close(io_resource_t* io_res);
+
+/// In einen Stream schreiben
+size_t lio_compat_write(const void* src, size_t blocksize, size_t blockcount,
+ io_resource_t* io_res);
+
/// Stream-Position setzen
bool lio_seek(struct lostio_internal_file* io_res, uint64_t offset, int origin);
diff --git a/src/modules/lib/lostio/client/file.c b/src/modules/lib/lostio/client/file.c
index 2e3dfbf..701e30e 100644
--- a/src/modules/lib/lostio/client/file.c
+++ b/src/modules/lib/lostio/client/file.c
@@ -31,6 +31,7 @@
#include <string.h>
#include <stdlib.h>
#include <rpc.h>
+#include <syscall.h>
io_resource_t* lio_compat_open(const char* filename, uint8_t attr)
{
@@ -65,3 +66,52 @@ out:
return result;
}
+
+int lio_compat_close(io_resource_t* io_res)
+{
+ uint32_t result;
+ result = rpc_get_dword(io_res->pid, "IO_CLOSE",
+ sizeof(io_resource_id_t), (char*) &(io_res->id));
+ free(io_res);
+
+ return result;
+}
+
+size_t lio_compat_write(const void* src, size_t blocksize, size_t blockcount,
+ io_resource_t* io_res)
+{
+ size_t data_size = blockcount * blocksize;
+ size_t request_size = sizeof(io_write_request_t);
+
+ // Bei mehr als einem Kilobyte wird shared memory fuer die Daten benutzt
+ if (data_size < 1024) {
+ request_size += data_size;
+ }
+ uint8_t request[request_size];
+
+ io_write_request_t* write_request = (io_write_request_t*) request;
+ write_request->id = io_res->id;
+ write_request->blocksize = blocksize;
+ write_request->blockcount = blockcount;
+
+ // Wenn kein SHM benutzt wird, werden die Daten direkt an den Request
+ // angehaengt
+ if (data_size < 1024) {
+ write_request->shared_mem_id = 0;
+ memcpy(write_request->data, src, data_size);
+ } else {
+ write_request->shared_mem_id = create_shared_memory(data_size);
+ void* data = open_shared_memory(write_request->shared_mem_id);
+ memcpy(data, src, data_size);
+ }
+
+ size_t resp = rpc_get_dword(io_res->pid, "IO_WRITE",
+ request_size, (char*) request);
+
+ if (data_size >= 1024) {
+ close_shared_memory(write_request->shared_mem_id);
+ }
+ return resp;
+}
+
+
diff --git a/src/modules/lib/stdlibc/file.c b/src/modules/lib/stdlibc/file.c
index 6c3c63f..beda758 100644
--- a/src/modules/lib/stdlibc/file.c
+++ b/src/modules/lib/stdlibc/file.c
@@ -175,8 +175,8 @@ FILE* freopen(const char* path, const char* mode, FILE* stream)
*/
int fclose (FILE* io_res)
{
- uint32_t result;
-
+ int result;
+
if((io_res == NULL) || (io_res->res->pid == 0))
{
return EOF;
@@ -185,12 +185,10 @@ int fclose (FILE* io_res)
fflush(io_res);
setvbuf(io_res, NULL, _IONBF, 0);
- result = rpc_get_dword(io_res->res->pid, "IO_CLOSE", sizeof(io_resource_id_t),
- (char*) &(io_res->res->id));
- free(io_res->res);
+ result = lio_compat_close(io_res->res);
free(io_res);
- return result;
+ return result ? EOF : 0;
}
@@ -396,47 +394,6 @@ int ungetc(int c, FILE* io_res)
}
/**
- *
- */
-static size_t io_write(const void* src, size_t blocksize, size_t blockcount,
- FILE* io_res)
-{
- size_t data_size = blockcount * blocksize;
- size_t request_size = sizeof(io_write_request_t);
-
- // Bei mehr als einem Kilobyte wird shared memory fuer die Daten benutzt
- if (data_size < 1024) {
- request_size += data_size;
- }
- uint8_t request[request_size];
-
- io_write_request_t* write_request = (io_write_request_t*) request;
- write_request->id = io_res->res->id;
- write_request->blocksize = blocksize;
- write_request->blockcount = blockcount;
-
- // Wenn kein SHM benutzt wird, werden die Daten direkt an den Request
- // angehaengt
- if (data_size < 1024) {
- write_request->shared_mem_id = 0;
- memcpy(write_request->data, src, data_size);
- } else {
- write_request->shared_mem_id = create_shared_memory(data_size);
- void* data = open_shared_memory(write_request->shared_mem_id);
- memcpy(data, src, data_size);
- }
-
- size_t resp = rpc_get_dword(io_res->res->pid, "IO_WRITE",
- request_size, (char*) request);
-
- if (data_size >= 1024) {
- close_shared_memory(write_request->shared_mem_id);
- }
- return resp;
-}
-
-
-/**
* Daten in die Datei schreiben.
*
* @param data Pointer auf die Quelldaten
@@ -471,7 +428,7 @@ size_t fwrite(const void* data, size_t blocksize, size_t blockcount,
// gespeichert, sondern direkt an den Zielprozess gesendet.
switch (io_res->buffer_mode) {
case IO_BUFFER_MODE_NONE:
- return io_write(data, blocksize, blockcount, io_res);
+ return lio_compat_write(data, blocksize, blockcount, io_res->res);
break;
case IO_BUFFER_MODE_FULL:
@@ -488,7 +445,7 @@ size_t fwrite(const void* data, size_t blocksize, size_t blockcount,
if (fflush(io_res) == EOF) {
return 0;
}
- return io_write(data, blocksize, blockcount, io_res);
+ return lio_compat_write(data, blocksize, blockcount, io_res->res);
}
break;
@@ -503,7 +460,7 @@ size_t fwrite(const void* data, size_t blocksize, size_t blockcount,
if (fflush(io_res) == EOF) {
return 0;
}
- return io_write(data, blocksize, blockcount, io_res);
+ return lio_compat_write(data, blocksize, blockcount, io_res->res);
} else {
//Daten in den Buffer kopieren.
memcpy((void*)((uint32_t) io_res->buffer_ptr +
@@ -712,7 +669,8 @@ int fflush(FILE* io_res)
return 0;
}
- size_t size = io_write(io_res->buffer_ptr, 1, io_res->buffer_pos, io_res);
+ size_t size = lio_compat_write(io_res->buffer_ptr,
+ 1, io_res->buffer_pos, io_res->res);
if (size == io_res->buffer_pos) {
io_res->buffer_pos = 0;
--
1.6.0.2