Skip to content

Commit

Permalink
feat: Adds rm, fixes buildpath
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathan Hui committed Sep 12, 2023
1 parent d35ff05 commit 08ad4c4
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 6 deletions.
64 changes: 59 additions & 5 deletions src/fileCLI/fileCLI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ FileCLI::menu_t FileCLI::fsExplorerMenu[] =
{'l', &FileCLI::list_dir},
{'p', &FileCLI::print_dir},
{'q', &FileCLI::exit},
{'r', &FileCLI::deleteFile},
{'\0', NULL}
};

Expand Down Expand Up @@ -87,7 +88,7 @@ void FileCLI::list_dir(void)
break;
}
strncpy(this->path_stack[this->current_dir], dirent->d_name, NAME_MAX);
const char* path = buildPath();
const char* path = buildPath(true);
if(stat(path, &file_stats))
{
FLOG_AddError(FLOG_FS_STAT_FAIL, errno);
Expand Down Expand Up @@ -121,15 +122,17 @@ void FileCLI::exit(void)
FLOG_AddError(FLOG_DEBUG, 0);
}

const char* FileCLI::buildPath(void)
const char* FileCLI::buildPath(bool is_dir)
{
size_t path_buffer_idx = 0;
int dir_idx = 0;

memset(path_buffer, 0, PATH_MAX);


for(; dir_idx <= this->current_dir; dir_idx++)
for(;
is_dir ? (dir_idx <= this->current_dir) : (dir_idx < this->current_dir);
dir_idx++)
{

strcpy(path_buffer + path_buffer_idx, PATH_SEP);
Expand Down Expand Up @@ -194,7 +197,7 @@ void FileCLI::change_dir(void)
rewinddir(cwd);
strncpy(this->path_stack[this->current_dir], dirent->d_name, NAME_MAX);
this->current_dir++;
path = buildPath();
path = buildPath(true);
this->dir_stack[this->current_dir] = opendir(path);
}

Expand All @@ -208,6 +211,57 @@ FileCLI::FileCLI(void)
void FileCLI::print_dir(void)
{
const char* path;
path = buildPath();
path = buildPath(true);
SF_OSAL_printf("%s" __NL__, path);
}

void FileCLI::deleteFile(void)
{
char input_buffer[FILE_CLI_INPUT_BUFFER_LEN];
DIR* cwd = this->dir_stack[this->current_dir];
struct dirent* dirent;
char f_type;
long idx;
int cmd_val;
const char* path;

idx = telldir(cwd);
while ((dirent = readdir(cwd)))
{
switch(dirent->d_type)
{
default:
case DT_REG:
f_type = ' ';
break;
case DT_DIR:
f_type = 'd';
break;
}
strncpy(this->path_stack[this->current_dir], dirent->d_name, NAME_MAX);
SF_OSAL_printf("%d: %c %-16s" __NL__,
idx,
f_type,
dirent->d_name);
idx = telldir(cwd);
}
rewinddir(cwd);
memset(this->path_stack[this->current_dir], 0, NAME_MAX);

SF_OSAL_printf("Enter the number of the file to remove: ");
getline(input_buffer, FILE_CLI_INPUT_BUFFER_LEN);
cmd_val = atoi(input_buffer);

seekdir(cwd, cmd_val);
dirent = readdir(cwd);
rewinddir(cwd);
strncpy(this->path_stack[this->current_dir], dirent->d_name, NAME_MAX);
this->current_dir++;
path = buildPath(false);
if(unlink(path))
{
SF_OSAL_printf("Failed to unlink file: %s" __NL__, strerror(errno));
}
this->current_dir--;
memset(this->path_stack[this->current_dir], 0, NAME_MAX);
}
2 changes: 1 addition & 1 deletion src/fileCLI/fileCLI.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,6 @@ class FileCLI{
* @return Pointer to menu entry if matching entry found, otherwise nullptr
*/
static menu_t* findCommand(const char* const cmd);
const char* buildPath(void);
const char* buildPath(bool is_dir);
};
#endif

0 comments on commit 08ad4c4

Please sign in to comment.