Skip to content

Commit

Permalink
Add commands LS and TREE
Browse files Browse the repository at this point in the history
  • Loading branch information
danbraik committed Feb 28, 2014
1 parent efdc025 commit 0fa82e3
Show file tree
Hide file tree
Showing 10 changed files with 177 additions and 8 deletions.
6 changes: 4 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
# --------

# Project to compile (use a preprocessor definition)
PROJECT_TO_COMPILE = TEST_FILESYSTEM
PROJECT_TO_COMPILE = TEST_COMMANDS
TRACES = _ #TRACE_ALLOC


# Executable name
EXE = prog.exe
Expand All @@ -15,7 +17,7 @@ OBJ_DIR = obj
# Compiler
CC = gcc
# Compiler options
CFLAGS = -D $(PROJECT_TO_COMPILE) -std=c99 -W -Wall -Wextra -g # -Werror
CFLAGS = -D $(PROJECT_TO_COMPILE) -D $(TRACES) -std=c99 -W -Wall -Wextra -g # -Werror
# Linker options
LDFLAGS =

Expand Down
8 changes: 6 additions & 2 deletions alloc.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <stdbool.h>
#include <stdio.h>
#include "alloc.h"


Expand Down Expand Up @@ -40,7 +41,9 @@ void * mem_alloc(mem_allocator *allocator, uint32_t size)
iterator->start = (uint8_t*) newnext;
iterator->size -= size;

#ifdef TRACE_ALLOC
printf("ALLOC %p\t%u\n", ptr, size);
#endif
return ptr;

} else {
Expand All @@ -61,7 +64,7 @@ void mem_free(mem_allocator *allocator, void *ptr, uint32_t size)

_mem_correct_size(&size);

const uint8_t *start = (uint8_t*) ptr;
uint8_t *start = (uint8_t*) ptr;
mem_allocator *iterator = allocator;
mem_allocator *prev_iterator = NULL;

Expand Down Expand Up @@ -101,8 +104,9 @@ void mem_free(mem_allocator *allocator, void *ptr, uint32_t size)
iterator->size = size;
iterator->start = start;
}

