Skip to content

Commit

Permalink
lxfs: stub for parsing directory tree
Browse files Browse the repository at this point in the history
  • Loading branch information
jewelcodes committed Sep 29, 2024
1 parent 0c85003 commit 88926f9
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
58 changes: 58 additions & 0 deletions fs/lxfs/src/dirtree.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* luxOS - a unix-like operating system
* Omar Elghoul, 2024
*
* lxfs: Driver for the lxfs file system
*/

#include <lxfs/lxfs.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>

/* pathDepth(): helper function to calculate the depth of a path
* params: path - full qualified path
* returns: maximum depth of the path
*/

int pathDepth(const char *path) {
if(!path || !strlen(path) || strlen(path) == 1) return 0;

int c = 0;
for(int i = 0; i < strlen(path); i++) {
if(path[i] == '/') c++;
}

return c;
}

/* pathComponent(): helper function to return the nth component of a path
* params: dest - destination buffer
* params: path - full qualified path
* params: n - component number
* returns: pointer to destination on success, NULL on fail
*/

char *pathComponent(char *dest, const char *path, int n) {
int depth = pathDepth(path);
if(n > depth) return NULL;

int c = 0;
int len = 0;
for(int i = 0; i < strlen(path); i++) {
if(path[i] == '/') c++;
if(c > n) {
i++;
while(path[i] && (path[i] != '/')) {
dest[len] = path[i];
len++;
i++;
}

dest[len] = 0;
return dest;
}
}

return NULL;
}
9 changes: 9 additions & 0 deletions fs/lxfs/src/include/lxfs/lxfs.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,12 @@ int lxfsWriteBlock(Mountpoint *, uint64_t, const void *);
uint64_t lxfsNextBlock(Mountpoint *, uint64_t);
uint64_t lxfsReadNextBlock(Mountpoint *, uint64_t, void *);
uint64_t lxfsWriteNextBlock(Mountpoint *, uint64_t, const void *);

Mountpoint *findMP(const char *);
int pathDepth(const char *);
char *pathComponent(char *, const char *, int);

void lxfsOpen(OpenCommand *);
void lxfsStat(StatCommand *);
void lxfsRead(RWCommand *);
void lxfsWrite(RWCommand *);

0 comments on commit 88926f9

Please sign in to comment.