43 static void __directoryDestroy(
struct object *
obj);
44 static void __directoryShutdown(
struct object *
obj,
UNUSED uint32_t
mode);
45 static int32_t __directoryWrite(
struct object *
obj, uint8_t *buf, uint32_t length);
46 static int32_t __directoryRead(
struct object *
obj, uint8_t *buf, uint32_t length);
62 static void __fileDestroy(
struct object *obj);
63 static void __fileShutdown(
struct object *obj,
UNUSED uint32_t
mode);
64 static int32_t __fileGetStatus(
struct object *obj,
UNUSED uint32_t
mode);
65 static int32_t __fileWrite(
struct object *obj, uint8_t *buf, uint32_t length);
66 static int32_t __fileRead(
struct object *obj, uint8_t *buf, uint32_t length);
82 static void __openedDirectoryDestroy(
struct object *obj);
83 static int32_t __openedDirectoryRead(
struct object *obj, uint8_t *buf, uint32_t length);
87 __openedDirectoryDestroy,
94 __openedDirectoryRead,
99 static void __openedFileDestroy(
struct object *obj);
100 static void __openedFileShutdown(
struct object *obj,
UNUSED uint32_t
mode);
101 static int32_t __openedFileGetStatus(
struct object *obj, uint32_t
mode);
102 static void __openedFileSignal(
struct object *obj, uint32_t result);
103 static int32_t __openedFileWrite(
struct object *obj, uint8_t *buf, uint32_t length);
104 static int32_t __openedFileRead(
struct object *obj, uint8_t *buf, uint32_t length);
110 __openedFileShutdown,
111 __openedFileGetStatus,
122 struct file *f, *__f;
123 struct directory *d, *__d;
136 static inline bool __isValidFilename(
struct object *current,
struct directory *
parent,
const char *buf, uint32_t length)
186 if (!(buffer =
heapAlloc(nameLength + 1)))
191 memcpy(buffer, name, nameLength);
192 buffer[nameLength] = 0;
196 __objectInit(&d->
obj, &directoryFunctions);
218 static void __directoryDestroy(
struct object *
obj)
226 __directoryShutdownChilds(d);
243 static void __directoryShutdown(
struct object *obj,
UNUSED uint32_t
mode)
266 static int32_t __directoryWrite(
struct object *obj, uint8_t *buf, uint32_t length)
271 while (length > 0 && !buf[length - 1]) length--;
273 if (length && !__isValidFilename(&d->
obj, d->
parent, (
char *)buf, length))
289 if (!d->
name)
return 0;
306 static int32_t __directoryRead(
struct object *obj, uint8_t *buf, uint32_t length)
309 uint32_t file_length;
310 if (!d->
name)
return -1;
315 if (length > file_length)
316 length = file_length;
345 if (!(buffer =
heapAlloc(nameLength + 1)))
350 memcpy(buffer, name, nameLength);
351 buffer[nameLength] = 0;
355 __objectInit(&f->
obj, &fileFunctions);
364 f->
size = staticSize;
388 static void __fileDestroy(
struct object *obj)
413 static void __fileShutdown(
struct object *obj,
UNUSED uint32_t mode)
436 static int32_t __fileGetStatus(
struct object *obj,
UNUSED uint32_t mode)
450 static int32_t __fileWrite(
struct object *obj, uint8_t *buf, uint32_t length)
455 while (length > 0 && !buf[length - 1]) length--;
457 if (length && !__isValidFilename(&f->
obj, f->
parent, (
char *)buf, length))
473 if (!f->
name)
return 0;
490 static int32_t __fileRead(
struct object *obj, uint8_t *buf, uint32_t length)
493 uint32_t file_length;
494 if (!f->
name)
return -1;
499 if (length > file_length)
500 length = file_length;
523 __objectInit(&h->
obj, &openedFileFunctions);
539 static void __openedFileDestroy(
struct object *obj)
562 static void __openedFileShutdown(
struct object *obj,
UNUSED uint32_t mode)
574 if (new_buffer || !h->
pos) f->
buffer = new_buffer;
591 static int32_t __openedFileGetStatus(
struct object *obj, uint32_t mode)
603 static void __openedFileSignal(
struct object *obj, uint32_t result)
618 static int32_t __openedFileWrite(
struct object *obj, uint8_t *buf, uint32_t length)
622 if (length == 0)
return 0;
672 static int32_t __openedFileRead(
struct object *obj, uint8_t *buf, uint32_t length)
676 if (h->
pos >= f->
size)
return -1;
709 __objectInit(&h->
obj, &openedDirectoryFunctions);
725 static void __openedDirectoryDestroy(
struct object *obj)
736 __objectRelease(h->
pos);
754 static int32_t __openedDirectoryRead(
struct object *obj, uint8_t *buf, uint32_t length)
763 uint32_t name_length;
810 if (length > name_length)
811 length = name_length;
813 memcpy(buf, name, length);
817 static inline bool __tarVerifyChecksum(
struct tarHeader *tar)
819 int8_t *buf = (int8_t *)tar;
820 uint32_t i, cksum = 0;
822 for (i = 0; i < 148; i++)
826 for (i = 0; i < 8; i++)
829 for (i = 156; i < 512; i++)
835 static inline bool __tarIsEOF(
struct tarHeader *tar, uint32_t length)
837 uint8_t *buf = (uint8_t *)tar;
872 while (length >=
sizeof(
struct tarHeader))
876 char *
name, namebuf[100+1+155+1];
879 if (__tarIsEOF(tar, length))
883 assert(__tarVerifyChecksum(tar));
885 assert(size != (uint32_t)-1);
902 name +=
sizeof(tar->
name);
911 f->
buffer = (uint8_t *)(tar + 1);;
917 size =
sizeof(
struct tarHeader) + ((size + 511) & ~511);
936 if (!obj)
return NULL;
938 if (obj->
functions == &openedDirectoryFunctions)
945 if (obj->
functions == &directoryFunctions)
962 if (!obj)
return NULL;
964 if (obj->
functions == &openedDirectoryFunctions)
1007 struct directory *d;
1008 uint32_t componentLength;
1017 while (pathLength > 0 && path[0] ==
'/')
1024 componentLength = 0;
1025 while (componentLength < pathLength && path[componentLength] !=
'/') componentLength++;
1028 if (!componentLength)
1037 path += componentLength;
1038 pathLength -= componentLength;
1042 directory = directory->
parent;
1052 path += componentLength;
1053 pathLength -= componentLength;
1060 if (!create)
return NULL;
1071 if (!d)
return NULL;
1073 path += componentLength;
1074 pathLength -= componentLength;
1095 struct directory *d;
1096 uint32_t componentLength;
1098 componentLength = pathLength;
1099 while (componentLength > 0 && path[componentLength-1] !=
'/') componentLength--;
1102 if (componentLength >= pathLength)
1106 if (!d)
return NULL;
1108 path += componentLength;
1109 pathLength -= componentLength;
1128 if (!create)
goto err;
1138 f =
fileCreate(directory, path, pathLength, NULL, 0);
struct file * fileCreate(struct directory *parent, char *name, uint32_t nameLength, uint8_t *staticBuffer, uint32_t staticSize)
Creates a new kernel file object.
struct directory * fileSystemIsValidDirectory(struct object *obj)
Checks if a given object is of the type directory and casts it if possible.
struct openedDirectory * directoryOpen(struct directory *directory)
Creates a new kernel openedDirectory object.
void * heapReAlloc(void *addr, uint32_t length)
Resizes a block of kernel memory.
const struct objectFunctions * functions
void * heapAlloc(uint32_t length)
Allocates a block of kernel memory.
struct openedFile * fileOpen(struct file *file)
Creates a new kernel openedFile object.
struct file * fileSystemIsValidFile(struct object *obj)
Checks if a given object is of the type file and casts it if possible.
void * memset(void *ptr, int value, size_t num)
Fills a memory region with some specific byte value.
#define objectShutdown(p, a)
uint32_t stringLength(const char *str)
Returns the length of a nullterminated string.
void heapFree(void *addr)
Deallocates a block of kernel memory.
struct directory * parent
#define LL_ENTRY(element, type, field)
struct linkedList directories
struct linkedList openedFiles
void fileSystemInit(void *addr, uint32_t length)
Initializes the root file system.
struct directory * fileSystemSearchDirectory(struct directory *directory, char *path, uint32_t pathLength, bool create)
Opens or creates a directory.
#define objectContainer(p, type, functions)
void * memcpy(void *destination, const void *source, size_t num)
Copies a block of memory from source to destination.
struct linkedList openedDirectories
struct directory * directory
struct directory * directoryCreate(struct directory *parent, char *name, uint32_t nameLength)
Creates a new kernel directory object.
uint32_t stringParseOctal(const char *str, uint32_t len)
Converts an octal string to a integer number.
struct directory * parent
struct directory * fileSystemGetRoot()
Returns a reference to the root node of the file system.
struct directory * fileSystemRoot
bool stringIsEqual(const char *str, const char *buf, uint32_t len)
Checks if the string is equal to the memory region.
#define LL_FOR_EACH_SAFE(element, next_element, list, type, field)
Allows to iterate a linkedList similar to a for-loop (safe when deleting elements).
struct file * fileSystemSearchFile(struct directory *directory, char *path, uint32_t pathLength, bool create)
Opens or creates a file.
#define LL_FOR_EACH(element, list, type, field)
Allows to iterate a linkedList similar to a for-loop.