First try in keyboard driver
[incOS.git] / programs / ramdisk / file.c
blob609b84624e1a7c3772bdc51e44e3bd62ea4d6772
1 /*
2 * Copyright (c) 2008 The LOST Project. All rights reserved.
4 * This code is derived from software contributed to the LOST Project
5 * by Alexander Siol and Janosch Graef.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the LOST Project
18 * and its contributors.
19 * 4. Neither the name of the LOST Project nor the names of its
20 * contributors may be used to endorse or promote products derived
21 * from this software without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
27 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
30 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
32 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
33 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 #include "ramdisk_cdi.h"
38 size_t ramdisk_fs_file_read(struct cdi_fs_stream* stream, uint64_t start,
39 size_t size, void* data)
41 struct ramdisk_fs_res* res = (struct ramdisk_fs_res*) stream->res;
43 // maximal bis Ende lesen.
44 if ((res->size - start) < size) {
45 size = res->size - start;
48 memcpy(data, res->buffer + start, size);
49 return size;
52 size_t ramdisk_fs_file_write(struct cdi_fs_stream* stream, uint64_t start,
53 size_t size, const void* data)
55 struct ramdisk_fs_res* res = (struct ramdisk_fs_res*) stream->res;
57 // Es soll mehr geschrieben werden als Platz ist. Buffer vergroessern!.
58 if ((res->size - start) < size) {
59 void* new_buffer;
60 size_t new_size;
61 new_size = start + size;
62 if (!(new_buffer = realloc(res->buffer,new_size))) {
63 stream->error = CDI_FS_ERROR_INTERNAL;
64 return 0;
66 memset(new_buffer + res->size, 0, new_size - res->size);
67 res->buffer = new_buffer;
68 res->size = new_size;
71 memcpy(res->buffer + start, data, size);
72 return size;
75 int ramdisk_fs_file_truncate(struct cdi_fs_stream* stream, uint64_t size)
77 struct ramdisk_fs_res* res = (struct ramdisk_fs_res*) stream->res;
79 // Nichts zu tun
80 if (size == res->size) {
81 return 1;
84 void* new_buffer;
85 if (!(new_buffer = realloc(res->buffer,size))) {
86 stream->error = CDI_FS_ERROR_INTERNAL;
88 if (res->size < size) {
89 memset(new_buffer + res->size,0,size - res->size);
92 res->buffer = new_buffer;
93 res->size = size;
94 return 1;