#ifdef TRACE_ALLOC
printf("FREE %p\t%u\n", ptr, size);
#endif
return;
} else {
// move iterator to next cell
Expand Down
1 change: 1 addition & 0 deletions alloc.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef _ALLOC_H_
#define _ALLOC_H_


#include <stdlib.h>
#include <inttypes.h>

Expand Down
47 changes: 47 additions & 0 deletions commands.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include <stdio.h>
#include "commands.h"


static void _tree_rec(fs_file *file, uint8_t deep)
{
for(uint8_t i = 0; i < deep ; ++i)
for(uint8_t j = 0; j < 4; j++)
putchar(' ');
puts(fs_get_name(file));

if (fs_is_directory(file)) {
fs_iterator iterator = fs_get_first_child(file);
while (iterator != NULL) {
_tree_rec(fs_get_file_by_iter(iterator), deep+1);
iterator = fs_get_next_child(iterator);
}
}
}

uint8_t tree(fs_file *file)
{
if (file == NULL)
return CMD_ERROR;

_tree_rec(file, 0);
return CMD_SUCCESS;
}


uint8_t ls(fs_file *file)
{
if (file == NULL)
return CMD_ERROR;

if (fs_is_directory(file)) {
fs_iterator iterator = fs_get_first_child(file);
while (iterator != NULL) {
puts(fs_get_name(fs_get_file_by_iter(iterator)));
iterator = fs_get_next_child(iterator);
}
} else {
puts(fs_get_name(file));
}

return CMD_SUCCESS;
}
17 changes: 17 additions & 0 deletions commands.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#ifndef _COMMANDS_H_
#define _COMMANDS_H_


#include "filesystem.h"

#define CMD_SUCCESS 1
#define CMD_ERROR 0

uint8_t tree(fs_file *file);

uint8_t ls(fs_file *file);




#endif
41 changes: 40 additions & 1 deletion filesystem.c
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,47 @@ int8_t fs_remove_file(mem_allocator *allocator,



int8_t fs_get_file(fs_file *root, fs_file *working, const char *filepath)
int8_t fs_get_file(fs_file *root, fs_file *working,
const char *filepath)
{
return FS_SUCCESS;
}



bool fs_is_directory(fs_file* file)
{
return file != NULL
&& file->file_type == FS_TYPE_DIRECTORY;
}

const char * fs_get_name(fs_file *file)
{
if (file == NULL)
return NULL;
return file->name;
}


fs_iterator fs_get_first_child(fs_file *dir)
{
if (!fs_is_directory(dir))
return NULL;
return dir->data.directory.children;
}


fs_iterator fs_get_next_child(fs_iterator iterator)
{
if (iterator == NULL)
return NULL;
return iterator->next;
}


fs_file* fs_get_file_by_iter(fs_iterator iterator)
{
if (iterator == NULL)
return NULL;
return iterator->file;
}
14 changes: 11 additions & 3 deletions filesystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define _FILESYSTEM_H_

#include <inttypes.h>
#include <stdbool.h>
#include "alloc.h"

#define FS_ERROR 0
Expand All @@ -19,10 +20,10 @@ typedef struct {
/*
* FS_DIRECTORY
*/
typedef struct fs_list_cell* fs_list;
typedef struct fs_list_cell* fs_iterator;

typedef struct {
fs_list children;
fs_iterator children;
} fs_directory;

/*
Expand All @@ -42,7 +43,7 @@ typedef struct {

typedef struct fs_list_cell {
fs_file *file;
struct fs_list_cell *next;
fs_iterator next;
} fs_list_cell;


Expand All @@ -64,8 +65,15 @@ int8_t fs_remove_file(mem_allocator *allocator,
fs_file *parent,
const char *name);

bool fs_is_directory(fs_file* file);

const char * fs_get_name(fs_file *file);

fs_iterator fs_get_first_child(fs_file *dir);

fs_iterator fs_get_next_child(fs_iterator iterator);

fs_file* fs_get_file_by_iter(fs_iterator iterator);


//void fs_get_root_cursor(fs_file *dir);
Expand Down
43 changes: 43 additions & 0 deletions test_commands.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#ifdef TEST_COMMANDS

#include "tests.h"

#include "commands.h"

int main(int argc, char const *argv[])
{
mem_allocator allocator;
uint8_t *memory = malloc(MEM_SIZE);
mem_init(&allocator, memory, MEM_SIZE);

fs_file root;
if(fs_new_root(&allocator, &root) == FS_ERROR)
puts("Error pour fs_init_directory");
if (fs_add_regular(&allocator, &root, "monFichierapappapapapapapapapapapapaas", NULL) == FS_ERROR)
puts("Error pour fs_add_file");
fs_file *home, *user;
fs_add_dir(&allocator, &root, "home", &home);
fs_add_dir(&allocator, home, "user", &user);
fs_add_regular(&allocator, user, "file1", NULL);
fs_add_regular(&allocator, user, "file2", NULL);

puts("* Test LS");
ls(&root);
puts("* End");

puts("* Test TREE");
tree(&root);
puts("* End");


if (fs_delete_root(&allocator, &root) == FS_ERROR)
puts("Error pour fs_delete_root");

// clean test
free(memory);

return 0;
}


#endif
1 change: 1 addition & 0 deletions test_filesystem.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ int main(int argc, char const *argv[])

mem_debug(&allocator);

// clean test
free(memory);

return 0;
Expand Down
7 changes: 7 additions & 0 deletions tests.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#ifndef TESTS_H
#define TESTS_H


#define MEM_SIZE 1024

#endif

0 comments on commit 0fa82e3

Please sign in to comment.