Nffs add mkdir_p

add ListFiles example
This commit is contained in:
hathach committed 2017-03-01 13:08:53 +07:00
1 parent 2dc6b89e2e
commit 1b555e7b52
9 files changed
+173 -8

No files matched your search

@@ -0,0 +1,126 @@
/*********************************************************************
This is an example for our nRF52 based Bluefruit LE modules
Pick one up today in the adafruit shop!
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
MIT license, check LICENSE for more information
All text above, and the splash screen below must be included in
any redistribution
*********************************************************************/
#include <bluefruit.h>
#include <NewtNffs.h>
/* This example print out NFFS content up to
* MAX_LEVEL level of directories (including root)
*/
/*
* WARNING: This example uses recursive call to print out directory tree
* be extra careful, high number of MAX_LEVEL can cause memory overflow
*/
#define MAX_LEVEL 2
// Current Working Directory Name
char cwd[256] = { 0 };
// the setup function runs once when you press reset or power the board
void setup()
{
Serial.begin(115200);
Serial.println("Nffs List Files Example");
Nffs.begin();
// Print whole directory tree of root whose level is 0
printTreeDir("/", 0);
// Print prompt
Serial.println();
Serial.println("Enter anything to print directory tree (again):");
}
// the loop function runs over and over again forever
void loop()
{
digitalToggle(LED_BUILTIN);
delay(1000);
}
/**************************************************************************/
/*!
@brief Print out whole directory tree of an folder
until the level reach MAX_LEVEL
@note Recursive call
*/
/**************************************************************************/
void printTreeDir(const char* path, uint8_t level)
{
// Open the input folder
NffsDir dir(path);
// Print root
if (level == 0) Serial.println("root");
// File Entry Information which hold file attribute and name
NffsDirEntry dirEntry;
// Loop through the directory
while( dir.read(&dirEntry) )
{
// Indentation according to dir level
for(int i=0; i<level; i++) Serial.print("| ");
Serial.print("|_ ");
char eName[64];
dirEntry.getName(eName, sizeof(eName));
Serial.print( eName );
if ( dirEntry.isDirectory() )
{
Serial.println("/");
// ATTENTION recursive call to print sub folder with level+1 !!!!!!!!
// High number of MAX_LEVEL can cause memory overflow
if ( level < MAX_LEVEL )
{
strcat(cwd, "/");
strcat(cwd, eName);
printTreeDir( cwd, level+1 );
}
}else
{
// Print file size starting from position 50
int pos = level*3 + 3 + strlen(eName);
// Print padding
for (int i=pos; i<50; i++) Serial.print(' ');
// Print at least one extra space in case current position > 50
Serial.print(' ');
// Print size in bytes
NffsFile file(eName);
Serial.print( file.size() );
Serial.println( " Bytes");
file.close();
}
}
dir.close();
// Change back to Parent if not root
if (level != 0)
{
char* slash = strrchr(cwd, '/');
*slash = 0;
}
}
@@ -12,7 +12,6 @@
any redistribution
*********************************************************************/
#include <bluefruit.h>
#include <NewtNffs.h>
#define FILENAME "/adafruit.txt"
@@ -24,9 +23,8 @@ NffsFile file;
void setup()
{
Serial.begin(115200);
Serial.println("nffs example");
Serial.println("Nffs Read Write File Example");
Bluefruit.begin();
Nffs.begin();
file.open(FILENAME, FS_ACCESS_READ);
+1 -1
View File
@@ -7,4 +7,4 @@ paragraph=Newt Newtron Flash Filesystem
category=Communication
url=https://github.com/adafruit/Adafruit_nRF52_Arduino
architectures=*
includes=nffs/include/nffs/nffs.h
includes=NewtNffs.h
+28
View File
@@ -54,6 +54,34 @@ bool NewtNffs::mkdir(const char* path)
return (errnum == FS_EOK);
}
/**
* Make a directory (and its parents if not existed) a.k.a mkdir -p
* @param path Absolute path of directory, must start with '/'
* @return
*/
bool NewtNffs::mkdir_p(const char* path)
{
char* parent = (char*) rtos_malloc(strlen(path)+1);
const char* slash = path+1; // skip root '/'
memclr(parent, strlen(path)+1);
while( NULL != (slash = strchr(slash, '/')) )
{
memcpy(parent, path, slash-path);
// make parent, ignore return value (OK or Already Existed)
fs_mkdir(parent);
slash++;
}
errnum = fs_mkdir(path);
rtos_free(parent);
return (errnum == FS_EOK);
}
bool NewtNffs::remove(const char* path)
{
errnum = fs_unlink(path);
+6 -1
View File
@@ -36,6 +36,8 @@
#ifndef NEWTNFFS_H_
#define NEWTNFFS_H_
#include <Arduino.h>
#include "nffs/nffs.h"
#include "fs/fsutil.h"
@@ -52,9 +54,12 @@ public:
void begin(void);
// Make a directory (and its parents if not existed) a.k.a mkdir -p
// Make a directory, All intermediate directories must already exist
bool mkdir(const char* path);
// Make a directory and its parents if not existed a.k.a "mkdir -p"
bool mkdir_p(const char* path);
// Remove a file or an empty directory
bool remove(const char* path);
bool rm (const char* path) { return remove(path); }
+8 -1
View File
@@ -93,9 +93,16 @@ bool NffsDir::existed(void)
NffsDirEntry NffsDir::read(void)
{
struct fs_dirent* dirent;
struct fs_dirent* dirent = NULL ;
errnum = fs_readdir(_dir, &dirent);
return NffsDirEntry(dirent);
}
bool NffsDir::read(NffsDirEntry* entry)
{
*entry = read();
return entry->existed();
}
+1
View File
@@ -60,6 +60,7 @@ public:
bool existed(void);
NffsDirEntry read(void);
bool read(NffsDirEntry* entry);
};
+1 -1
View File
@@ -46,7 +46,7 @@ NffsDirEntry::NffsDirEntry(struct fs_dirent* entry)
_dirent = entry;
}
NffsDirEntry& NffsDirEntry::operator+(const NffsDirEntry& rhs)
NffsDirEntry& NffsDirEntry::operator=(const NffsDirEntry& rhs)
{
_dirent = rhs._dirent;
+1 -1
View File
@@ -47,7 +47,7 @@ public:
NffsDirEntry(void);
NffsDirEntry(struct fs_dirent* entry);
NffsDirEntry& operator+(const NffsDirEntry& rhs);
NffsDirEntry& operator=(const NffsDirEntry& rhs);
bool existed(void);